@@ -21,13 +21,13 @@ package e2e
2121
2222import (
2323 "context"
24- "errors"
2524 "fmt"
2625
2726 "github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2020-02-01/containerservice"
2827 "github.com/Azure/go-autorest/autorest/azure/auth"
2928 . "github.com/onsi/ginkgo"
3029 . "github.com/onsi/gomega"
30+ "github.com/pkg/errors"
3131 "golang.org/x/mod/semver"
3232 "k8s.io/apimachinery/pkg/types"
3333 infraexpv1 "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
@@ -226,8 +226,14 @@ func GetAKSKubernetesVersion(ctx context.Context, e2eConfig *clusterctl.E2EConfi
226226 settings , err := auth .GetSettingsFromEnvironment ()
227227 Expect (err ).NotTo (HaveOccurred ())
228228 subscriptionID := settings .GetSubscriptionID ()
229- maxVersion , err := GetWorkingAKSKubernetesVersion (ctx , subscriptionID , location , e2eAKSVersion )
230- Expect (err ).NotTo (HaveOccurred ())
229+ var maxVersion string
230+ if e2eAKSVersion == "latest" {
231+ maxVersion , err = GetLatestStableAKSKubernetesVersion (ctx , subscriptionID , location )
232+ Expect (err ).NotTo (HaveOccurred ())
233+ } else {
234+ maxVersion , err = GetWorkingAKSKubernetesVersion (ctx , subscriptionID , location , e2eAKSVersion )
235+ Expect (err ).NotTo (HaveOccurred ())
236+ }
231237
232238 return maxVersion , nil
233239}
@@ -249,17 +255,17 @@ func byClusterOptions(name, namespace string) []client.ListOption {
249255func GetWorkingAKSKubernetesVersion (ctx context.Context , subscriptionID , location , version string ) (string , error ) {
250256 settings , err := auth .GetSettingsFromEnvironment ()
251257 if err != nil {
252- return "" , err
258+ return "" , errors . Wrap ( err , "GetSettingsFromEnvironment failed." )
253259 }
254260 authorizer , err := settings .GetAuthorizer ()
255261 if err != nil {
256- return "" , err
262+ return "" , errors . Wrap ( err , "Failed to create an Authorizer." )
257263 }
258264 containerServiceClient := containerservice .NewContainerServicesClient (subscriptionID )
259265 containerServiceClient .Authorizer = authorizer
260266 result , err := containerServiceClient .ListOrchestrators (ctx , location , ManagedClustersResourceType )
261267 if err != nil {
262- return "" , err
268+ return "" , errors . Wrap ( err , "Failed to list Orchestrators." )
263269 }
264270
265271 var latestStableVersionDesired bool
@@ -304,3 +310,44 @@ func GetWorkingAKSKubernetesVersion(ctx context.Context, subscriptionID, locatio
304310
305311 return maxVersion , nil
306312}
313+
314+ // GetLatestStableAKSKubernetesVersion returns the latest stable available Kubernetes version of AKS.
315+ func GetLatestStableAKSKubernetesVersion (ctx context.Context , subscriptionID , location string ) (string , error ) {
316+ settings , err := auth .GetSettingsFromEnvironment ()
317+ if err != nil {
318+ return "" , errors .Wrap (err , "GetSettingsFromEnvironment failed." )
319+ }
320+ authorizer , err := settings .GetAuthorizer ()
321+ if err != nil {
322+ return "" , errors .Wrap (err , "Failed to create an Authorizer." )
323+ }
324+ containerServiceClient := containerservice .NewContainerServicesClient (subscriptionID )
325+ containerServiceClient .Authorizer = authorizer
326+ result , err := containerServiceClient .ListOrchestrators (ctx , location , ManagedClustersResourceType )
327+ if err != nil {
328+ return "" , errors .Wrap (err , "Failed to list Orchestrators." )
329+ }
330+
331+ var orchestratorversions []string
332+ var foundWorkingVersion bool
333+ var orchVersion string
334+ var maxVersion string
335+
336+ for _ , o := range * result .Orchestrators {
337+ orchVersion = * o .OrchestratorVersion
338+ // semver comparisons require a "v" prefix
339+ if orchVersion [:1 ] != "v" && o .IsPreview == nil {
340+ orchVersion = fmt .Sprintf ("v%s" , * o .OrchestratorVersion )
341+ }
342+ orchestratorversions = append (orchestratorversions , orchVersion )
343+ }
344+ semver .Sort (orchestratorversions )
345+ maxVersion = orchestratorversions [len (orchestratorversions )- 1 ]
346+ if semver .IsValid (maxVersion ) {
347+ foundWorkingVersion = true
348+ }
349+ if ! foundWorkingVersion {
350+ return "" , errors .New (fmt .Sprintf ("Latest stable AKS version not found." ))
351+ }
352+ return maxVersion , nil
353+ }
0 commit comments