sigs.k8s.io/cluster-api-provider-azure@v1.14.3/test/e2e/helm.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  	"os/exec"
    25  
    26  	. "github.com/onsi/gomega"
    27  	"sigs.k8s.io/cluster-api/test/framework"
    28  )
    29  
    30  // HelmOptions handles arguments to a `helm install` command.
    31  type HelmOptions struct {
    32  	StringValues []string // --set-string
    33  	ValueFiles   []string // --values
    34  	Values       []string // --set
    35  }
    36  
    37  // InstallHelmChart takes a Helm repo URL, a chart name, and release name, and creates a Helm release on the E2E workload cluster.
    38  func InstallHelmChart(ctx context.Context, clusterProxy framework.ClusterProxy, namespace, repoURL, chartName, releaseName string, options *HelmOptions, version string) {
    39  	// Check that Helm v3 is installed
    40  	helm, err := exec.LookPath("helm")
    41  	Expect(err).NotTo(HaveOccurred(), "No helm binary found in PATH")
    42  	cmd := exec.Command(helm, "version", "--short")
    43  	stdout, err := cmd.Output()
    44  	Expect(err).NotTo(HaveOccurred())
    45  	Logf("Helm version: %s", stdout)
    46  	Expect(stdout).To(HavePrefix("v3."), "Helm v3 is required")
    47  
    48  	// Set up the Helm command arguments.
    49  	args := []string{
    50  		"upgrade", releaseName, chartName, "--install",
    51  		"--kubeconfig", clusterProxy.GetKubeconfigPath(),
    52  		"--create-namespace", "--namespace", namespace,
    53  	}
    54  	if repoURL != "" {
    55  		args = append(args, "--repo", repoURL)
    56  	}
    57  	for _, stringValue := range options.StringValues {
    58  		args = append(args, "--set-string", stringValue)
    59  	}
    60  	for _, valueFile := range options.ValueFiles {
    61  		args = append(args, "--values", valueFile)
    62  	}
    63  	for _, value := range options.Values {
    64  		args = append(args, "--set", value)
    65  	}
    66  	if version != "" {
    67  		args = append(args, "--version", version)
    68  	}
    69  
    70  	// Install the chart and retry if needed
    71  	Eventually(func() error {
    72  		cmd := exec.Command(helm, args...)
    73  		Logf("Helm command: %s", cmd.String())
    74  		output, err := cmd.CombinedOutput()
    75  		Logf("Helm install output: %s", string(output))
    76  		return err
    77  	}, helmInstallTimeout, retryableOperationSleepBetweenRetries).Should(Succeed())
    78  }