sigs.k8s.io/cluster-api-provider-azure@v1.14.3/test/e2e/azure_edgezone.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/compute/armcompute/v5"
    27  	. "github.com/onsi/ginkgo/v2"
    28  	. "github.com/onsi/gomega"
    29  	corev1 "k8s.io/api/core/v1"
    30  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    31  	azureutil "sigs.k8s.io/cluster-api-provider-azure/util/azure"
    32  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    33  	"sigs.k8s.io/cluster-api/test/framework"
    34  	"sigs.k8s.io/cluster-api/test/framework/clusterctl"
    35  	"sigs.k8s.io/controller-runtime/pkg/client"
    36  )
    37  
    38  // AzureEdgeZoneClusterSpecInput is the input for Azure
    39  type AzureEdgeZoneClusterSpecInput struct {
    40  	BootstrapClusterProxy framework.ClusterProxy
    41  	Namespace             *corev1.Namespace
    42  	ClusterName           string
    43  	E2EConfig             *clusterctl.E2EConfig
    44  }
    45  
    46  func AzureEdgeZoneClusterSpec(ctx context.Context, inputGetter func() AzureEdgeZoneClusterSpecInput) {
    47  	var (
    48  		specName = "azure-edgezone-cluster"
    49  		input    AzureEdgeZoneClusterSpecInput
    50  	)
    51  
    52  	input = inputGetter()
    53  	Expect(input).NotTo(BeNil())
    54  	Expect(input.BootstrapClusterProxy).NotTo(BeNil(), "Invalid argument. input.BootstrapClusterProxy can't be nil when calling %s spec", specName)
    55  	Expect(input.Namespace).NotTo(BeNil(), "Invalid argument. input.Namespace can't be nil when calling %s spec", specName)
    56  
    57  	By("creating a Kubernetes client to the workload cluster")
    58  	workloadClusterProxy := input.BootstrapClusterProxy.GetWorkloadCluster(ctx, input.Namespace.Name, input.ClusterName)
    59  	Expect(workloadClusterProxy).NotTo(BeNil())
    60  	mgmtClient := bootstrapClusterProxy.GetClient()
    61  	Expect(mgmtClient).NotTo(BeNil())
    62  
    63  	By("Retrieving all machines from the machine template spec")
    64  	machineList := &infrav1.AzureMachineList{}
    65  	// list all of the requested objects within the cluster namespace with the cluster name label
    66  	Logf("Listing machines in namespace %s with label %s=%s", input.Namespace.Name, clusterv1.ClusterNameLabel, workloadClusterProxy.GetName())
    67  	err := mgmtClient.List(ctx, machineList, client.InNamespace(input.Namespace.Name), client.MatchingLabels{clusterv1.ClusterNameLabel: workloadClusterProxy.GetName()})
    68  	Expect(err).NotTo(HaveOccurred())
    69  
    70  	By("Getting extendedLocation Name and Type from environment variables or e2e config file")
    71  	extendedLocationType := input.E2EConfig.GetVariable(AzureExtendedLocationType)
    72  	extendedLocationName := input.E2EConfig.GetVariable(AzureExtendedLocationName)
    73  
    74  	cred, err := azidentity.NewDefaultAzureCredential(nil)
    75  	Expect(err).NotTo(HaveOccurred())
    76  
    77  	if len(machineList.Items) > 0 {
    78  		By("Creating a VM client")
    79  		// create a VM client
    80  		vmClient, err := armcompute.NewVirtualMachinesClient(getSubscriptionID(Default), cred, nil)
    81  		Expect(err).NotTo(HaveOccurred())
    82  
    83  		// get the resource group name
    84  		resource, err := azureutil.ParseResourceID(*machineList.Items[0].Spec.ProviderID)
    85  		Expect(err).NotTo(HaveOccurred())
    86  
    87  		var vms []*armcompute.VirtualMachine
    88  		pager := vmClient.NewListPager(resource.ResourceGroupName, nil)
    89  		for pager.More() {
    90  			nextResult, err := pager.NextPage(ctx)
    91  			Expect(err).NotTo(HaveOccurred())
    92  			vms = append(vms, nextResult.Value...)
    93  		}
    94  
    95  		By("Verifying VMs' extendedLocation property is correct")
    96  		for _, machine := range vms {
    97  			Expect(*machine.ExtendedLocation.Name).To(Equal(extendedLocationName))
    98  			Expect(string(*machine.ExtendedLocation.Type)).To(Equal(extendedLocationType))
    99  		}
   100  	}
   101  }