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

     1  //go:build e2e
     2  // +build e2e
     3  
     4  /*
     5  Copyright 2022 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  )
    34  
    35  type AKSAzureClusterAutoscalerSettingsSpecInput struct {
    36  	Cluster       *clusterv1.Cluster
    37  	WaitIntervals []interface{}
    38  }
    39  
    40  func AKSAzureClusterAutoscalerSettingsSpec(ctx context.Context, inputGetter func() AKSAzureClusterAutoscalerSettingsSpecInput) {
    41  	input := inputGetter()
    42  
    43  	mgmtClient := bootstrapClusterProxy.GetClient()
    44  	Expect(mgmtClient).NotTo(BeNil())
    45  	cred, err := azidentity.NewDefaultAzureCredential(nil)
    46  	Expect(err).NotTo(HaveOccurred())
    47  	containerserviceClient, err := armcontainerservice.NewManagedClustersClient(getSubscriptionID(Default), cred, nil)
    48  	Expect(err).NotTo(HaveOccurred())
    49  
    50  	var expectedAksExpander armcontainerservice.Expander
    51  	var newExpanderValue infrav1.Expander
    52  	var amcpInitialAutoScalerProfile = &infrav1.AutoScalerProfile{}
    53  	amcp := &infrav1.AzureManagedControlPlane{}
    54  	Eventually(func(g Gomega) {
    55  		err = mgmtClient.Get(ctx, types.NamespacedName{
    56  			Namespace: input.Cluster.Spec.ControlPlaneRef.Namespace,
    57  			Name:      input.Cluster.Spec.ControlPlaneRef.Name,
    58  		}, amcp)
    59  		g.Expect(err).NotTo(HaveOccurred())
    60  		amcpInitialAutoScalerProfile = amcp.Spec.AutoScalerProfile
    61  
    62  		aks, err := containerserviceClient.Get(ctx, amcp.Spec.ResourceGroupName, amcp.Name, nil)
    63  		g.Expect(err).NotTo(HaveOccurred())
    64  		g.Expect(aks.Properties.ProvisioningState).To(Equal(ptr.To("Succeeded")))
    65  		aksInitialAutoScalerProfile := aks.Properties.AutoScalerProfile
    66  
    67  		// Conditional is based off of the actual AKS settings not the AzureManagedControlPlane
    68  		if aksInitialAutoScalerProfile == nil {
    69  			expectedAksExpander = armcontainerservice.ExpanderLeastWaste
    70  			newExpanderValue = infrav1.ExpanderLeastWaste
    71  		} else if aksInitialAutoScalerProfile.Expander == ptr.To(armcontainerservice.ExpanderLeastWaste) {
    72  			expectedAksExpander = armcontainerservice.ExpanderMostPods
    73  			newExpanderValue = infrav1.ExpanderMostPods
    74  		}
    75  
    76  		amcp.Spec.AutoScalerProfile = nil
    77  		err = mgmtClient.Get(ctx, types.NamespacedName{
    78  			Namespace: input.Cluster.Spec.ControlPlaneRef.Namespace,
    79  			Name:      input.Cluster.Spec.ControlPlaneRef.Name,
    80  		}, amcp)
    81  		g.Expect(err).NotTo(HaveOccurred())
    82  		g.Expect(mgmtClient.Update(ctx, amcp)).To(Succeed())
    83  	}, input.WaitIntervals...).Should(Succeed())
    84  	Eventually(func(g Gomega) {
    85  		err = mgmtClient.Get(ctx, types.NamespacedName{
    86  			Namespace: input.Cluster.Spec.ControlPlaneRef.Namespace,
    87  			Name:      input.Cluster.Spec.ControlPlaneRef.Name,
    88  		}, amcp)
    89  		g.Expect(err).NotTo(HaveOccurred())
    90  		g.Expect(amcp.Spec.AutoScalerProfile).To(BeNil())
    91  	}, input.WaitIntervals...).Should(Succeed())
    92  
    93  	// Now set to the new value
    94  	Eventually(func(g Gomega) {
    95  		err = mgmtClient.Get(ctx, types.NamespacedName{
    96  			Namespace: input.Cluster.Spec.ControlPlaneRef.Namespace,
    97  			Name:      input.Cluster.Spec.ControlPlaneRef.Name,
    98  		}, amcp)
    99  		g.Expect(err).NotTo(HaveOccurred())
   100  		amcp.Spec.AutoScalerProfile = &infrav1.AutoScalerProfile{
   101  			Expander: (*infrav1.Expander)(ptr.To(string(newExpanderValue))),
   102  		}
   103  		g.Expect(mgmtClient.Update(ctx, amcp)).To(Succeed())
   104  	}, input.WaitIntervals...).Should(Succeed())
   105  	By("Verifying the cluster-autoscaler settings have changed")
   106  	Eventually(func(g Gomega) {
   107  		// Check that the autoscaler settings have been sync'd to AKS
   108  		aks, err := containerserviceClient.Get(ctx, amcp.Spec.ResourceGroupName, amcp.Name, nil)
   109  		g.Expect(err).NotTo(HaveOccurred())
   110  		g.Expect(aks.Properties.ProvisioningState).To(Equal(ptr.To("Succeeded")))
   111  		g.Expect(aks.Properties.AutoScalerProfile).NotTo(BeNil())
   112  		g.Expect(aks.Properties.AutoScalerProfile.Expander).To(Equal(&expectedAksExpander))
   113  	}, input.WaitIntervals...).Should(Succeed())
   114  
   115  	Eventually(func(g Gomega) {
   116  		err = mgmtClient.Get(ctx, types.NamespacedName{
   117  			Namespace: input.Cluster.Spec.ControlPlaneRef.Namespace,
   118  			Name:      input.Cluster.Spec.ControlPlaneRef.Name,
   119  		}, amcp)
   120  		g.Expect(err).NotTo(HaveOccurred())
   121  		amcp.Spec.AutoScalerProfile = amcpInitialAutoScalerProfile
   122  		g.Expect(mgmtClient.Update(ctx, amcp)).To(Succeed())
   123  	}, input.WaitIntervals...).Should(Succeed())
   124  	Eventually(func(g Gomega) {
   125  		err = mgmtClient.Get(ctx, types.NamespacedName{
   126  			Namespace: input.Cluster.Spec.ControlPlaneRef.Namespace,
   127  			Name:      input.Cluster.Spec.ControlPlaneRef.Name,
   128  		}, amcp)
   129  		g.Expect(err).NotTo(HaveOccurred())
   130  		g.Expect(amcp.Spec.AutoScalerProfile).To(Equal(amcpInitialAutoScalerProfile))
   131  	}, input.WaitIntervals...).Should(Succeed())
   132  }