Skip to content

Commit c5e3aa7

Browse files
authoredDec 13, 2023
Fix NodeGroup taints updates (#78)
Issue: aws-controllers-k8s/community#1950 Description of changes: Add custom diff for taints + tests
1 parent 83145a1 commit c5e3aa7

File tree

10 files changed

+220
-20
lines changed

10 files changed

+220
-20
lines changed
 

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
/docs/site
66
bin
77
build
8-
.env
8+
.env
9+
vendor
+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2023-12-12T20:01:34Z"
3-
build_hash: 2cc7afebf2d80513f9ed3aba95b9a2bd8dfe3d6a
4-
go_version: go1.21.1
5-
version: v0.27.1-7-g2cc7afe
6-
api_directory_checksum: a543a43cd1b4533a3bb3abb15a960b6358035ecc
2+
build_date: "2023-12-13T14:11:41Z"
3+
build_hash: 3653329ceeb20015851b8776a6061a3fb0ec2935
4+
go_version: go1.20.6
5+
version: v0.27.1-6-g3653329
6+
api_directory_checksum: 72b28de134a8fcb097e4f04b8482d7882a01940b
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.48.4
99
generator_config_info:
10-
file_checksum: c5c0aeda28af64e2b7ead52aa3563fe6e35d6cf8
10+
file_checksum: 9b2af7d38552d1e11a320576c802f375dcba3a06
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

‎apis/v1alpha1/generator.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ resources:
168168
service_name: ec2
169169
resource: Subnet
170170
path: Status.SubnetID
171+
Taints:
172+
compare:
173+
is_ignored: true
171174
renames:
172175
operations:
173176
CreateNodegroup:
@@ -198,7 +201,7 @@ resources:
198201
- ValidationError
199202
hooks:
200203
delta_pre_compare:
201-
code: customPreCompare(a, b)
204+
code: customPreCompare(delta, a, b)
202205
sdk_create_post_set_output:
203206
template_path: hooks/nodegroup/sdk_create_post_set_output.go.tpl
204207
sdk_read_one_post_set_output:

‎generator.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ resources:
168168
service_name: ec2
169169
resource: Subnet
170170
path: Status.SubnetID
171+
Taints:
172+
compare:
173+
is_ignored: true
171174
renames:
172175
operations:
173176
CreateNodegroup:
@@ -198,7 +201,7 @@ resources:
198201
- ValidationError
199202
hooks:
200203
delta_pre_compare:
201-
code: customPreCompare(a, b)
204+
code: customPreCompare(delta, a, b)
202205
sdk_create_post_set_output:
203206
template_path: hooks/nodegroup/sdk_create_post_set_output.go.tpl
204207
sdk_read_one_post_set_output:

‎go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/aws/aws-sdk-go v1.48.4
1010
github.com/go-logr/logr v1.2.3
1111
github.com/spf13/pflag v1.0.5
12+
github.com/stretchr/testify v1.8.4
1213
k8s.io/api v0.26.8
1314
k8s.io/apimachinery v0.26.8
1415
k8s.io/client-go v0.26.8
@@ -47,6 +48,7 @@ require (
4748
github.com/modern-go/reflect2 v1.0.2 // indirect
4849
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4950
github.com/pkg/errors v0.9.1 // indirect
51+
github.com/pmezard/go-difflib v1.0.0 // indirect
5052
github.com/prometheus/client_golang v1.14.0 // indirect
5153
github.com/prometheus/client_model v0.3.0 // indirect
5254
github.com/prometheus/common v0.37.0 // indirect

‎go.sum

+3-2
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,15 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
277277
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
278278
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
279279
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
280-
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
280+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
281281
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
282282
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
283283
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
284284
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
285285
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
286286
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
287-
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
287+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
288+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
288289
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
289290
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
290291
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

‎pkg/resource/nodegroup/delta.go

+1-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/resource/nodegroup/hook.go

+23
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package nodegroup
1616
import (
1717
"context"
1818
"fmt"
19+
"reflect"
1920
"time"
2021

2122
svcapitypes "github.com/aws-controllers-k8s/eks-controller/apis/v1alpha1"
@@ -61,6 +62,7 @@ var (
6162
// appropriately replaced with empty maps or structs depending on the default
6263
// output of the SDK.
6364
func customPreCompare(
65+
delta *ackcompare.Delta,
6466
a *resource,
6567
b *resource,
6668
) {
@@ -77,6 +79,27 @@ func customPreCompare(
7779
if a.ko.Spec.Tags == nil && b.ko.Spec.Tags != nil {
7880
a.ko.Spec.Tags = map[string]*string{}
7981
}
82+
if a.ko.Spec.Taints != nil && a.ko.Spec.Taints == nil || a.ko.Spec.Taints == nil && a.ko.Spec.Taints != nil {
83+
delta.Add("Spec.Taints", a.ko.Spec.Taints, b.ko.Spec.Taints)
84+
} else if a.ko.Spec.Taints != nil {
85+
if len(a.ko.Spec.Taints) != len(b.ko.Spec.Taints) {
86+
delta.Add("Spec.Taints", a.ko.Spec.Taints, b.ko.Spec.Taints)
87+
} else {
88+
for _, taintA := range a.ko.Spec.Taints {
89+
var matched = false
90+
for _, taintB := range b.ko.Spec.Taints {
91+
if reflect.DeepEqual(taintA, taintB) {
92+
matched = true
93+
break
94+
}
95+
}
96+
if !matched {
97+
delta.Add("Spec.Taints", a.ko.Spec.Taints, b.ko.Spec.Taints)
98+
break
99+
}
100+
}
101+
}
102+
}
80103
}
81104

82105
// requeueWaitUntilCanModify returns a `ackrequeue.RequeueNeededAfter` struct

‎pkg/resource/nodegroup/hook_test.go

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package nodegroup
2+
3+
import (
4+
"testing"
5+
6+
"github.com/aws-controllers-k8s/eks-controller/apis/v1alpha1"
7+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
9+
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestTaints(t *testing.T) {
14+
15+
noSchedule := "NO_SCHEDULE"
16+
owner := "owner"
17+
project := "project"
18+
19+
teamOne := "teamone"
20+
projectOne := "projectone"
21+
22+
a := &resource{
23+
ko: &v1alpha1.Nodegroup{
24+
ObjectMeta: v1.ObjectMeta{
25+
Name: "test",
26+
Namespace: "test",
27+
},
28+
Spec: v1alpha1.NodegroupSpec{
29+
Taints: []*v1alpha1.Taint{
30+
{
31+
Effect: &noSchedule,
32+
Key: &owner,
33+
Value: &teamOne,
34+
},
35+
{
36+
Effect: &noSchedule,
37+
Key: &project,
38+
Value: &projectOne,
39+
},
40+
},
41+
},
42+
},
43+
}
44+
45+
b := &resource{
46+
ko: &v1alpha1.Nodegroup{
47+
ObjectMeta: v1.ObjectMeta{
48+
Name: "test",
49+
Namespace: "test",
50+
},
51+
Spec: v1alpha1.NodegroupSpec{
52+
Taints: []*v1alpha1.Taint{
53+
{
54+
Effect: &noSchedule,
55+
Key: &project,
56+
Value: &projectOne,
57+
},
58+
{
59+
Effect: &noSchedule,
60+
Key: &owner,
61+
Value: &teamOne,
62+
},
63+
},
64+
},
65+
},
66+
}
67+
68+
delta := &ackcompare.Delta{}
69+
customPreCompare(delta, a, b)
70+
assert.False(t, delta.DifferentAt("Spec.Taints"), "Taints are equals")
71+
72+
projectTwo := "projecttwo"
73+
74+
b = &resource{
75+
ko: &v1alpha1.Nodegroup{
76+
ObjectMeta: v1.ObjectMeta{
77+
Name: "test",
78+
Namespace: "test",
79+
},
80+
Spec: v1alpha1.NodegroupSpec{
81+
Taints: []*v1alpha1.Taint{
82+
{
83+
Effect: &noSchedule,
84+
Key: &project,
85+
Value: &projectTwo,
86+
},
87+
{
88+
Effect: &noSchedule,
89+
Key: &owner,
90+
Value: &teamOne,
91+
},
92+
},
93+
},
94+
},
95+
}
96+
97+
delta = &ackcompare.Delta{}
98+
customPreCompare(delta, a, b)
99+
assert.True(t, delta.DifferentAt("Spec.Taints"), "Taints are different")
100+
101+
other := "other"
102+
103+
b = &resource{
104+
ko: &v1alpha1.Nodegroup{
105+
ObjectMeta: v1.ObjectMeta{
106+
Name: "test",
107+
Namespace: "test",
108+
},
109+
Spec: v1alpha1.NodegroupSpec{
110+
Taints: []*v1alpha1.Taint{
111+
{
112+
Effect: &noSchedule,
113+
Key: &project,
114+
Value: &projectOne,
115+
},
116+
{
117+
Effect: &noSchedule,
118+
Key: &owner,
119+
Value: &teamOne,
120+
},
121+
{
122+
Effect: &noSchedule,
123+
Key: &other,
124+
Value: &other,
125+
},
126+
},
127+
},
128+
},
129+
}
130+
131+
delta = &ackcompare.Delta{}
132+
customPreCompare(delta, a, b)
133+
assert.True(t, delta.DifferentAt("Spec.Taints"), "Taints have different length")
134+
}

‎test/e2e/tests/test_nodegroup.py

+41-5
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ def test_create_update_delete_nodegroup(self, simple_nodegroup, eks_client):
153153
"value": "noexecuted",
154154
"effect": "NO_EXECUTE"
155155
},
156+
{
157+
"key": "owner",
158+
"value": "teamone",
159+
"effect": "NO_SCHEDULE"
160+
},
156161
],
157162
}
158163
}
@@ -171,10 +176,13 @@ def test_create_update_delete_nodegroup(self, simple_nodegroup, eks_client):
171176
assert aws_res["nodegroup"]["labels"]["toot"] == "shoot"
172177
assert aws_res["nodegroup"]["labels"]["boot"] == "snoot"
173178

174-
assert len(aws_res["nodegroup"]["taints"]) == 1
175-
assert aws_res["nodegroup"]["taints"][0]["key"] == "ifbooted"
176-
assert aws_res["nodegroup"]["taints"][0]["value"] == "noexecuted"
177-
assert aws_res["nodegroup"]["taints"][0]["effect"] == "NO_EXECUTE"
179+
assert len(aws_res["nodegroup"]["taints"]) == 2
180+
assert aws_res["nodegroup"]["taints"][0]["key"] in ["ifbooted", "owner"]
181+
assert aws_res["nodegroup"]["taints"][0]["value"] in ["noexecuted", "teamone"]
182+
assert aws_res["nodegroup"]["taints"][0]["effect"] in ["NO_EXECUTE", "NO_SCHEDULE"]
183+
assert aws_res["nodegroup"]["taints"][1]["key"] in ["ifbooted", "owner"]
184+
assert aws_res["nodegroup"]["taints"][1]["value"] in ["noexecuted", "teamone"]
185+
assert aws_res["nodegroup"]["taints"][1]["effect"] in ["NO_EXECUTE", "NO_SCHEDULE"]
178186

179187
# Remove a label, update a label and remove a taint
180188
updates = {
@@ -183,7 +191,13 @@ def test_create_update_delete_nodegroup(self, simple_nodegroup, eks_client):
183191
"toot": "updooted",
184192
"boot": None
185193
},
186-
"taints": [],
194+
"taints": [
195+
{
196+
"key": "ifbooted",
197+
"value": "noexecuted",
198+
"effect": "NO_EXECUTE"
199+
}
200+
],
187201
}
188202
}
189203

@@ -201,4 +215,26 @@ def test_create_update_delete_nodegroup(self, simple_nodegroup, eks_client):
201215
assert aws_res["nodegroup"]["labels"]["toot"] == "updooted"
202216
assert "boot" not in aws_res["nodegroup"]["labels"]
203217

218+
assert len(aws_res["nodegroup"]["taints"]) == 1
219+
assert aws_res["nodegroup"]["taints"][0]["key"] == "ifbooted"
220+
assert aws_res["nodegroup"]["taints"][0]["value"] == "noexecuted"
221+
assert aws_res["nodegroup"]["taints"][0]["effect"] == "NO_EXECUTE"
222+
223+
# Remove last taint
224+
updates = {
225+
"spec": {
226+
"taints": [],
227+
}
228+
}
229+
230+
k8s.patch_custom_resource(ref, updates)
231+
time.sleep(MODIFY_WAIT_AFTER_SECONDS)
232+
233+
wait_for_nodegroup_active(eks_client, cluster_name, nodegroup_name)
234+
235+
aws_res = eks_client.describe_nodegroup(
236+
clusterName=cluster_name,
237+
nodegroupName=nodegroup_name
238+
)
239+
204240
assert "taints" not in aws_res["nodegroup"]

0 commit comments

Comments
 (0)
Please sign in to comment.