Skip to content

Commit

Permalink
Merge pull request #725 from mjura/ebs-v2.9
Browse files Browse the repository at this point in the history
[v2.9] Set information about CSI EBS driver
  • Loading branch information
mjura committed Aug 6, 2024
2 parents 2ed127e + 285dccd commit 670e34c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions controller/eks-cluster-config-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (h *Handler) checkAndUpdate(ctx context.Context, config *eksv1.EKSClusterCo
return h.eksCC.UpdateStatus(config)
}

upstreamSpec, clusterARN, err := BuildUpstreamClusterState(ctx, config.Spec.DisplayName, config.Status.ManagedLaunchTemplateID, clusterState, nodeGroupStates, awsSVCs.ec2, true)
upstreamSpec, clusterARN, err := BuildUpstreamClusterState(ctx, config.Spec.DisplayName, config.Status.ManagedLaunchTemplateID, clusterState, nodeGroupStates, awsSVCs, true)
if err != nil {
return config, err
}
Expand Down Expand Up @@ -1000,7 +1000,7 @@ func (h *Handler) updateUpstreamClusterState(ctx context.Context, upstreamSpec *

// check if ebs csi driver needs to be enabled
if aws.ToBool(config.Spec.EBSCSIDriver) {
installedArn, err := awsservices.CheckEBSAddon(ctx, awsSVCs.eks, config)
installedArn, err := awsservices.CheckEBSAddon(ctx, config.Spec.DisplayName, awsSVCs.eks)
if err != nil {
return nil, fmt.Errorf("error checking if ebs csi driver addon is installed: %w", err)
}
Expand Down
15 changes: 13 additions & 2 deletions controller/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"fmt"
"strconv"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/eks"
Expand Down Expand Up @@ -48,7 +49,7 @@ func NodeGroupIssueIsUpdatable(code string) bool {
}

// BuildUpstreamClusterState builds the upstream cluster state from the given eks cluster and node group states.
func BuildUpstreamClusterState(ctx context.Context, name, managedTemplateID string, clusterState *eks.DescribeClusterOutput, nodeGroupStates []*eks.DescribeNodegroupOutput, ec2Service services.EC2ServiceInterface, includeManagedLaunchTemplate bool) (*eksv1.EKSClusterConfigSpec, string, error) {
func BuildUpstreamClusterState(ctx context.Context, name, managedTemplateID string, clusterState *eks.DescribeClusterOutput, nodeGroupStates []*eks.DescribeNodegroupOutput, awsSVCs *awsServices, includeManagedLaunchTemplate bool) (*eksv1.EKSClusterConfigSpec, string, error) {
upstreamSpec := &eksv1.EKSClusterConfigSpec{}

upstreamSpec.Imported = true
Expand Down Expand Up @@ -98,6 +99,16 @@ func BuildUpstreamClusterState(ctx context.Context, name, managedTemplateID stri
}
}

// set ebs csi driver
upstreamSpec.EBSCSIDriver = aws.Bool(false)
currentARN, err := awsservices.CheckEBSAddon(ctx, name, awsSVCs.eks)
if err != nil {
return nil, "", fmt.Errorf("error checking if ebs csi driver addon is installed: %w", err)
}
if strings.Contains(currentARN, "aws-ebs-csi-driver") {
upstreamSpec.EBSCSIDriver = aws.Bool(true)
}

// set node groups
upstreamSpec.NodeGroups = make([]eksv1.NodeGroup, 0, len(nodeGroupStates))
for _, ng := range nodeGroupStates {
Expand Down Expand Up @@ -142,7 +153,7 @@ func BuildUpstreamClusterState(ctx context.Context, name, managedTemplateID stri
if managedTemplateID == aws.ToString(ngToAdd.LaunchTemplate.ID) {
// If this is a rancher-managed launch template, then we move the data from the launch template to the node group.
launchTemplateRequestOutput, err := awsservices.GetLaunchTemplateVersions(ctx, &awsservices.GetLaunchTemplateVersionsOpts{
EC2Service: ec2Service,
EC2Service: awsSVCs.ec2,
LaunchTemplateID: ngToAdd.LaunchTemplate.ID,
Versions: []*string{ng.Nodegroup.LaunchTemplate.Version},
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/eks/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func GetLaunchTemplateVersions(ctx context.Context, opts *GetLaunchTemplateVersi

// CheckEBSAddon checks if the EBS CSI driver add-on is installed. If it is, it will return
// the ARN of the add-on. If it is not, it will return an empty string. Otherwise, it will return an error
func CheckEBSAddon(ctx context.Context, eksService services.EKSServiceInterface, config *eksv1.EKSClusterConfig) (string, error) {
func CheckEBSAddon(ctx context.Context, clusterName string, eksService services.EKSServiceInterface) (string, error) {
input := eks.DescribeAddonInput{
AddonName: aws.String(ebsCSIAddonName),
ClusterName: aws.String(config.Spec.DisplayName),
ClusterName: aws.String(clusterName),
}

output, err := eksService.DescribeAddon(ctx, &input)
Expand Down
6 changes: 3 additions & 3 deletions pkg/eks/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,23 @@ var _ = Describe("GetLaunchTemplateVersions", func() {
},
}
eksServiceMock.EXPECT().DescribeAddon(ctx, gomock.Any()).Return(eksDescribeAddonOutput, nil)
addonArn, err := CheckEBSAddon(ctx, enableEBSCSIDriverInput.EKSService, enableEBSCSIDriverInput.Config)
addonArn, err := CheckEBSAddon(ctx, enableEBSCSIDriverInput.Config.Spec.DisplayName, enableEBSCSIDriverInput.EKSService)
Expect(err).To(Succeed())
Expect(addonArn).To(Equal("arn:aws::ebs-csi-driver"))
})

It("should detect that addon is not installed", func() {
eksDescribeAddonOutput = &eks.DescribeAddonOutput{}
eksServiceMock.EXPECT().DescribeAddon(ctx, gomock.Any()).Return(eksDescribeAddonOutput, nil)
addonArn, err := CheckEBSAddon(ctx, enableEBSCSIDriverInput.EKSService, enableEBSCSIDriverInput.Config)
addonArn, err := CheckEBSAddon(ctx, enableEBSCSIDriverInput.Config.Spec.DisplayName, enableEBSCSIDriverInput.EKSService)
Expect(err).To(Succeed())
Expect(addonArn).To(Equal(""))
})

It("should fail to check if addon is not installed", func() {
eksDescribeAddonOutput = &eks.DescribeAddonOutput{}
eksServiceMock.EXPECT().DescribeAddon(ctx, gomock.Any()).Return(nil, fmt.Errorf("failed to describe addon"))
_, err := CheckEBSAddon(ctx, enableEBSCSIDriverInput.EKSService, enableEBSCSIDriverInput.Config)
_, err := CheckEBSAddon(ctx, enableEBSCSIDriverInput.Config.Spec.DisplayName, enableEBSCSIDriverInput.EKSService)
Expect(err).ToNot(Succeed())
})
})
Expand Down

0 comments on commit 670e34c

Please sign in to comment.