Skip to content

Commit

Permalink
chore: Support grabbing of environment variables (#257)
Browse files Browse the repository at this point in the history
* Update of helpers to support grabbing of environment variables and environment file references.

* removed commented out code.

* Sample using JSON Path

* Removed commented out code

Co-authored-by: Narayan Sainaney <[email protected]>
  • Loading branch information
robblovell and nsainaney authored Jun 15, 2021
1 parent 08d7064 commit 55853fb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
46 changes: 44 additions & 2 deletions packages/common/src/helpers/deployment.ts
Original file line number Diff line number Diff line change
@@ -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<T extends Deployment = Deployment> extends DeploymentHelperContract<T> {

resourceList: DeploymentList

static from = (namespace?: string, name?: string) =>
new DeploymentHelper(DeploymentHelperContract.template(namespace, name))

Expand All @@ -20,4 +23,43 @@ export class DeploymentHelper<T extends Deployment = Deployment> 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<keyValue> = {}) {
const result = await cluster.read(this.resource)
result.throwIfError()
this.resourceList = result.as<DeploymentList>()
return DeploymentHelper.toKeyValues(result.object.items as T[], await merge)
}
}
9 changes: 1 addition & 8 deletions packages/common/src/helpers/secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ export class SecretHelper<T extends Secret = Secret> extends SecretHelperContrac
const result = await cluster.read(this.resource)
result.throwIfError()
this.resourceList = result.as<SecretList>()
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)
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 55853fb

Please sign in to comment.