Flux Kustomization
Overview
This topic covers how to mix in a Flux Kustomization to all components.  We'll
use the Artifacts field of ComponentConfig defined by the author schema.
The Code
Generating the structure
Use holos to generate a minimal platform directory structure.  Start by
creating a blank directory to hold the platform configuration.
mkdir holos-flux-kustomization && cd holos-flux-kustomization
holos init platform v1alpha5
Creating an example Component
Create a directory for the example podinfo component we'll use to render
platform manifests.
mkdir -p components/podinfo
Create the CUE configuration for the example podinfo component.
cat <<EOF >components/podinfo/podinfo.cue
package holos
holos: Component.BuildPlan
Component: #Helm & {
	Name: "podinfo"
	Chart: {
		version: "6.6.2"
		repository: {
			name: "podinfo"
			url:  "https://stefanprodan.github.io/podinfo"
		}
	}
	Values: ui: {
		message: string | *"Hello World" @tag(message, type=string)
	}
}
EOF
Integrate the podinfo component into the platform.
cat <<EOF >platform/podinfo.cue
package holos
Platform: Components: podinfo: {
	name: "podinfo"
	path: "components/podinfo"
}
EOF
Adding Flux Kustomizations
Configure Holos to render a Kustomization by defining an Artifact for it in
every BuildPlan holos produces.  We're unifying our custom configuration with
the existing #ComponentConfig defined in schema.cue.
cat <<EOF >flux-kustomization.cue
package holos
import (
	"path"
	flux "kustomize.toolkit.fluxcd.io/kustomization/v1"
)
#ComponentConfig: {
	Name:          _
	OutputBaseDir: _
	let ArtifactPath = path.Join([OutputBaseDir, "gitops", "\(Name).kustomization.gen.yaml"], path.Unix)
	let ResourcesPath = path.Join(["deploy", OutputBaseDir, "components", Name], path.Unix)
	Artifacts: "\(Name)-kustomization": {
		artifact: ArtifactPath
		generators: [{
			kind:   "Resources"
			output: artifact
			resources: Kustomization: (Name): flux.#Kustomization & {
				metadata: name:      Name
				metadata: namespace: "default"
				spec: {
					interval: "5m"
					timeout:  "1m"
					prune:    true
					path:     ResourcesPath
					sourceRef: {
						kind: "GitRepository"
						name: "webapp"
					}
				}
			}
		}]
	}
}
EOF
Inspecting the BuildPlan
Our customized #ComponentConfig results in the following BuildPlan.
The second artifact around line 40 contains the configured Kustomization
resource.
- Command
- Output
holos cue export --expression holos --out=yaml ./components/podinfo
kind: BuildPlan
apiVersion: v1alpha5
metadata:
  name: podinfo
spec:
  artifacts:
    - artifact: components/podinfo/podinfo.gen.yaml
      generators:
        - kind: Helm
          output: helm.gen.yaml
          helm:
            chart:
              name: podinfo
              version: 6.6.2
              release: podinfo
              repository:
                name: podinfo
                url: https://stefanprodan.github.io/podinfo
            values:
              ui:
                message: Hello World
            enableHooks: false
        - kind: Resources
          output: resources.gen.yaml
          resources: {}
      validators: []
      transformers:
        - kind: Kustomize
          inputs:
            - helm.gen.yaml
            - resources.gen.yaml
          output: components/podinfo/podinfo.gen.yaml
          kustomize:
            kustomization:
              resources:
                - helm.gen.yaml
                - resources.gen.yaml
              kind: Kustomization
              apiVersion: kustomize.config.k8s.io/v1beta1
    - artifact: gitops/podinfo.kustomization.gen.yaml
      generators:
        - kind: Resources
          output: gitops/podinfo.kustomization.gen.yaml
          resources:
            Kustomization:
              podinfo:
                apiVersion: kustomize.toolkit.fluxcd.io/v1
                kind: Kustomization
                metadata:
                  name: podinfo
                  namespace: default
                spec:
                  interval: 5m
                  path: deploy/components/podinfo
                  prune: true
                  sourceRef:
                    kind: GitRepository
                    name: webapp
                  timeout: 1m
Rendering manifests
- Command
- Output
holos render platform
rendered podinfo in 140.341417ms
rendered platform in 140.441333ms
Reviewing the Kustomization
The Artifact we added to #ComponentConfig will produce a Flux Kustomization
resource for every component in the platform.  The output in this example is
located at:
deploy/gitops/podinfo.kustomization.gen.yaml
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
    name: podinfo
    namespace: default
spec:
    interval: 5m
    path: deploy/components/podinfo
    prune: true
    sourceRef:
        kind: GitRepository
        name: webapp
    timeout: 1m