Files
holos/internal/helm/helm.go
Jeff McCune f71d6d5bd9 helm: support private helm repositories (#370)
Previously holos unconditionally executed helm repo add which failed for
private repositories requiring basic authentication.

This patch addresses the problem by using the Helm SDK to pull and cache
charts without adding them as repositories.  New fields for the
core.Helm type allow basic auth credentials to be read from environment
variables.

Multiple repositories are supported by using different env vars for
different repositories.
2024-12-06 15:38:46 -08:00

53 lines
1.5 KiB
Go

package helm
import (
"context"
"fmt"
"github.com/holos-run/holos/internal/errors"
"github.com/holos-run/holos/internal/logger"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
)
// PullChart downloads and caches a Helm chart locally. It handles both OCI and
// HTTP repositories. Returns the path to the cached chart and any error
// encountered. Attribution: Helm SDK Examples [Pull Action].
//
// For convenience, initialize SDK setting via CLI mechanism:
//
// settings := cli.New()
//
// [Pull Action]: https://helm.sh/docs/sdk/examples/#pull-action
func PullChart(ctx context.Context, settings *cli.EnvSettings, chartRef, chartVersion, repoURL, destDir, username, password string) error {
log := logger.FromContext(ctx)
actionConfig, err := initActionConfig(ctx, settings)
if err != nil {
return errors.Format("failed to init action config: %w", err)
}
registryClient, err := newDefaultRegistryClient(settings, false)
if err != nil {
return errors.Format("failed to created registry client: %w", err)
}
actionConfig.RegistryClient = registryClient
pullClient := action.NewPullWithOpts(action.WithConfig(actionConfig))
pullClient.Untar = true
pullClient.RepoURL = repoURL
pullClient.DestDir = destDir
pullClient.Settings = settings
pullClient.Version = chartVersion
pullClient.Username = username
pullClient.Password = password
result, err := pullClient.Run(chartRef)
if err != nil {
return errors.Format("failed to pull chart: %w", err)
}
log.DebugContext(ctx, fmt.Sprintf("%+v", result))
return nil
}