Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/ctl/utils/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ package utils
func ValidateLoggingFlags(toEnable, toDisable []string) error {
return validateLoggingFlags(toEnable, toDisable)
}

var UpdateAuthenticationModeForCluster = updateAuthenticationModeForCluster
18 changes: 15 additions & 3 deletions pkg/ctl/utils/update_authentication_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
"github.com/weaveworks/eksctl/pkg/eks"
"github.com/weaveworks/eksctl/pkg/eks/waiter"
)

Expand Down Expand Up @@ -51,12 +52,23 @@ func doUpdateAuthenticationMode(cmd *cmdutils.Cmd, authenticationMode string) er
return err
}

return updateAuthenticationModeForCluster(ctx, clusterProvider, cmd.ClusterConfig)
}

func updateAuthenticationModeForCluster(ctx context.Context, clusterProvider *eks.ClusterProvider, clusterConfig *api.ClusterConfig) error {
authenticationMode := clusterConfig.AccessConfig.AuthenticationMode
clusterName := clusterConfig.Metadata.Name

if cluster := clusterProvider.GetClusterState(); cluster.AccessConfig != nil && cluster.AccessConfig.AuthenticationMode == authenticationMode {
logger.Info("authentication mode is already %s on cluster %s, skipping update", authenticationMode, clusterName)
return nil
}

logger.Info("setting cluster's authentication mode to %s", authenticationMode)
clusterName := cmd.ClusterConfig.Metadata.Name
output, err := clusterProvider.AWSProvider.EKS().UpdateClusterConfig(ctx, &awseks.UpdateClusterConfigInput{
Name: &clusterName,
AccessConfig: &ekstypes.UpdateAccessConfigRequest{
AuthenticationMode: cmd.ClusterConfig.AccessConfig.AuthenticationMode,
AuthenticationMode: authenticationMode,
},
})
if err != nil {
Expand All @@ -79,7 +91,7 @@ func doUpdateAuthenticationMode(cmd *cmdutils.Cmd, authenticationMode string) er
return fmt.Errorf("failed to update cluster authentication mode: %s", e.UpdateError)

case nil:
logger.Info("authentication mode was successfully updated to %s on cluster %s", cmd.ClusterConfig.AccessConfig.AuthenticationMode, clusterName)
logger.Info("authentication mode was successfully updated to %s on cluster %s", authenticationMode, clusterName)
return nil

default:
Expand Down
77 changes: 77 additions & 0 deletions pkg/ctl/utils/update_authentication_mode_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package utils_test

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
awseks "github.com/aws/aws-sdk-go-v2/service/eks"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"

api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/ctl/utils"
"github.com/weaveworks/eksctl/pkg/eks"
"github.com/weaveworks/eksctl/pkg/testutils/mockprovider"
)

var _ = Describe("update authentication mode", func() {
Expand Down Expand Up @@ -37,4 +48,70 @@ var _ = Describe("update authentication mode", func() {
args: []string{"--authentication-mode", "CONFIG_MAP", "--config-file", "../../../examples/01-simple-cluster.yaml"},
}),
)

Describe("updating the cluster", func() {
const clusterName = "test"

makeClusterProvider := func(currentMode ekstypes.AuthenticationMode) (*mockprovider.MockProvider, *eks.ClusterProvider) {
provider := mockprovider.NewMockProvider()
return provider, &eks.ClusterProvider{
AWSProvider: provider,
Status: &eks.ProviderStatus{
ClusterInfo: &eks.ClusterInfo{
Cluster: &ekstypes.Cluster{
Name: aws.String(clusterName),
AccessConfig: &ekstypes.AccessConfigResponse{
AuthenticationMode: currentMode,
},
},
},
},
}
}

makeClusterConfig := func(desiredMode ekstypes.AuthenticationMode) *api.ClusterConfig {
clusterConfig := api.NewClusterConfig()
clusterConfig.Metadata.Name = clusterName
clusterConfig.AccessConfig.AuthenticationMode = desiredMode
return clusterConfig
}

When("the cluster already has the desired authentication mode", func() {
It("skips the update", func() {
provider, clusterProvider := makeClusterProvider(ekstypes.AuthenticationModeApiAndConfigMap)

err := utils.UpdateAuthenticationModeForCluster(context.Background(), clusterProvider, makeClusterConfig(ekstypes.AuthenticationModeApiAndConfigMap))
Expect(err).NotTo(HaveOccurred())
provider.MockEKS().AssertNotCalled(GinkgoT(), "UpdateClusterConfig", mock.Anything, mock.Anything)
})
})

When("the cluster has a different authentication mode", func() {
It("updates the cluster with the desired mode", func() {
provider, clusterProvider := makeClusterProvider(ekstypes.AuthenticationModeConfigMap)

updateID := aws.String("update-1")
provider.MockEKS().On("UpdateClusterConfig", mock.Anything, mock.MatchedBy(func(input *awseks.UpdateClusterConfigInput) bool {
return *input.Name == clusterName && input.AccessConfig.AuthenticationMode == ekstypes.AuthenticationModeApiAndConfigMap
})).Return(&awseks.UpdateClusterConfigOutput{
Update: &ekstypes.Update{
Id: updateID,
},
}, nil).Once()
provider.MockEKS().On("DescribeUpdate", mock.Anything, mock.MatchedBy(func(input *awseks.DescribeUpdateInput) bool {
return *input.UpdateId == *updateID
}), mock.Anything).Return(&awseks.DescribeUpdateOutput{
Update: &ekstypes.Update{
Id: updateID,
Type: ekstypes.UpdateTypeAccessConfigUpdate,
Status: ekstypes.UpdateStatusSuccessful,
},
}, nil).Once()

err := utils.UpdateAuthenticationModeForCluster(context.Background(), clusterProvider, makeClusterConfig(ekstypes.AuthenticationModeApiAndConfigMap))
Expect(err).NotTo(HaveOccurred())
provider.MockEKS().AssertExpectations(GinkgoT())
})
})
})
})
Loading