Init project
This commit is contained in:
91
internal/tools/helm/helmlib.go
Normal file
91
internal/tools/helm/helmlib.go
Normal file
@@ -0,0 +1,91 @@
|
||||
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
|
||||
}
|
||||
|
24
internal/tools/helm/helmlib_test.go
Normal file
24
internal/tools/helm/helmlib_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package helm_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/allanger/yaho/internal/tools/helm"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPullChart(t *testing.T) {
|
||||
helmlib := &helm.HelmLib{}
|
||||
path, err := helmlib.PullChart("https://coredns.github.io/helm", "coredns", "1.42.0")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "test", path)
|
||||
}
|
||||
|
||||
func TestPullChartOci(t *testing.T) {
|
||||
helmlib := &helm.HelmLib{}
|
||||
path, err := helmlib.PullChart("oci://ghcr.io/allanger/allangers-charts", "memos", "0.6.0")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "test", path)
|
||||
}
|
||||
|
||||
|
5
internal/tools/helm/types.go
Normal file
5
internal/tools/helm/types.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package helm
|
||||
|
||||
type Helm interface {
|
||||
PullChart() (string, error)
|
||||
}
|
Reference in New Issue
Block a user