Skip to content

Commit

Permalink
Merge pull request #20 from ThinkDeepTech/hm/init-impl
Browse files Browse the repository at this point in the history
Updated README
  • Loading branch information
haydenmcp authored Apr 9, 2022
2 parents 650a2c0 + 93c9aa9 commit 7c9a85f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
76 changes: 75 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,78 @@ Simple interface wrapping the kubernetes javascript client.

# Dependencies
- [Kubernetes javascript client](https://github.com/kubernetes-client/javascript) v0.15
- Tested on Node v16.13.1
- Node v16.13.1

# Installation
```console
npm i @thinkdeep/k8s
```

# Usage

Assuming the role binding linking the necessary role and service account has the needed permissions:

```javascript

import { K8sClient, stringify } from '@thinkdeep/k8s';

const client = await new K8sClient().init();

const options = {
name: 'dynamic-cron-job',
namespace: 'production',
schedule: '* * * * *',
command: 'echo',
args: ['Hello World']
};

// Assuming environment variables have been defined...
const cronJob = await client.create(`
apiVersion: "batch/v1"
kind: "CronJob"
metadata:
name: "${options.name}"
namespace: "${options.namespace || "default"}"
spec:
schedule: "${options.schedule}"
jobTemplate:
spec:
template:
spec:
containers:
- name: "${process.env.HELM_RELEASE_NAME}-data-collector"
image: "${options.image}"
command: ["${options.command}"]
args: ${JSON.stringify(options.args)}
envFrom:
- secretRef:
name: "${process.env.HELM_RELEASE_NAME}-deep-microservice-collection-secret"
${ process.env.PREDECOS_KAFKA_SECRET ? `
- secretRef:
name: "${process.env.PREDECOS_KAFKA_SECRET}"
` : ``}
serviceAccountName: "${process.env.HELM_RELEASE_NAME}-secret-accessor-service-account"
restartPolicy: "Never"
imagePullSecrets:
- name: "docker-secret"
`);

console.log(`Created cron job:\n${stringify(cronJob)}`);

const microserviceDeployment = await client.get('deployment', 'my-deployment', 'production');
if (!microserviceDeployment.status.readyReplicas) {
await client.delete(cronJob);
console.log(`Deleted cron job:\n${stringify(cronJob)}`);
}

const cronJobs = await client.getAll('cronjob', 'production');
for (const cronJob of cronJobs) {
console.log(`Found:\n${stringify(cronJob)}`);

if (cronJob.metadata.name === 'some-name-thats-present') {
cronJob.spec.schedule = '0 */12 * * *';
await client.apply(cronJob);
}
}

```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@thinkdeep/k8s",
"version": "3.0.4",
"version": "3.0.5",
"description": "K8s node client tag for processing yaml configurations in javascript code.",
"main": "src/k8s-client.mjs",
"repository": "[email protected]:ThinkDeepTech/k8s.git",
Expand Down

0 comments on commit 7c9a85f

Please sign in to comment.