Skip to content

Commit

Permalink
Kustomization Templating
Browse files Browse the repository at this point in the history
Each KustomizationRef can have substitute values:
. Values is a map[string]string
. ValuesFrom references ConfigMap/Secret instances whose Data
sections contain values.

These key-value pairs can optionally leverage Go templates for further processing.
During deployment, Sveltos iterates through the key-value pairs.
If a key-value pair's value is a template, Sveltos evaluates the template
using data available in the context (e.g., cluster information).
This allows to dynamically construct values based on other resources or variables.

Finally, Sveltos uses the processed key-value pairs to replace placeholder
values (identified using {{ .VariableName}} ) within the output generated by the
Kustomize SDK.

Let's take an example.

Values:
   `Region`:  `{{ index .Cluster.metadata.labels "region" }}`,
   `Version`: `v1.2.0`,

And then the output of Kustomize SDK can have a deployment

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
  namespace:  test
  labels:
    region: {{ default "west" .Region }}
spec:
  ...
  image: nginx:{{ .Version }}
```

What Sveltos will do in this case:

1. instantiate Values. So Sveltos will take the Cluster instance
representing the cluster where Sveltos is about to deploy the
Kustomize resources, it will take the Cluster's labels.
if Cluster has labels: ```region: east```  then instantiated Values
will be:

   `Region`:  `east`,
   `Version`: `v1.2.0`,

2. Sveltos will take the Kustomize resources and will use the
substitute Values, so:

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
  namespace:  test
  labels:
    region: east
spec:
  ...
  image: nginx:v1.2.0
```

3. Sveltos will deploy resources in the managed cluster
  • Loading branch information
mgianluc committed May 1, 2024
1 parent 8ff617a commit cf94bad
Show file tree
Hide file tree
Showing 20 changed files with 1,127 additions and 44 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ docker-push: ## Push docker image with the manager.
docker-buildx: ## docker build for multiple arch and push to docker hub
docker buildx build --push --platform linux/amd64,linux/arm64 -t $(CONTROLLER_IMG):$(TAG) .


.PHONY: load-image
load-image: docker-build $(KIND)
$(KIND) load docker-image $(CONTROLLER_IMG):$(TAG) --name $(CONTROL_CLUSTER_NAME)
Expand Down
54 changes: 53 additions & 1 deletion api/v1alpha1/spec.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023. projectsveltos.io. All rights reserved.
Copyright 2023-24. projectsveltos.io. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -119,6 +119,23 @@ const (
DeploymentTypeRemote = DeploymentType("Remote")
)

type ValueFrom struct {
// Namespace of the referenced resource.
// For ClusterProfile namespace can be left empty. In such a case, namespace will
// be implicit set to cluster's namespace.
// For Profile namespace must be left empty. The Profile namespace will be used.
Namespace string `json:"namespace"`

// Name of the referenced resource.
// +kubebuilder:validation:MinLength=1
Name string `json:"name"`

// Kind of the resource. Supported kinds are:
// - ConfigMap/Secret
// +kubebuilder:validation:Enum=ConfigMap;Secret
Kind string `json:"kind"`
}

// HelmChartAction specifies action on an helm chart
// +kubebuilder:validation:Enum:=Install;Uninstall
type HelmChartAction string
Expand Down Expand Up @@ -277,6 +294,41 @@ type KustomizationRef struct {
// +kubebuilder:default:=Remote
// +optional
DeploymentType DeploymentType `json:"deploymentType,omitempty"`

// Values is a map[string]string type that allows to define a set of key-value pairs.
// These key-value pairs can optionally leverage Go templates for further processing.
// With Sveltos, you can define key-value pairs where the values can be Go templates.
// These templates have access to management cluster information during deployment. This allows
// to do more than just replace placeholders. Variables can be used to dynamically
// construct values based on other resources or variables within the Kustomize output.
// For example, imagine you have a Region key with a template value like:
// '{{ index .Cluster.metadata.labels "region" }}'.
// This template retrieves the region label from the cluster instance metadata.
// Finally, Sveltos uses these processed values to fill placeholders in the Kustomize output.
// The output itself can also contain templates, like:
// region: '{{ default "west" .Region }}'.
// This way, the final output from Kustomize will have the region set dynamically based on
// the actual region retrieved earlier.
// +optional
Values map[string]string `json:"values,omitempty"`

// ValuesFrom can reference ConfigMap/Secret instances. Within the ConfigMap or Secret data,
// it is possible to define key-value pairs. These key-value pairs can optionally leverage
// Go templates for further processing.
// With Sveltos, you can define key-value pairs where the values can be Go templates.
// These templates have access to management cluster information during deployment. This allows
// to do more than just replace placeholders. Variables can be used to dynamically
// construct values based on other resources or variables within the Kustomize output.
// For example, imagine you have a Region key with a template value like:
// '{{ index .Cluster.metadata.labels "region" }}'.
// This template retrieves the region label from the cluster instance metadata.
// Finally, Sveltos uses these processed values to fill placeholders in the Kustomize output.
// The output itself can also contain templates, like:
// region: '{{ default "west" .Region }}'.
// This way, the final output from Kustomize will have the region set dynamically based on
// the actual region retrieved earlier.
// +optional
ValuesFrom []ValueFrom `json:"valuesFrom,omitempty"`
}

// StopMatchingBehavior indicates what will happen when Cluster stops matching
Expand Down
31 changes: 30 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions config/crd/bases/config.projectsveltos.io_clusterprofiles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,69 @@ spec:
maxLength: 63
minLength: 1
type: string
values:
additionalProperties:
type: string
description: |-
Values is a map[string]string type that allows to define a set of key-value pairs.
These key-value pairs can optionally leverage Go templates for further processing.
With Sveltos, you can define key-value pairs where the values can be Go templates.
These templates have access to management cluster information during deployment. This allows
to do more than just replace placeholders. Variables can be used to dynamically
construct values based on other resources or variables within the Kustomize output.
For example, imagine you have a Region key with a template value like:
'{{ index .Cluster.metadata.labels "region" }}'.
This template retrieves the region label from the cluster instance metadata.
Finally, Sveltos uses these processed values to fill placeholders in the Kustomize output.
The output itself can also contain templates, like:
region: '{{ default "west" .Region }}'.
This way, the final output from Kustomize will have the region set dynamically based on
the actual region retrieved earlier.
type: object
valuesFrom:
description: |-
ValuesFrom can reference ConfigMap/Secret instances. Within the ConfigMap or Secret data,
it is possible to define key-value pairs. These key-value pairs can optionally leverage
Go templates for further processing.
With Sveltos, you can define key-value pairs where the values can be Go templates.
These templates have access to management cluster information during deployment. This allows
to do more than just replace placeholders. Variables can be used to dynamically
construct values based on other resources or variables within the Kustomize output.
For example, imagine you have a Region key with a template value like:
'{{ index .Cluster.metadata.labels "region" }}'.
This template retrieves the region label from the cluster instance metadata.
Finally, Sveltos uses these processed values to fill placeholders in the Kustomize output.
The output itself can also contain templates, like:
region: '{{ default "west" .Region }}'.
This way, the final output from Kustomize will have the region set dynamically based on
the actual region retrieved earlier.
items:
properties:
kind:
description: |-
Kind of the resource. Supported kinds are:
- ConfigMap/Secret
enum:
- ConfigMap
- Secret
type: string
name:
description: Name of the referenced resource.
minLength: 1
type: string
namespace:
description: |-
Namespace of the referenced resource.
For ClusterProfile namespace can be left empty. In such a case, namespace will
be implicit set to cluster's namespace.
For Profile namespace must be left empty. The Profile namespace will be used.
type: string
required:
- kind
- name
- namespace
type: object
type: array
required:
- kind
- name
Expand Down
63 changes: 63 additions & 0 deletions config/crd/bases/config.projectsveltos.io_clustersummaries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,69 @@ spec:
maxLength: 63
minLength: 1
type: string
values:
additionalProperties:
type: string
description: |-
Values is a map[string]string type that allows to define a set of key-value pairs.
These key-value pairs can optionally leverage Go templates for further processing.
With Sveltos, you can define key-value pairs where the values can be Go templates.
These templates have access to management cluster information during deployment. This allows
to do more than just replace placeholders. Variables can be used to dynamically
construct values based on other resources or variables within the Kustomize output.
For example, imagine you have a Region key with a template value like:
'{{ index .Cluster.metadata.labels "region" }}'.
This template retrieves the region label from the cluster instance metadata.
Finally, Sveltos uses these processed values to fill placeholders in the Kustomize output.
The output itself can also contain templates, like:
region: '{{ default "west" .Region }}'.
This way, the final output from Kustomize will have the region set dynamically based on
the actual region retrieved earlier.
type: object
valuesFrom:
description: |-
ValuesFrom can reference ConfigMap/Secret instances. Within the ConfigMap or Secret data,
it is possible to define key-value pairs. These key-value pairs can optionally leverage
Go templates for further processing.
With Sveltos, you can define key-value pairs where the values can be Go templates.
These templates have access to management cluster information during deployment. This allows
to do more than just replace placeholders. Variables can be used to dynamically
construct values based on other resources or variables within the Kustomize output.
For example, imagine you have a Region key with a template value like:
'{{ index .Cluster.metadata.labels "region" }}'.
This template retrieves the region label from the cluster instance metadata.
Finally, Sveltos uses these processed values to fill placeholders in the Kustomize output.
The output itself can also contain templates, like:
region: '{{ default "west" .Region }}'.
This way, the final output from Kustomize will have the region set dynamically based on
the actual region retrieved earlier.
items:
properties:
kind:
description: |-
Kind of the resource. Supported kinds are:
- ConfigMap/Secret
enum:
- ConfigMap
- Secret
type: string
name:
description: Name of the referenced resource.
minLength: 1
type: string
namespace:
description: |-
Namespace of the referenced resource.
For ClusterProfile namespace can be left empty. In such a case, namespace will
be implicit set to cluster's namespace.
For Profile namespace must be left empty. The Profile namespace will be used.
type: string
required:
- kind
- name
- namespace
type: object
type: array
required:
- kind
- name
Expand Down
63 changes: 63 additions & 0 deletions config/crd/bases/config.projectsveltos.io_profiles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,69 @@ spec:
maxLength: 63
minLength: 1
type: string
values:
additionalProperties:
type: string
description: |-
Values is a map[string]string type that allows to define a set of key-value pairs.
These key-value pairs can optionally leverage Go templates for further processing.
With Sveltos, you can define key-value pairs where the values can be Go templates.
These templates have access to management cluster information during deployment. This allows
to do more than just replace placeholders. Variables can be used to dynamically
construct values based on other resources or variables within the Kustomize output.
For example, imagine you have a Region key with a template value like:
'{{ index .Cluster.metadata.labels "region" }}'.
This template retrieves the region label from the cluster instance metadata.
Finally, Sveltos uses these processed values to fill placeholders in the Kustomize output.
The output itself can also contain templates, like:
region: '{{ default "west" .Region }}'.
This way, the final output from Kustomize will have the region set dynamically based on
the actual region retrieved earlier.
type: object
valuesFrom:
description: |-
ValuesFrom can reference ConfigMap/Secret instances. Within the ConfigMap or Secret data,
it is possible to define key-value pairs. These key-value pairs can optionally leverage
Go templates for further processing.
With Sveltos, you can define key-value pairs where the values can be Go templates.
These templates have access to management cluster information during deployment. This allows
to do more than just replace placeholders. Variables can be used to dynamically
construct values based on other resources or variables within the Kustomize output.
For example, imagine you have a Region key with a template value like:
'{{ index .Cluster.metadata.labels "region" }}'.
This template retrieves the region label from the cluster instance metadata.
Finally, Sveltos uses these processed values to fill placeholders in the Kustomize output.
The output itself can also contain templates, like:
region: '{{ default "west" .Region }}'.
This way, the final output from Kustomize will have the region set dynamically based on
the actual region retrieved earlier.
items:
properties:
kind:
description: |-
Kind of the resource. Supported kinds are:
- ConfigMap/Secret
enum:
- ConfigMap
- Secret
type: string
name:
description: Name of the referenced resource.
minLength: 1
type: string
namespace:
description: |-
Namespace of the referenced resource.
For ClusterProfile namespace can be left empty. In such a case, namespace will
be implicit set to cluster's namespace.
For Profile namespace must be left empty. The Profile namespace will be used.
type: string
required:
- kind
- name
- namespace
type: object
type: array
required:
- kind
- name
Expand Down
Loading

0 comments on commit cf94bad

Please sign in to comment.