From 0a9d00d32260aeb1fbeb1defbb4d3f33920d4bdb Mon Sep 17 00:00:00 2001 From: michael mccune Date: Tue, 26 Mar 2024 15:02:43 -0400 Subject: [PATCH] UPSTREAM: : add check for taint.value == nil This change adds an extra check to ensure that the `value` field is not nil. --- .../clusterapi/clusterapi_controller_test.go | 8 +++++++ .../clusterapi/clusterapi_unstructured.go | 5 ++++- .../clusterapi_unstructured_test.go | 22 +++++++++++++++---- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller_test.go b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller_test.go index edb61f8dc13e..1be6823c61fc 100644 --- a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller_test.go +++ b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller_test.go @@ -353,6 +353,10 @@ func createTestConfigs(specs ...testSpec) []*testConfig { "value": "test", "effect": "NoSchedule", }, + map[string]interface{}{ + "key": "test-no-value", + "effect": "NoSchedule", + }, }, }, }, @@ -398,6 +402,10 @@ func createTestConfigs(specs ...testSpec) []*testConfig { "value": "test", "effect": "NoSchedule", }, + map[string]interface{}{ + "key": "test-no-value", + "effect": "NoSchedule", + }, }, }, }, diff --git a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_unstructured.go b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_unstructured.go index 2fef94ebb769..7f210ece00fd 100644 --- a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_unstructured.go +++ b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_unstructured.go @@ -243,7 +243,10 @@ func unstructuredToTaint(unstructuredTaintInterface interface{}) *corev1.Taint { taint := &corev1.Taint{} taint.Key = unstructuredTaint["key"].(string) - taint.Value = unstructuredTaint["value"].(string) + // value is optional and could be nil if not present + if unstructuredTaint["value"] != nil { + taint.Value = unstructuredTaint["value"].(string) + } taint.Effect = corev1.TaintEffect(unstructuredTaint["effect"].(string)) return taint } diff --git a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_unstructured_test.go b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_unstructured_test.go index 82a01a881be8..48e633436070 100644 --- a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_unstructured_test.go +++ b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_unstructured_test.go @@ -226,8 +226,16 @@ func TestReplicas(t *testing.T) { func TestTaints(t *testing.T) { initialReplicas := 1 - expectedTaints := []v1.Taint{{Key: "test", Effect: v1.TaintEffectNoSchedule, Value: "test"}} - expectedTaintsWithAnnotations := []v1.Taint{{Key: "test", Effect: v1.TaintEffectNoSchedule, Value: "test"}, {Key: "key1", Effect: v1.TaintEffectNoSchedule, Value: "value1"}, {Key: "key2", Effect: v1.TaintEffectNoExecute, Value: "value2"}} + expectedTaints := []v1.Taint{ + {Key: "test", Effect: v1.TaintEffectNoSchedule, Value: "test"}, + {Key: "test-no-value", Effect: v1.TaintEffectNoSchedule}, + } + expectedTaintsWithAnnotations := []v1.Taint{ + {Key: "test", Effect: v1.TaintEffectNoSchedule, Value: "test"}, + {Key: "test-no-value", Effect: v1.TaintEffectNoSchedule}, + {Key: "key1", Effect: v1.TaintEffectNoSchedule, Value: "value1"}, + {Key: "key2", Effect: v1.TaintEffectNoExecute, Value: "value2"}, + } taintAnnotation := "key1=value1:NoSchedule,key2=value2:NoExecute" test := func(t *testing.T, testConfig *testConfig) { @@ -350,8 +358,14 @@ func TestAnnotations(t *testing.T) { gpuQuantity := resource.MustParse("1") maxPodsQuantity := resource.MustParse("42") - // Note, the first taint comes from the spec of the machine template, the rest from the annotations. - expectedTaints := []v1.Taint{{Key: "test", Effect: v1.TaintEffectNoSchedule, Value: "test"}, {Key: "key1", Effect: v1.TaintEffectNoSchedule, Value: "value1"}, {Key: "key2", Effect: v1.TaintEffectNoExecute, Value: "value2"}} + // Note, the first two taints comes from the spec of the machine template, the rest from the annotations. + expectedTaints := []v1.Taint{ + {Key: "test", Effect: v1.TaintEffectNoSchedule, Value: "test"}, + {Key: "test-no-value", Effect: v1.TaintEffectNoSchedule}, + {Key: "key1", Effect: v1.TaintEffectNoSchedule, Value: "value1"}, + {Key: "key2", Effect: v1.TaintEffectNoExecute, Value: "value2"}, + } + annotations := map[string]string{ cpuKey: cpuQuantity.String(), memoryKey: memQuantity.String(),