diff --git a/packages/common/package.json b/packages/common/package.json index 411aca2a..e58dde8e 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -29,6 +29,7 @@ "@provisioner/contracts": "^0.0.29", "debug": "^4.1.1", "generate-password": "^1.6.0", + "jsonpath-plus": "^5.0.7", "jsonpointer": "^4.1.0", "mixwith": "^0.1.1", "ulid": "^2.3.0" diff --git a/packages/common/src/helpers/deployment.ts b/packages/common/src/helpers/deployment.ts index d5a4b0c0..8fcc0879 100644 --- a/packages/common/src/helpers/deployment.ts +++ b/packages/common/src/helpers/deployment.ts @@ -1,9 +1,12 @@ -import { Cluster } from '@c6o/kubeclient-contracts' -import { Deployment } from '@c6o/kubeclient-resources/apps/v1' +import { JSONPath } from 'jsonpath-plus' +import { Cluster, keyValue } from '@c6o/kubeclient-contracts' +import { Deployment, DeploymentList } from '@c6o/kubeclient-resources/apps/v1' import { DeploymentHelper as DeploymentHelperContract } from '@provisioner/contracts' export class DeploymentHelper extends DeploymentHelperContract { + resourceList: DeploymentList + static from = (namespace?: string, name?: string) => new DeploymentHelper(DeploymentHelperContract.template(namespace, name)) @@ -20,4 +23,43 @@ export class DeploymentHelper extends Deploym await cluster.patch(deployment, { spec: { replicas: previousCount } }) return deployment } + + /** Get the containers from the template spec for pods. */ + static containers(deployments: Deployment[], section?) { + return deployments.reduce((acc, deployment) => { + return [...acc, ...deployment.spec.template.spec.containers.reduce((acc2, container) => { + if (section && container[section]) acc2.push(container[section]) + else if (!section) acc2.push(container) + return acc2 + }, [])] + }, []) + } + + /** Returns container references to environment variables, the envFrom list. */ + static keyMapReferences(deployments: Deployment[]) { + const containers = DeploymentHelper.containers(deployments, 'envFrom') + return containers ? containers.reduce((acc1, container) => { + return container ? container.reduce((acc2, env) => { + acc2.push(env) + return acc2 + }, acc1) : [] + }, []) : [] + } + + /** Get environment variables that are written directly into the deployment's templates for pods. */ + static toKeyValues(deployments: Deployment[], merge: keyValue = {}): keyValue { + const envs = JSONPath({ path: '$[*].spec.template.spec.containers[*].env[*]', json: deployments }) + return envs.reduce((acc, env) => { + acc[env.name] = env.value + return acc + }, merge) + } + + /** Get the resource and convert the environment variables to a key map and return it */ + async toKeyValues(cluster: Cluster, merge: keyValue | Promise = {}) { + const result = await cluster.read(this.resource) + result.throwIfError() + this.resourceList = result.as() + return DeploymentHelper.toKeyValues(result.object.items as T[], await merge) + } } \ No newline at end of file diff --git a/packages/common/src/helpers/secret.ts b/packages/common/src/helpers/secret.ts index d9245607..4b9d501b 100644 --- a/packages/common/src/helpers/secret.ts +++ b/packages/common/src/helpers/secret.ts @@ -24,13 +24,6 @@ export class SecretHelper extends SecretHelperContrac const result = await cluster.read(this.resource) result.throwIfError() this.resourceList = result.as() - return result.object.items.reduce((acc, secret: Secret) => { - if (!secret.data) return acc - const data = {} - Object.keys(secret.data).forEach(key => - data[key] = Buffer.from(secret.data[key], 'base64') - ) - return { ...acc, ...data } - }, await merge) + return SecretHelper.toKeyValues(result.object.items as Secret[], await merge) } } \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 208d89ec..eff3c5b5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8059,6 +8059,11 @@ jsonpath-plus@^0.19.0: resolved "https://registry.yarnpkg.com/jsonpath-plus/-/jsonpath-plus-0.19.0.tgz#b901e57607055933dc9a8bef0cc25160ee9dd64c" integrity sha512-GSVwsrzW9LsA5lzsqe4CkuZ9wp+kxBb2GwNniaWzI2YFn5Ig42rSW8ZxVpWXaAfakXNrx5pgY5AbQq7kzX29kg== +jsonpath-plus@^5.0.7: + version "5.0.7" + resolved "https://registry.yarnpkg.com/jsonpath-plus/-/jsonpath-plus-5.0.7.tgz#95fb437ebb69c67595208711a69c95735cbff45b" + integrity sha512-7TS6wsiw1s2UMK/A6nA4n0aUJuirCVhJ87nWX5je5MPOl0z5VTr2qs7nMP8NZ2ed3rlt6kePTqddgVPE9F0i0w== + jsonpointer@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/jsonpointer/-/jsonpointer-4.1.0.tgz#501fb89986a2389765ba09e6053299ceb4f2c2cc"