timoni icon indicating copy to clipboard operation
timoni copied to clipboard

CUE templating advice

Open artemlive opened this issue 7 months ago • 4 comments

Hello! First of all, thanks for such a great tool. I'm enjoying playing around with Timoni. I'm trying to learn how to work with CUE, and I want to ask for advice. I want to create an abstract package that can create various configmaps/services/deployments, etc. In my experiments, I have values like this:

values: {
	configMaps: "first": {
		annotations: {
			"test":  "testsstestsssssstestssssssssss"
			"test2": "test2"
		}
		name: "first"
		data: {"1": ""}
	}
	configMaps: "second": {
		annotations: {
			"foo": "bar"
		}
		name: "second"
		data: {
			"1": "2"
			"2": """
                               test
                               test2
                               """
		}
	}
}

And then in the instance:

#CMConfig: {
	// Metadata (common to all resources)
	annotations?: {[string & =~"^(([A-Za-z0-9][-A-Za-z0-9_./]*)?[A-Za-z0-9])?$" & strings.MaxRunes(63)]: string}
	name!: string & =~"^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?$" & strings.MaxRunes(63)
	data!: {[string]: string}
}

// Instance takes the config values and outputs the Kubernetes objects.
#Instance: {
	config: #Config
	objects: {
		for c,v in config.configMaps {
				"cm-\(c)":  #ConfigMap & {
						_config: config
						_cm_config: #CMConfig & {
							annotations: v.annotations
							name: v.name
							data: v.data
						}
				}
		}

And it works as I expected:

---
apiVersion: v1
data:
  "1": ""
immutable: true
kind: ConfigMap
metadata:
  annotations:
    test: testsstestsssssstestssssssssss
    test2: test2
  labels:
    app.kubernetes.io/name: backend
    app.kubernetes.io/version: 0.0.0-devel
  name: first
  namespace: default
---
apiVersion: v1
data:
  "1": "2"
  "2": |-
    test
    test2
immutable: true
kind: ConfigMap
metadata:
  annotations:
    foo: bar
  labels:
    app.kubernetes.io/name: backend
    app.kubernetes.io/version: 0.0.0-devel
  name: second
  namespace: default

But I'm curious: am I using the right approach? There are only a few examples of how to work with Timoni, and they are straightforward when values are passed to the statically defined objects. So I wonder whether I'm doing something wrong or it looks fine. :) In perspective, I want to migrate all our helm charts to Timoni. Thanks for any advice.

artemlive avatar Nov 24 '23 18:11 artemlive