Getting started with Kubernetes + CUE
The CUE
Central Registry
provides a well-known location for well-known schemas,
including some from the Kubernetes project. In the
past, you had to use the cue get go command to generate these schemas from
their Go source code. By using the curated schemas published on the
Central Registry,
now you can get started creating validated Kubernetes manifests in minutes!
This guide shows you how to get started defining your Kubernetes configurations using a curated module from the schema library.
Initialize your local CUE module
$ cue mod init cue.exampleCUE that uses schemas and modules from the Central Registry needs to exist within its own CUE module. You can choose any module name you like, and change it later.
Create a Kubernetes manifest using CUE
If you don’t have an example you want to
use, create the manifest.cue file with this contents:
package kube
import apps "cue.dev/x/k8s.io/api/apps/v1"
apps.#Deployment & {
apiVersion: "apps/v1"
kind: "Deployment"
metadata: {
labels: app: "example1"
name: "example1"
}
spec: {
replicas: 1
selector: matchLabels: app: "example1"
template: {
metadata: labels: app: "example1"
spec: containers: [{
image: "nginx:latest"
name: "nginx"
}]
}
}
}
import at the top references the appropriate curated module for the
deployment contained in the manifest.
Its path is currently temporary, but only while its proper location is being decided.
The temporary path isn’t a problem: once a schema is published to the Central Registry, it is always available at that location.
When versions are published at the curated module’s final location, use the cue refactor imports command to update your CUE.Tidy your CUE module
$ cue mod tidyTidying a module is part of using curated modules from the
Central Registry.
Always use
cue mod tidy
when you use a curated module for the first time.
If you see an error message mentioning “too many requests” then login to the Central Registry and re-run this command. The Central Registry allows more requests from authenticated users.
Validate your manifest
$ cue vet -cBecause cue vet doesn’t display any errors, you know that the curated schema has validated your manifest.
Export your manifest as YAML
$ cue export --outfile manifest.ymlIf you used the example manifest from above, your validated YAML manifest will look like this:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: example1
name: example1
spec:
replicas: 1
selector:
matchLabels:
app: example1
template:
metadata:
labels:
app: example1
spec:
containers:
- image: nginx:latest
name: nginx