Introduction
If you’re still using YAML or JSON to interact with Kubernetes, it’s time to explore a new option. Today, I’m going to introduce you to a language that offers more reliability and flexibility. Without further delay, let’s get started with CUE Basics: A Simple Guide.
What is CUE?
CUE stands for Configure, Unify, Execute. CUE is an open-source data validation language developed at Google by Marcel van Lohuizen Marcel van Lohuizen. The CUE is the superset of JSON. CUE is different from JSON as JSON is a data format but CUE is a data constraint language that allows us to embed logic within data.
Why to use CUE?
When we use YAML to define our Kubernetes objects, it does not provide any built-in validations. Validation typically needs to be done separately using tools like JSON Schema or custom scripts. However, CUE provides various features such as code extraction and built-in data validation. It allows you to embed programming logic with the data, such as if-else statements and loops. Additionally, we can define schemas for our Kubernetes objects and write reusable code using CUE. CUE makes it easy to validate data, write schemas, and ensure configurations align with policies.
Application of CUE
- Data validation: different departments or groups can each define their own constraints to apply to the same set of data.
- Code extraction and generation: extract CUE definitions from multiple sources (Go code, Protobuf), combine them into a single definition, and use that to generate definitions in another format (e.g. OpenAPI).
- Configuration: values can be combined from different sources without one having to import the other.
CUE installation
To install CUE on your Ubuntu first, we need to install brew
$ bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
$ echo 'export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH:/bin:/usr/bin"' >> $HOME/.bashrc
$ source ./bashrc
then to install CUE run the following command
$ brew install cue-lang/tap/cue
To verify the installation run the following command
$ cue version
CUE Example
Step 1: Create a file with the name Hello.cue and paste the following content
_name: "shahenvaz"
_company: "NASHTECH"
message: "Hello I am (_name) and currently working in (_company)!"
Step 2: Evaluate the CUE file and output the resulting data.
$ cue eval Hello.cue
Output
message: "Hello I am shahenvaz and currently working in NASHTECH!"
Conclusion
This is just an overview of CUE BASIC: A simple guide where I have covered the basics of CUE with installation on Ubuntu. In the upcoming blog, I will try to cover some more use cases and also explain how we can use it in Kubernetes. So that’s it for now, Happy Learning.