Skip to content

Commit 6c3cc71

Browse files
committed
fix(taint deletion): handle nil values to prevent panic
Signed-off-by: nueavv <nuguni@kakao.com>
1 parent 2fe1c84 commit 6c3cc71

File tree

3 files changed

+199
-3
lines changed

3 files changed

+199
-3
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
github.com/sirupsen/logrus v1.9.3
4141
github.com/spf13/cobra v1.8.1
4242
github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace
43+
github.com/stretchr/testify v1.9.0
4344
github.com/zgalor/weberr v0.8.2
4445
golang.org/x/crypto v0.22.0
4546
golang.org/x/text v0.14.0
@@ -169,6 +170,7 @@ require (
169170
github.com/pelletier/go-toml v1.9.5 // indirect
170171
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
171172
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
173+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
172174
github.com/prometheus/client_model v0.6.1 // indirect
173175
github.com/prometheus/common v0.52.2 // indirect
174176
github.com/prometheus/procfs v0.13.0 // indirect
@@ -185,7 +187,6 @@ require (
185187
github.com/spf13/cast v1.6.0 // indirect
186188
github.com/spf13/viper v1.18.2 // indirect
187189
github.com/stoewer/go-strcase v1.2.0 // indirect
188-
github.com/stretchr/objx v0.5.2 // indirect
189190
github.com/subosito/gotenv v1.6.0 // indirect
190191
github.com/valyala/fastjson v1.6.4 // indirect
191192
github.com/vincent-petithory/dataurl v1.0.0 // indirect

pkg/cloud/converters/eks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ func TaintsFromSDK(taints []*eks.Taint) (expinfrav1.Taints, error) {
111111
}
112112
converted = append(converted, expinfrav1.Taint{
113113
Effect: convertedEffect,
114-
Key: *taint.Key,
115-
Value: *taint.Value,
114+
Key: aws.StringValue(taint.Key),
115+
Value: aws.StringValue(taint.Value),
116116
})
117117
}
118118

pkg/cloud/converters/eks_test.go

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package converters
18+
19+
import (
20+
"testing"
21+
22+
"github.com/aws/aws-sdk-go/service/eks"
23+
"github.com/stretchr/testify/assert"
24+
25+
expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2"
26+
)
27+
28+
func TestTaintsFromSDK(t *testing.T) {
29+
tests := []struct {
30+
name string
31+
input []*eks.Taint
32+
expected expinfrav1.Taints
33+
expectErr bool
34+
}{
35+
{
36+
name: "Single taint conversion",
37+
input: []*eks.Taint{
38+
{
39+
Key: awsString("key1"),
40+
Value: awsString("value1"),
41+
Effect: awsString(eks.TaintEffectNoSchedule),
42+
},
43+
},
44+
expected: expinfrav1.Taints{
45+
{
46+
Key: "key1",
47+
Value: "value1",
48+
Effect: expinfrav1.TaintEffectNoSchedule,
49+
},
50+
},
51+
expectErr: false,
52+
},
53+
{
54+
name: "Multiple taints conversion",
55+
input: []*eks.Taint{
56+
{
57+
Key: awsString("key1"),
58+
Value: awsString("value1"),
59+
Effect: awsString(eks.TaintEffectNoExecute),
60+
},
61+
{
62+
Key: awsString("key2"),
63+
Value: awsString("value2"),
64+
Effect: awsString(eks.TaintEffectPreferNoSchedule),
65+
},
66+
},
67+
expected: expinfrav1.Taints{
68+
{
69+
Key: "key1",
70+
Value: "value1",
71+
Effect: expinfrav1.TaintEffectNoExecute,
72+
},
73+
{
74+
Key: "key2",
75+
Value: "value2",
76+
Effect: expinfrav1.TaintEffectPreferNoSchedule,
77+
},
78+
},
79+
expectErr: false,
80+
},
81+
{
82+
name: "Unknown taint effect",
83+
input: []*eks.Taint{
84+
{
85+
Key: awsString("key1"),
86+
Value: awsString("value1"),
87+
Effect: awsString("UnknownEffect"),
88+
},
89+
},
90+
expected: nil,
91+
expectErr: true,
92+
},
93+
{
94+
name: "Nil taint value",
95+
input: []*eks.Taint{
96+
{
97+
Key: awsString("key1"),
98+
Value: nil,
99+
Effect: awsString(eks.TaintEffectNoSchedule),
100+
},
101+
},
102+
expected: expinfrav1.Taints{
103+
{
104+
Key: "key1",
105+
Value: "",
106+
Effect: expinfrav1.TaintEffectNoSchedule,
107+
},
108+
},
109+
expectErr: false,
110+
},
111+
{
112+
name: "Empty input slice",
113+
input: []*eks.Taint{},
114+
expected: expinfrav1.Taints{},
115+
expectErr: false,
116+
},
117+
{
118+
name: "Nil input",
119+
input: nil,
120+
expected: expinfrav1.Taints{},
121+
expectErr: false,
122+
},
123+
{
124+
name: "Mixed valid and invalid taints",
125+
input: []*eks.Taint{
126+
{
127+
Key: awsString("key1"),
128+
Value: awsString("value1"),
129+
Effect: awsString(eks.TaintEffectNoExecute),
130+
},
131+
{
132+
Key: awsString("key2"),
133+
Value: awsString("value2"),
134+
Effect: awsString("InvalidEffect"),
135+
},
136+
},
137+
expected: nil,
138+
expectErr: true,
139+
},
140+
{
141+
name: "Empty key",
142+
input: []*eks.Taint{
143+
{
144+
Key: awsString(""),
145+
Value: awsString("value1"),
146+
Effect: awsString(eks.TaintEffectNoSchedule),
147+
},
148+
},
149+
expected: expinfrav1.Taints{
150+
{
151+
Key: "",
152+
Value: "value1",
153+
Effect: expinfrav1.TaintEffectNoSchedule,
154+
},
155+
},
156+
expectErr: false,
157+
},
158+
{
159+
name: "Empty value",
160+
input: []*eks.Taint{
161+
{
162+
Key: awsString("key1"),
163+
Value: awsString(""),
164+
Effect: awsString(eks.TaintEffectNoSchedule),
165+
},
166+
},
167+
expected: expinfrav1.Taints{
168+
{
169+
Key: "key1",
170+
Value: "",
171+
Effect: expinfrav1.TaintEffectNoSchedule,
172+
},
173+
},
174+
expectErr: false,
175+
},
176+
}
177+
178+
for _, tt := range tests {
179+
t.Run(tt.name, func(t *testing.T) {
180+
result, err := TaintsFromSDK(tt.input)
181+
if tt.expectErr {
182+
assert.Error(t, err)
183+
assert.Nil(t, result, "Expected result to be nil on error")
184+
} else {
185+
assert.NoError(t, err)
186+
assert.Equal(t, tt.expected, result, "Converted taints do not match expected")
187+
}
188+
})
189+
}
190+
}
191+
192+
// Helper function to create AWS String pointers.
193+
func awsString(value string) *string {
194+
return &value
195+
}

0 commit comments

Comments
 (0)