1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/packages/core/src/common/k8s/resource-stack.ts
Sebastian Malton 2789bcebcb
Convert runMany and runManySync to use injectManyWithMeta + move to seperate package (#7244)
* Convert runMany and runManySync to use injectManyWithMeta

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup type errors due to new Runnable requirements

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Add documentation for verifyRunnablesAreDAG

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Simplify convertToWithIdWith

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move all utility functions to separate package

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move testing utilities to separate package

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move run-many and run-many-sync to separate package

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Replace all internal uses of utilities with new packages

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Use new @k8slens/run-many package in core

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Add dep to open-lens

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup type errors

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup uses of @k8slens/test-utils

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup getGlobalOverride

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Move tests to new package too

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix type errors

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup uses of AsyncResult and autoBind

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fixup remaining import issues

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Finial fixups to fix build

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix lint

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Revert moving "testUsingFakeTime" to separate package

- This fixes tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix integration tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix unit test failing due to spelling fix

Signed-off-by: Sebastian Malton <sebastian@malton.name>

---------

Signed-off-by: Sebastian Malton <sebastian@malton.name>
2023-03-10 10:07:28 +02:00

144 lines
5.1 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import hb from "handlebars";
import type { KubernetesCluster } from "../catalog-entities";
import yaml from "js-yaml";
import { getLegacyGlobalDiForExtensionApi } from "../../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
import productNameInjectable from "../vars/product-name.injectable";
import type { AsyncResult } from "@k8slens/utilities";
import type { Logger } from "../logger";
import type { KubectlApplyAll, KubectlDeleteAll } from "../kube-helpers/channels";
import type { ReadDirectory } from "../fs/read-directory.injectable";
import type { JoinPaths } from "../path/join-paths.injectable";
import type { ReadFile } from "../fs/read-file.injectable";
import { hasTypedProperty, isObject } from "@k8slens/utilities";
export interface ResourceApplyingStack {
kubectlApplyFolder(folderPath: string, templateContext?: any, extraArgs?: string[]): Promise<string>;
kubectlDeleteFolder(folderPath: string, templateContext?: any, extraArgs?: string[]): Promise<string>;
}
export interface ResourceStackDependencies {
readonly logger: Logger;
kubectlApplyAll: KubectlApplyAll;
kubectlDeleteAll: KubectlDeleteAll;
readDirectory: ReadDirectory;
joinPaths: JoinPaths;
readFile: ReadFile;
}
export class ResourceStack {
constructor(
protected readonly dependencies: ResourceStackDependencies,
protected readonly cluster: KubernetesCluster,
protected readonly name: string,
) {}
/**
*
* @param folderPath folder path that is searched for files defining kubernetes resources.
* @param templateContext sets the template parameters that are to be applied to any templated kubernetes resources that are to be applied.
*/
async kubectlApplyFolder(folderPath: string, templateContext?: any, extraArgs?: string[]): Promise<string> {
const resources = await this.renderTemplates(folderPath, templateContext);
const result = await this.applyResources(resources, extraArgs);
if (result.callWasSuccessful) {
return result.response;
}
this.dependencies.logger.warn(`[RESOURCE-STACK]: failed to apply resources: ${result.error}`);
throw new Error(result.error);
}
/**
*
* @param folderPath folder path that is searched for files defining kubernetes resources.
* @param templateContext sets the template parameters that are to be applied to any templated kubernetes resources that are to be applied.
*/
async kubectlDeleteFolder(folderPath: string, templateContext?: any, extraArgs?: string[]): Promise<string> {
const resources = await this.renderTemplates(folderPath, templateContext);
const result = await this.deleteResources(resources, extraArgs);
if (result.callWasSuccessful) {
return result.response;
}
this.dependencies.logger.warn(`[RESOURCE-STACK]: failed to delete resources: ${result.error}`);
return "";
}
protected async applyResources(resources: string[], extraArgs: string[] = []): AsyncResult<string, string> {
const kubectlArgs = [...extraArgs, ...this.getAdditionalArgs(extraArgs)];
return this.dependencies.kubectlApplyAll({
clusterId: this.cluster.getId(),
resources,
extraArgs: kubectlArgs,
});
}
protected async deleteResources(resources: string[], extraArgs: string[] = []): AsyncResult<string, string> {
const kubectlArgs = [...extraArgs, ...this.getAdditionalArgs(extraArgs)];
return this.dependencies.kubectlDeleteAll({
clusterId: this.cluster.getId(),
resources,
extraArgs: kubectlArgs,
});
}
protected getAdditionalArgs(kubectlArgs: string[]): string[] {
if (!kubectlArgs.includes("-l") && !kubectlArgs.includes("--label")) {
return ["-l", `app.kubernetes.io/name=${this.name}`];
}
return [];
}
protected async renderTemplates(folderPath: string, templateContext: any): Promise<string[]> {
const resources: string[] = [];
const di = getLegacyGlobalDiForExtensionApi();
const productName = di.inject(productNameInjectable);
this.dependencies.logger.info(`[RESOURCE-STACK]: render templates from ${folderPath}`);
const files = await this.dependencies.readDirectory(folderPath);
for (const filename of files) {
const file = this.dependencies.joinPaths(folderPath, filename);
const raw = await this.dependencies.readFile(file);
const data = (
filename.endsWith(".hb")
? hb.compile(raw)(templateContext)
: raw
).trim();
if (!data) {
continue;
}
for (const entry of yaml.loadAll(data)) {
if (typeof entry !== "object" || !entry) {
continue;
}
if (hasTypedProperty(entry, "metadata", isObject)) {
const labels = (entry.metadata.labels ??= {}) as Partial<Record<string, string>>;
labels["app.kubernetes.io/name"] = this.name;
labels["app.kubernetes.io/managed-by"] = productName;
labels["app.kubernetes.io/created-by"] = "resource-stack";
}
resources.push(yaml.dump(entry));
}
}
return resources;
}
}