@@ -21,6 +21,7 @@ import (
2121 "testing"
2222
2323 "github.com/aws/aws-sdk-go-v2/aws"
24+ "github.com/aws/aws-sdk-go-v2/aws/awserr"
2425 "github.com/aws/aws-sdk-go-v2/service/eks"
2526 ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
2627 "github.com/aws/aws-sdk-go-v2/service/iam"
@@ -474,6 +475,123 @@ func TestReconcileClusterVersion(t *testing.T) {
474475 }
475476}
476477
478+ func TestReconcileAccessConfig (t * testing.T ) {
479+ clusterName := "default.cluster"
480+ tests := []struct {
481+ name string
482+ expect func (m * mock_eksiface.MockEKSAPIMockRecorder )
483+ expectError bool
484+ }{
485+ {
486+ name : "no upgrade necessary" ,
487+ expect : func (m * mock_eksiface.MockEKSAPIMockRecorder ) {
488+ m .
489+ DescribeCluster (gomock .Eq (context .TODO ()), gomock .AssignableToTypeOf (& eks.DescribeClusterInput {})).
490+ Return (& eks.DescribeClusterOutput {
491+ Cluster : & ekstypes.Cluster {
492+ Name : aws .String ("default.cluster" ),
493+ AccessConfig : & ekstypes.AccessConfigResponse {
494+ AuthenticationMode : string (ekstypes .AuthenticationModeApiAndConfigMap ),
495+ },
496+ },
497+ }, nil )
498+ },
499+ expectError : false ,
500+ },
501+ {
502+ name : "needs upgrade" ,
503+ expect : func (m * mock_eksiface.MockEKSAPIMockRecorder ) {
504+ m .
505+ DescribeCluster (gomock .Eq (context .TODO ()), gomock .AssignableToTypeOf (& eks.DescribeClusterInput {})).
506+ Return (& eks.DescribeClusterOutput {
507+ Cluster : & ekstypes.Cluster {
508+ Name : aws .String ("default.cluster" ),
509+ AccessConfig : & ekstypes.AccessConfigResponse {
510+ AuthenticationMode : string (ekstypes .AuthenticationModeConfigMap ),
511+ },
512+ },
513+ }, nil )
514+ m .WaitUntilClusterUpdating (
515+ gomock .Eq (context .TODO ()),
516+ gomock .AssignableToTypeOf (& eks.DescribeClusterInput {}),
517+ gomock .Any (),
518+ ).Return (nil )
519+ m .
520+ UpdateClusterConfig (gomock .Eq (context .TODO ()), gomock .AssignableToTypeOf (& eks.UpdateClusterConfigInput {})).
521+ Return (& eks.UpdateClusterConfigOutput {}, nil )
522+ },
523+ expectError : false ,
524+ },
525+ {
526+ name : "api error" ,
527+ expect : func (m * mock_eksiface.MockEKSAPIMockRecorder ) {
528+ m .
529+ DescribeCluster (gomock .Eq (context .TODO ()), gomock .AssignableToTypeOf (& eks.DescribeClusterInput {})).
530+ Return (& eks.DescribeClusterOutput {
531+ Cluster : & ekstypes.Cluster {
532+ Name : aws .String ("default.cluster" ),
533+ AccessConfig : & ekstypes.AccessConfigResponse {
534+ AuthenticationMode : string (ekstypes .AuthenticationModeApi ),
535+ },
536+ },
537+ }, nil )
538+ m .
539+ UpdateClusterConfig (gomock .Eq (context .TODO ()), gomock .AssignableToTypeOf (& eks.UpdateClusterConfigInput {})).
540+ Return (& eks.UpdateClusterConfigOutput {}, awserr .New (string (ekstypes .ErrCodeInvalidParameterException ), "Unsupported authentication mode update" , nil ))
541+ },
542+ expectError : true ,
543+ },
544+ }
545+
546+ for _ , tc := range tests {
547+ t .Run (tc .name , func (t * testing.T ) {
548+ g := NewWithT (t )
549+
550+ mockControl := gomock .NewController (t )
551+ defer mockControl .Finish ()
552+
553+ eksMock := mock_eksiface .NewMockEKSAPI (mockControl )
554+
555+ scheme := runtime .NewScheme ()
556+ _ = infrav1 .AddToScheme (scheme )
557+ _ = ekscontrolplanev1 .AddToScheme (scheme )
558+ client := fake .NewClientBuilder ().WithScheme (scheme ).Build ()
559+ scope , err := scope .NewManagedControlPlaneScope (scope.ManagedControlPlaneScopeParams {
560+ Client : client ,
561+ Cluster : & clusterv1.Cluster {
562+ ObjectMeta : metav1.ObjectMeta {
563+ Namespace : "ns" ,
564+ Name : clusterName ,
565+ },
566+ },
567+ ControlPlane : & ekscontrolplanev1.AWSManagedControlPlane {
568+ Spec : ekscontrolplanev1.AWSManagedControlPlaneSpec {
569+ EKSClusterName : clusterName ,
570+ AccessConfig : & ekscontrolplanev1.AccessConfig {
571+ AuthenticationMode : ekscontrolplanev1 .EKSAuthenticationModeApiAndConfigMap ,
572+ },
573+ },
574+ },
575+ })
576+ g .Expect (err ).To (BeNil ())
577+
578+ tc .expect (eksMock .EXPECT ())
579+ s := NewService (scope )
580+ s .EKSClient = eksMock
581+
582+ cluster , err := s .describeEKSCluster (context .TODO (), clusterName )
583+ g .Expect (err ).To (BeNil ())
584+
585+ err = s .reconcileAccessConfig (cluster .AccessConfig )
586+ if tc .expectError {
587+ g .Expect (err ).To (HaveOccurred ())
588+ return
589+ }
590+ g .Expect (err ).To (BeNil ())
591+ })
592+ }
593+ }
594+
477595func TestCreateCluster (t * testing.T ) {
478596 clusterName := "cluster.default"
479597 version := aws .String ("1.24" )
0 commit comments