Kubernetes Integration

You could stop at just building and pushing images.

But, because building images is so easy with ko, and because building with ko only requires a string importpath to identify the image, we can integrate this with YAML generation to make Kubernetes use cases much simpler.

YAML Changes

Traditionally, you might have a Kubernetes deployment, defined in a YAML file, that runs an image:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: my-deployment
  5. spec:
  6. replicas: 3
  7. ...
  8. template:
  9. spec:
  10. containers:
  11. - name: my-app
  12. image: registry.example.com/my-app:v1.2.3

…which you apply to your cluster with kubectl apply:

  1. kubectl apply -f deployment.yaml

With ko, you can instead reference your Go binary by its importpath, prefixed with ko://:

  1. ...
  2. spec:
  3. containers:
  4. - name: my-app
  5. image: ko://github.com/my-user/my-repo/cmd/app

ko resolve

With this small change, running ko resolve -f deployment.yaml will instruct ko to:

  1. scan the YAML file(s) for values with the ko:// prefix,
  2. for each unique ko://-prefixed string, execute ko build <importpath> to build and push an image,
  3. replace ko://-prefixed string(s) in the input YAML with the fully-specified image reference of the built image(s), as above.
  4. Print the resulting resolved YAML to stdout.

The result can be redirected to a file, to distribute to others:

  1. ko resolve -f config/ > release.yaml

Taken together, ko resolve aims to make packaging, pushing, and referencing container images an invisible implementation detail of your Kubernetes deployment, and let you focus on writing code in Go.

ko apply

To apply the resulting resolved YAML config, you can redirect the output of ko resolve to kubectl apply:

  1. ko resolve -f config/ | kubectl apply -f -

Since this is a relatively common use case, the same functionality is available using ko apply:

  1. ko apply -f config/

Also, any flags passed after -- are passed to kubectl apply directly, for example to specify context and kubeconfig:

  1. ko apply -f config -- --context=foo --kubeconfig=cfg.yaml

NB: This requires that kubectl is available.

ko delete

To teardown resources applied using ko apply, you can run ko delete:

  1. ko delete -f config/

This is purely a convenient alias for kubectl delete, and doesn’t perform any builds, or delete any previously built images.