sigs.k8s.io/cluster-api-provider-azure@v1.14.3/test/e2e/cloud-provider-azure.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  	"fmt"
    25  	"os"
    26  	"strings"
    27  
    28  	. "github.com/onsi/ginkgo/v2"
    29  	. "github.com/onsi/gomega"
    30  	"k8s.io/apimachinery/pkg/types"
    31  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    32  	"sigs.k8s.io/cluster-api/test/framework/clusterctl"
    33  )
    34  
    35  const (
    36  	cloudProviderAzureHelmRepoURL     = "https://raw.githubusercontent.com/kubernetes-sigs/cloud-provider-azure/master/helm/repo"
    37  	cloudProviderAzureChartName       = "cloud-provider-azure"
    38  	cloudProviderAzureHelmReleaseName = "cloud-provider-azure-oot"
    39  	azureDiskCSIDriverHelmRepoURL     = "https://raw.githubusercontent.com/kubernetes-sigs/azuredisk-csi-driver/master/charts"
    40  	azureDiskCSIDriverChartName       = "azuredisk-csi-driver"
    41  	azureDiskCSIDriverHelmReleaseName = "azuredisk-csi-driver-oot"
    42  	azureDiskCSIDriverCAAPHLabelName  = "azuredisk-csi"
    43  )
    44  
    45  // EnsureCNIAndCloudProviderAzureHelmChart installs the official cloud-provider-azure helm chart
    46  // and a CNI and validates that expected pods exist and are Ready.
    47  func EnsureCNIAndCloudProviderAzureHelmChart(ctx context.Context, input clusterctl.ApplyCustomClusterTemplateAndWaitInput, installHelmChart bool, cidrBlocks []string, hasWindows bool) {
    48  	specName := "ensure-cloud-provider-azure"
    49  	clusterProxy := input.ClusterProxy.GetWorkloadCluster(ctx, input.Namespace, input.ClusterName)
    50  
    51  	if installHelmChart {
    52  		By("Installing cloud-provider-azure components via helm")
    53  		options := &HelmOptions{
    54  			Values: []string{
    55  				fmt.Sprintf("infra.clusterName=%s", input.ClusterName),
    56  				"cloudControllerManager.logVerbosity=4",
    57  			},
    58  			StringValues: []string{fmt.Sprintf("cloudControllerManager.clusterCIDR=%s", strings.Join(cidrBlocks, `\,`))},
    59  		}
    60  		// If testing a CI version of Kubernetes, use CCM and CNM images built from source.
    61  		if useCIArtifacts || usePRArtifacts {
    62  			options.Values = append(options.Values, fmt.Sprintf("cloudControllerManager.imageName=%s", os.Getenv("CCM_IMAGE_NAME")))
    63  			options.Values = append(options.Values, fmt.Sprintf("cloudNodeManager.imageName=%s", os.Getenv("CNM_IMAGE_NAME")))
    64  			options.Values = append(options.Values, fmt.Sprintf("cloudControllerManager.imageRepository=%s", os.Getenv("IMAGE_REGISTRY")))
    65  			options.Values = append(options.Values, fmt.Sprintf("cloudNodeManager.imageRepository=%s", os.Getenv("IMAGE_REGISTRY")))
    66  			options.StringValues = append(options.StringValues, fmt.Sprintf("cloudControllerManager.imageTag=%s", os.Getenv("IMAGE_TAG_CCM")))
    67  			options.StringValues = append(options.StringValues, fmt.Sprintf("cloudNodeManager.imageTag=%s", os.Getenv("IMAGE_TAG_CNM")))
    68  		}
    69  
    70  		if strings.Contains(input.ClusterName, "flatcar") {
    71  			options.StringValues = append(options.StringValues, "cloudControllerManager.caCertDir=/usr/share/ca-certificates")
    72  		}
    73  
    74  		InstallHelmChart(ctx, clusterProxy, defaultNamespace, cloudProviderAzureHelmRepoURL, cloudProviderAzureChartName, cloudProviderAzureHelmReleaseName, options, "")
    75  	} else {
    76  		By("Ensuring cloud-provider-azure is installed via CAAPH")
    77  	}
    78  
    79  	// We do this before waiting for the pods to be ready because there is a co-dependency between CNI (nodes ready) and cloud-provider being initialized.
    80  	EnsureCNI(ctx, input, installHelmChart, cidrBlocks, hasWindows)
    81  
    82  	By("Waiting for Ready cloud-controller-manager deployment pods")
    83  	for _, d := range []string{"cloud-controller-manager"} {
    84  		waitInput := GetWaitForDeploymentsAvailableInput(ctx, clusterProxy, d, kubesystem, specName)
    85  		WaitForDeploymentsAvailable(ctx, waitInput, e2eConfig.GetIntervals(specName, "wait-deployment")...)
    86  	}
    87  }
    88  
    89  // EnsureAzureDiskCSIDriverHelmChart installs the official azure-disk CSI driver helm chart
    90  func EnsureAzureDiskCSIDriverHelmChart(ctx context.Context, input clusterctl.ApplyCustomClusterTemplateAndWaitInput, installHelmChart bool, hasWindows bool) {
    91  	specName := "ensure-azuredisk-csi-drivers"
    92  	clusterProxy := input.ClusterProxy.GetWorkloadCluster(ctx, input.Namespace, input.ClusterName)
    93  	mgmtClient := input.ClusterProxy.GetClient()
    94  
    95  	if installHelmChart {
    96  		By("Installing azure-disk CSI driver components via helm")
    97  		options := &HelmOptions{
    98  			Values: []string{"controller.replicas=1", "controller.runOnControlPlane=true"},
    99  		}
   100  		// TODO: make this always true once HostProcessContainers are on for all supported k8s versions.
   101  		if hasWindows {
   102  			options.Values = append(options.Values, "windows.useHostProcessContainers=true")
   103  		}
   104  		InstallHelmChart(ctx, clusterProxy, kubesystem, azureDiskCSIDriverHelmRepoURL, azureDiskCSIDriverChartName, azureDiskCSIDriverHelmReleaseName, options, "")
   105  	} else {
   106  		By("Ensuring azure-disk CSI driver is installed via CAAPH")
   107  		cluster := &clusterv1.Cluster{}
   108  		Eventually(func(g Gomega) {
   109  			g.Expect(mgmtClient.Get(ctx, types.NamespacedName{
   110  				Namespace: input.Namespace,
   111  				Name:      input.ClusterName,
   112  			}, cluster)).To(Succeed())
   113  			// Label the cluster so that CAAPH installs the azuredisk-csi helm chart via existing HelmChartProxy resource
   114  			if cluster.Labels != nil {
   115  				cluster.Labels[azureDiskCSIDriverCAAPHLabelName] = "true"
   116  			} else {
   117  				cluster.Labels = map[string]string{
   118  					azureDiskCSIDriverCAAPHLabelName: "true",
   119  				}
   120  			}
   121  			g.Expect(mgmtClient.Update(ctx, cluster)).To(Succeed())
   122  		}, e2eConfig.GetIntervals(specName, "wait-deployment")...).Should(Succeed())
   123  	}
   124  
   125  	By("Waiting for Ready csi-azuredisk-controller deployment pods")
   126  	for _, d := range []string{"csi-azuredisk-controller"} {
   127  		waitInput := GetWaitForDeploymentsAvailableInput(ctx, clusterProxy, d, kubesystem, specName)
   128  		WaitForDeploymentsAvailable(ctx, waitInput, e2eConfig.GetIntervals(specName, "wait-deployment")...)
   129  	}
   130  }