1.0.1 • Published 1 year ago

nestjs-k8s-operator v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Typesafe, contract-driven kubernetes operator module for a NestJS application.

Uses zod object defintions to create typesafe resource watchers, with automatic validation of crds.

Installation

npm i nestjs-k8s-operator

Example

  1. Register in app.module
@Module({
  imports: [
    KubernetesOperatorModule.forRootAsync(KubernetesOperatorModule, {
      useFactory: () => {
        return {
          enabled: true,
        };
      },
    }),
  ],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {}
}
  1. Create your crd schema contract
const contract = CustomResourceContract.createForOrg('exampleOrg')
  .kind('yourResource')
  .version('v1', {
    spec: z.object({
      test: z.string(),
      bla: z.string(),
    }),
    metadata: z.object({
      name: z.string(),
    }),
  })
  .build();
  1. Register your resource watcher
import * as z from 'zod';
import { Injectable } from '@nestjs/common';
import {
  CustomResourceContract,
  KubernetesOperator,
  CustomResource,
  KubernetesResourceWatcher,
} from 'nestjs-k8s-operator';

@Injectable()
@KubernetesResourceWatcher(contract, 'foo')
export class ExampleWatcher {
  async added(crd: CustomResource<typeof contract.foo>) {}

  async modified(crd: CustomResource<typeof contract.foo>) {}

  async deleted(crd: CustomResource<typeof contract.foo>) {}
}
  1. Profit