Cue can turn Go types into Cue definitions.
This is helpful when you have existing code
and you want to validate Yaml or JSON that
will be unmarshaled into these types.
You can also generate Go helpers functions for validating these types
from the Cue schema (see Cuelang docs, requires writing go).
Before we can import Go to Cue,
we need to setup a file needed for Cue modules to work.
We’ll talk about modules and packages in
an upcoming section,
for now create the following file cue.mod/module.cue:
Then use the following command to init go.mod file,
go mod init "github.com/hofstadter-io/cuetorials.com"
With that in place, what we are going to do is:
Download the Kubernetes API code (you need a local copy)
Import the Go into Cue definitions
Apply the Kubernetes Cue to our Cuetorials Cue
Validate that our config complies with the Kubernetes spec
# "go get" the Kubernetes codego get k8s.io/api/...
# "cue get go" to import into Cue defscue get go k8s.io/api/...
# inspect what "cue get go" into the cue.mod/gen directorytree cue.mod/gen/
Now add the following lines to your cuetorials.cue file:
Now try running cue eval cuetorials.cue. You are likely to see some errors.
This is because Kubernetes accepts both strings and integers in several places.
To fix these, add the IntVal: <#> before the integer (or StrVal: if you are using a different yaml that has strings).
deployment >>> rollingUpdate
deployment >>> httpGet
service >>> targetPort
ingress >>> servicePort
Once you get cue eval working (printing out Cue values)
you can run cue trim on the file to remove redundant code
(based on minimization and defaults).
Advanced trimming is on the roadmap for Cue via anti-unification.
You may be saying, “this wackiness about int/str ports does not match my kubernetes files”
and you are totally right. We’ll see later how to work with OpenAPI specs and more
in the integrations sections.