Skip to content

Commit

Permalink
PCP-3571 Adding tests and fixing lint
Browse files Browse the repository at this point in the history
  • Loading branch information
vishu2498 authored and jayesh-srivastava committed Dec 6, 2024
1 parent 92e64d4 commit b18cbb0
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 8 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ require (
k8s.io/cli-runtime v0.24.0
k8s.io/client-go v0.25.0
k8s.io/component-base v0.25.0
k8s.io/klog/v2 v2.70.1
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed
k8s.io/klog/v2 v2.80.1
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078
sigs.k8s.io/aws-iam-authenticator v0.5.9
sigs.k8s.io/cluster-api v1.2.0
sigs.k8s.io/cluster-api/test v1.2.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1301,8 +1301,8 @@ k8s.io/klog/v2 v2.8.0/go.mod h1:hy9LJ/NvuK+iVyP4Ehqva4HxZG/oXyIS3n3Jmire4Ec=
k8s.io/klog/v2 v2.9.0/go.mod h1:hy9LJ/NvuK+iVyP4Ehqva4HxZG/oXyIS3n3Jmire4Ec=
k8s.io/klog/v2 v2.10.0/go.mod h1:hy9LJ/NvuK+iVyP4Ehqva4HxZG/oXyIS3n3Jmire4Ec=
k8s.io/klog/v2 v2.60.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/klog/v2 v2.70.1 h1:7aaoSdahviPmR+XkS7FyxlkkXs6tHISSG03RxleQAVQ=
k8s.io/klog/v2 v2.70.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4=
k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20210305001622-591a79e4bda7/go.mod h1:wXW5VT87nVfh/iLV8FpR2uDvrFyomxbtb1KivDbvPTE=
k8s.io/kube-openapi v0.0.0-20210421082810-95288971da7e/go.mod h1:vHXdDvt9+2spS2Rx9ql3I8tycm3H9FDfdUoIuKCefvw=
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42/go.mod h1:Z/45zLw8lUo4wdiUkI+v/ImEGAvu3WatcZl3lPMR4Rk=
Expand All @@ -1318,8 +1318,8 @@ k8s.io/utils v0.0.0-20210722164352-7f3ee0f31471/go.mod h1:jPW/WVKK9YHAvNhRxK0md/
k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20210820185131-d34e5cb4466e/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed h1:jAne/RjBTyawwAy0utX5eqigAwz/lQhTmy+Hr/Cpue4=
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078 h1:jGnCPejIetjiy2gqaJ5V0NLwTpF4wbQ6cZIItJCSHno=
k8s.io/utils v0.0.0-20241104163129-6fe5fd82f078/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloud/services/eks/securitygroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (s *Service) reconcileSecurityGroups(cluster *eks.Cluster) error {
return nil
}

// desiredTags will return the default tags of EKS cluster with the spec.additionalTags from AWSManagedControlPlane
// desiredTags will return the default tags of EKS cluster with the spec.additionalTags from AWSManagedControlPlane.
func desiredTags(existingTags, additionalTags infrav1.Tags) infrav1.Tags {
merged := make(infrav1.Tags)

Expand All @@ -115,7 +115,7 @@ func desiredTags(existingTags, additionalTags infrav1.Tags) infrav1.Tags {
return merged
}

// updateTagsForEKSManagedSecurityGroup will update the tags in the EKS security group with the desired tags via create/update/delete operations
// updateTagsForEKSManagedSecurityGroup will update the tags in the EKS security group with the desired tags via create/update/delete operations.
func (s *Service) updateTagsForEKSManagedSecurityGroup(securityGroupID *string, existingTags, desiredTags infrav1.Tags) error {
tagsToDelete, newTags := getTagUpdates(existingTags, desiredTags)

Expand Down
196 changes: 196 additions & 0 deletions pkg/cloud/services/eks/securitygroup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package eks

import (
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/golang/mock/gomock"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/controlplane/eks/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/converters"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/scope"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services/eks/mock_eksiface"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services/iamauth/mock_iamauth"
"sigs.k8s.io/cluster-api-provider-aws/test/mocks"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

func TestDesiredTags(t *testing.T) {
g := NewWithT(t)

existingTags := infrav1.Tags{
"aws:eks:cluster-name": "cluster-name",
"kubernetes.io/cluster/cluster-name": "owned",
"Name": "eks-cluster-sg-cluster-name-1488456827",
"tagKeyNotToBeIncluded": "tagValueNotToBeIncluded",
"sampleTag": "sampleOldValue",
}

additionalTags := infrav1.Tags{
"hello": "there",
"new": "tag",
"tagKey": "tagValue",
"sampleTag": "sampleNewValue",
}

expectedDesiredTags := infrav1.Tags{
"aws:eks:cluster-name": "cluster-name",
"kubernetes.io/cluster/cluster-name": "owned",
"Name": "eks-cluster-sg-cluster-name-1488456827",
"hello": "there",
"new": "tag",
"tagKey": "tagValue",
"sampleTag": "sampleNewValue",
}

desiredTags := desiredTags(existingTags, additionalTags)
g.Expect(desiredTags).To(Equal(expectedDesiredTags))
}

func TestUpdateTagsForEKSManagedSecurityGroup(t *testing.T) {
g := NewWithT(t)

mockControl := gomock.NewController(t)
defer mockControl.Finish()

eksMock := mock_eksiface.NewMockEKSAPI(mockControl)
iamMock := mock_iamauth.NewMockIAMAPI(mockControl)
ec2Mock := mocks.NewMockEC2API(mockControl)

scheme := runtime.NewScheme()
_ = infrav1.AddToScheme(scheme)
_ = ekscontrolplanev1.AddToScheme(scheme)
client := fake.NewClientBuilder().WithScheme(scheme).Build()
vpcSpec := infrav1.VPCSpec{
CidrBlock: "2001:db8:85a3::/56",
}
scope, err := scope.NewManagedControlPlaneScope(scope.ManagedControlPlaneScopeParams{
Client: client,
Cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: "cluster-name",
},
},
ControlPlane: &ekscontrolplanev1.AWSManagedControlPlane{
Spec: ekscontrolplanev1.AWSManagedControlPlaneSpec{
RoleName: ptr.To[string]("arn-role"),
Version: aws.String("1.29"),
Region: "us-east-1",
NetworkSpec: infrav1.NetworkSpec{
Subnets: []infrav1.SubnetSpec{
{
ID: "sub-1",
CidrBlock: "10.0.10.0/24",
AvailabilityZone: "us-west-2a",
IsPublic: true,
},
{
ID: "sub-2",
CidrBlock: "10.0.10.0/24",
AvailabilityZone: "us-west-2b",
IsPublic: false,
},
},
VPC: vpcSpec,
},
},
},
})
g.Expect(err).To(BeNil())

s := NewService(scope)
s.EKSClient = eksMock
s.IAMClient = iamMock
s.EC2Client = ec2Mock

existingTags := infrav1.Tags{
"aws:eks:cluster-name": "cluster-name",
"kubernetes.io/cluster/cluster-name": "owned",
"Name": "eks-cluster-sg-cluster-name-1488456827",
}

desiredTags := infrav1.Tags{
"hello": "there",
"new": "tag",
"aws:eks:cluster-name": "cluster-name",
"kubernetes.io/cluster/cluster-name": "owned",
"Name": "eks-cluster-sg-cluster-name-1488456827",
"tagKey": "tagValue",
"sampleTag": "sampleNewValue",
}

sampleEKSecurityGroupID := aws.String("sg-025f2495c64d5")

createSGinput := &ec2.CreateSecurityGroupInput{
GroupName: sampleEKSecurityGroupID,
TagSpecifications: []*ec2.TagSpecification{
{
Tags: converters.MapToTags(desiredTags),
},
},
}

ec2Mock.EXPECT().CreateSecurityGroup(createSGinput).Return(&ec2.CreateSecurityGroupOutput{
GroupId: sampleEKSecurityGroupID,
}, nil)

_, err = ec2Mock.CreateSecurityGroup(createSGinput)
g.Expect(err).To(BeNil())

ec2Mock.EXPECT().CreateTags(gomock.Any()).Return(nil, nil)

err = s.updateTagsForEKSManagedSecurityGroup(sampleEKSecurityGroupID, existingTags, desiredTags)
g.Expect(err).To(BeNil())

describeSGinput := &ec2.DescribeSecurityGroupsInput{
GroupIds: []*string{sampleEKSecurityGroupID},
}

ec2Mock.EXPECT().DescribeSecurityGroups(describeSGinput).Return(&ec2.DescribeSecurityGroupsOutput{
SecurityGroups: []*ec2.SecurityGroup{
{
GroupId: sampleEKSecurityGroupID,
Tags: converters.MapToTags(desiredTags),
Description: aws.String("EKS created security group applied to ENI that is attached to EKS Control Plane master nodes, as well as any managed workloads."),
},
},
}, nil)

describeOutput, err := ec2Mock.DescribeSecurityGroups(&ec2.DescribeSecurityGroupsInput{
GroupIds: []*string{sampleEKSecurityGroupID},
})
g.Expect(err).To(BeNil())

// Denotes that this EKS security group is created by AWS.
g.Expect(*describeOutput.SecurityGroups[0].Description).To(Equal("EKS created security group applied to ENI that is attached to EKS Control Plane master nodes, as well as any managed workloads."))

g.Expect(len(describeOutput.SecurityGroups[0].Tags)).To(Equal(len(desiredTags)))

for key, value := range desiredTags {
g.Expect(converters.TagsToMap(describeOutput.SecurityGroups[0].Tags)).To(HaveKeyWithValue(key, value))
}
}

0 comments on commit b18cbb0

Please sign in to comment.