@leverage/kubernetes
NOTE: This library is quite new and things are still being fleshed out. It should work, but be prepared for a few quirks!
Installation
First, install the package with your preferred package manager.
npm install @leverage/kubernetes
Then, add the kubernetes plugin to your Leverage manager.
import { add } from "@leverage/core"; import { kubernetes } from "@leverage/kubernetes"; add(kubernetes);
Usage
Kubernetes Resources
You can use this plugin in combination with Senchou to generate and manage Kubernetes resources. First, use Senchou to generate a module with Kubernetes API Object definitions.
Then, create a KubernetesComponent for your resource.
// my-pod.ts import { Pod } from "./senchou/k8s.ts"; import { useKubernetes } from "@leverage/kubernetes"; export const init = () => { useKubernetes({ name: "my-pod", }); }; export const render = () => { return Pod({ metadata: { name: "my-pod", labels: { "x-generated-by": "leverage", }, }, spec: { containers: [{ image: "my-image" }], }, }); };
And finally add that unit to your manager instance and render.
import { add } from "@leverage/core"; import { kubernetes, render } from "@leverage/kubernetes"; import myPod from "./my-pod"; add(kubernetes, myPod); const output = await render();
Helm Resources
You can use existing helm charts by creating a HelmComponent.
// my-helm-component.ts import { useHelm } from "@leverage/kubernetes"; export const init = () => { useHelm({ name: "my-traefik-release", chart: "traefik/traefik", repository: { name: "traefik", url: "https://helm.traefik.io/traefik", }, }); }; export const values = () => { return { mySetting: true, }; };
Then add the component and plugin to Leverage in order to render the chart.
import { add } from "@leverage/core"; import { kubernetes, render } from "@leverage/kubernetes"; import myHelmComponent from "./my-helm-component"; add(kubernetes, myHelmComponent); const output = await render();
Custom Helm Charts
You can use this plugin with @senchou/helm
to create your own charts.
Start by creating a ChartComponent.
// my-chart.ts import { useChart } from "@leverage/kubernetes"; export const init = () => { useChart({ name: "my-chart", meta: { version: "0.0.0", }, values: { http: { enable: true, port: 80, }, }, }); };
Then, create a ChartTemplateComponent to add a resource to the chart.
// my-pod-template.ts import { useChartTemplate } from "@leverage/kubernetes"; import { template } from "@senchou/helm"; import { Pod, ContainerPort } from "./senchou/k8s"; export const init = () => { useChartTemplate({ name: "my-pod", }); }; export const render = () => { return template(Pod, { metadata: { name: "my-pod", }, spec: { containers: [ { name: "my-container", image: "my-image", ports: [ template.if<ContainerPort>({ type: Object, condition: ".Values.http.enable", body: { name: "http", containerPort: template.string( ".Values.http.port" ), }, else: [], }), ], }, ], }, }); };
Finally, add this plugin and all of your units to your Leverage Manager and call render.
import { add } from "@leverage/core"; import { kubernetes, render } from "@leverage/kubernetes"; import myChart from "./my-chart"; import myPodTemplate from "./my-pod-template"; add(kubernetes, myChart, myPodTemplate); const output = await render();
Patch Resources
You can apply patches to Kubernetes resources by creating
a PatchComponent.
// my-patch.ts import { usePatch } from "@leverage/kubernetes"; import { isPod } from "./senchou/k8s"; export const init = () => { usePatch({ name: "my-patch", }); }; export const patch = (resource: object) => { // This patch will be responsible for patching Pod resources. if (isPod(resource)) { resource.metadata.labels = { ...resource.metadata.labels, "x-patched-by": "leverage", }; } };
Then add your patch along with all of your other components. When rendered, patches will be applied before the final output is returned.
import { add } from "@leverage/core"; import { kubernetes, render } from "@leverage/kubernetes"; import myPod from "./my-pod"; import myPatch from "./my-patch"; add(kubernetes, myPod, myPatch); const output = await render();
