Skip to content

Commit 5d6639e

Browse files
committed
add parameters to template with default value
1 parent 847b6b1 commit 5d6639e

File tree

7 files changed

+88
-5
lines changed

7 files changed

+88
-5
lines changed

apis/v1/objecttemplate_types.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,17 @@ type Object struct {
3535
TemplateBody string `json:"templateBody"`
3636
}
3737

38+
// Parameter defines a single parameter
39+
type Parameter struct {
40+
Name string `json:"name"`
41+
Default string `json:"default"`
42+
}
43+
3844
// ObjectTemplateSpec defines the desired state of ObjectTemplate
3945
type ObjectTemplateSpec struct {
40-
Description string `json:"description,omitempty"`
41-
Objects []Object `json:"objects"`
46+
Description string `json:"description,omitempty"`
47+
Parameters []Parameter `json:"parameters"`
48+
Objects []Object `json:"objects"`
4249
}
4350

4451
// ObjectTemplateStatus defines the observed state of ObjectTemplate

apis/v1/zz_generated.deepcopy.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/template.k8s.ericogr.com.br_objecttemplates.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,22 @@ spec:
7676
- templateBody
7777
type: object
7878
type: array
79+
parameters:
80+
items:
81+
description: Parameter defines a single parameter
82+
properties:
83+
default:
84+
type: string
85+
name:
86+
type: string
87+
required:
88+
- default
89+
- name
90+
type: object
91+
type: array
7992
required:
8093
- objects
94+
- parameters
8195
type: object
8296
status:
8397
description: ObjectTemplateStatus defines the observed state of ObjectTemplate

config/samples/template.k8s.ericogr.com.br_v1_objecttemplate.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ metadata:
55
name: objecttemplate-configmap-test
66
spec:
77
description: ConfigMap test
8+
parameters:
9+
- name: name
10+
- name: age
11+
- name: country
12+
default: myCountry
813
objects:
914
- kind: ConfigMap
1015
apiVersion: v1
@@ -19,4 +24,5 @@ spec:
1924
templateBody: |-
2025
data:
2126
name: '{{ .name }}'
22-
age: '{{ .age }}'
27+
age: '{{ .age }}'
28+
country: '{{ .country }}'

controllers/template/commons.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ type Common struct {
2828
}
2929

3030
// UpdateObjectsByTemplate update object
31-
func (c *Common) UpdateObjectsByTemplate(ot otv1.ObjectTemplate, owners []metav1.OwnerReference, namespaceName string, values map[string]string) error {
31+
func (c *Common) UpdateObjectsByTemplate(ot otv1.ObjectTemplate, owners []metav1.OwnerReference, namespaceName string, paramsValues map[string]string) error {
3232
for _, obj := range ot.Spec.Objects {
33-
err := c.UpdateSingleObjectByTemplate(obj, owners, namespaceName, values)
33+
normParams := c.normalizeParametersValues(ot.Spec.Parameters, paramsValues)
34+
err := c.UpdateSingleObjectByTemplate(obj, owners, namespaceName, normParams)
3435

3536
if err != nil {
3637
return err
@@ -40,6 +41,19 @@ func (c *Common) UpdateObjectsByTemplate(ot otv1.ObjectTemplate, owners []metav1
4041
return nil
4142
}
4243

44+
func (c *Common) normalizeParametersValues(templateParamsValues []otv1.Parameter, paramsValues map[string]string) (params map[string]string) {
45+
params = map[string]string{}
46+
for _, tp := range templateParamsValues {
47+
if len(paramsValues[tp.Name]) > 0 {
48+
params[tp.Name] = paramsValues[tp.Name]
49+
} else {
50+
params[tp.Name] = tp.Default
51+
}
52+
}
53+
54+
return params
55+
}
56+
4357
// UpdateSingleObjectByTemplate update object
4458
func (c *Common) UpdateSingleObjectByTemplate(obj otv1.Object, owners []metav1.OwnerReference, namespaceName string, values map[string]string) error {
4559
ctx := context.Background()

controllers/template/controller_map_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ var _ = Describe("ObjectTemplateParams controller (ConfigMap)", func() {
5252
},
5353
Spec: otv1.ObjectTemplateSpec{
5454
Description: "namespace-template",
55+
Parameters: []otv1.Parameter{
56+
{
57+
Name: "lives",
58+
Default: "1",
59+
},
60+
{
61+
Name: "properties_file",
62+
Default: "",
63+
},
64+
{
65+
Name: "character_name",
66+
Default: "maria",
67+
},
68+
},
5569
Objects: []otv1.Object{
5670
{
5771
Kind: "ConfigMap",
@@ -69,6 +83,7 @@ var _ = Describe("ObjectTemplateParams controller (ConfigMap)", func() {
6983
Name: NewObjectName,
7084
TemplateBody: `data:
7185
player_initial_lives: "{{ .lives }}"
86+
player_name: "{{ .character_name }}"
7287
ui_properties_file_name: "{{ .properties_file }}"`,
7388
},
7489
},
@@ -117,6 +132,7 @@ var _ = Describe("ObjectTemplateParams controller (ConfigMap)", func() {
117132
return err == nil
118133
}, timeout, interval).Should(BeTrue())
119134
Expect(configmap.Data["player_initial_lives"]).Should(BeIdenticalTo("3"))
135+
Expect(configmap.Data["player_name"]).Should(BeIdenticalTo("maria"))
120136
Expect(configmap.Data["ui_properties_file_name"]).Should(BeIdenticalTo("user-interface.properties"))
121137
Expect(configmap.ObjectMeta.Annotations["annotation1"]).Should(BeIdenticalTo("value_annotation1"))
122138
Expect(configmap.ObjectMeta.Annotations["annotation2"]).Should(BeIdenticalTo("value_annotation2"))

controllers/template/controller_pod_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ var _ = Describe("ObjectTemplateParams controller (POD)", func() {
5252
},
5353
Spec: otv1.ObjectTemplateSpec{
5454
Description: "namespace-template",
55+
Parameters: []otv1.Parameter{
56+
{
57+
Name: "imageName",
58+
Default: "latest",
59+
},
60+
},
5561
Objects: []otv1.Object{
5662
{
5763
Kind: "Pod",

0 commit comments

Comments
 (0)