github.com/pachyderm/pachyderm@v1.13.4/src/client/pkg/helm/deploy.go (about)

     1  package helm
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	"github.com/pachyderm/pachyderm/src/client/pkg/config"
     8  	"github.com/pachyderm/pachyderm/src/client/pkg/errors"
     9  
    10  	"helm.sh/helm/v3/pkg/action"
    11  	"helm.sh/helm/v3/pkg/chart/loader"
    12  	"helm.sh/helm/v3/pkg/cli"
    13  	"helm.sh/helm/v3/pkg/getter"
    14  	"helm.sh/helm/v3/pkg/release"
    15  	"helm.sh/helm/v3/pkg/repo"
    16  	"helm.sh/helm/v3/pkg/storage/driver"
    17  	"sigs.k8s.io/yaml"
    18  )
    19  
    20  func updateConfig(envSettings *cli.EnvSettings, repoName, repoURL string) (*repo.ChartRepository, error) {
    21  	configBytes, err := ioutil.ReadFile(envSettings.RepositoryConfig)
    22  	if err != nil && !errors.Is(err, os.ErrNotExist) {
    23  		return nil, errors.Wrapf(err, "failed to read helm config")
    24  	}
    25  
    26  	var config repo.File
    27  	if err := yaml.Unmarshal(configBytes, &config); err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	repoEntry := repo.Entry{
    32  		Name: repoName,
    33  		URL:  repoURL,
    34  	}
    35  
    36  	chartRepository, err := repo.NewChartRepository(&repoEntry, getter.All(envSettings))
    37  	if err != nil {
    38  		return nil, errors.Wrapf(err, "failed to construct helm chart repository")
    39  	}
    40  	if _, err := chartRepository.DownloadIndexFile(); err != nil {
    41  		return nil, errors.Wrapf(err, "failed to download helm index file at %q", chartRepository.Config.URL)
    42  	}
    43  
    44  	config.Update(&repoEntry)
    45  	if err := config.WriteFile(envSettings.RepositoryConfig, 0644); err != nil {
    46  		return nil, errors.Wrapf(err, "failed to write helm config")
    47  	}
    48  
    49  	return chartRepository, nil
    50  }
    51  
    52  // Deploy installs a helm chart.
    53  func Deploy(context *config.Context, repoName, repoURL, installName, chartName, chartVersion string, values map[string]interface{}) (*release.Release, error) {
    54  	envSettings, actionConfig, err := configureHelm(context, "")
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	upgrade := action.NewUpgrade(actionConfig)
    60  	upgrade.Version = chartVersion
    61  	upgrade.Namespace = context.Namespace
    62  
    63  	_, err = updateConfig(envSettings, repoName, repoURL)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	chartPath, err := upgrade.ChartPathOptions.LocateChart(chartName, envSettings)
    69  	if err != nil {
    70  		return nil, errors.Wrapf(err, "could not locate helm chart")
    71  	}
    72  
    73  	chart, err := loader.Load(chartPath)
    74  	if err != nil {
    75  		return nil, errors.Wrapf(err, "could not load helm chart")
    76  	}
    77  
    78  	history := action.NewHistory(actionConfig)
    79  	history.Max = 1
    80  	if _, err := history.Run(installName); errors.Is(err, driver.ErrReleaseNotFound) {
    81  		// install
    82  		install := action.NewInstall(actionConfig)
    83  		install.ChartPathOptions = upgrade.ChartPathOptions
    84  		install.Version = chartVersion
    85  		install.Namespace = context.Namespace
    86  		install.ReleaseName = installName
    87  		rel, err := install.Run(chart, values)
    88  		if err != nil {
    89  			return nil, errors.Wrapf(err, "install failed")
    90  		}
    91  		return rel, nil
    92  	} else if err != nil {
    93  		return nil, errors.Wrapf(err, "could not fetch helm history")
    94  	}
    95  
    96  	// upgrade
    97  	rel, err := upgrade.Run(installName, chart, values)
    98  	if err != nil {
    99  		return nil, errors.Wrapf(err, "upgrade failed")
   100  	}
   101  	return rel, nil
   102  }