@thinkdeep/k8s

5.0.11 • Public • Published

@thinkdeep/k8s

CircleCI Quality Gate Status Security Rating Coverage Status Maintainability Vulnerabilities

Node client interfacing with a k8s cluster using simple functions and yaml template strings. If you're just interested in creating k8s javascript client objects using template strings, see @thinkdeep/k8s-manifest.

Dependencies

Installation

    npm i @thinkdeep/k8s

Usage

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

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

    const config = new KubeConfig(); // KubeConfig exported directly from https://github.com/kubernetes-client/javascript

    config.loadFromCluster(); // Or whatever desired loading mechanism

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

    // Set default namespace fallback to whatever you desire or use the default provided 'default'.
    client.defaultNamespace = 'development';

    const options = {
        name: 'dynamic-cron-job',
        namespace: 'production',
        schedule: '* * * * *',
        image: 'busybox',
        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);
        }
    }

Package Sidebar

Install

npm i @thinkdeep/k8s

Weekly Downloads

11

Version

5.0.11

License

AGPL-3.0-only

Unpacked Size

79.6 kB

Total Files

10

Last publish

Collaborators

  • haydenmcp