mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Cleanup 'Cluster' to remove environment specific details (#6951)
- requestNamespaceListPermissions is infallable so no need to have the extra try/catch - Refactor isMetricHidden method away from Cluster - Refactor shouldShowResource out of Cluster - Refactor isInLocalKubeconfig out of Cluster - Remove depecrated and unused workspace from Cluster - Refactor out kubectl as a dependency of Cluster - Remove from cluster getter used only once - Split out ClusterConnection from Cluster - Also split out KubeAuthProxyServer from ContextHandler - Rename ContextHandler to PrometheusHandler - Cleanup onNetworkOffline/Online impls within ClusterManager - Remove annotations from ClusterConnection - Remove mobx annotations from Cluster - Rename loadConfigFromFileInjectable - Remove all uses of dead createClusterInjectionToken - Fix type errors related to broadcastConnectionUpdate Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
e6d6d1d8f7
commit
ad9bafe2a5
133 files changed
+2544
-1961
No files matched your search
@@ -8,8 +8,6 @@ import type { GetCustomKubeConfigFilePath } from "../app-paths/get-custom-kube-c
|
||||
import getCustomKubeConfigFilePathInjectable from "../app-paths/get-custom-kube-config-directory/get-custom-kube-config-directory.injectable";
|
||||
import clusterStoreInjectable from "../cluster-store/cluster-store.injectable";
|
||||
import type { DiContainer } from "@ogre-tools/injectable";
|
||||
import type { CreateCluster } from "../cluster/create-cluster-injection-token";
|
||||
import { createClusterInjectionToken } from "../cluster/create-cluster-injection-token";
|
||||
import directoryForUserDataInjectable from "../app-paths/directory-for-user-data/directory-for-user-data.injectable";
|
||||
import { getDiForUnitTesting } from "../../main/getDiForUnitTesting";
|
||||
import assert from "assert";
|
||||
@@ -27,6 +25,7 @@ import type { WriteFileSync } from "../fs/write-file-sync.injectable";
|
||||
import writeFileSyncInjectable from "../fs/write-file-sync.injectable";
|
||||
import type { WriteBufferSync } from "../fs/write-buffer-sync.injectable";
|
||||
import writeBufferSyncInjectable from "../fs/write-buffer-sync.injectable";
|
||||
import { Cluster } from "../cluster/cluster";
|
||||
|
||||
// NOTE: this is intended to read the actual file system
|
||||
const testDataIcon = readFileSync("test-data/cluster-store-migration-icon.png");
|
||||
@@ -58,7 +57,6 @@ users:
|
||||
describe("cluster-store", () => {
|
||||
let di: DiContainer;
|
||||
let clusterStore: ClusterStore;
|
||||
let createCluster: CreateCluster;
|
||||
let writeJsonSync: WriteJsonSync;
|
||||
let writeFileSync: WriteFileSync;
|
||||
let writeBufferSync: WriteBufferSync;
|
||||
@@ -74,7 +72,6 @@ describe("cluster-store", () => {
|
||||
di.override(kubectlBinaryNameInjectable, () => "kubectl");
|
||||
di.override(kubectlDownloadingNormalizedArchInjectable, () => "amd64");
|
||||
di.override(normalizedPlatformInjectable, () => "darwin");
|
||||
|
||||
writeJsonSync = di.inject(writeJsonSyncInjectable);
|
||||
writeFileSync = di.inject(writeFileSyncInjectable);
|
||||
writeBufferSync = di.inject(writeBufferSyncInjectable);
|
||||
@@ -84,7 +81,6 @@ describe("cluster-store", () => {
|
||||
|
||||
describe("empty config", () => {
|
||||
beforeEach(async () => {
|
||||
createCluster = di.inject(createClusterInjectionToken);
|
||||
getCustomKubeConfigFilePath = di.inject(getCustomKubeConfigFilePathInjectable);
|
||||
|
||||
writeJsonSync("/some-directory-for-user-data/lens-cluster-store.json", {});
|
||||
@@ -94,7 +90,7 @@ describe("cluster-store", () => {
|
||||
|
||||
describe("with foo cluster added", () => {
|
||||
beforeEach(() => {
|
||||
const cluster = createCluster({
|
||||
const cluster = new Cluster({
|
||||
id: "foo",
|
||||
contextName: "foo",
|
||||
preferences: {
|
||||
@@ -201,7 +197,6 @@ describe("cluster-store", () => {
|
||||
],
|
||||
});
|
||||
|
||||
createCluster = di.inject(createClusterInjectionToken);
|
||||
getCustomKubeConfigFilePath = di.inject(getCustomKubeConfigFilePathInjectable);
|
||||
|
||||
clusterStore = di.inject(clusterStoreInjectable);
|
||||
@@ -256,7 +251,6 @@ describe("cluster-store", () => {
|
||||
],
|
||||
});
|
||||
|
||||
createCluster = di.inject(createClusterInjectionToken);
|
||||
getCustomKubeConfigFilePath = di.inject(getCustomKubeConfigFilePathInjectable);
|
||||
|
||||
clusterStore = di.inject(clusterStoreInjectable);
|
||||
@@ -274,7 +268,6 @@ describe("cluster-store", () => {
|
||||
beforeEach(() => {
|
||||
di.override(storeMigrationVersionInjectable, () => "3.6.0");
|
||||
|
||||
createCluster = di.inject(createClusterInjectionToken);
|
||||
getCustomKubeConfigFilePath = di.inject(getCustomKubeConfigFilePathInjectable);
|
||||
|
||||
writeJsonSync("/some-directory-for-user-data/lens-cluster-store.json", {
|
||||
@@ -302,9 +295,9 @@ describe("cluster-store", () => {
|
||||
});
|
||||
|
||||
it("migrates to modern format with kubeconfig in a file", async () => {
|
||||
const config = clusterStore.clustersList[0].kubeConfigPath;
|
||||
const configPath = clusterStore.clustersList[0].kubeConfigPath.get();
|
||||
|
||||
expect(readFileSync(config)).toBe(minimalValidKubeConfig);
|
||||
expect(readFileSync(configPath)).toBe(minimalValidKubeConfig);
|
||||
});
|
||||
|
||||
it("migrates to modern format with icon not in file", async () => {
|
||||
|
||||
@@ -13,6 +13,7 @@ import { requestClusterActivation, requestClusterDisconnection } from "../../ren
|
||||
import KubeClusterCategoryIcon from "./icons/kubernetes.svg";
|
||||
import getClusterByIdInjectable from "../cluster-store/get-by-id.injectable";
|
||||
import { getLegacyGlobalDiForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
|
||||
import clusterConnectionInjectable from "../../main/cluster/cluster-connection.injectable";
|
||||
|
||||
export interface KubernetesClusterPrometheusMetrics {
|
||||
address?: {
|
||||
@@ -79,8 +80,15 @@ export class KubernetesCluster<
|
||||
if (app) {
|
||||
const di = getLegacyGlobalDiForExtensionApi();
|
||||
const getClusterById = di.inject(getClusterByIdInjectable);
|
||||
const cluster = getClusterById(this.getId());
|
||||
|
||||
await getClusterById(this.getId())?.activate();
|
||||
if (!cluster) {
|
||||
return;
|
||||
}
|
||||
|
||||
const connectionCluster = di.inject(clusterConnectionInjectable, cluster);
|
||||
|
||||
await connectionCluster.activate();
|
||||
} else {
|
||||
await requestClusterActivation(this.getId(), false);
|
||||
}
|
||||
@@ -90,8 +98,15 @@ export class KubernetesCluster<
|
||||
if (app) {
|
||||
const di = getLegacyGlobalDiForExtensionApi();
|
||||
const getClusterById = di.inject(getClusterByIdInjectable);
|
||||
const cluster = getClusterById(this.getId());
|
||||
|
||||
getClusterById(this.getId())?.disconnect();
|
||||
if (!cluster) {
|
||||
return;
|
||||
}
|
||||
|
||||
const connectionCluster = di.inject(clusterConnectionInjectable, cluster);
|
||||
|
||||
connectionCluster.disconnect();
|
||||
} else {
|
||||
await requestClusterDisconnection(this.getId(), false);
|
||||
}
|
||||
|
||||
@@ -241,7 +241,7 @@ export interface CatalogEntityMetadata extends EntityMetadataObject {
|
||||
shortName?: string;
|
||||
description?: string;
|
||||
source?: string;
|
||||
labels: Record<string, string>;
|
||||
labels: Partial<Record<string, string>>;
|
||||
}
|
||||
|
||||
export interface CatalogEntityStatus {
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
*/
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import { ClusterStore } from "./cluster-store";
|
||||
import { createClusterInjectionToken } from "../cluster/create-cluster-injection-token";
|
||||
import readClusterConfigSyncInjectable from "./read-cluster-config.injectable";
|
||||
import emitAppEventInjectable from "../app-event-bus/emit-event.injectable";
|
||||
import directoryForUserDataInjectable from "../app-paths/directory-for-user-data/directory-for-user-data.injectable";
|
||||
@@ -23,7 +22,6 @@ const clusterStoreInjectable = getInjectable({
|
||||
id: "cluster-store",
|
||||
|
||||
instantiate: (di) => new ClusterStore({
|
||||
createCluster: di.inject(createClusterInjectionToken),
|
||||
readClusterConfigSync: di.inject(readClusterConfigSyncInjectable),
|
||||
emitAppEvent: di.inject(emitAppEventInjectable),
|
||||
directoryForUserData: di.inject(directoryForUserDataInjectable),
|
||||
|
||||
@@ -10,7 +10,6 @@ import { BaseStore } from "../base-store/base-store";
|
||||
import { Cluster } from "../cluster/cluster";
|
||||
import { toJS } from "../utils";
|
||||
import type { ClusterModel, ClusterId } from "../cluster-types";
|
||||
import type { CreateCluster } from "../cluster/create-cluster-injection-token";
|
||||
import type { ReadClusterConfigSync } from "./read-cluster-config.injectable";
|
||||
import type { EmitAppEvent } from "../app-event-bus/emit-event.injectable";
|
||||
|
||||
@@ -19,7 +18,6 @@ export interface ClusterStoreModel {
|
||||
}
|
||||
|
||||
interface Dependencies extends BaseStoreDependencies {
|
||||
createCluster: CreateCluster;
|
||||
readClusterConfigSync: ReadClusterConfigSync;
|
||||
emitAppEvent: EmitAppEvent;
|
||||
}
|
||||
@@ -64,7 +62,7 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
|
||||
|
||||
const cluster = clusterOrModel instanceof Cluster
|
||||
? clusterOrModel
|
||||
: this.dependencies.createCluster(
|
||||
: new Cluster(
|
||||
clusterOrModel,
|
||||
this.dependencies.readClusterConfigSync(clusterOrModel),
|
||||
);
|
||||
@@ -87,7 +85,7 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
|
||||
if (cluster) {
|
||||
cluster.updateModel(clusterModel);
|
||||
} else {
|
||||
cluster = this.dependencies.createCluster(
|
||||
cluster = new Cluster(
|
||||
clusterModel,
|
||||
this.dependencies.readClusterConfigSync(clusterModel),
|
||||
);
|
||||
|
||||
@@ -39,10 +39,6 @@ export const updateClusterModelChecker = Joi.object<UpdateClusterModel>({
|
||||
contextName: Joi.string()
|
||||
.required()
|
||||
.min(1),
|
||||
workspace: Joi.string()
|
||||
.optional(),
|
||||
workspaces: Joi.array()
|
||||
.items(Joi.string()),
|
||||
preferences: Joi.object(),
|
||||
metadata: Joi.object(),
|
||||
accessibleNamespaces: Joi.array()
|
||||
@@ -70,18 +66,6 @@ export interface ClusterModel {
|
||||
/** Path to cluster kubeconfig */
|
||||
kubeConfigPath: string;
|
||||
|
||||
/**
|
||||
* Workspace id
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
workspace?: string;
|
||||
|
||||
/**
|
||||
* @deprecated this is used only for hotbar migrations from 4.2.X
|
||||
*/
|
||||
workspaces?: string[];
|
||||
|
||||
/** User context in kubeconfig */
|
||||
contextName: string;
|
||||
|
||||
@@ -97,7 +81,7 @@ export interface ClusterModel {
|
||||
/**
|
||||
* Labels for the catalog entity
|
||||
*/
|
||||
labels?: Record<string, string>;
|
||||
labels?: Partial<Record<string, string>>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -206,6 +190,6 @@ export interface ClusterState {
|
||||
ready: boolean;
|
||||
isAdmin: boolean;
|
||||
allowedNamespaces: string[];
|
||||
allowedResources: string[];
|
||||
resourcesToShow: string[];
|
||||
isGlobalWatchEnabled: boolean;
|
||||
}
|
||||
@@ -6,7 +6,6 @@
|
||||
import type { KubeConfig, V1ResourceAttributes } from "@kubernetes/client-node";
|
||||
import { AuthorizationV1Api } from "@kubernetes/client-node";
|
||||
import { getInjectable } from "@ogre-tools/injectable";
|
||||
import type { Logger } from "../logger";
|
||||
import loggerInjectable from "../logger.injectable";
|
||||
|
||||
/**
|
||||
@@ -19,41 +18,33 @@ export type CanI = (resourceAttributes: V1ResourceAttributes) => Promise<boolean
|
||||
/**
|
||||
* @param proxyConfig This config's `currentContext` field must be set, and will be used as the target cluster
|
||||
*/
|
||||
export type AuthorizationReview = (proxyConfig: KubeConfig) => CanI;
|
||||
export type CreateAuthorizationReview = (proxyConfig: KubeConfig) => CanI;
|
||||
|
||||
interface Dependencies {
|
||||
logger: Logger;
|
||||
}
|
||||
|
||||
const authorizationReview = ({ logger }: Dependencies): AuthorizationReview => {
|
||||
return (proxyConfig) => {
|
||||
const api = proxyConfig.makeApiClient(AuthorizationV1Api);
|
||||
|
||||
return async (resourceAttributes: V1ResourceAttributes): Promise<boolean> => {
|
||||
try {
|
||||
const { body } = await api.createSelfSubjectAccessReview({
|
||||
apiVersion: "authorization.k8s.io/v1",
|
||||
kind: "SelfSubjectAccessReview",
|
||||
spec: { resourceAttributes },
|
||||
});
|
||||
|
||||
return body.status?.allowed ?? false;
|
||||
} catch (error) {
|
||||
logger.error(`[AUTHORIZATION-REVIEW]: failed to create access review: ${error}`, { resourceAttributes });
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
const authorizationReviewInjectable = getInjectable({
|
||||
const createAuthorizationReviewInjectable = getInjectable({
|
||||
id: "authorization-review",
|
||||
instantiate: (di) => {
|
||||
instantiate: (di): CreateAuthorizationReview => {
|
||||
const logger = di.inject(loggerInjectable);
|
||||
|
||||
return authorizationReview({ logger });
|
||||
return (proxyConfig) => {
|
||||
const api = proxyConfig.makeApiClient(AuthorizationV1Api);
|
||||
|
||||
return async (resourceAttributes: V1ResourceAttributes): Promise<boolean> => {
|
||||
try {
|
||||
const { body } = await api.createSelfSubjectAccessReview({
|
||||
apiVersion: "authorization.k8s.io/v1",
|
||||
kind: "SelfSubjectAccessReview",
|
||||
spec: { resourceAttributes },
|
||||
});
|
||||
|
||||
return body.status?.allowed ?? false;
|
||||
} catch (error) {
|
||||
logger.error(`[AUTHORIZATION-REVIEW]: failed to create access review: ${error}`, { resourceAttributes });
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default authorizationReviewInjectable;
|
||||
export default createAuthorizationReviewInjectable;
|
||||
@@ -3,164 +3,74 @@
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { action, comparer, computed, makeObservable, observable, reaction, runInAction, when } from "mobx";
|
||||
import type { ClusterContextHandler } from "../../main/context-handler/context-handler";
|
||||
import type { KubeConfig } from "@kubernetes/client-node";
|
||||
import { HttpError } from "@kubernetes/client-node";
|
||||
import type { Kubectl } from "../../main/kubectl/kubectl";
|
||||
import type { KubeconfigManager } from "../../main/kubeconfig-manager/kubeconfig-manager";
|
||||
import type { KubeApiResource, KubeApiResourceDescriptor } from "../rbac";
|
||||
import { formatKubeApiResource } from "../rbac";
|
||||
import plimit from "p-limit";
|
||||
import type { ClusterState, ClusterMetricsResourceType, ClusterId, ClusterMetadata, ClusterModel, ClusterPreferences, ClusterPrometheusPreferences, UpdateClusterModel, KubeAuthUpdate, ClusterConfigData } from "../cluster-types";
|
||||
import { ClusterMetadataKey, initialNodeShellImage, ClusterStatus, clusterModelIdChecker, updateClusterModelChecker } from "../cluster-types";
|
||||
import { disposer, isDefined, isRequestError, toJS } from "../utils";
|
||||
import { clusterListNamespaceForbiddenChannel } from "../ipc/cluster";
|
||||
import type { CanI } from "./authorization-review.injectable";
|
||||
import type { ListNamespaces } from "./list-namespaces.injectable";
|
||||
import assert from "assert";
|
||||
import type { Logger } from "../logger";
|
||||
import type { BroadcastMessage } from "../ipc/broadcast-message.injectable";
|
||||
import type { LoadConfigfromFile } from "../kube-helpers/load-config-from-file.injectable";
|
||||
import type { CanListResource, RequestNamespaceListPermissions, RequestNamespaceListPermissionsFor } from "./request-namespace-list-permissions.injectable";
|
||||
import type { RequestApiResources } from "../../main/cluster/request-api-resources.injectable";
|
||||
import type { DetectClusterMetadata } from "../../main/cluster-detectors/detect-cluster-metadata.injectable";
|
||||
import type { FalibleOnlyClusterMetadataDetector } from "../../main/cluster-detectors/token";
|
||||
import { computed, observable, toJS, runInAction } from "mobx";
|
||||
import type { KubeApiResource } from "../rbac";
|
||||
import type { ClusterState, ClusterId, ClusterMetadata, ClusterModel, ClusterPreferences, ClusterPrometheusPreferences, UpdateClusterModel, ClusterConfigData } from "../cluster-types";
|
||||
import { ClusterMetadataKey, clusterModelIdChecker, updateClusterModelChecker } from "../cluster-types";
|
||||
import type { IObservableValue } from "mobx";
|
||||
import { replaceObservableObject } from "../utils/replace-observable-object";
|
||||
import { pick } from "lodash";
|
||||
|
||||
export interface ClusterDependencies {
|
||||
readonly directoryForKubeConfigs: string;
|
||||
readonly logger: Logger;
|
||||
readonly clusterVersionDetector: FalibleOnlyClusterMetadataDetector;
|
||||
detectClusterMetadata: DetectClusterMetadata;
|
||||
createKubeconfigManager: (cluster: Cluster) => KubeconfigManager;
|
||||
createContextHandler: (cluster: Cluster) => ClusterContextHandler;
|
||||
createKubectl: (clusterVersion: string) => Kubectl;
|
||||
createAuthorizationReview: (config: KubeConfig) => CanI;
|
||||
requestApiResources: RequestApiResources;
|
||||
requestNamespaceListPermissionsFor: RequestNamespaceListPermissionsFor;
|
||||
createListNamespaces: (config: KubeConfig) => ListNamespaces;
|
||||
broadcastMessage: BroadcastMessage;
|
||||
loadConfigfromFile: LoadConfigfromFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cluster
|
||||
*
|
||||
* @beta
|
||||
*/
|
||||
export class Cluster implements ClusterModel {
|
||||
/** Unique id for a cluster */
|
||||
public readonly id: ClusterId;
|
||||
private kubeCtl: Kubectl | undefined;
|
||||
export class Cluster {
|
||||
/**
|
||||
* Context handler
|
||||
*
|
||||
* @internal
|
||||
* Unique id for a cluster
|
||||
*/
|
||||
protected readonly _contextHandler: ClusterContextHandler | undefined;
|
||||
protected readonly _proxyKubeconfigManager: KubeconfigManager | undefined;
|
||||
protected readonly eventsDisposer = disposer();
|
||||
protected activated = false;
|
||||
|
||||
public get contextHandler() {
|
||||
// TODO: remove these once main/renderer are seperate classes
|
||||
assert(this._contextHandler, "contextHandler is only defined in the main environment");
|
||||
|
||||
return this._contextHandler;
|
||||
}
|
||||
|
||||
protected get proxyKubeconfigManager() {
|
||||
// TODO: remove these once main/renderer are seperate classes
|
||||
assert(this._proxyKubeconfigManager, "proxyKubeconfigManager is only defined in the main environment");
|
||||
|
||||
return this._proxyKubeconfigManager;
|
||||
}
|
||||
|
||||
get whenReady() {
|
||||
return when(() => this.ready);
|
||||
}
|
||||
readonly id: ClusterId;
|
||||
|
||||
/**
|
||||
* Kubeconfig context name
|
||||
*
|
||||
* @observable
|
||||
*/
|
||||
@observable contextName!: string;
|
||||
readonly contextName = observable.box() as IObservableValue<string>;
|
||||
|
||||
/**
|
||||
* Path to kubeconfig
|
||||
*
|
||||
* @observable
|
||||
*/
|
||||
@observable kubeConfigPath!: string;
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
@observable workspace?: string;
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
@observable workspaces?: string[];
|
||||
readonly kubeConfigPath = observable.box() as IObservableValue<string>;
|
||||
|
||||
/**
|
||||
* Kubernetes API server URL
|
||||
*
|
||||
* @observable
|
||||
*/
|
||||
@observable apiUrl: string; // cluster server url
|
||||
readonly apiUrl: IObservableValue<string>;
|
||||
|
||||
/**
|
||||
* Is cluster online
|
||||
*
|
||||
* @observable
|
||||
* Describes if we can detect that cluster is online
|
||||
*/
|
||||
@observable online = false; // describes if we can detect that cluster is online
|
||||
readonly online = observable.box(false);
|
||||
|
||||
/**
|
||||
* Can user access cluster resources
|
||||
*
|
||||
* @observable
|
||||
* Describes if user is able to access cluster resources
|
||||
*/
|
||||
@observable accessible = false; // if user is able to access cluster resources
|
||||
readonly accessible = observable.box(false);
|
||||
|
||||
/**
|
||||
* Is cluster instance in usable state
|
||||
*
|
||||
* @observable
|
||||
*/
|
||||
@observable ready = false; // cluster is in usable state
|
||||
/**
|
||||
* Is cluster currently reconnecting
|
||||
*
|
||||
* @observable
|
||||
*/
|
||||
@observable reconnecting = false;
|
||||
readonly ready = observable.box(false);
|
||||
|
||||
/**
|
||||
* Is cluster disconnected. False if user has selected to connect.
|
||||
*
|
||||
* @observable
|
||||
*/
|
||||
@observable disconnected = true;
|
||||
readonly disconnected = observable.box(true);
|
||||
|
||||
/**
|
||||
* Does user have admin like access
|
||||
*
|
||||
* @observable
|
||||
*/
|
||||
@observable isAdmin = false;
|
||||
readonly isAdmin = observable.box(false);
|
||||
|
||||
/**
|
||||
* Global watch-api accessibility , e.g. "/api/v1/services?watch=1"
|
||||
*
|
||||
* @observable
|
||||
*/
|
||||
@observable isGlobalWatchEnabled = false;
|
||||
readonly isGlobalWatchEnabled = observable.box(false);
|
||||
|
||||
/**
|
||||
* Preferences
|
||||
*
|
||||
* @observable
|
||||
*/
|
||||
@observable preferences: ClusterPreferences = {};
|
||||
readonly preferences = observable.object<ClusterPreferences>({});
|
||||
|
||||
/**
|
||||
* Metadata
|
||||
*
|
||||
* @observable
|
||||
*/
|
||||
@observable metadata: ClusterMetadata = {};
|
||||
readonly metadata = observable.object<ClusterMetadata>({});
|
||||
|
||||
/**
|
||||
* List of allowed namespaces verified via K8S::SelfSubjectAccessReview api
|
||||
@@ -172,73 +82,47 @@ export class Cluster implements ClusterModel {
|
||||
*/
|
||||
readonly accessibleNamespaces = observable.array<string>();
|
||||
|
||||
private readonly knownResources = observable.array<KubeApiResource>();
|
||||
/**
|
||||
* The list of all known resources associated with this cluster
|
||||
*/
|
||||
readonly knownResources = observable.array<KubeApiResource>();
|
||||
|
||||
// The formatting of this is `group.name` or `name` (if in core)
|
||||
private readonly allowedResources = observable.set<string>();
|
||||
/**
|
||||
* The formatting of this is `group.name` or `name` (if in core)
|
||||
*/
|
||||
readonly resourcesToShow = observable.set<string>();
|
||||
|
||||
/**
|
||||
* Labels for the catalog entity
|
||||
*/
|
||||
@observable labels: Record<string, string> = {};
|
||||
readonly labels = observable.object<Partial<Record<string, string>>>({});
|
||||
|
||||
/**
|
||||
* Is cluster available
|
||||
*
|
||||
* @computed
|
||||
*/
|
||||
@computed get available() {
|
||||
return this.accessible && !this.disconnected;
|
||||
}
|
||||
readonly available = computed(() => this.accessible.get() && !this.disconnected.get());
|
||||
|
||||
/**
|
||||
* Cluster name
|
||||
*
|
||||
* @computed
|
||||
*/
|
||||
@computed get name() {
|
||||
return this.preferences.clusterName || this.contextName;
|
||||
}
|
||||
readonly name = computed(() => this.preferences.clusterName || this.contextName.get());
|
||||
|
||||
/**
|
||||
* The detected kubernetes distribution
|
||||
*/
|
||||
@computed get distribution(): string {
|
||||
return this.metadata[ClusterMetadataKey.DISTRIBUTION]?.toString() || "unknown";
|
||||
}
|
||||
readonly distribution = computed(() => this.metadata[ClusterMetadataKey.DISTRIBUTION]?.toString() || "unknown");
|
||||
|
||||
/**
|
||||
* The detected kubernetes version
|
||||
*/
|
||||
@computed get version(): string {
|
||||
return this.metadata[ClusterMetadataKey.VERSION]?.toString() || "unknown";
|
||||
}
|
||||
readonly version = computed(() => this.metadata[ClusterMetadataKey.VERSION]?.toString() || "unknown");
|
||||
|
||||
/**
|
||||
* Prometheus preferences
|
||||
*
|
||||
* @computed
|
||||
* @internal
|
||||
*/
|
||||
@computed get prometheusPreferences(): ClusterPrometheusPreferences {
|
||||
const { prometheus, prometheusProvider } = this.preferences;
|
||||
|
||||
return toJS({ prometheus, prometheusProvider });
|
||||
}
|
||||
|
||||
/**
|
||||
* defaultNamespace preference
|
||||
*
|
||||
* @computed
|
||||
* @internal
|
||||
*/
|
||||
@computed get defaultNamespace(): string | undefined {
|
||||
return this.preferences.defaultNamespace;
|
||||
}
|
||||
|
||||
constructor(private readonly dependencies: ClusterDependencies, { id, ...model }: ClusterModel, configData: ClusterConfigData) {
|
||||
makeObservable(this);
|
||||
readonly prometheusPreferences = computed(() => pick(toJS(this.preferences), "prometheus", "prometheusProvider") as ClusterPrometheusPreferences);
|
||||
|
||||
constructor({ id, ...model }: ClusterModel, configData: ClusterConfigData) {
|
||||
const { error } = clusterModelIdChecker.validate({ id });
|
||||
|
||||
if (error) {
|
||||
@@ -247,16 +131,7 @@ export class Cluster implements ClusterModel {
|
||||
|
||||
this.id = id;
|
||||
this.updateModel(model);
|
||||
this.apiUrl = configData.clusterServerUrl;
|
||||
|
||||
// for the time being, until renderer gets its own cluster type
|
||||
this._contextHandler = this.dependencies.createContextHandler(this);
|
||||
this._proxyKubeconfigManager = this.dependencies.createKubeconfigManager(this);
|
||||
this.dependencies.logger.debug(`[CLUSTER]: Cluster init success`, {
|
||||
id: this.id,
|
||||
context: this.contextName,
|
||||
apiUrl: this.apiUrl,
|
||||
});
|
||||
this.apiUrl = observable.box(configData.clusterServerUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -264,7 +139,7 @@ export class Cluster implements ClusterModel {
|
||||
*
|
||||
* @param model
|
||||
*/
|
||||
@action updateModel(model: UpdateClusterModel) {
|
||||
updateModel(model: UpdateClusterModel) {
|
||||
// Note: do not assign ID as that should never be updated
|
||||
|
||||
const { error } = updateClusterModelChecker.validate(model, { allowUnknown: true });
|
||||
@@ -273,454 +148,83 @@ export class Cluster implements ClusterModel {
|
||||
throw error;
|
||||
}
|
||||
|
||||
this.kubeConfigPath = model.kubeConfigPath;
|
||||
this.contextName = model.contextName;
|
||||
|
||||
if (model.workspace) {
|
||||
this.workspace = model.workspace;
|
||||
}
|
||||
|
||||
if (model.workspaces) {
|
||||
this.workspaces = model.workspaces;
|
||||
}
|
||||
|
||||
if (model.preferences) {
|
||||
this.preferences = model.preferences;
|
||||
}
|
||||
|
||||
if (model.metadata) {
|
||||
this.metadata = model.metadata;
|
||||
}
|
||||
|
||||
if (model.accessibleNamespaces) {
|
||||
this.accessibleNamespaces.replace(model.accessibleNamespaces);
|
||||
}
|
||||
|
||||
if (model.labels) {
|
||||
this.labels = model.labels;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
protected bindEvents() {
|
||||
this.dependencies.logger.info(`[CLUSTER]: bind events`, this.getMeta());
|
||||
const refreshTimer = setInterval(() => !this.disconnected && this.refresh(), 30000); // every 30s
|
||||
const refreshMetadataTimer = setInterval(() => this.available && this.refreshAccessibilityAndMetadata(), 900000); // every 15 minutes
|
||||
|
||||
this.eventsDisposer.push(
|
||||
reaction(
|
||||
() => this.prometheusPreferences,
|
||||
prefs => this.contextHandler.setupPrometheus(prefs),
|
||||
{ equals: comparer.structural },
|
||||
),
|
||||
() => clearInterval(refreshTimer),
|
||||
() => clearInterval(refreshMetadataTimer),
|
||||
reaction(() => this.defaultNamespace, () => this.recreateProxyKubeconfig()),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
protected async recreateProxyKubeconfig() {
|
||||
this.dependencies.logger.info("[CLUSTER]: Recreating proxy kubeconfig");
|
||||
|
||||
try {
|
||||
await this.proxyKubeconfigManager.clear();
|
||||
await this.getProxyKubeconfig();
|
||||
} catch (error) {
|
||||
this.dependencies.logger.error(`[CLUSTER]: failed to recreate proxy kubeconfig`, error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param force force activation
|
||||
* @internal
|
||||
*/
|
||||
@action
|
||||
async activate(force = false) {
|
||||
if (this.activated && !force) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.dependencies.logger.info(`[CLUSTER]: activate`, this.getMeta());
|
||||
|
||||
if (!this.eventsDisposer.length) {
|
||||
this.bindEvents();
|
||||
}
|
||||
|
||||
if (this.disconnected || !this.accessible) {
|
||||
try {
|
||||
this.broadcastConnectUpdate("Starting connection ...");
|
||||
await this.reconnect();
|
||||
} catch (error) {
|
||||
this.broadcastConnectUpdate(`Failed to start connection: ${error}`, "error");
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
this.broadcastConnectUpdate("Refreshing connection status ...");
|
||||
await this.refreshConnectionStatus();
|
||||
} catch (error) {
|
||||
this.broadcastConnectUpdate(`Failed to connection status: ${error}`, "error");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.accessible) {
|
||||
try {
|
||||
this.broadcastConnectUpdate("Refreshing cluster accessibility ...");
|
||||
await this.refreshAccessibility();
|
||||
} catch (error) {
|
||||
this.broadcastConnectUpdate(`Failed to refresh accessibility: ${error}`, "error");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// download kubectl in background, so it's not blocking dashboard
|
||||
this.ensureKubectl()
|
||||
.catch(error => this.dependencies.logger.warn(`[CLUSTER]: failed to download kubectl for clusterId=${this.id}`, error));
|
||||
this.broadcastConnectUpdate("Connected, waiting for view to load ...");
|
||||
}
|
||||
|
||||
this.activated = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
async ensureKubectl() {
|
||||
this.kubeCtl ??= this.dependencies.createKubectl(this.version);
|
||||
|
||||
await this.kubeCtl.ensureKubectl();
|
||||
|
||||
return this.kubeCtl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
@action
|
||||
async reconnect() {
|
||||
this.dependencies.logger.info(`[CLUSTER]: reconnect`, this.getMeta());
|
||||
await this.contextHandler?.restartServer();
|
||||
this.disconnected = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
@action disconnect(): void {
|
||||
if (this.disconnected) {
|
||||
return void this.dependencies.logger.debug("[CLUSTER]: already disconnected", { id: this.id });
|
||||
}
|
||||
|
||||
this.dependencies.logger.info(`[CLUSTER]: disconnecting`, { id: this.id });
|
||||
this.eventsDisposer();
|
||||
this.contextHandler?.stopServer();
|
||||
this.disconnected = true;
|
||||
this.online = false;
|
||||
this.accessible = false;
|
||||
this.ready = false;
|
||||
this.activated = false;
|
||||
this.allowedNamespaces.clear();
|
||||
this.dependencies.logger.info(`[CLUSTER]: disconnected`, { id: this.id });
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
@action
|
||||
async refresh() {
|
||||
this.dependencies.logger.info(`[CLUSTER]: refresh`, this.getMeta());
|
||||
await this.refreshConnectionStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
@action
|
||||
async refreshAccessibilityAndMetadata() {
|
||||
await this.refreshAccessibility();
|
||||
await this.refreshMetadata();
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
async refreshMetadata() {
|
||||
this.dependencies.logger.info(`[CLUSTER]: refreshMetadata`, this.getMeta());
|
||||
|
||||
const newMetadata = await this.dependencies.detectClusterMetadata(this);
|
||||
|
||||
runInAction(() => {
|
||||
this.metadata = {
|
||||
...this.metadata,
|
||||
...newMetadata,
|
||||
};
|
||||
});
|
||||
}
|
||||
this.kubeConfigPath.set(model.kubeConfigPath);
|
||||
this.contextName.set(model.contextName);
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private async refreshAccessibility(): Promise<void> {
|
||||
this.dependencies.logger.info(`[CLUSTER]: refreshAccessibility`, this.getMeta());
|
||||
const proxyConfig = await this.getProxyKubeconfig();
|
||||
const canI = this.dependencies.createAuthorizationReview(proxyConfig);
|
||||
const requestNamespaceListPermissions = this.dependencies.requestNamespaceListPermissionsFor(proxyConfig);
|
||||
|
||||
this.isAdmin = await canI({
|
||||
namespace: "kube-system",
|
||||
resource: "*",
|
||||
verb: "create",
|
||||
});
|
||||
this.isGlobalWatchEnabled = await canI({
|
||||
verb: "watch",
|
||||
resource: "*",
|
||||
});
|
||||
this.allowedNamespaces.replace(await this.requestAllowedNamespaces(proxyConfig));
|
||||
|
||||
const knownResources = await this.dependencies.requestApiResources(this);
|
||||
|
||||
if (knownResources.callWasSuccessful) {
|
||||
this.knownResources.replace(knownResources.response);
|
||||
} else if (this.knownResources.length > 0) {
|
||||
this.dependencies.logger.warn(`[CLUSTER]: failed to list KUBE resources, sticking with previous list`);
|
||||
} else {
|
||||
this.dependencies.logger.warn(`[CLUSTER]: failed to list KUBE resources for the first time, blocking connection to cluster...`);
|
||||
this.broadcastConnectUpdate("Failed to list kube API resources, please reconnect...", "error");
|
||||
}
|
||||
|
||||
this.allowedResources.replace(await this.getAllowedResources(requestNamespaceListPermissions));
|
||||
this.ready = this.knownResources.length > 0;
|
||||
this.dependencies.logger.debug(`[CLUSTER]: refreshed accessibility data`, this.getState());
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
@action
|
||||
async refreshConnectionStatus() {
|
||||
const connectionStatus = await this.getConnectionStatus();
|
||||
|
||||
this.online = connectionStatus > ClusterStatus.Offline;
|
||||
this.accessible = connectionStatus == ClusterStatus.AccessGranted;
|
||||
}
|
||||
|
||||
async getKubeconfig(): Promise<KubeConfig> {
|
||||
const { config } = await this.dependencies.loadConfigfromFile(this.kubeConfigPath);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
async getProxyKubeconfig(): Promise<KubeConfig> {
|
||||
const proxyKCPath = await this.getProxyKubeconfigPath();
|
||||
const { config } = await this.dependencies.loadConfigfromFile(proxyKCPath);
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
async getProxyKubeconfigPath(): Promise<string> {
|
||||
return this.proxyKubeconfigManager.getPath();
|
||||
}
|
||||
|
||||
protected async getConnectionStatus(): Promise<ClusterStatus> {
|
||||
try {
|
||||
const versionData = await this.dependencies.clusterVersionDetector.detect(this);
|
||||
|
||||
this.metadata.version = versionData.value;
|
||||
|
||||
return ClusterStatus.AccessGranted;
|
||||
} catch (error) {
|
||||
this.dependencies.logger.error(`[CLUSTER]: Failed to connect to "${this.contextName}": ${error}`);
|
||||
|
||||
if (isRequestError(error)) {
|
||||
if (error.statusCode) {
|
||||
if (error.statusCode >= 400 && error.statusCode < 500) {
|
||||
this.broadcastConnectUpdate("Invalid credentials", "error");
|
||||
|
||||
return ClusterStatus.AccessDenied;
|
||||
}
|
||||
|
||||
const message = String(error.error || error.message) || String(error);
|
||||
|
||||
this.broadcastConnectUpdate(message, "error");
|
||||
|
||||
return ClusterStatus.Offline;
|
||||
}
|
||||
|
||||
if (error.failed === true) {
|
||||
if (error.timedOut === true) {
|
||||
this.broadcastConnectUpdate("Connection timed out", "error");
|
||||
|
||||
return ClusterStatus.Offline;
|
||||
}
|
||||
|
||||
this.broadcastConnectUpdate("Failed to fetch credentials", "error");
|
||||
|
||||
return ClusterStatus.AccessDenied;
|
||||
}
|
||||
|
||||
const message = String(error.error || error.message) || String(error);
|
||||
|
||||
this.broadcastConnectUpdate(message, "error");
|
||||
} else if (error instanceof Error || typeof error === "string") {
|
||||
this.broadcastConnectUpdate(`${error}`, "error");
|
||||
} else {
|
||||
this.broadcastConnectUpdate("Unknown error has occurred", "error");
|
||||
if (model.preferences) {
|
||||
replaceObservableObject(this.preferences, model.preferences);
|
||||
}
|
||||
|
||||
return ClusterStatus.Offline;
|
||||
}
|
||||
if (model.metadata) {
|
||||
replaceObservableObject(this.metadata, model.metadata);
|
||||
}
|
||||
|
||||
if (model.accessibleNamespaces) {
|
||||
this.accessibleNamespaces.replace(model.accessibleNamespaces);
|
||||
}
|
||||
|
||||
if (model.labels) {
|
||||
replaceObservableObject(this.labels, model.labels);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
toJSON(): ClusterModel {
|
||||
return toJS({
|
||||
return {
|
||||
id: this.id,
|
||||
contextName: this.contextName,
|
||||
kubeConfigPath: this.kubeConfigPath,
|
||||
workspace: this.workspace,
|
||||
workspaces: this.workspaces,
|
||||
preferences: this.preferences,
|
||||
metadata: this.metadata,
|
||||
accessibleNamespaces: this.accessibleNamespaces,
|
||||
labels: this.labels,
|
||||
});
|
||||
contextName: this.contextName.get(),
|
||||
kubeConfigPath: this.kubeConfigPath.get(),
|
||||
preferences: toJS(this.preferences),
|
||||
metadata: toJS(this.metadata),
|
||||
accessibleNamespaces: this.accessibleNamespaces.toJSON(),
|
||||
labels: toJS(this.labels),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializable cluster-state used for sync btw main <-> renderer
|
||||
*/
|
||||
getState(): ClusterState {
|
||||
return toJS({
|
||||
apiUrl: this.apiUrl,
|
||||
online: this.online,
|
||||
ready: this.ready,
|
||||
disconnected: this.disconnected,
|
||||
accessible: this.accessible,
|
||||
isAdmin: this.isAdmin,
|
||||
allowedNamespaces: this.allowedNamespaces,
|
||||
allowedResources: [...this.allowedResources],
|
||||
isGlobalWatchEnabled: this.isGlobalWatchEnabled,
|
||||
});
|
||||
return {
|
||||
apiUrl: this.apiUrl.get(),
|
||||
online: this.online.get(),
|
||||
ready: this.ready.get(),
|
||||
disconnected: this.disconnected.get(),
|
||||
accessible: this.accessible.get(),
|
||||
isAdmin: this.isAdmin.get(),
|
||||
allowedNamespaces: this.allowedNamespaces.toJSON(),
|
||||
resourcesToShow: this.resourcesToShow.toJSON(),
|
||||
isGlobalWatchEnabled: this.isGlobalWatchEnabled.get(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @param state cluster state
|
||||
*/
|
||||
@action setState(state: ClusterState) {
|
||||
this.accessible = state.accessible;
|
||||
this.allowedNamespaces.replace(state.allowedNamespaces);
|
||||
this.allowedResources.replace(state.allowedResources);
|
||||
this.apiUrl = state.apiUrl;
|
||||
this.disconnected = state.disconnected;
|
||||
this.isAdmin = state.isAdmin;
|
||||
this.isGlobalWatchEnabled = state.isGlobalWatchEnabled;
|
||||
this.online = state.online;
|
||||
this.ready = state.ready;
|
||||
setState(state: ClusterState) {
|
||||
runInAction(() => {
|
||||
this.accessible.set(state.accessible);
|
||||
this.allowedNamespaces.replace(state.allowedNamespaces);
|
||||
this.resourcesToShow.replace(state.resourcesToShow);
|
||||
this.apiUrl.set(state.apiUrl);
|
||||
this.disconnected.set(state.disconnected);
|
||||
this.isAdmin.set(state.isAdmin);
|
||||
this.isGlobalWatchEnabled.set(state.isGlobalWatchEnabled);
|
||||
this.online.set(state.online);
|
||||
this.ready.set(state.ready);
|
||||
});
|
||||
}
|
||||
|
||||
// get cluster system meta, e.g. use in "logger"
|
||||
getMeta() {
|
||||
return {
|
||||
id: this.id,
|
||||
name: this.contextName,
|
||||
ready: this.ready,
|
||||
online: this.online,
|
||||
accessible: this.accessible,
|
||||
disconnected: this.disconnected,
|
||||
name: this.contextName.get(),
|
||||
ready: this.ready.get(),
|
||||
online: this.online.get(),
|
||||
accessible: this.accessible.get(),
|
||||
disconnected: this.disconnected.get(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* broadcast an authentication update concerning this cluster
|
||||
* @internal
|
||||
*/
|
||||
broadcastConnectUpdate(message: string, level: KubeAuthUpdate["level"] = "info"): void {
|
||||
const update: KubeAuthUpdate = { message, level };
|
||||
|
||||
this.dependencies.logger.debug(`[CLUSTER]: broadcasting connection update`, { ...update, meta: this.getMeta() });
|
||||
this.dependencies.broadcastMessage(`cluster:${this.id}:connection-update`, update);
|
||||
}
|
||||
|
||||
protected async requestAllowedNamespaces(proxyConfig: KubeConfig) {
|
||||
if (this.accessibleNamespaces.length) {
|
||||
return this.accessibleNamespaces;
|
||||
}
|
||||
|
||||
try {
|
||||
const listNamespaces = this.dependencies.createListNamespaces(proxyConfig);
|
||||
|
||||
return await listNamespaces();
|
||||
} catch (error) {
|
||||
const ctx = proxyConfig.getContextObject(this.contextName);
|
||||
const namespaceList = [ctx?.namespace].filter(isDefined);
|
||||
|
||||
if (namespaceList.length === 0 && error instanceof HttpError && error.statusCode === 403) {
|
||||
const { response } = error as HttpError & { response: { body: unknown }};
|
||||
|
||||
this.dependencies.logger.info("[CLUSTER]: listing namespaces is forbidden, broadcasting", { clusterId: this.id, error: response.body });
|
||||
this.dependencies.broadcastMessage(clusterListNamespaceForbiddenChannel, this.id);
|
||||
}
|
||||
|
||||
return namespaceList;
|
||||
}
|
||||
}
|
||||
|
||||
protected async getAllowedResources(requestNamespaceListPermissions: RequestNamespaceListPermissions) {
|
||||
if (!this.allowedNamespaces.length || !this.knownResources.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
const apiLimit = plimit(5); // 5 concurrent api requests
|
||||
const canListResourceCheckers = await Promise.all((
|
||||
this.allowedNamespaces.map(namespace => apiLimit(() => requestNamespaceListPermissions(namespace)))
|
||||
));
|
||||
const canListNamespacedResource: CanListResource = (resource) => canListResourceCheckers.some(fn => fn(resource));
|
||||
|
||||
return this.knownResources
|
||||
.filter(canListNamespacedResource)
|
||||
.map(formatKubeApiResource);
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
shouldShowResource(resource: KubeApiResourceDescriptor): boolean {
|
||||
if (this.allowedResources.size === 0) {
|
||||
// better to show than hide everything
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.allowedResources.has(formatKubeApiResource(resource));
|
||||
}
|
||||
|
||||
isMetricHidden(resource: ClusterMetricsResourceType): boolean {
|
||||
return Boolean(this.preferences.hiddenMetrics?.includes(resource));
|
||||
}
|
||||
|
||||
get nodeShellImage(): string {
|
||||
return this.preferences?.nodeShellImage || initialNodeShellImage;
|
||||
}
|
||||
|
||||
get imagePullSecret(): string | undefined {
|
||||
return this.preferences?.imagePullSecret;
|
||||
}
|
||||
|
||||
isInLocalKubeconfig() {
|
||||
return this.kubeConfigPath.startsWith(this.dependencies.directoryForKubeConfigs);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import { getInjectionToken } from "@ogre-tools/injectable";
|
||||
import type { ClusterConfigData, ClusterModel } from "../cluster-types";
|
||||
import type { Cluster } from "./cluster";
|
||||
|
||||
export type CreateCluster = (model: ClusterModel, configData: ClusterConfigData) => Cluster;
|
||||
|
||||
export const createClusterInjectionToken = getInjectionToken<CreateCluster>({
|
||||
id: "create-cluster-token",
|
||||
});
|
||||
@@ -9,21 +9,21 @@ import { isDefined } from "../utils";
|
||||
|
||||
export type ListNamespaces = () => Promise<string[]>;
|
||||
|
||||
export function listNamespaces(config: KubeConfig): ListNamespaces {
|
||||
const coreApi = config.makeApiClient(CoreV1Api);
|
||||
export type CreateListNamespaces = (config: KubeConfig) => ListNamespaces;
|
||||
|
||||
return async () => {
|
||||
const { body: { items }} = await coreApi.listNamespace();
|
||||
const createListNamespacesInjectable = getInjectable({
|
||||
id: "create-list-namespaces",
|
||||
instantiate: (): CreateListNamespaces => (config) => {
|
||||
const coreApi = config.makeApiClient(CoreV1Api);
|
||||
|
||||
return items
|
||||
.map(ns => ns.metadata?.name)
|
||||
.filter(isDefined);
|
||||
};
|
||||
}
|
||||
return async () => {
|
||||
const { body: { items }} = await coreApi.listNamespace();
|
||||
|
||||
const listNamespacesInjectable = getInjectable({
|
||||
id: "list-namespaces",
|
||||
instantiate: () => listNamespaces,
|
||||
return items
|
||||
.map(ns => ns.metadata?.name)
|
||||
.filter(isDefined);
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default listNamespacesInjectable;
|
||||
export default createListNamespacesInjectable;
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
import type { KubeConfig } from "@kubernetes/client-node";
|
||||
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
|
||||
import type { Cluster } from "./cluster";
|
||||
import loadConfigFromFileInjectable from "../kube-helpers/load-config-from-file.injectable";
|
||||
import type { ConfigResult } from "../kube-helpers";
|
||||
|
||||
export interface LoadKubeconfig {
|
||||
(fullResult?: false): Promise<KubeConfig>;
|
||||
(fullResult: true): Promise<ConfigResult>;
|
||||
}
|
||||
|
||||
const loadKubeconfigInjectable = getInjectable({
|
||||
id: "load-kubeconfig",
|
||||
instantiate: (di, cluster) => {
|
||||
const loadConfigFromFile = di.inject(loadConfigFromFileInjectable);
|
||||
|
||||
return (async (fullResult = false) => {
|
||||
const result = await loadConfigFromFile(cluster.kubeConfigPath.get());
|
||||
|
||||
if (fullResult) {
|
||||
return result;
|
||||
}
|
||||
|
||||
return result.config;
|
||||
}) as LoadKubeconfig;
|
||||
},
|
||||
lifecycle: lifecycleEnum.keyedSingleton({
|
||||
getInstanceKey: (di, cluster: Cluster) => cluster.id,
|
||||
}),
|
||||
});
|
||||
|
||||
export default loadKubeconfigInjectable;
|
||||
@@ -17,10 +17,10 @@ import { KubeApi } from "../kube-api";
|
||||
import { KubeObject } from "../kube-object";
|
||||
import { KubeObjectStore } from "../kube-object.store";
|
||||
import maybeKubeApiInjectable from "../maybe-kube-api.injectable";
|
||||
import { createClusterInjectionToken } from "../../cluster/create-cluster-injection-token";
|
||||
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
import { KubeApi as ExternalKubeApi } from "../../../extensions/common-api/k8s-api";
|
||||
import { Cluster } from "../../cluster/cluster";
|
||||
|
||||
class TestApi extends KubeApi<KubeObject> {
|
||||
protected async checkPreferredVersion() {
|
||||
@@ -43,9 +43,7 @@ describe("ApiManager", () => {
|
||||
di.override(directoryForKubeConfigsInjectable, () => "/some-kube-configs");
|
||||
di.override(storesAndApisCanBeCreatedInjectable, () => true);
|
||||
|
||||
const createCluster = di.inject(createClusterInjectionToken);
|
||||
|
||||
di.override(hostedClusterInjectable, () => createCluster({
|
||||
di.override(hostedClusterInjectable, () => new Cluster({
|
||||
contextName: "some-context-name",
|
||||
id: "some-cluster-id",
|
||||
kubeConfigPath: "/some-path-to-a-kubeconfig",
|
||||
|
||||
@@ -22,7 +22,7 @@ import type { DiContainer } from "@ogre-tools/injectable";
|
||||
import ingressApiInjectable from "../endpoints/ingress.api.injectable";
|
||||
import loggerInjectable from "../../logger.injectable";
|
||||
import maybeKubeApiInjectable from "../maybe-kube-api.injectable";
|
||||
import { createClusterInjectionToken } from "../../cluster/create-cluster-injection-token";
|
||||
import { Cluster } from "../../cluster/cluster";
|
||||
|
||||
describe("KubeApi", () => {
|
||||
let fetchMock: AsyncFnMock<Fetch>;
|
||||
@@ -39,9 +39,7 @@ describe("KubeApi", () => {
|
||||
di.override(directoryForKubeConfigsInjectable, () => "/some-kube-configs");
|
||||
di.override(storesAndApisCanBeCreatedInjectable, () => true);
|
||||
|
||||
const createCluster = di.inject(createClusterInjectionToken);
|
||||
|
||||
di.override(hostedClusterInjectable, () => createCluster({
|
||||
di.override(hostedClusterInjectable, () => new Cluster({
|
||||
contextName: "some-context-name",
|
||||
id: "some-cluster-id",
|
||||
kubeConfigPath: "/some-path-to-a-kubeconfig",
|
||||
|
||||
@@ -35,7 +35,7 @@ import namespaceApiInjectable from "../endpoints/namespace.api.injectable";
|
||||
// NOTE: this is fine because we are testing something that only exported
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
import { PodsApi } from "../../../extensions/common-api/k8s-api";
|
||||
import { createClusterInjectionToken } from "../../cluster/create-cluster-injection-token";
|
||||
import { Cluster } from "../../cluster/cluster";
|
||||
|
||||
describe("createKubeApiForRemoteCluster", () => {
|
||||
let createKubeApiForRemoteCluster: CreateKubeApiForRemoteCluster;
|
||||
@@ -48,9 +48,7 @@ describe("createKubeApiForRemoteCluster", () => {
|
||||
di.override(directoryForKubeConfigsInjectable, () => "/some-kube-configs");
|
||||
di.override(storesAndApisCanBeCreatedInjectable, () => true);
|
||||
|
||||
const createCluster = di.inject(createClusterInjectionToken);
|
||||
|
||||
di.override(hostedClusterInjectable, () => createCluster({
|
||||
di.override(hostedClusterInjectable, () => new Cluster({
|
||||
contextName: "some-context-name",
|
||||
id: "some-cluster-id",
|
||||
kubeConfigPath: "/some-path-to-a-kubeconfig",
|
||||
@@ -154,10 +152,9 @@ describe("KubeApi", () => {
|
||||
fetchMock = asyncFn();
|
||||
di.override(fetchInjectable, () => fetchMock);
|
||||
|
||||
const createCluster = di.inject(createClusterInjectionToken);
|
||||
const createKubeJsonApi = di.inject(createKubeJsonApiInjectable);
|
||||
|
||||
di.override(hostedClusterInjectable, () => createCluster({
|
||||
di.override(hostedClusterInjectable, () => new Cluster({
|
||||
contextName: "some-context-name",
|
||||
id: "some-cluster-id",
|
||||
kubeConfigPath: "/some-path-to-a-kubeconfig",
|
||||
|
||||
@@ -8,11 +8,11 @@ import type { ConfigResult } from "../kube-helpers";
|
||||
import { loadConfigFromString } from "../kube-helpers";
|
||||
import resolveTildeInjectable from "../path/resolve-tilde.injectable";
|
||||
|
||||
export type LoadConfigfromFile = (filePath: string) => Promise<ConfigResult>;
|
||||
export type LoadConfigFromFile = (filePath: string) => Promise<ConfigResult>;
|
||||
|
||||
const loadConfigfromFileInjectable = getInjectable({
|
||||
id: "load-configfrom-file",
|
||||
instantiate: (di): LoadConfigfromFile => {
|
||||
const loadConfigFromFileInjectable = getInjectable({
|
||||
id: "load-config-from-file",
|
||||
instantiate: (di): LoadConfigFromFile => {
|
||||
const readFile = di.inject(readFileInjectable);
|
||||
const resolveTilde = di.inject(resolveTildeInjectable);
|
||||
|
||||
@@ -20,4 +20,4 @@ const loadConfigfromFileInjectable = getInjectable({
|
||||
},
|
||||
});
|
||||
|
||||
export default loadConfigfromFileInjectable;
|
||||
export default loadConfigFromFileInjectable;
|
||||
@@ -25,7 +25,7 @@ export interface BackoffCallerOptions<E> {
|
||||
maxAttempts?: number;
|
||||
|
||||
/**
|
||||
* In miliseconds
|
||||
* In milliseconds
|
||||
* @default 1000
|
||||
*/
|
||||
initialTimeout?: number;
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||
*/
|
||||
|
||||
import { runInAction } from "mobx";
|
||||
|
||||
export function replaceObservableObject<Key extends string | number | symbol>(target: Partial<Record<Key, unknown>>, source: Partial<Record<Key, unknown>>): void {
|
||||
runInAction(() => {
|
||||
for (const key in target) {
|
||||
if (!(key in source)) {
|
||||
delete target[key];
|
||||
}
|
||||
}
|
||||
|
||||
Object.assign(target, source);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user