Skip to content

Instantly share code, notes, and snippets.

@manzil-infinity180
Last active March 17, 2025 19:45
Show Gist options
  • Save manzil-infinity180/0411ae731ba17c3b1ee2a23d533f50f1 to your computer and use it in GitHub Desktop.
Save manzil-infinity180/0411ae731ba17c3b1ee2a23d533f50f1 to your computer and use it in GitHub Desktop.
Install chart using go-sdk
// https://github.com/helm/helm/issues/7310
// https://github.com/helm/helm/issues/12931
// helm 3.0.2 SDK - example of install from remote repository #7310
// https://github.com/helm/helm/issues/12907
// https://github.com/helm/helm-www/pull/1543
import (
"log"
"os"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/storage/driver"
)
func installOrUpgradeRelease(releaseName string, repoUrl string, chartName string, values map[string]interface{}, targetNamespace string) (*release.Release, error) {
actionConfig, settings, err := createActionConfig(targetNamespace)
if err != nil {
return nil, err
}
var chartPathOptions action.ChartPathOptions = action.ChartPathOptions{
RepoURL: repoUrl,
}
chart, err := getChart(chartPathOptions, chartName, settings)
if err != nil {
return nil, err
}
var rel *release.Release
var requestError error
histClient := action.NewHistory(actionConfig)
histClient.Max = 1
if _, err := histClient.Run(releaseName); err == driver.ErrReleaseNotFound {
clientInstall := action.NewInstall(actionConfig)
clientInstall.ReleaseName = releaseName
clientInstall.Namespace = targetNamespace
clientInstall.ChartPathOptions = chartPathOptions
rel, requestError = clientInstall.Run(chart, values)
} else {
clientUpgrade := action.NewUpgrade(actionConfig)
clientUpgrade.Namespace = targetNamespace
clientUpgrade.ChartPathOptions = chartPathOptions
rel, requestError = clientUpgrade.Run(releaseName, chart, values)
}
return rel, requestError
}
func createActionConfig(targetNamespace string) (*action.Configuration, *cli.EnvSettings, error) {
settings := cli.New()
actionConfig := new(action.Configuration)
err := actionConfig.Init(settings.RESTClientGetter(), targetNamespace, os.Getenv("HELM_DRIVER"), log.Printf)
return actionConfig, settings, err
}
func getChart(chartPathOption action.ChartPathOptions, chartName string, settings *cli.EnvSettings) (*chart.Chart, error) {
chartPath, err := chartPathOption.LocateChart(chartName, settings)
if err != nil {
return nil, err
}
chart, err := loader.Load(chartPath)
if err != nil {
return nil, err
}
return chart, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment