Skip to content

Commit

Permalink
allow template labels to be parameterised
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Minter committed Sep 28, 2017
1 parent 092c32e commit 107ca81
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 0 additions & 2 deletions pkg/template/apis/template/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"regexp"

unversionedvalidation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
kapi "k8s.io/kubernetes/pkg/api"
kapihelper "k8s.io/kubernetes/pkg/api/helper"
Expand Down Expand Up @@ -52,7 +51,6 @@ func validateTemplateBody(template *templateapi.Template) (allErrs field.ErrorLi
for i := range template.Parameters {
allErrs = append(allErrs, ValidateParameter(&template.Parameters[i], field.NewPath("parameters").Index(i))...)
}
allErrs = append(allErrs, unversionedvalidation.ValidateLabels(template.ObjectLabels, field.NewPath("labels"))...)
return
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func (p *Processor) Process(template *templateapi.Template) field.ErrorList {
// instruct a user on next steps for the template.
template.Message, _ = p.EvaluateParameterSubstitution(paramMap, template.Message)

// Substitute parameters in ObjectLabels - must be done before the template
// objects themselves are iterated.
for k, v := range template.ObjectLabels {
template.ObjectLabels[k], _ = p.EvaluateParameterSubstitution(paramMap, v)
}

itemPath := field.NewPath("item")
for i, item := range template.Objects {
idxPath := itemPath.Index(i)
Expand Down
34 changes: 34 additions & 0 deletions pkg/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,40 @@ func TestEvaluateLabels(t *testing.T) {
}`,
Labels: map[string]string{"key2": "v3"},
},
"parameterised label": {
Input: `{
"kind":"Template", "apiVersion":"v1",
"objects": [
{
"kind": "Service", "apiVersion": "v1",
"metadata": {"labels": {"key1": "v1", "key2": "v2"} }
}
],
"parameters": [
{
"name": "PARAMETER",
"value": "parameter-value"
}
]
}`,
Output: `{
"kind":"Template","apiVersion":"template.openshift.io/v1","metadata":{"creationTimestamp":null},
"objects":[
{
"apiVersion":"v1","kind":"Service","metadata":{
"labels":{"key1":"v1","key2":"v2","key3":"parameter-value"}}
}
],
"parameters":[
{
"name":"PARAMETER",
"value":"parameter-value"
}
],
"labels":{"key3":"parameter-value"}
}`,
Labels: map[string]string{"key3": "${PARAMETER}"},
},
}

for k, testCase := range testCases {
Expand Down

0 comments on commit 107ca81

Please sign in to comment.