Skip to content

Commit 24f8b66

Browse files
committed
string type, use current UpdateClusterConfig
1 parent fd458b2 commit 24f8b66

File tree

6 files changed

+71
-90
lines changed

6 files changed

+71
-90
lines changed

config/crd/bases/controlplane.cluster.x-k8s.io_awsmanagedcontrolplanes.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,12 +2985,13 @@ spec:
29852985
upgradePolicy:
29862986
description: |-
29872987
The support policy to use for the cluster.
2988-
Extended support allows you to remain on specific Kubernetes versions for longer.
2988+
Extended support indicates that the cluster will not be automatically upgraded
2989+
when it leaves the standard support period, and will enter extended support.
29892990
Clusters in extended support have higher costs.
2990-
The default value is EXTENDED. Use STANDARD to disable extended support.
2991+
The default value is extended. Use standard to disable extended support.
29912992
enum:
2992-
- EXTENDED
2993-
- STANDARD
2993+
- extended
2994+
- standard
29942995
type: string
29952996
version:
29962997
description: |-

controlplane/eks/api/v1beta2/awsmanagedcontrolplane_types.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,13 @@ type AWSManagedControlPlaneSpec struct { //nolint: maligned
211211
KubeProxy KubeProxy `json:"kubeProxy,omitempty"`
212212

213213
// The support policy to use for the cluster.
214-
// Extended support allows you to remain on specific Kubernetes versions for longer.
214+
// Extended support indicates that the cluster will not be automatically upgraded
215+
// when it leaves the standard support period, and will enter extended support.
215216
// Clusters in extended support have higher costs.
216-
// The default value is EXTENDED. Use STANDARD to disable extended support.
217-
// +kubebuilder:validation:Enum=EXTENDED;STANDARD
217+
// The default value is extended. Use standard to disable extended support.
218+
// +kubebuilder:validation:Enum=extended;standard
218219
// +optional
219-
UpgradePolicy *string `json:"upgradePolicy,omitempty"`
220+
UpgradePolicy *UpgradePolicy `json:"upgradePolicy,omitempty"`
220221
}
221222

222223
// KubeProxy specifies how the kube-proxy daemonset is managed.

controlplane/eks/api/v1beta2/types.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,24 @@ type AddonIssue struct {
212212
ResourceIDs []string `json:"resourceIds,omitempty"`
213213
}
214214

215+
// UpgradePolicy defines the support policy to use for the cluster.
216+
type UpgradePolicy string
217+
218+
var (
219+
// UpgradePolicyExtended indicates that the cluster will not be automatically upgraded
220+
// when it leaves the standard support period, and will enter extended support.
221+
// Clusters in extended support have higher costs.
222+
UpgradePolicyExtended = UpgradePolicy("extended")
223+
224+
// UpgradePolicyStandard indicates that the cluster will be automatically upgraded
225+
// when it leaves the standard support period.
226+
UpgradePolicyStandard = UpgradePolicy("standard")
227+
)
228+
229+
func (e UpgradePolicy) String() string {
230+
return string(e)
231+
}
232+
215233
const (
216234
// SecurityGroupCluster is the security group for communication between EKS
217235
// control plane and managed node groups.

controlplane/eks/api/v1beta2/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cloud/services/eks/cluster.go

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"net"
23+
"strings"
2324
"time"
2425

2526
"github.com/aws/aws-sdk-go/aws"
@@ -139,10 +140,6 @@ func (s *Service) reconcileCluster(ctx context.Context) error {
139140
return errors.Wrap(err, "failed reconciling OIDC provider for cluster")
140141
}
141142

142-
if err := s.reconcileClusterUpgradePolicy(cluster); err != nil {
143-
return errors.Wrap(err, "failed reconciling cluster upgrade policy")
144-
}
145-
146143
return nil
147144
}
148145

@@ -472,7 +469,7 @@ func (s *Service) createCluster(eksClusterName string) (*eks.Cluster, error) {
472469

473470
if s.scope.ControlPlane.Spec.UpgradePolicy != nil {
474471
upgradePolicy = &eks.UpgradePolicyRequest{
475-
SupportType: s.scope.ControlPlane.Spec.UpgradePolicy,
472+
SupportType: aws.String(s.scope.ControlPlane.Spec.UpgradePolicy.String()),
476473
}
477474
}
478475

@@ -549,6 +546,15 @@ func (s *Service) reconcileClusterConfig(cluster *eks.Cluster) error {
549546
input.ResourcesVpcConfig = updateVpcConfig
550547
}
551548

549+
updateUpgradePolicy, err := s.reconcileUpgradePolicy(cluster.UpgradePolicy)
550+
if err != nil {
551+
return errors.Wrap(err, "couldn't update upgrade policy for cluster")
552+
}
553+
if updateUpgradePolicy != nil {
554+
needsUpdate = true
555+
input.UpgradePolicy = updateUpgradePolicy
556+
}
557+
552558
if needsUpdate {
553559
if err := input.Validate(); err != nil {
554560
return errors.Wrap(err, "created invalid UpdateClusterConfigInput")
@@ -739,61 +745,29 @@ func (s *Service) reconcileClusterVersion(cluster *eks.Cluster) error {
739745
return nil
740746
}
741747

742-
func (s *Service) reconcileClusterUpgradePolicy(cluster *eks.Cluster) error {
748+
func (s *Service) reconcileUpgradePolicy(upgradePolicy *eks.UpgradePolicyResponse) (*eks.UpgradePolicyRequest, error) {
743749
s.Info("reconciling upgrade policy")
744750

745-
clusterUpgradePolicy := cluster.UpgradePolicy.SupportType
751+
clusterUpgradePolicy := upgradePolicy.SupportType
746752

747753
if s.scope.ControlPlane.Spec.UpgradePolicy == nil {
748754
s.Debug("upgrade policy not given, no action")
749-
return nil
755+
return nil, nil
750756
}
751757

752758
if clusterUpgradePolicy == nil {
753759
s.Debug("cannot get cluster upgrade policy, no action")
754-
return nil
760+
return nil, nil
755761
}
756762

757-
if *clusterUpgradePolicy == *s.scope.ControlPlane.Spec.UpgradePolicy {
763+
if strings.ToLower(*clusterUpgradePolicy) == s.scope.ControlPlane.Spec.UpgradePolicy.String() {
758764
s.Debug("upgrade policy unchanged, no action")
759-
return nil
760-
}
761-
762-
input := &eks.UpdateClusterConfigInput{
763-
Name: aws.String(s.scope.KubernetesClusterName()),
764-
UpgradePolicy: &eks.UpgradePolicyRequest{
765-
SupportType: s.scope.ControlPlane.Spec.UpgradePolicy,
766-
},
767-
}
768-
769-
if err := wait.WaitForWithRetryable(wait.NewBackoff(), func() (bool, error) {
770-
if _, err := s.EKSClient.UpdateClusterConfig(input); err != nil {
771-
if aerr, ok := err.(awserr.Error); ok {
772-
return false, aerr
773-
}
774-
return false, err
775-
}
776-
777-
// Wait until status transitions to UPDATING because there's a short
778-
// window after UpdateClusterConfig returns where the cluster
779-
// status is ACTIVE and the update would be tried again
780-
if err := s.EKSClient.WaitUntilClusterUpdating(
781-
&eks.DescribeClusterInput{Name: aws.String(s.scope.KubernetesClusterName())},
782-
request.WithWaiterLogger(&awslog{s.GetLogger()}),
783-
); err != nil {
784-
return false, err
785-
}
786-
787-
conditions.MarkTrue(s.scope.ControlPlane, ekscontrolplanev1.EKSControlPlaneUpdatingCondition)
788-
record.Eventf(s.scope.ControlPlane, "InitiatedUpdateEKSControlPlane", "Initiated update of EKS control plane %s to upgrade policy %s", s.scope.KubernetesClusterName(), s.scope.ControlPlane.Spec.UpgradePolicy)
789-
790-
return true, nil
791-
}); err != nil {
792-
record.Warnf(s.scope.ControlPlane, "FailedUpdateEKSControlPlane", "failed to update the EKS control plane: %v", err)
793-
return errors.Wrapf(err, "failed to update EKS cluster")
765+
return nil, nil
794766
}
795767

796-
return nil
768+
return &eks.UpgradePolicyRequest{
769+
SupportType: convertUpgradePolicy(*s.scope.ControlPlane.Spec.UpgradePolicy),
770+
}, nil
797771
}
798772

799773
func (s *Service) describeEKSCluster(eksClusterName string) (*eks.Cluster, error) {
@@ -925,3 +899,10 @@ func getKeyArn(encryptionConfig *eks.EncryptionConfig) string {
925899
}
926900
return ""
927901
}
902+
903+
func convertUpgradePolicy(input ekscontrolplanev1.UpgradePolicy) *string {
904+
if input == ekscontrolplanev1.UpgradePolicyStandard {
905+
return aws.String(eks.SupportTypeStandard)
906+
}
907+
return aws.String(eks.SupportTypeExtended)
908+
}

pkg/cloud/services/eks/cluster_test.go

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ func TestCreateCluster(t *testing.T) {
524524
RoleName: tc.role,
525525
NetworkSpec: infrav1.NetworkSpec{Subnets: tc.subnets},
526526
BootstrapSelfManagedAddons: false,
527-
UpgradePolicy: aws.String(eks.SupportTypeStandard),
527+
UpgradePolicy: &ekscontrolplanev1.UpgradePolicyStandard,
528528
},
529529
},
530530
})
@@ -679,57 +679,43 @@ func TestReconcileEKSEncryptionConfig(t *testing.T) {
679679
}
680680
}
681681

682-
func TestReconcileEKSUpgradePolicy(t *testing.T) {
682+
func TestReconcileUpgradePolicy(t *testing.T) {
683683
clusterName := "default.cluster"
684684
tests := []struct {
685685
name string
686686
oldUpgradePolicy *string
687-
newUpgradePolicy *string
688-
expect func(m *mock_eksiface.MockEKSAPIMockRecorder)
687+
newUpgradePolicy *ekscontrolplanev1.UpgradePolicy
688+
expect *eks.UpgradePolicyRequest
689689
expectError bool
690690
}{
691691
{
692692
name: "no update necessary - no upgrade policy specified",
693693
oldUpgradePolicy: aws.String(eks.SupportTypeStandard),
694-
expect: func(m *mock_eksiface.MockEKSAPIMockRecorder) {},
694+
expect: nil,
695695
expectError: false,
696696
},
697697
{
698698
name: "no update necessary - cannot get cluster upgrade policy",
699-
newUpgradePolicy: aws.String(eks.SupportTypeStandard),
700-
expect: func(m *mock_eksiface.MockEKSAPIMockRecorder) {},
699+
newUpgradePolicy: &ekscontrolplanev1.UpgradePolicyStandard,
700+
expect: nil,
701701
expectError: false,
702702
},
703703
{
704704
name: "no update necessary - upgrade policy unchanged",
705705
oldUpgradePolicy: aws.String(eks.SupportTypeStandard),
706-
newUpgradePolicy: aws.String(eks.SupportTypeStandard),
707-
expect: func(m *mock_eksiface.MockEKSAPIMockRecorder) {},
706+
newUpgradePolicy: &ekscontrolplanev1.UpgradePolicyStandard,
707+
expect: nil,
708708
expectError: false,
709709
},
710710
{
711711
name: "needs update",
712712
oldUpgradePolicy: aws.String(eks.SupportTypeStandard),
713-
newUpgradePolicy: aws.String(eks.SupportTypeExtended),
714-
expect: func(m *mock_eksiface.MockEKSAPIMockRecorder) {
715-
m.WaitUntilClusterUpdating(
716-
gomock.AssignableToTypeOf(&eks.DescribeClusterInput{}), gomock.Any(),
717-
).Return(nil)
718-
m.UpdateClusterConfig(gomock.AssignableToTypeOf(&eks.UpdateClusterConfigInput{})).Return(&eks.UpdateClusterConfigOutput{}, nil)
713+
newUpgradePolicy: &ekscontrolplanev1.UpgradePolicyExtended,
714+
expect: &eks.UpgradePolicyRequest{
715+
SupportType: aws.String(eks.SupportTypeExtended),
719716
},
720717
expectError: false,
721718
},
722-
{
723-
name: "api error",
724-
oldUpgradePolicy: aws.String(eks.SupportTypeExtended),
725-
newUpgradePolicy: aws.String(eks.SupportTypeStandard),
726-
expect: func(m *mock_eksiface.MockEKSAPIMockRecorder) {
727-
m.
728-
UpdateClusterConfig(gomock.AssignableToTypeOf(&eks.UpdateClusterConfigInput{})).
729-
Return(&eks.UpdateClusterConfigOutput{}, errors.New(""))
730-
},
731-
expectError: true,
732-
},
733719
}
734720

735721
for _, tc := range tests {
@@ -739,8 +725,6 @@ func TestReconcileEKSUpgradePolicy(t *testing.T) {
739725
mockControl := gomock.NewController(t)
740726
defer mockControl.Finish()
741727

742-
eksMock := mock_eksiface.NewMockEKSAPI(mockControl)
743-
744728
scheme := runtime.NewScheme()
745729
_ = infrav1.AddToScheme(scheme)
746730
_ = ekscontrolplanev1.AddToScheme(scheme)
@@ -762,20 +746,16 @@ func TestReconcileEKSUpgradePolicy(t *testing.T) {
762746
})
763747
g.Expect(err).To(BeNil())
764748

765-
tc.expect(eksMock.EXPECT())
766749
s := NewService(scope)
767-
s.EKSClient = eksMock
768750

769-
err = s.reconcileClusterUpgradePolicy(&eks.Cluster{
770-
UpgradePolicy: &eks.UpgradePolicyResponse{
771-
SupportType: tc.oldUpgradePolicy,
772-
},
751+
upgradePolicyRequest, err := s.reconcileUpgradePolicy(&eks.UpgradePolicyResponse{
752+
SupportType: tc.oldUpgradePolicy,
773753
})
774754
if tc.expectError {
775755
g.Expect(err).To(HaveOccurred())
776756
return
777757
}
778-
g.Expect(err).To(BeNil())
758+
g.Expect(upgradePolicyRequest).To(Equal(tc.expect))
779759
})
780760
}
781761
}

0 commit comments

Comments
 (0)