Skip to content

Commit 13edd36

Browse files
authored
Merge pull request #3677 from Skarlso/fix-cni-environment-property-update-logic
Fix the update logic for aws-node daemonset environment properties
2 parents 154f9f9 + 8af61b4 commit 13edd36

File tree

2 files changed

+109
-6
lines changed

2 files changed

+109
-6
lines changed

pkg/cloud/services/awsnode/cni.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ func (s *Service) ReconcileCNI(ctx context.Context) error {
159159
}
160160

161161
s.scope.Info("updating containers", "cluster-name", s.scope.Name(), "cluster-namespace", s.scope.Namespace())
162-
for _, container := range ds.Spec.Template.Spec.Containers {
163-
if container.Name == "aws-node" {
164-
container.Env = append(s.filterEnv(container.Env),
162+
for i := range ds.Spec.Template.Spec.Containers {
163+
if ds.Spec.Template.Spec.Containers[i].Name == "aws-node" {
164+
ds.Spec.Template.Spec.Containers[i].Env = append(s.filterEnv(ds.Spec.Template.Spec.Containers[i].Env),
165165
corev1.EnvVar{
166166
Name: "AWS_VPC_K8S_CNI_CUSTOM_NETWORK_CFG",
167167
Value: "true",

pkg/cloud/services/awsnode/cni_test.go

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import (
2020
"context"
2121
"testing"
2222

23+
"github.com/aws/aws-sdk-go/aws"
2324
"github.com/golang/mock/gomock"
2425
. "github.com/onsi/gomega"
2526
v1 "k8s.io/api/apps/v1"
2627
corev1 "k8s.io/api/core/v1"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
"sigs.k8s.io/controller-runtime/pkg/client"
2930

31+
infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
3032
ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/controlplane/eks/api/v1beta1"
3133
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/scope"
3234
)
@@ -206,6 +208,92 @@ func TestReconcileCniVpcCniValues(t *testing.T) {
206208
}
207209
}
208210

211+
func TestReconcileCniVpcCniValuesWithSecondaryCidrBlock(t *testing.T) {
212+
mockCtrl := gomock.NewController(t)
213+
defer mockCtrl.Finish()
214+
g := NewWithT(t)
215+
daemonSet := &v1.DaemonSet{
216+
TypeMeta: metav1.TypeMeta{
217+
Kind: "DaemonSet",
218+
},
219+
ObjectMeta: metav1.ObjectMeta{
220+
Name: "aws-node",
221+
Namespace: "kube-system",
222+
},
223+
Spec: v1.DaemonSetSpec{
224+
Template: corev1.PodTemplateSpec{
225+
ObjectMeta: metav1.ObjectMeta{},
226+
Spec: corev1.PodSpec{
227+
Containers: []corev1.Container{
228+
{
229+
Name: "aws-node",
230+
Env: []corev1.EnvVar{
231+
{
232+
Name: "NAME1",
233+
Value: "OVERWRITE",
234+
},
235+
{
236+
Name: "NAME3",
237+
Value: "VALUE3",
238+
},
239+
},
240+
},
241+
},
242+
},
243+
},
244+
},
245+
}
246+
values := ekscontrolplanev1.VpcCni{
247+
Env: []corev1.EnvVar{
248+
{
249+
Name: "NAME1",
250+
Value: "VALUE1",
251+
},
252+
},
253+
}
254+
mockClient := &cachingClient{
255+
getValue: daemonSet,
256+
}
257+
m := &mockScope{
258+
client: mockClient,
259+
cni: values,
260+
secondaryCidrBlock: aws.String("100.0.0.1/20"),
261+
securityGroups: map[infrav1.SecurityGroupRole]infrav1.SecurityGroup{
262+
"node": {
263+
ID: "sg-1234",
264+
Name: "node",
265+
},
266+
},
267+
subnets: []infrav1.SubnetSpec{},
268+
}
269+
s := NewService(m)
270+
271+
err := s.ReconcileCNI(context.Background())
272+
g.Expect(err).NotTo(HaveOccurred())
273+
g.Expect(mockClient.updateChain).NotTo(BeEmpty())
274+
ds, ok := mockClient.updateChain[0].(*v1.DaemonSet)
275+
g.Expect(ok).To(BeTrue())
276+
g.Expect(ds.Spec.Template.Spec.Containers).NotTo(BeEmpty())
277+
g.Expect(ds.Spec.Template.Spec.Containers[0].Env).To(ConsistOf([]corev1.EnvVar{
278+
{
279+
Name: "NAME1",
280+
Value: "VALUE1",
281+
},
282+
{
283+
Name: "NAME3",
284+
Value: "VALUE3",
285+
},
286+
{
287+
Name: "AWS_VPC_K8S_CNI_CUSTOM_NETWORK_CFG",
288+
Value: "true",
289+
},
290+
{
291+
Name: "ENI_CONFIG_LABEL_DEF",
292+
Value: "failure-domain.beta.kubernetes.io/zone",
293+
},
294+
}))
295+
}
296+
209297
type cachingClient struct {
210298
client.Client
211299
getValue client.Object
@@ -225,10 +313,17 @@ func (c *cachingClient) Update(ctx context.Context, obj client.Object, opts ...c
225313
return nil
226314
}
227315

316+
func (c *cachingClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
317+
return nil
318+
}
319+
228320
type mockScope struct {
229321
scope.AWSNodeScope
230-
client client.Client
231-
cni ekscontrolplanev1.VpcCni
322+
client client.Client
323+
cni ekscontrolplanev1.VpcCni
324+
secondaryCidrBlock *string
325+
securityGroups map[infrav1.SecurityGroupRole]infrav1.SecurityGroup
326+
subnets infrav1.Subnets
232327
}
233328

234329
func (s *mockScope) RemoteClient() (client.Client, error) {
@@ -256,5 +351,13 @@ func (s *mockScope) DisableVPCCNI() bool {
256351
}
257352

258353
func (s *mockScope) SecondaryCidrBlock() *string {
259-
return nil
354+
return s.secondaryCidrBlock
355+
}
356+
357+
func (s *mockScope) SecurityGroups() map[infrav1.SecurityGroupRole]infrav1.SecurityGroup {
358+
return s.securityGroups
359+
}
360+
361+
func (s *mockScope) Subnets() infrav1.Subnets {
362+
return s.subnets
260363
}

0 commit comments

Comments
 (0)