Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kosko - 改用 JavaScript 來管理 Kubernetes YAML (COSC...

Tommy Chen
September 06, 2021

Kosko - 改用 JavaScript 來管理 Kubernetes YAML (COSCUP 2021)

Tommy Chen

September 06, 2021
Tweet

More Decks by Tommy Chen

Other Decks in Technology

Transcript

  1. import { Pod } from "kubernetes-models/v1/Pod"; const pod = new

    Pod({ metadata: { name: "busybox" }, spec: { containers: [ { name: "busybox", image: "busybox", command: ["sleep", "10000"] } ] } }); export default pod; apiVersion: v1 kind: Pod metadata: name: busybox spec: containers: - name: busybox image: busybox command: - sleep - '10000'
  2. import { Deployment } from "kubernetes-models/apps/v1/Deployment"; import { Service }

    from "kubernetes-models/v1/Service"; const deployment = new Deployment({ metadata: { name: "nginx" }, spec: { // ... } }); const service = new Service({ metadata: { name: "nginx" }, spec: { // ... } }); export default [deployment, service]; --- apiVersion: apps/v1 kind: Deployment metadata: name: nginx --- apiVersion: v1 kind: Service metadata: name: nginx
  3. import { Deployment } from "kubernetes-models/apps/v1/Deployment"; import { Service }

    from "kubernetes-models/v1/Service"; import { PersistentVolumeClaim } from "kubernetes- models/v1/PersistentVolumeClaim"; const db = [ new Deployment({ metadata: { name: "mysql" } }), new Service({ metadata: { name: "mysql" } }), new PersistentVolumeClaim({ metadata: { name: "mysql" } }) ]; const deployment = new Deployment({ metadata: { name: "api" } }); const service = new Service({ metadata: { name: "api" } }); export default [db, deployment, service];
  4. --- apiVersion: apps/v1 kind: Deployment metadata: name: mysql --- apiVersion:

    v1 kind: Service metadata: name: mysql --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysql --- apiVersion: apps/v1 kind: Deployment metadata: name: api --- apiVersion: v1 kind: Service metadata: name: api
  5. import { Secret } from "kubernetes-models/v1/Secret"; const secret = new

    Secret({ metadata: { name: "api-key" }, type: "Opaque", data: { secret: Buffer.from("confidential").toString("base64") } }); export default secret; apiVersion: v1 kind: Secret metadata: name: api-key type: Opaque data: secret: Y29uZmlkZW50aWFs
  6. import { loadFile, loadUrl, loadString } from "@kosko/yaml"; const manifest

    = loadFile("manifest.yaml"); const certManager = loadUrl( "https://github.com/jetstack/cert-manager/releases/download/v1.0.4/cert- manager.yaml" ); const pod = loadString(` apiVersion: v1 kind: Pod metadata: name: my-pod `); export default [manifest, certManager, pod];
  7. import { loadChart } from "@kosko/helm"; const prometheus = loadChart({

    chart: "prometheus", repo: "https://prometheus-community.github.io/helm-charts", version: "13.6.0" }); export default prometheus;
  8. // Global variables - environments/dev/index.js export default { namespace: "dev"

    }; // Component variables - environments/dev/nginx.js export default { replicas: 3 }; // components/nginx.js import { Deployment } from "kubernetes-models/apps/v1/Deployment"; import env from "@kosko/env"; const params = env.component("nginx"); // { namespace: "dev", replicas: 3 } const deployment = new Deployment({ metadata: { name: "nginx", namespace: params.namespace }, spec: { replicas: params.replicas, template: { spec: { containers: [{ name: "nginx", image: "nginx:stable" }] } } } }); apiVersion: apps/v1 kind: Deployment metadata: name: nginx namespace: dev spec: replicas: 3 template: spec: containers: - name: nginx image: nginx:stable
  9. const labels = { app: "demo" }; const deployment =

    new Deployment({ spec: { selector: { matchLabels: labels }, template: { metadata: { labels } } } }); const service = new Service({ spec: { selector: labels } });
  10. const httpPort = 80; const deployment = new Deployment({ spec:

    { template: { spec: { containers: [{ ports: [{ containerPort: httpPort }] }] } } } }); const service = new Service({ spec: { ports: [{ port: httpPort }] } });
  11. export function envVars(envs: Record<string, string>): IEnvVar[] { return Object.entries(envs).map(([name, value])

    => { return { name, value }; }); } // Before [ { name: "LOG_LEVEL", value: "info" }, { name: "API_URL", value: "https://example.com" } ]; // After envVars({ LOG_LEVEL: "info", API_URL: "https://example.com" }); • •
  12. function withEnvoy(spec: IPodSpec): IPodSpec { return { ...spec, containers: [

    ...spec.containers, { name: "envoy", image: "envoyproxy/envoy:v1.17.0" } ] }; } const deployment = new Deployment({ spec: { selector: {}, template: { spec: withEnvoy({ containers: [{ name: "demo", image: "demo" }] }) } } }); apiVersion: apps/v1 kind: Deployment spec: selector: {} template: spec: containers: - name: demo image: demo - name: envoy image: envoyproxy/envoy:v1.17.0
  13. function createDatabase(name: string) { return [ new Deployment({ metadata: {

    name } }), new Service({ metadata: { name } }), new PersistentVolumeClaim({ metadata: { name } }) ]; } /* components/post-api.js */ export default [ createDatabase("post-api-db"), new Deployment({ metadata: { name: "post-api" } }), new Service({ metadata: { name: "post-api" } }) ]; /* components/user-api.js */ export default [ createDatabase("user-api-db"), new Deployment({ metadata: { name: "user-api" } }), new Service({ metadata: { name: "user-api" } }) ];