92 lines
2.0 KiB
Go
92 lines
2.0 KiB
Go
package helm
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"helm.sh/helm/pkg/chartutil"
|
|
"helm.sh/helm/pkg/engine"
|
|
"helm.sh/helm/v3/pkg/action"
|
|
"helm.sh/helm/v3/pkg/cli"
|
|
"helm.sh/helm/v3/pkg/registry"
|
|
"honnef.co/go/tools/go/loader"
|
|
"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.UntarDir = workdir
|
|
client.Version = version
|
|
path, err := client.Run(chartRemote)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return path, 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
|
|
}
|
|
values := chartutil.Values{}
|
|
values["Values"] = nil
|
|
values["Release"] = map[string]string{
|
|
"Name": name,
|
|
"Namespace": namespace,
|
|
}
|
|
values["Capabilities"] = map[string]map[string]string{
|
|
"KubeVersion": {
|
|
"Version": "v1.27.9",
|
|
"GitVersion": "v1.27.9",
|
|
},
|
|
}
|
|
files, err := engine.Engine{Strict: false}.Render(chartObj, values)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for file, data := range files {
|
|
log.Info("File is rendered", "data", file)
|
|
}
|
|
return nil
|
|
}
|
|
|