sigs.k8s.io/cluster-api-provider-azure@v1.14.3/test/e2e/aks_clusterclass.go (about)

     1  //go:build e2e
     2  // +build e2e
     3  
     4  /*
     5  Copyright 2023 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package e2e
    21  
    22  import (
    23  	"context"
    24  
    25  	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    26  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4"
    27  	. "github.com/onsi/ginkgo/v2"
    28  	. "github.com/onsi/gomega"
    29  	"k8s.io/apimachinery/pkg/types"
    30  	"k8s.io/utils/ptr"
    31  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    32  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    33  	expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
    34  	"sigs.k8s.io/controller-runtime/pkg/client"
    35  )
    36  
    37  type AKSClusterClassInput struct {
    38  	Cluster                    *clusterv1.Cluster
    39  	MachinePool                *expv1.MachinePool
    40  	WaitIntervals              []interface{}
    41  	WaitUpgradeIntervals       []interface{}
    42  	KubernetesVersionUpgradeTo string
    43  }
    44  
    45  func AKSClusterClassSpec(ctx context.Context, inputGetter func() AKSClusterClassInput) {
    46  	input := inputGetter()
    47  
    48  	cred, err := azidentity.NewDefaultAzureCredential(nil)
    49  	Expect(err).NotTo(HaveOccurred())
    50  
    51  	managedClustersClient, err := armcontainerservice.NewManagedClustersClient(getSubscriptionID(Default), cred, nil)
    52  	Expect(err).NotTo(HaveOccurred())
    53  
    54  	agentPoolsClient, err := armcontainerservice.NewAgentPoolsClient(getSubscriptionID(Default), cred, nil)
    55  	Expect(err).NotTo(HaveOccurred())
    56  
    57  	mgmtClient := bootstrapClusterProxy.GetClient()
    58  	Expect(mgmtClient).NotTo(BeNil())
    59  
    60  	amcp := &infrav1.AzureManagedControlPlane{}
    61  	err = mgmtClient.Get(ctx, types.NamespacedName{
    62  		Namespace: input.Cluster.Spec.ControlPlaneRef.Namespace,
    63  		Name:      input.Cluster.Spec.ControlPlaneRef.Name,
    64  	}, amcp)
    65  	Expect(err).NotTo(HaveOccurred())
    66  
    67  	By("Editing the AzureManagedMachinePoolTemplate to change the scale down mode")
    68  	ammpt := &infrav1.AzureManagedMachinePoolTemplate{}
    69  
    70  	clusterClass := &clusterv1.ClusterClass{}
    71  	err = mgmtClient.Get(ctx, types.NamespacedName{
    72  		Namespace: input.Cluster.Namespace,
    73  		Name:      "default",
    74  	}, clusterClass)
    75  
    76  	Eventually(func(g Gomega) {
    77  		for i := range clusterClass.Spec.Workers.MachinePools {
    78  			err = mgmtClient.Get(ctx, types.NamespacedName{
    79  				Namespace: clusterClass.Spec.Workers.MachinePools[i].Template.Infrastructure.Ref.Namespace,
    80  				Name:      clusterClass.Spec.Workers.MachinePools[i].Template.Infrastructure.Ref.Name,
    81  			}, ammpt)
    82  			Expect(err).NotTo(HaveOccurred())
    83  			if ammpt.Spec.Template.Spec.OsDiskType != nil && *ammpt.Spec.Template.Spec.OsDiskType != "Ephemeral" {
    84  				ammpt.Spec.Template.Spec.ScaleDownMode = ptr.To("Deallocate")
    85  				g.Expect(mgmtClient.Update(ctx, ammpt)).To(Succeed())
    86  			}
    87  		}
    88  	}, inputGetter().WaitIntervals...).Should(Succeed())
    89  
    90  	ammp := &infrav1.AzureManagedMachinePool{}
    91  
    92  	Eventually(func(g Gomega) {
    93  		err = mgmtClient.Get(ctx, types.NamespacedName{
    94  			Namespace: input.MachinePool.Spec.Template.Spec.InfrastructureRef.Namespace,
    95  			Name:      input.MachinePool.Spec.Template.Spec.InfrastructureRef.Name,
    96  		}, ammp)
    97  		Expect(err).NotTo(HaveOccurred())
    98  		if ammp.Spec.OsDiskType != nil && *ammp.Spec.OsDiskType != "Ephemeral" {
    99  			g.Expect(ammp.Spec.ScaleDownMode).To(Equal(ptr.To("Deallocate")))
   100  		}
   101  	}, inputGetter().WaitIntervals...).Should(Succeed())
   102  
   103  	Eventually(func(g Gomega) {
   104  		resp, err := agentPoolsClient.Get(ctx, amcp.Spec.ResourceGroupName, amcp.Name, *ammp.Spec.Name, nil)
   105  		g.Expect(err).NotTo(HaveOccurred())
   106  		agentPool := resp.AgentPool
   107  		g.Expect(agentPool.Properties).NotTo(BeNil())
   108  		g.Expect(agentPool.Properties.ScaleDownMode).NotTo(BeNil())
   109  		g.Expect(*agentPool.Properties.ScaleDownMode).To(Equal(armcontainerservice.ScaleDownModeDeallocate))
   110  	}, input.WaitIntervals...).Should(Succeed())
   111  
   112  	By("Upgrading the cluster topology version")
   113  	Eventually(func(g Gomega) {
   114  		err := mgmtClient.Get(ctx, client.ObjectKeyFromObject(input.Cluster), input.Cluster)
   115  		g.Expect(err).NotTo(HaveOccurred())
   116  		input.Cluster.Spec.Topology.Version = input.KubernetesVersionUpgradeTo
   117  		g.Expect(mgmtClient.Update(ctx, input.Cluster)).To(Succeed())
   118  	}, inputGetter().WaitIntervals...).Should(Succeed())
   119  
   120  	Eventually(func(g Gomega) {
   121  		resp, err := managedClustersClient.Get(ctx, amcp.Spec.ResourceGroupName, amcp.Name, nil)
   122  		g.Expect(err).NotTo(HaveOccurred())
   123  		aksCluster := resp.ManagedCluster
   124  		g.Expect(aksCluster.Properties).NotTo(BeNil())
   125  		g.Expect(aksCluster.Properties.KubernetesVersion).NotTo(BeNil())
   126  		g.Expect("v" + *aksCluster.Properties.KubernetesVersion).To(Equal(input.KubernetesVersionUpgradeTo))
   127  		g.Expect(aksCluster.Properties.ProvisioningState).To(Equal(ptr.To("Succeeded")))
   128  	}, input.WaitUpgradeIntervals...).Should(Succeed())
   129  
   130  	By("Ensuring the upgrade is reflected in the amcp")
   131  	Eventually(func(g Gomega) {
   132  		g.Expect(mgmtClient.Get(ctx, types.NamespacedName{
   133  			Namespace: input.Cluster.Spec.ControlPlaneRef.Namespace,
   134  			Name:      input.Cluster.Spec.ControlPlaneRef.Name,
   135  		}, amcp)).To(Succeed())
   136  		g.Expect(amcp.Spec.Version).To(Equal(input.KubernetesVersionUpgradeTo))
   137  	}, input.WaitIntervals...).Should(Succeed())
   138  }