github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/infrastructure/utils/helm_wrapper.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package utils
    21  
    22  import (
    23  	"fmt"
    24  	"path/filepath"
    25  
    26  	"helm.sh/helm/v3/pkg/repo"
    27  	"k8s.io/klog/v2"
    28  
    29  	"github.com/1aal/kubeblocks/pkg/cli/cmd/infrastructure/types"
    30  	"github.com/1aal/kubeblocks/pkg/cli/util/helm"
    31  )
    32  
    33  type HelmInstallHelper struct {
    34  	types.HelmChart
    35  	kubeconfig string
    36  }
    37  
    38  func (h *HelmInstallHelper) Install(name, ns string) error {
    39  	if err := h.addRepo(); err != nil {
    40  		return err
    41  	}
    42  	helmConfig := helm.NewConfig(ns, h.kubeconfig, "", klog.V(1).Enabled())
    43  	installOpts := h.buildChart(name, ns)
    44  	output, err := installOpts.Install(helmConfig)
    45  	if err != nil && helm.ReleaseNotFound(err) {
    46  		fmt.Println(output)
    47  		return nil
    48  	}
    49  	return err
    50  }
    51  
    52  func NewHelmInstaller(chart types.HelmChart, kubeconfig string) Installer {
    53  	installer := HelmInstallHelper{
    54  		HelmChart:  chart,
    55  		kubeconfig: kubeconfig}
    56  	return &installer
    57  }
    58  
    59  func (h *HelmInstallHelper) buildChart(name, ns string) *helm.InstallOpts {
    60  	return &helm.InstallOpts{
    61  		Name:            name,
    62  		Chart:           h.getChart(name),
    63  		Wait:            true,
    64  		Version:         h.Version,
    65  		Namespace:       ns,
    66  		ValueOpts:       &h.ValueOptions,
    67  		TryTimes:        3,
    68  		CreateNamespace: true,
    69  		Atomic:          true,
    70  	}
    71  }
    72  
    73  func (h *HelmInstallHelper) getChart(name string) string {
    74  	if h.Name == "" {
    75  		return ""
    76  	}
    77  	// install helm package form local path
    78  	if h.Repo != "" {
    79  		return filepath.Join(h.Name, name)
    80  	}
    81  	if h.Path != "" {
    82  		return filepath.Join(h.Path, name)
    83  	}
    84  	return ""
    85  }
    86  
    87  func (h *HelmInstallHelper) addRepo() error {
    88  	if h.Repo == "" {
    89  		return nil
    90  	}
    91  	return helm.AddRepo(&repo.Entry{Name: h.Name, URL: h.Repo})
    92  }