github.com/nais/knorten@v0.0.0-20240104110906-55926958e361/pkg/helm/chart.go (about)

     1  package helm
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"helm.sh/helm/v3/pkg/action"
     8  	"helm.sh/helm/v3/pkg/chart"
     9  	"helm.sh/helm/v3/pkg/chart/loader"
    10  	"helm.sh/helm/v3/pkg/cli"
    11  	"helm.sh/helm/v3/pkg/getter"
    12  	"helm.sh/helm/v3/pkg/registry"
    13  	"helm.sh/helm/v3/pkg/repo"
    14  )
    15  
    16  func FetchChart(repo, chartName, version string) (*chart.Chart, error) {
    17  	settings := cli.New()
    18  	chartRef := fmt.Sprintf("%v/%v", repo, chartName)
    19  	destDir := "/tmp"
    20  
    21  	registryClient, err := registry.NewClient(
    22  		registry.ClientOptDebug(settings.Debug),
    23  		registry.ClientOptEnableCache(true),
    24  		registry.ClientOptWriter(os.Stderr),
    25  		registry.ClientOptCredentialsFile(settings.RegistryConfig),
    26  	)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	actionConfig := new(action.Configuration)
    32  	actionConfig.RegistryClient = registryClient
    33  	client := action.NewPullWithOpts(action.WithConfig(actionConfig))
    34  	client.Settings = settings
    35  	client.DestDir = destDir
    36  	client.Version = version
    37  
    38  	_, err = client.Run(chartRef)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	return loader.Load(fmt.Sprintf("%v/%v-%v.tgz", destDir, chartName, version))
    44  }
    45  
    46  func UpdateHelmRepositories() error {
    47  	settings := cli.New()
    48  	repoFile := settings.RepositoryConfig
    49  
    50  	f, err := repo.LoadFile(repoFile)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	var repos []*repo.ChartRepository
    56  	for _, cfg := range f.Repositories {
    57  		r, err := repo.NewChartRepository(cfg, getter.All(settings))
    58  		if err != nil {
    59  			return err
    60  		}
    61  		repos = append(repos, r)
    62  	}
    63  
    64  	for _, re := range repos {
    65  		if _, err := re.DownloadIndexFile(); err != nil {
    66  			return err
    67  		}
    68  	}
    69  
    70  	return nil
    71  }