Files
holos/internal/helm/driver.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

67 lines
1.6 KiB
Go

package helm
import (
"context"
"fmt"
"os"
"github.com/holos-run/holos/internal/server/middleware/logger"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/registry"
)
// https://helm.sh/docs/sdk/examples/#driver
var helmDriver string = os.Getenv("HELM_DRIVER")
func initActionConfig(ctx context.Context, settings *cli.EnvSettings) (*action.Configuration, error) {
return initActionConfigList(ctx, settings, false)
}
func initActionConfigList(ctx context.Context, settings *cli.EnvSettings, allNamespaces bool) (*action.Configuration, error) {
log := logger.FromContext(ctx)
actionConfig := new(action.Configuration)
namespace := func() string {
// For list action, you can pass an empty string instead of settings.Namespace() to list
// all namespaces
if allNamespaces {
return ""
}
return settings.Namespace()
}()
debug := func(format string, a ...any) {
log.DebugContext(ctx, fmt.Sprintf(format, a...))
}
if err := actionConfig.Init(
settings.RESTClientGetter(),
namespace,
helmDriver,
debug); err != nil {
return nil, err
}
return actionConfig, nil
}
func newDefaultRegistryClient(settings *cli.EnvSettings, plainHTTP bool) (*registry.Client, error) {
opts := []registry.ClientOption{
registry.ClientOptDebug(settings.Debug),
registry.ClientOptEnableCache(true),
registry.ClientOptWriter(os.Stderr),
registry.ClientOptCredentialsFile(settings.RegistryConfig),
}
if plainHTTP {
opts = append(opts, registry.ClientOptPlainHTTP())
}
// Create a new registry client
registryClient, err := registry.NewClient(opts...)
if err != nil {
return nil, err
}
return registryClient, nil
}