Components
A component is a collection of Kubernetes manifests including all resources needed for an application, such as a Deployment, a Service or a Secret.
Folder Structure
A component should be placed in components/<name>.js or components/<name>/index.js.
.
└── components
├── foo
│ └── index.js
└── nginx.js
When you run kosko generate, Kosko will find all files or folders in components folder that matching the pattern set in components configuration (it is * by default, which means everything in components folder). You can add component names or patterns to the command to specify which components to generate.
# Use `components` in `kosko.toml`.
kosko generate
# Specify which components to generate
kosko generate nginx dev_*
Basic
Let's start with a basic component. In the following example, we will create a new file components/busybox.js that exports a Pod.
- TypeScript
- JavaScript (ESM)
- JavaScript (CJS)
import { Pod } from "kubernetes-models/v1/Pod";
const pod = new Pod({
metadata: { name: "busybox" },
spec: {
containers: [{ name: "busybox", image: "busybox" }]
}
});
export default pod;
import { Pod } from "kubernetes-models/v1/Pod";
const pod = new Pod({
metadata: { name: "busybox" },
spec: {
containers: [{ name: "busybox", image: "busybox" }]
}
});
export default pod;
const { Pod } = require("kubernetes-models/v1/Pod");
const pod = new Pod({
metadata: { name: "busybox" },
spec: {
containers: [{ name: "busybox", image: "busybox" }]
}
});
module.exports = pod;
Then you can run kosko generate to print YAML.
apiVersion: v1
kind: Pod
metadata:
name: busybox
spec:
containers:
- name: busybox
image: busybox
At the first line, we import Pod class from kubernetes-models package. This is a separated package which is generated automatically from Kubernetes OpenAPI Specification. It provides type definitions and validation. You can import corresponding resources as below.
// Class
import { Kind } from "kubernetes-models/<apiVersion>/<kind>";
// Interface
import { IKind } from "kubernetes-models/<apiVersion>/<kind>";
Kind can be omitted from the import path if you want to import multiple resources at a time. Please note that this might have impact on the module loading speed.
import { Pod, Service } from "kubernetes-models/v1";
kubernetes-models also supports custom resource definitions (CRDs). You can see here for a full list of supported 3rd-party models.
import { Certificate } from "@kubernetes-models/cert-manager/cert-manager.io/v1/Certificate";
If the CRD package is not available yet, or you want to skip validation on certain manifests, Kosko also supports plain objects.
const pod = {
apiVersion: "v1",
kind: "Pod"
};
Export More Manifests
In the following example, we will create a file components/nginx.js that exports a Deployment and Service.
- TypeScript
- JavaScript (ESM)
- JavaScript (CJS)
import { Deployment } from "kubernetes-models/apps/v1/Deployment";
import { Service } from "kubernetes-models/v1/Service";
const labels = { app: "nginx" };
const deployment = new Deployment({
metadata