Skip to content

Commit bcaee3d

Browse files
feat(addons): add updateAddonTagsWithLatestVersion to set addon tags to latest version
- Fetches the addon versions based on the Kubernetes version. - Implement updateAddonTagsWithLatestVersion function to automatically update addon tags with the latest available version from EKS. Signed-off-by: Thiha Min Thant <[email protected]>
1 parent 85759ce commit bcaee3d

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

controlplane/eks/controllers/awsmanagedcontrolplane_controller_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,8 @@ func mockedEKSCluster(g *WithT, eksRec *mock_eksiface.MockEKSAPIMockRecorder, ia
924924
ClusterName: aws.String("test-cluster"),
925925
}).Return(&eks.ListAddonsOutput{}, nil)
926926

927+
eksRec.DescribeAddonVersions(&eks.DescribeAddonVersionsInput{}).Return(&eks.DescribeAddonVersionsOutput{}, nil)
928+
927929
awsNodeRec.ReconcileCNI(gomock.Any()).Return(nil)
928930
kubeProxyRec.ReconcileKubeProxy(gomock.Any()).Return(nil)
929931
iamAuthenticatorRec.ReconcileIAMAuthenticator(gomock.Any()).Return(nil)

pkg/cloud/services/eks/addons.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ func (s *Service) reconcileAddons(ctx context.Context) error {
5252
// Get the addons from the spec we want for the cluster
5353
desiredAddons := s.translateAPIToAddon(s.scope.Addons())
5454

55+
// Update the tags with the latest version if available
56+
if desiredAddons, err = s.updateAddonTagsWithLatestVersion(desiredAddons); err != nil {
57+
return fmt.Errorf("updating addon tags with latest version: %w", err)
58+
}
59+
5560
// If there are no addons desired or installed then do nothing
5661
if len(installed) == 0 && len(desiredAddons) == 0 {
5762
s.scope.Info("no addons installed and no addons to install, no action needed")
@@ -209,6 +214,45 @@ func (s *Service) translateAPIToAddon(addons []ekscontrolplanev1.Addon) []*eksad
209214
return converted
210215
}
211216

217+
func (s *Service) updateAddonTagsWithLatestVersion(addons []*eksaddons.EKSAddon) ([]*eksaddons.EKSAddon, error) {
218+
s.Debug("Updating addon tags with the latest available version")
219+
220+
input := &eks.DescribeAddonVersionsInput{
221+
KubernetesVersion: s.scope.ControlPlane.Spec.Version,
222+
}
223+
224+
output, err := s.EKSClient.DescribeAddonVersions(input)
225+
if err != nil {
226+
return nil, fmt.Errorf("error describing addon versions: %w", err)
227+
}
228+
if len(output.Addons) == 0 {
229+
s.Debug("No addons found for the specified Kubernetes version")
230+
return addons, nil
231+
}
232+
233+
latestVersions := make(map[string]string)
234+
for _, addon := range output.Addons {
235+
if len(addon.AddonVersions) > 0 {
236+
latestVersions[*addon.AddonName] = *addon.AddonVersions[0].AddonVersion
237+
for _, version := range addon.AddonVersions[1:] {
238+
if *version.AddonVersion > latestVersions[*addon.AddonName] {
239+
latestVersions[*addon.AddonName] = *version.AddonVersion
240+
}
241+
}
242+
}
243+
}
244+
245+
for _, addon := range addons {
246+
if addon.Version != nil && *addon.Version == "latest" {
247+
if latestVersion, ok := latestVersions[*addon.Name]; ok {
248+
addon.Version = &latestVersion
249+
}
250+
}
251+
}
252+
253+
return addons, nil
254+
}
255+
212256
func convertConflictResolution(conflict ekscontrolplanev1.AddonResolution) *string {
213257
if conflict == ekscontrolplanev1.AddonResolutionNone {
214258
return aws.String(eks.ResolveConflictsNone)

0 commit comments

Comments
 (0)