diff --git a/api/v1alpha1/template_types.go b/api/v1alpha1/template_types.go index 0c86603..90331a5 100644 --- a/api/v1alpha1/template_types.go +++ b/api/v1alpha1/template_types.go @@ -50,7 +50,7 @@ type TemplateSource struct { Object *LocalObjectReference `json:"object,omitempty"` // Value is a literal yaml value to use as source. // +optional - Value apiextensionsv1.JSON `json:"value,omitempty"` + Value *apiextensionsv1.JSON `json:"value,omitempty"` } // LocalObjectReference references an object in a specific api version. diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 7085f39..63afcd1 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -22,6 +22,7 @@ package v1alpha1 import ( + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" ) @@ -161,7 +162,11 @@ func (in *TemplateSource) DeepCopyInto(out *TemplateSource) { *out = new(LocalObjectReference) **out = **in } - in.Value.DeepCopyInto(&out.Value) + if in.Value != nil { + in, out := &in.Value, &out.Value + *out = new(apiextensionsv1.JSON) + (*in).DeepCopyInto(*out) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TemplateSource. diff --git a/charts/template-operator/Chart.yaml b/charts/template-operator/Chart.yaml index fc979c3..20935b7 100644 --- a/charts/template-operator/Chart.yaml +++ b/charts/template-operator/Chart.yaml @@ -2,7 +2,7 @@ apiVersion: v2 name: template-operator description: A Helm chart for the template operator type: application -version: 0.1.5 +version: 0.1.6 appVersion: 0.1.0 engine: gotpl home: https://github.com/onmetal/template-operator diff --git a/config/samples/template_v1alpha1_template.yaml b/config/samples/template_v1alpha1_template.yaml index 2706312..c625349 100644 --- a/config/samples/template_v1alpha1_template.yaml +++ b/config/samples/template_v1alpha1_template.yaml @@ -13,19 +13,20 @@ spec: managed-by: template-sample prune: true sources: - # - name: token - # object: - # apiVersion: v1 - # kind: Secret - # namespace: default - # name: default-token-rtxld + - name: secretName + value: my-secret + # - name: token + # object: + # apiVersion: v1 + # kind: Secret + # name: default-token-rtxld data: inline: |- apiVersion: v1 kind: Secret metadata: namespace: {{ .Template.metadata.namespace }} - name: my-secret + name: {{ .Values.secretName }} type: Opaque data: kubeconfig: {{ .Template.metadata.name | b64enc }} diff --git a/pkg/template/template.go b/pkg/template/template.go index 307178a..e54e9ab 100644 --- a/pkg/template/template.go +++ b/pkg/template/template.go @@ -32,6 +32,7 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/util/json" "k8s.io/apimachinery/pkg/util/yaml" "k8s.io/client-go/kubernetes/scheme" ctrl "sigs.k8s.io/controller-runtime" @@ -179,7 +180,13 @@ func (e *Engine) resolveSources(ctx context.Context, logger logr.Logger, templat values[src.Name] = u.Object default: - values[src.Name] = src.Value + var v interface{} + if src.Value != nil { + if err := json.Unmarshal(src.Value.Raw, &v); err != nil { + return nil, fmt.Errorf("error unmarshaling raw value %s: %w", src.Name, err) + } + } + values[src.Name] = v } }