Skip to content

Commit e11b105

Browse files
committed
add parameters to template with default value and support to template
1 parent 5d6639e commit e11b105

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

controllers/template/commons.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import (
1818
)
1919

2020
var (
21-
otGV = otv1.GroupVersion.String()
21+
otGV = otv1.GroupVersion.String()
22+
prefix = "__"
2223
)
2324

2425
// Common common controllers things
@@ -28,10 +29,15 @@ type Common struct {
2829
}
2930

3031
// UpdateObjectsByTemplate update object
31-
func (c *Common) UpdateObjectsByTemplate(ot otv1.ObjectTemplate, owners []metav1.OwnerReference, namespaceName string, paramsValues map[string]string) error {
32+
func (c *Common) UpdateObjectsByTemplate(ot otv1.ObjectTemplate, owners []metav1.OwnerReference, namespaceName string, paramsValues map[string]string) (err error) {
3233
for _, obj := range ot.Spec.Objects {
33-
normParams := c.normalizeParametersValues(ot.Spec.Parameters, paramsValues)
34-
err := c.UpdateSingleObjectByTemplate(obj, owners, namespaceName, normParams)
34+
normParams, err := c.normalizeParametersValues(obj, namespaceName, ot.Spec.Parameters, paramsValues)
35+
36+
if err != nil {
37+
return err
38+
}
39+
40+
err = c.UpdateSingleObjectByTemplate(obj, owners, namespaceName, normParams)
3541

3642
if err != nil {
3743
return err
@@ -41,17 +47,28 @@ func (c *Common) UpdateObjectsByTemplate(ot otv1.ObjectTemplate, owners []metav1
4147
return nil
4248
}
4349

44-
func (c *Common) normalizeParametersValues(templateParamsValues []otv1.Parameter, paramsValues map[string]string) (params map[string]string) {
50+
func (c *Common) normalizeParametersValues(obj otv1.Object, namespaceName string, templateParamsValues []otv1.Parameter, paramsValues map[string]string) (params map[string]string, err error) {
51+
templateValues := c.addRuntimeVariablesToMap(map[string]string{}, obj, namespaceName)
52+
4553
params = map[string]string{}
4654
for _, tp := range templateParamsValues {
55+
var pvalue string
4756
if len(paramsValues[tp.Name]) > 0 {
48-
params[tp.Name] = paramsValues[tp.Name]
57+
pvalue = paramsValues[tp.Name]
4958
} else {
50-
params[tp.Name] = tp.Default
59+
pvalue = tp.Default
5160
}
61+
62+
templateExecuted, err := executeTemplate(pvalue, templateValues)
63+
64+
if err != nil {
65+
return params, err
66+
}
67+
68+
params[tp.Name] = templateExecuted
5269
}
5370

54-
return params
71+
return params, nil
5572
}
5673

5774
// UpdateSingleObjectByTemplate update object
@@ -221,10 +238,10 @@ func (c *Common) UpdateStatus(ctx context.Context, obj runtime.Object) {
221238
func (c *Common) addRuntimeVariablesToMap(values map[string]string, obj otv1.Object, namespaceName string) map[string]string {
222239
newMap := copyMap(values)
223240

224-
newMap["__namespace"] = namespaceName
225-
newMap["__apiVersion"] = obj.APIVersion
226-
newMap["__kind"] = obj.Kind
227-
newMap["__name"] = obj.Name
241+
newMap[prefix+"namespace"] = namespaceName
242+
newMap[prefix+"apiVersion"] = obj.APIVersion
243+
newMap[prefix+"kind"] = obj.Kind
244+
newMap[prefix+"name"] = obj.Name
228245

229246
return newMap
230247
}

controllers/template/controller_pod_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ var _ = Describe("ObjectTemplateParams controller (POD)", func() {
5757
Name: "imageName",
5858
Default: "latest",
5959
},
60+
{
61+
Name: "containerName",
62+
Default: "{{ .__namespace }}",
63+
},
6064
},
6165
Objects: []otv1.Object{
6266
{
@@ -75,7 +79,7 @@ var _ = Describe("ObjectTemplateParams controller (POD)", func() {
7579
Name: NewObjectName,
7680
TemplateBody: `spec:
7781
containers:
78-
- name: mynginx
82+
- name: {{ .containerName }}
7983
image: {{ .imageName }}
8084
activeDeadlineSeconds: 60
8185
`,
@@ -125,7 +129,7 @@ var _ = Describe("ObjectTemplateParams controller (POD)", func() {
125129
return err == nil
126130
}, timeout, interval).Should(BeTrue())
127131

128-
Expect(pod.Spec.Containers[0].Name).Should(BeIdenticalTo("mynginx"))
132+
Expect(pod.Spec.Containers[0].Name).Should(BeIdenticalTo(ObjectTemplateParamsNamespace))
129133
Expect(pod.Spec.Containers[0].Image).Should(BeIdenticalTo("nginx"))
130134
Expect(*pod.Spec.ActiveDeadlineSeconds == 60).Should(BeTrue())
131135
Expect(pod.ObjectMeta.Annotations["annotation1"]).Should(BeIdenticalTo("value_annotation1"))
@@ -142,7 +146,7 @@ var _ = Describe("ObjectTemplateParams controller (POD)", func() {
142146
createdObjectTemplate.Spec.Objects[0].Metadata.Labels["label3"] = "value_label3"
143147
createdObjectTemplate.Spec.Objects[0].TemplateBody = `spec:
144148
containers:
145-
- name: mynginx
149+
- name: {{ .containerName }}
146150
image: {{ .imageName }}
147151
activeDeadlineSeconds: 30
148152
`
@@ -154,7 +158,7 @@ var _ = Describe("ObjectTemplateParams controller (POD)", func() {
154158
return false
155159
}
156160

157-
return pod.Spec.Containers[0].Name == "mynginx" &&
161+
return pod.Spec.Containers[0].Name == ObjectTemplateParamsNamespace &&
158162
pod.Spec.Containers[0].Image == "nginx" &&
159163
*pod.Spec.ActiveDeadlineSeconds == 30 &&
160164
pod.Annotations["annotation1"] == "value_annotation1" &&

controllers/template/objecttemplate_controller.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ func (r *ObjectTemplateReconciler) SetupWithManager(mgr ctrl.Manager) error {
5353
func (r *ObjectTemplateReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
5454
ctx := context.Background()
5555
log := r.Log.WithValues("objecttemplate", otGV)
56-
var ot otv1.ObjectTemplate
57-
err := r.Get(ctx, req.NamespacedName, &ot)
56+
var objectTemplate otv1.ObjectTemplate
57+
err := r.Get(ctx, req.NamespacedName, &objectTemplate)
5858
common := Common{r.Client, log}
5959

6060
if err != nil {
61-
ot.Status.Status = err.Error()
61+
objectTemplate.Status.Status = err.Error()
6262

6363
if k8sErrors.IsNotFound(err) {
6464
// Object not found, return. Created objects are automatically garbage collected
@@ -69,19 +69,19 @@ func (r *ObjectTemplateReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
6969
return ctrl.Result{}, err
7070
}
7171

72-
defer common.UpdateStatus(ctx, &ot)
72+
defer common.UpdateStatus(ctx, &objectTemplate)
7373

74-
otParams, err := common.FindObjectTemplateParamsByTemplateName(ot.Name)
74+
otParams, err := common.FindObjectTemplateParamsByTemplateName(objectTemplate.Name)
7575

7676
if err != nil {
77-
ot.Status.Status = err.Error()
77+
objectTemplate.Status.Status = err.Error()
7878
return ctrl.Result{}, err
7979
}
8080

8181
lu := LogUtil{Log: log}
8282
for _, otParam := range otParams {
8383
paramNamespace := otParam.Namespace
84-
paramValues, err := otParam.Spec.GetParametersByTemplateName(ot.Name)
84+
paramValues, err := otParam.Spec.GetParametersByTemplateName(objectTemplate.Name)
8585

8686
if err != nil {
8787
lu.Error(err, "Error getting parameters by template name")
@@ -92,15 +92,15 @@ func (r *ObjectTemplateReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
9292
gvk := otv1.GroupVersion.WithKind(kind)
9393
controllerRef := metav1.NewControllerRef(otParam.GetObjectMeta(), gvk)
9494

95-
if err := common.UpdateObjectsByTemplate(ot, []metav1.OwnerReference{*controllerRef}, paramNamespace, paramValues.Values); err != nil {
95+
if err := common.UpdateObjectsByTemplate(objectTemplate, []metav1.OwnerReference{*controllerRef}, paramNamespace, paramValues.Values); err != nil {
9696
lu.Error(err, "Failed to update ObjectTemplate")
9797
continue
9898
}
9999
}
100100

101-
ot.Status.Status = "OK"
101+
objectTemplate.Status.Status = "OK"
102102
if lu.HasError() {
103-
ot.Status.Status = lu.AllErrorsMessages()
103+
objectTemplate.Status.Status = lu.AllErrorsMessages()
104104
}
105105

106106
return ctrl.Result{Requeue: false}, nil

0 commit comments

Comments
 (0)