istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/helm/helm.go (about)

     1  //  Copyright Istio Authors
     2  //
     3  //  Licensed under the Apache License, Version 2.0 (the "License");
     4  //  you may not use this file except in compliance with the License.
     5  //  You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //  Unless required by applicable law or agreed to in writing, software
    10  //  distributed under the License is distributed on an "AS IS" BASIS,
    11  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //  See the License for the specific language governing permissions and
    13  //  limitations under the License.
    14  
    15  package helm
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"time"
    21  
    22  	"istio.io/istio/pkg/test/scopes"
    23  	"istio.io/istio/pkg/test/shell"
    24  )
    25  
    26  // Helm allows clients to interact with helm commands in their cluster
    27  type Helm struct {
    28  	kubeConfig string
    29  }
    30  
    31  // New returns a new instance of a helm object.
    32  func New(kubeConfig string) *Helm {
    33  	return &Helm{
    34  		kubeConfig: kubeConfig,
    35  	}
    36  }
    37  
    38  // InstallChartWithValues installs the specified chart with its given name to the given namespace
    39  func (h *Helm) InstallChartWithValues(name, chartPath, namespace string, values []string, timeout time.Duration) error {
    40  	command := fmt.Sprintf("helm install %s %s --namespace %s --kubeconfig %s --timeout %s %s",
    41  		name, chartPath, namespace, h.kubeConfig, timeout, strings.Join(values, " "))
    42  	_, err := execCommand(command)
    43  	return err
    44  }
    45  
    46  // InstallChart installs the specified chart with its given name to the given namespace
    47  func (h *Helm) InstallChart(name, chartPath, namespace, overridesFile string, timeout time.Duration, args ...string) error {
    48  	command := fmt.Sprintf("helm install %s %s --namespace %s -f %s --kubeconfig %s --timeout %s %s ",
    49  		name, chartPath, namespace, overridesFile, h.kubeConfig, timeout, strings.Join(args, " "))
    50  	_, err := execCommand(command)
    51  	return err
    52  }
    53  
    54  // UpgradeChart upgrades the specified chart with its given name to the given namespace; does not use baseWorkDir
    55  // but the full path passed
    56  func (h *Helm) UpgradeChart(name, chartPath, namespace, overridesFile string, timeout time.Duration, args ...string) error {
    57  	command := fmt.Sprintf("helm upgrade %s %s --namespace %s -f %s --kubeconfig %s --timeout %s %v",
    58  		name, chartPath, namespace, overridesFile, h.kubeConfig, timeout, strings.Join(args, " "))
    59  	_, err := execCommand(command)
    60  	return err
    61  }
    62  
    63  // DeleteChart deletes the specified chart with its given name in the given namespace
    64  func (h *Helm) DeleteChart(name, namespace string) error {
    65  	command := fmt.Sprintf("helm delete %s --namespace %s --kubeconfig %s", name, namespace, h.kubeConfig)
    66  	_, err := execCommand(command)
    67  	return err
    68  }
    69  
    70  // Template runs the template command and applies the generated file with kubectl
    71  func (h *Helm) Template(name, chartPath, namespace, templateFile string, timeout time.Duration, args ...string) (string, error) {
    72  	command := fmt.Sprintf("helm template %s %s --namespace %s -s %s --kubeconfig %s --timeout %s %s ",
    73  		name, chartPath, namespace, templateFile, h.kubeConfig, timeout, strings.Join(args, " "))
    74  
    75  	return execCommand(command)
    76  }
    77  
    78  func execCommand(cmd string) (string, error) {
    79  	scopes.Framework.Infof("Applying helm command: %s", cmd)
    80  
    81  	s, err := shell.Execute(true, cmd)
    82  	if err != nil {
    83  		scopes.Framework.Infof("(FAILED) Executing helm: %s (err: %v): %s", cmd, err, s)
    84  		return "", fmt.Errorf("%v: %s", err, s)
    85  	}
    86  
    87  	return s, nil
    88  }