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.
The latest pre-release of the cue
command is required – please
upgrade to this version if it’s not already installed:
$ cue version
cue version v0.13.0-alpha.3
...
Login to the Central Registry
$ cue login # only during beta
The Central Registry requires authentication while it’s in beta testing, so you need to login before you can use its schemas.
Initialise your local CUE module
$ cue mod init cue.example
CUE 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 - it’s easy to 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:
// filepath: manifest.cue
package kube
import apps "test.cue.works/x1/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"
}]
}
}
}
The 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 because one important property of the
Central Registry
is that, once a schema is published, it will always be
available at that location.
When the curated module’s location is finalised and versions are published
under the new path, you can use the
cue refactor imports
command to update your CUE easily, so it reflects the new location.
Tidy your CUE module
$ cue mod tidy
Tidying a module is an important part of using curated modules from the
Central Registry.
Always use
cue mod tidy
when you use a curated module for the first time.
Validate your manifest
$ cue vet -c
Because 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.yml
If you used the example manifest from above, your validated YAML manifest will look like this:
# filepath: manifest.yml
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