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

     1  //go:build e2e
     2  // +build e2e
     3  
     4  /*
     5  Copyright 2020 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  	"os"
    25  
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  	corev1 "k8s.io/api/core/v1"
    29  	apimachinerytypes "k8s.io/apimachinery/pkg/types"
    30  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    31  	"sigs.k8s.io/cluster-api/test/framework"
    32  )
    33  
    34  // AzureFailureDomainsSpecInput is the input for AzureFailureDomainSpec.
    35  type AzureFailureDomainsSpecInput struct {
    36  	BootstrapClusterProxy framework.ClusterProxy
    37  	Cluster               *clusterv1.Cluster
    38  	Namespace             *corev1.Namespace
    39  	ClusterName           string
    40  }
    41  
    42  // AzureFailureDomainsSpec implements a test that checks that control plane machines are spread
    43  // across Azure failure domains.
    44  func AzureFailureDomainsSpec(ctx context.Context, inputGetter func() AzureFailureDomainsSpecInput) {
    45  	var (
    46  		specName    = "azure-failuredomains"
    47  		input       AzureFailureDomainsSpecInput
    48  		machineType = os.Getenv("AZURE_CONTROL_PLANE_MACHINE_TYPE")
    49  		location    = os.Getenv("AZURE_LOCATION")
    50  		zones       []string
    51  	)
    52  
    53  	input = inputGetter()
    54  	Expect(input.Namespace).NotTo(BeNil(), "Invalid argument. input.Namespace can't be nil when calling %s spec", specName)
    55  	Expect(input.ClusterName).NotTo(BeEmpty(), "Invalid argument. input.ClusterName can't be empty when calling %s spec", specName)
    56  
    57  	zones, err := getAvailabilityZonesForRegion(location, machineType)
    58  	Expect(err).NotTo(HaveOccurred())
    59  
    60  	if zones != nil {
    61  		// location supports zones for selected machine type
    62  		By("Ensuring zones match CAPI failure domains")
    63  
    64  		// fetch updated cluster object to ensure Status.FailureDomains is up-to-date
    65  		Eventually(func(g Gomega) {
    66  			err := input.BootstrapClusterProxy.GetClient().Get(ctx,
    67  				apimachinerytypes.NamespacedName{
    68  					Namespace: input.Namespace.Name,
    69  					Name:      input.ClusterName,
    70  				}, input.Cluster)
    71  			if err != nil {
    72  				LogWarning(err.Error())
    73  			}
    74  			g.Expect(err).NotTo(HaveOccurred())
    75  			g.Expect(input.Cluster.Status.FailureDomains).To(HaveLen(len(zones)))
    76  			for _, z := range zones {
    77  				g.Expect(input.Cluster.Status.FailureDomains[z]).NotTo(BeNil())
    78  			}
    79  		}, retryableOperationTimeout, retryableOperationSleepBetweenRetries).Should(Succeed())
    80  
    81  		// TODO: Find alternative when the number of zones is > 1 but doesn't equal to number of control plane machines.
    82  		if len(input.Cluster.Status.FailureDomains) == 3 {
    83  			By("Ensuring control planes are spread across availability zones.")
    84  			failureDomainsInput := framework.AssertControlPlaneFailureDomainsInput{
    85  				Lister:  input.BootstrapClusterProxy.GetClient(),
    86  				Cluster: input.Cluster,
    87  			}
    88  			framework.AssertControlPlaneFailureDomains(ctx, failureDomainsInput)
    89  		}
    90  	}
    91  }