sigs.k8s.io/cluster-api-provider-azure@v1.14.3/test/e2e/cni.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  	"path/filepath"
    27  
    28  	. "github.com/onsi/ginkgo/v2"
    29  	. "github.com/onsi/gomega"
    30  	k8snet "k8s.io/utils/net"
    31  	"sigs.k8s.io/cluster-api/test/framework/clusterctl"
    32  )
    33  
    34  const (
    35  	calicoHelmChartRepoURL   string = "https://docs.tigera.io/calico/charts"
    36  	calicoOperatorNamespace  string = "tigera-operator"
    37  	CalicoSystemNamespace    string = "calico-system"
    38  	CalicoAPIServerNamespace string = "calico-apiserver"
    39  	calicoHelmReleaseName    string = "projectcalico"
    40  	calicoHelmChartName      string = "tigera-operator"
    41  	kubeadmConfigMapName     string = "kubeadm-config"
    42  	AzureCNIv1               string = "azure-cni-v1"
    43  )
    44  
    45  // EnsureCNI installs the CNI plugin depending on the input.CNIManifestPath
    46  func EnsureCNI(ctx context.Context, input clusterctl.ApplyCustomClusterTemplateAndWaitInput, installHelmChart bool, cidrBlocks []string, hasWindows bool) {
    47  	if input.CNIManifestPath != "" {
    48  		InstallCNIManifest(ctx, input, cidrBlocks, hasWindows)
    49  	} else {
    50  		EnsureCalicoIsReady(ctx, input, installHelmChart, cidrBlocks, hasWindows)
    51  	}
    52  }
    53  
    54  // InstallCNIManifest installs the CNI manifest provided by the user
    55  func InstallCNIManifest(ctx context.Context, input clusterctl.ApplyCustomClusterTemplateAndWaitInput, cidrBlocks []string, hasWindows bool) {
    56  	By("Installing a CNI plugin to the workload cluster")
    57  	workloadCluster := input.ClusterProxy.GetWorkloadCluster(ctx, input.Namespace, input.ClusterName)
    58  
    59  	cniYaml, err := os.ReadFile(input.CNIManifestPath)
    60  	Expect(err).NotTo(HaveOccurred())
    61  
    62  	Expect(workloadCluster.Apply(ctx, cniYaml)).To(Succeed())
    63  }
    64  
    65  // EnsureCalicoIsReady copies the kubeadm configmap to the calico-system namespace and waits for the calico pods to be ready.
    66  func EnsureCalicoIsReady(ctx context.Context, input clusterctl.ApplyCustomClusterTemplateAndWaitInput, installHelmChart bool, cidrBlocks []string, hasWindows bool) {
    67  	specName := "ensure-calico"
    68  
    69  	clusterProxy := input.ClusterProxy.GetWorkloadCluster(ctx, input.Namespace, input.ClusterName)
    70  	if installHelmChart {
    71  		By("Installing Calico CNI via Helm Chart")
    72  		values := getCalicoValues(cidrBlocks)
    73  		InstallHelmChart(ctx, clusterProxy, calicoOperatorNamespace, calicoHelmChartRepoURL, calicoHelmChartName, calicoHelmReleaseName, values, os.Getenv(CalicoVersion))
    74  	} else {
    75  		By("Ensuring Calico CNI is installed via CAAPH")
    76  	}
    77  
    78  	By("Copying kubeadm config map to calico-system namespace")
    79  	workloadClusterClient := clusterProxy.GetClient()
    80  
    81  	if hasWindows {
    82  		// Copy the kubeadm configmap to the calico-system namespace. This is a workaround needed for the calico-node-windows daemonset to be able to run in the calico-system namespace.
    83  		CopyConfigMap(ctx, input, workloadClusterClient, kubeadmConfigMapName, kubesystem, CalicoSystemNamespace)
    84  	}
    85  
    86  	By("Waiting for Ready tigera-operator deployment pods")
    87  	for _, d := range []string{"tigera-operator"} {
    88  		waitInput := GetWaitForDeploymentsAvailableInput(ctx, clusterProxy, d, calicoOperatorNamespace, specName)
    89  		WaitForDeploymentsAvailable(ctx, waitInput, e2eConfig.GetIntervals(specName, "wait-deployment")...)
    90  	}
    91  
    92  	By("Waiting for Ready calico-system deployment pods")
    93  	for _, d := range []string{"calico-kube-controllers", "calico-typha"} {
    94  		waitInput := GetWaitForDeploymentsAvailableInput(ctx, clusterProxy, d, CalicoSystemNamespace, specName)
    95  		WaitForDeploymentsAvailable(ctx, waitInput, e2eConfig.GetIntervals(specName, "wait-deployment")...)
    96  	}
    97  	By("Waiting for Ready calico-apiserver deployment pods")
    98  	for _, d := range []string{"calico-apiserver"} {
    99  		waitInput := GetWaitForDeploymentsAvailableInput(ctx, clusterProxy, d, CalicoAPIServerNamespace, specName)
   100  		WaitForDeploymentsAvailable(ctx, waitInput, e2eConfig.GetIntervals(specName, "wait-deployment")...)
   101  	}
   102  }
   103  
   104  func getCalicoValues(cidrBlocks []string) *HelmOptions {
   105  	var ipv6CidrBlock, ipv4CidrBlock string
   106  	var values *HelmOptions
   107  	for _, cidr := range cidrBlocks {
   108  		if k8snet.IsIPv6CIDRString(cidr) {
   109  			ipv6CidrBlock = cidr
   110  		} else {
   111  			Expect(k8snet.IsIPv4CIDRString(cidr)).To(BeTrue(), "CIDR %s is not a valid IPv4 or IPv6 CIDR", cidr)
   112  			ipv4CidrBlock = cidr
   113  		}
   114  	}
   115  	addonsPath := e2eConfig.GetVariable(AddonsPath)
   116  	switch {
   117  	case ipv6CidrBlock != "" && ipv4CidrBlock != "":
   118  		By("Configuring calico CNI helm chart for dual-stack configuration")
   119  		values = &HelmOptions{
   120  			StringValues: []string{fmt.Sprintf("installation.calicoNetwork.ipPools[0].cidr=%s", ipv4CidrBlock), fmt.Sprintf("installation.calicoNetwork.ipPools[1].cidr=%s", ipv6CidrBlock)},
   121  			ValueFiles:   []string{filepath.Join(addonsPath, "calico-dual-stack", "values.yaml")},
   122  		}
   123  	case ipv6CidrBlock != "":
   124  		By("Configuring calico CNI helm chart for IPv6 configuration")
   125  		values = &HelmOptions{
   126  			StringValues: []string{fmt.Sprintf("installation.calicoNetwork.ipPools[0].cidr=%s", ipv6CidrBlock)},
   127  			ValueFiles:   []string{filepath.Join(addonsPath, "calico-ipv6", "values.yaml")},
   128  		}
   129  	default:
   130  		By("Configuring calico CNI helm chart for IPv4 configuration")
   131  		values = &HelmOptions{
   132  			StringValues: []string{fmt.Sprintf("installation.calicoNetwork.ipPools[0].cidr=%s", ipv4CidrBlock)},
   133  			ValueFiles:   []string{filepath.Join(addonsPath, "calico", "values.yaml")},
   134  		}
   135  	}
   136  	return values
   137  }