Files
yaho/internal/tools/helm/helmlib.go
Nikolai Rodionov 1659245f00
Some checks failed
Lint / Run on Ubuntu (push) Has been cancelled
E2E Tests / Run on Ubuntu (push) Has been cancelled
Tests / Run on Ubuntu (push) Has been cancelled
Some updates
Signed-off-by: Nikolai Rodionov <allanger@badhouseplants.net>
2025-07-15 20:30:21 +02:00

81 lines
1.9 KiB
Go

package helm
import (
"context"
"fmt"
"os"
"strings"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/registry"
"sigs.k8s.io/controller-runtime/pkg/log"
)
type HelmLib struct {}
const (
helmRepo = "helm"
ociRepo = "oci"
)
func (h *HelmLib) PullChart(ctx context.Context, repository, chart, version string) (string, error) {
log := log.FromContext(ctx)
workdir, err := os.MkdirTemp("/tmp", "helmdownloader")
if err != nil {
return "", err
}
log.Info("A temporary directory is created", "path", workdir)
config := new(action.Configuration)
cl := cli.New()
registry, err := registry.NewClient()
if err != nil {
return "", err
}
client := action.NewPullWithOpts(action.WithConfig(config))
prefix := repository[:strings.IndexByte(repository, ':')]
var chartRemote string
switch prefix {
case "oci":
chartRemote = fmt.Sprintf("%s/%s", repository, chart)
client.SetRegistryClient(registry)
case "https", "http":
client.RepoURL = repository
chartRemote = chart
default:
return "", fmt.Errorf("unknown repo kind: %s", prefix)
}
client.Settings = cl
client.Version = version
client.DestDir = workdir
path, err := client.Run(chartRemote)
if err != nil {
log.Error(err, "An unexpected error occurred while fetching the chart", "chart", chartRemote)
return "", err
}
log.Info("Path is", "path", path)
// there must be a better way, but I couldn't find it yet
return fmt.Sprintf("%s/%s-%s.tgz", workdir, chart, version), nil
}
func (h *HelmLib) InstallChart(ctx context.Context, path string, name, namespace string) error {
log := log.FromContext(ctx)
chartObj, err := loader.Load(path)
if err != nil {
return err
}
client := action.NewInstall(&action.Configuration{})
client.DryRun = true
release, err := client.RunWithContext(ctx, chartObj, nil)
if err != nil {
return err
}
log.Info("Got a manifest", "manifest", release.Manifest)
return nil
}