sigs.k8s.io/cluster-api-provider-azure@v1.14.3/test/e2e/aks_machinepools.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  	"sync"
    25  
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  	"k8s.io/apimachinery/pkg/types"
    29  	"k8s.io/utils/ptr"
    30  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    31  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    32  	expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
    33  	"sigs.k8s.io/cluster-api/test/framework"
    34  	"sigs.k8s.io/controller-runtime/pkg/client"
    35  )
    36  
    37  type AKSMachinePoolSpecInput struct {
    38  	Cluster       *clusterv1.Cluster
    39  	MachinePools  []*expv1.MachinePool
    40  	WaitIntervals []interface{}
    41  }
    42  
    43  func AKSMachinePoolSpec(ctx context.Context, inputGetter func() AKSMachinePoolSpecInput) {
    44  	input := inputGetter()
    45  	var wg sync.WaitGroup
    46  
    47  	originalReplicas := map[types.NamespacedName]int32{}
    48  	for _, mp := range input.MachinePools {
    49  		originalReplicas[client.ObjectKeyFromObject(mp)] = ptr.Deref[int32](mp.Spec.Replicas, 0)
    50  	}
    51  
    52  	By("Scaling the machine pools out")
    53  	for _, mp := range input.MachinePools {
    54  		wg.Add(1)
    55  		go func(mp *expv1.MachinePool) {
    56  			defer GinkgoRecover()
    57  			defer wg.Done()
    58  			framework.ScaleMachinePoolAndWait(ctx, framework.ScaleMachinePoolAndWaitInput{
    59  				ClusterProxy:              bootstrapClusterProxy,
    60  				Cluster:                   input.Cluster,
    61  				Replicas:                  ptr.Deref[int32](mp.Spec.Replicas, 0) + 1,
    62  				MachinePools:              []*expv1.MachinePool{mp},
    63  				WaitForMachinePoolToScale: input.WaitIntervals,
    64  			})
    65  		}(mp)
    66  	}
    67  	wg.Wait()
    68  
    69  	By("Scaling the machine pools in")
    70  	for _, mp := range input.MachinePools {
    71  		wg.Add(1)
    72  		go func(mp *expv1.MachinePool) {
    73  			defer GinkgoRecover()
    74  			defer wg.Done()
    75  			framework.ScaleMachinePoolAndWait(ctx, framework.ScaleMachinePoolAndWaitInput{
    76  				ClusterProxy:              bootstrapClusterProxy,
    77  				Cluster:                   input.Cluster,
    78  				Replicas:                  ptr.Deref[int32](mp.Spec.Replicas, 0) - 1,
    79  				MachinePools:              []*expv1.MachinePool{mp},
    80  				WaitForMachinePoolToScale: input.WaitIntervals,
    81  			})
    82  		}(mp)
    83  	}
    84  	wg.Wait()
    85  
    86  	By("Scaling the machine pools to zero")
    87  	// System node pools cannot be scaled to 0, so only include user node pools.
    88  	var machinePoolsToScale []*expv1.MachinePool
    89  	for _, mp := range input.MachinePools {
    90  		ammp := &infrav1.AzureManagedMachinePool{}
    91  		err := bootstrapClusterProxy.GetClient().Get(ctx, types.NamespacedName{
    92  			Namespace: mp.Spec.Template.Spec.InfrastructureRef.Namespace,
    93  			Name:      mp.Spec.Template.Spec.InfrastructureRef.Name,
    94  		}, ammp)
    95  		Expect(err).NotTo(HaveOccurred())
    96  
    97  		if ammp.Spec.Mode != string(infrav1.NodePoolModeSystem) {
    98  			machinePoolsToScale = append(machinePoolsToScale, mp)
    99  		}
   100  	}
   101  
   102  	framework.ScaleMachinePoolAndWait(ctx, framework.ScaleMachinePoolAndWaitInput{
   103  		ClusterProxy:              bootstrapClusterProxy,
   104  		Cluster:                   input.Cluster,
   105  		Replicas:                  0,
   106  		MachinePools:              machinePoolsToScale,
   107  		WaitForMachinePoolToScale: input.WaitIntervals,
   108  	})
   109  
   110  	By("Restoring initial replica count")
   111  	for _, mp := range input.MachinePools {
   112  		wg.Add(1)
   113  		go func(mp *expv1.MachinePool) {
   114  			defer GinkgoRecover()
   115  			defer wg.Done()
   116  			framework.ScaleMachinePoolAndWait(ctx, framework.ScaleMachinePoolAndWaitInput{
   117  				ClusterProxy:              bootstrapClusterProxy,
   118  				Cluster:                   input.Cluster,
   119  				Replicas:                  originalReplicas[client.ObjectKeyFromObject(mp)],
   120  				MachinePools:              []*expv1.MachinePool{mp},
   121  				WaitForMachinePoolToScale: input.WaitIntervals,
   122  			})
   123  		}(mp)
   124  	}
   125  	wg.Wait()
   126  }