From 59dd8b16085625d38dff2fb761a05a28085280a4 Mon Sep 17 00:00:00 2001 From: Jonah Back Date: Thu, 29 Apr 2021 10:09:01 -0700 Subject: [PATCH] fix: only set restricted field to default value if none of the conditionals set it (#284) Signed-off-by: Jonah Back --- controllers/provisioners/config.go | 8 ++++---- controllers/provisioners/config_test.go | 8 +++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/controllers/provisioners/config.go b/controllers/provisioners/config.go index a9eb3826..dd2da00e 100644 --- a/controllers/provisioners/config.go +++ b/controllers/provisioners/config.go @@ -168,17 +168,17 @@ func (c *ProvisionerConfiguration) setRestrictedFields(unstructuredInstanceGroup for _, pathStr := range c.Boundaries.Restricted { path := common.FieldPath(pathStr) // if a default value exists for the path, set it on the instance group - var setField = false - var field interface{} + var setFieldInAnyConditional = false for _, conditional := range applicableConditionals { - if field, setField, _ = unstructured.NestedFieldCopy(conditional.Defaults, path...); setField { + if field, setField, _ := unstructured.NestedFieldCopy(conditional.Defaults, path...); setField { + setFieldInAnyConditional = true err := unstructured.SetNestedField(unstructuredInstanceGroup, field, path...) if err != nil { errors.Wrap(err, "failed to set nested field") } } } - if setField { + if setFieldInAnyConditional { continue } if field, ok, _ := unstructured.NestedFieldCopy(c.Defaults, path...); ok { diff --git a/controllers/provisioners/config_test.go b/controllers/provisioners/config_test.go index caa1fa98..c03e51c8 100644 --- a/controllers/provisioners/config_test.go +++ b/controllers/provisioners/config_test.go @@ -193,12 +193,18 @@ func TestSetDefaultsWithRestrictedConditional(t *testing.T) { eks: configuration: image: ami-22222222222 -- annotationSelector: "instancemgr.keikoproj.io/arch=arm64" +- annotationSelector: "instancemgr.keikoproj.io/arch in (arm64), instancemgr.keikoproj.io/os-family in (windows)" defaults: spec: eks: configuration: image: ami-33333333333 +- annotationSelector: "instancemgr.keikoproj.io/arch in (arm64), instancemgr.keikoproj.io/os-family in (windows)" + defaults: + spec: + eks: + configuration: + foo: bar ` mockDefaults := `