Skip to content

Instantly share code, notes, and snippets.

View binura-g's full-sized avatar

Binura Gunasekara binura-g

  • WSO2 Inc
  • Sydney, Australia
View GitHub Profile
#@ load("@ytt:data", "data")
#@ load("@ytt:struct", "struct")
#@ load("@ytt:assert", "assert")
#@ app = data.values.app
## Note how all the logic is written in yaml comments - hence why it's been excluded from the shortlist.
---
apiVersion: apps/v1
#@data/values-schema
app:
name: ""
image: "nginx:1.27"
replicas: 1
ingressClass: "nginx"
serviceType: "ClusterIP"
gatewayName: "default-gateway"
endpoints:
- name: ""
apiVersion: platform.example.org/v1alpha1
kind: WebApp
metadata:
name: react-app
spec:
image: ghcr.io/acme/react-app:main
replicas: 2
endpoints:
- name: react-ui
host: app.foo.com
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: webapp
spec:
compositeTypeRef:
apiVersion: platform.example.org/v1alpha1
kind: WebApp
resources:
- name: deployment
apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
name: webapps.platform.example.org
spec:
group: platform.example.org
names:
kind: WebApp
plural: webapps
claimNames:
apiVersion: kro.run/v1alpha1
kind: ResourceGraphDefinition
metadata:
name: webapps.kro.run
spec:
schema:
apiVersion: v1alpha1
kind: WebApp
spec:
name: string

Comparing schema validations between jsonnet and CUE.

Jsonnet requires validation functions to be written - with CUE it's built it and the result is much less verbose.

Jsonnet validation example (can be inlined with the schema, but gets verbose. Best split into a different file).

local std = std;

{
local web = import 'webApp.libsonnet';
web.webApp({
name: 'react',
image: 'ghcr.io/acme/react-app:main',
replicas: 2,
gatewayName: 'default-gateway',
ingressClass: 'nginx',
serviceType: 'ClusterIP',
endpoints: [
local std = std;
{
webApp(v):
local name = v.name;
local image = v.image;
local replicas = std.get(v, 'replicas', 1);
local ingressClass = std.get(v, 'ingressClass', 'nginx');
local serviceType = std.get(v, 'serviceType', 'ClusterIP');
local gatewayName = std.get(v, 'gatewayName', 'default-gateway');
package webapp
# This can be a 'normal' YAML as well. CUE can import raw YAMLs directly.
app: {
name: "react"
image: "ghcr.io/acme/react-app:main"
replicas: 2
ingressClass: "nginx"