sigs.k8s.io/cluster-api-provider-azure@v1.14.3/test/e2e/aks_spot.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/resourcemanager/containerservice/armcontainerservice/v4"
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  	corev1 "k8s.io/api/core/v1"
    29  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    30  	"k8s.io/apimachinery/pkg/api/resource"
    31  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    32  	"k8s.io/utils/ptr"
    33  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    34  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    35  	expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
    36  	"sigs.k8s.io/cluster-api/util/conditions"
    37  	"sigs.k8s.io/controller-runtime/pkg/client"
    38  )
    39  
    40  type AKSSpotSpecInput struct {
    41  	Cluster           *clusterv1.Cluster
    42  	KubernetesVersion string
    43  	WaitIntervals     []interface{}
    44  }
    45  
    46  func AKSSpotSpec(ctx context.Context, inputGetter func() AKSSpotSpecInput) {
    47  	input := inputGetter()
    48  
    49  	mgmtClient := bootstrapClusterProxy.GetClient()
    50  	Expect(mgmtClient).NotTo(BeNil())
    51  
    52  	infraControlPlane := &infrav1.AzureManagedControlPlane{}
    53  	err := mgmtClient.Get(ctx, client.ObjectKey{Namespace: input.Cluster.Spec.ControlPlaneRef.Namespace, Name: input.Cluster.Spec.ControlPlaneRef.Name}, infraControlPlane)
    54  	Expect(err).NotTo(HaveOccurred())
    55  
    56  	scaling := infrav1.ManagedMachinePoolScaling{
    57  		MaxSize: ptr.To(9),
    58  		MinSize: ptr.To(0),
    59  	}
    60  	spotMaxPrice := resource.MustParse("123.456789")
    61  
    62  	By("Creating node pool")
    63  	infraMachinePool := &infrav1.AzureManagedMachinePool{
    64  		ObjectMeta: metav1.ObjectMeta{
    65  			Name:      "poolspot",
    66  			Namespace: input.Cluster.Namespace,
    67  		},
    68  		Spec: infrav1.AzureManagedMachinePoolSpec{
    69  			AzureManagedMachinePoolClassSpec: infrav1.AzureManagedMachinePoolClassSpec{
    70  				Mode:             "User",
    71  				SKU:              "Standard_D2s_v3",
    72  				ScaleSetPriority: ptr.To(string(armcontainerservice.ScaleSetPrioritySpot)),
    73  				Scaling:          &scaling,
    74  				SpotMaxPrice:     &spotMaxPrice,
    75  				ScaleDownMode:    ptr.To("Deallocate"),
    76  			},
    77  		},
    78  	}
    79  	err = mgmtClient.Create(ctx, infraMachinePool)
    80  	Expect(err).NotTo(HaveOccurred())
    81  
    82  	machinePool := &expv1.MachinePool{
    83  		ObjectMeta: metav1.ObjectMeta{
    84  			Namespace: infraMachinePool.Namespace,
    85  			Name:      infraMachinePool.Name,
    86  		},
    87  		Spec: expv1.MachinePoolSpec{
    88  			ClusterName: input.Cluster.Name,
    89  			Replicas:    ptr.To[int32](0),
    90  			Template: clusterv1.MachineTemplateSpec{
    91  				Spec: clusterv1.MachineSpec{
    92  					Bootstrap: clusterv1.Bootstrap{
    93  						DataSecretName: ptr.To(""),
    94  					},
    95  					ClusterName: input.Cluster.Name,
    96  					InfrastructureRef: corev1.ObjectReference{
    97  						APIVersion: infrav1.GroupVersion.String(),
    98  						Kind:       "AzureManagedMachinePool",
    99  						Name:       infraMachinePool.Name,
   100  					},
   101  					Version: ptr.To(input.KubernetesVersion),
   102  				},
   103  			},
   104  		},
   105  	}
   106  	err = mgmtClient.Create(ctx, machinePool)
   107  	Expect(err).NotTo(HaveOccurred())
   108  
   109  	defer func() {
   110  		By("Deleting the node pool")
   111  		err := mgmtClient.Delete(ctx, machinePool)
   112  		Expect(err).NotTo(HaveOccurred())
   113  
   114  		Eventually(func(g Gomega) {
   115  			err := mgmtClient.Get(ctx, client.ObjectKeyFromObject(machinePool), &expv1.MachinePool{})
   116  			g.Expect(apierrors.IsNotFound(err)).To(BeTrue())
   117  		}, input.WaitIntervals...).Should(Succeed(), "Deleted MachinePool %s/%s still exists", machinePool.Namespace, machinePool.Name)
   118  
   119  		Eventually(func(g Gomega) {
   120  			err := mgmtClient.Get(ctx, client.ObjectKeyFromObject(infraMachinePool), &infrav1.AzureManagedMachinePool{})
   121  			g.Expect(apierrors.IsNotFound(err)).To(BeTrue())
   122  		}, input.WaitIntervals...).Should(Succeed(), "Deleted AzureManagedMachinePool %s/%s still exists", infraMachinePool.Namespace, infraMachinePool.Name)
   123  	}()
   124  
   125  	By("Verifying the AzureManagedMachinePool becomes ready")
   126  	Eventually(func(g Gomega) {
   127  		infraMachinePool := &infrav1.AzureManagedMachinePool{}
   128  		err := mgmtClient.Get(ctx, client.ObjectKeyFromObject(machinePool), infraMachinePool)
   129  		g.Expect(err).NotTo(HaveOccurred())
   130  		g.Expect(conditions.IsTrue(infraMachinePool, infrav1.AgentPoolsReadyCondition)).To(BeTrue())
   131  	}, input.WaitIntervals...).Should(Succeed())
   132  }