shoebill/internal/utils/helmhelper/helm.go

228 lines
6.1 KiB
Go
Raw Normal View History

2023-07-20 09:26:25 +00:00
package helmhelper
import (
"fmt"
"os"
2023-12-20 16:36:14 +00:00
"strings"
2023-07-20 09:26:25 +00:00
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/repo"
)
type Helm struct{}
func NewHelm() Helmhelper {
return &Helm{}
}
2023-10-13 16:02:11 +00:00
func getDownloadDirPath(workdirPath string) string {
return fmt.Sprintf("%s/.charts", workdirPath)
}
2023-12-20 16:36:14 +00:00
func getChartDirPath(downloadDirPath string, release *ChartData) string {
return fmt.Sprintf("%s/%s-%s-%s", downloadDirPath, release.RepositoryName, release.Name, release.Version)
2023-10-13 16:02:11 +00:00
}
2023-12-20 16:36:14 +00:00
func (h *Helm) PullChart(workdirPath string, release *ChartData) (path string, err error) {
2023-10-13 16:02:11 +00:00
downloadDirPath := getDownloadDirPath(workdirPath)
if err := os.MkdirAll(downloadDirPath, 0777); err != nil {
2023-07-20 09:26:25 +00:00
return "", err
}
2023-10-13 16:02:11 +00:00
2023-07-20 09:26:25 +00:00
config := new(action.Configuration)
cl := cli.New()
2023-10-20 11:31:30 +00:00
chartDir := getChartDirPath(downloadDirPath, release)
2023-07-20 09:26:25 +00:00
_, err = os.Stat(chartDir)
2023-10-13 16:02:11 +00:00
2023-07-20 09:26:25 +00:00
if err != nil && !os.IsNotExist(err) {
return "", nil
} else if os.IsNotExist(err) {
if err := os.Mkdir(chartDir, 0777); err != nil {
return "", err
}
registry, err := registry.NewClient()
if err != nil {
return "", err
}
var path string
// Download the chart to the workdir
2023-10-20 11:31:30 +00:00
if release.RepositoryKind != "oci" {
2023-07-20 09:26:25 +00:00
r, err := repo.NewChartRepository(&repo.Entry{
2023-10-20 11:31:30 +00:00
Name: release.RepositoryName,
URL: release.RepositoryURL,
2023-07-20 09:26:25 +00:00
}, getter.All(cl))
if err != nil {
return "", err
}
path = r.Config.Name
} else {
2023-10-20 11:31:30 +00:00
path = release.RepositoryURL
2023-07-20 09:26:25 +00:00
}
client := action.NewPullWithOpts(action.WithConfig(config))
client.SetRegistryClient(registry)
2023-12-20 16:36:14 +00:00
client.Untar = true
2023-07-20 09:26:25 +00:00
client.DestDir = chartDir
client.Settings = cl
2023-12-20 16:36:14 +00:00
chartRemote := fmt.Sprintf("%s/%s", path, release.Name)
2023-07-20 09:26:25 +00:00
logrus.Infof("trying to pull: %s", chartRemote)
if _, err = client.Run(chartRemote); err != nil {
return "", err
}
2023-10-13 16:02:11 +00:00
}
path, err = getChartPathFromDir(chartDir)
if err != nil {
return "", err
2023-07-20 09:26:25 +00:00
}
2023-10-13 16:02:11 +00:00
return path, nil
}
2023-07-20 09:26:25 +00:00
2023-12-20 16:36:14 +00:00
func (h *Helm) FindLatestVersion(workdirPath string, release *ChartData) (version string, err error) {
2023-10-13 16:02:11 +00:00
downloadDirPath := getDownloadDirPath(workdirPath)
if err := os.MkdirAll(downloadDirPath, 0777); err != nil {
return "", err
}
config := new(action.Configuration)
cl := cli.New()
2023-10-20 11:31:30 +00:00
chartDir := getChartDirPath(downloadDirPath, release)
chartPath, err := h.PullChart(workdirPath, release)
2023-07-20 09:26:25 +00:00
if err != nil {
return "", err
}
2023-10-13 16:02:11 +00:00
showAction := action.NewShowWithConfig(action.ShowChart, config)
2023-07-20 09:26:25 +00:00
res, err := showAction.LocateChart(fmt.Sprintf("%s/%s", chartDir, chartPath), cl)
if err != nil {
return "", err
}
res, err = showAction.Run(res)
if err != nil {
return "", nil
}
chartData, err := chartFromString(res)
if err != nil {
return "", err
}
2023-12-20 16:36:14 +00:00
logrus.Infof("the latest version of %s is %s", release.Name, chartData.Version)
release.Version = chartData.Version
2023-10-20 11:31:30 +00:00
versionedChartDir := getChartDirPath(downloadDirPath, release)
2023-12-20 16:36:14 +00:00
if err := os.Rename(chartDir, versionedChartDir); err != nil {
return "", err
}
2023-07-20 09:26:25 +00:00
return chartData.Version, err
}
2023-12-20 16:36:14 +00:00
func (h *Helm) PushChart(workdirPath string, server, prefix, username, password string, chartdata *ChartData) (err error) {
2023-10-13 16:02:11 +00:00
downloadDirPath := getDownloadDirPath(workdirPath)
2023-12-20 16:36:14 +00:00
chartDir := getChartDirPath(downloadDirPath, chartdata)
_, err = os.Stat(chartDir)
if err != nil && !os.IsNotExist(err) {
2023-10-13 16:02:11 +00:00
return err
2023-12-20 16:36:14 +00:00
} else if os.IsNotExist(err) {
if err := os.Mkdir(chartDir, 0777); err != nil {
return err
}
2023-10-13 16:02:11 +00:00
}
2023-12-20 16:36:14 +00:00
regclient, err := registry.NewClient()
options := registry.LoginOptBasicAuth("allanger", "")
tls := registry.LoginOptInsecure(true)
serverClean := strings.ReplaceAll(server, "oci://", "")
if err :=regclient.Login(serverClean, options, tls); err != nil {
2023-10-13 16:02:11 +00:00
return err
}
2023-12-20 16:36:14 +00:00
if err != nil {
return err
}
versionedChartDir := fmt.Sprintf("%s/%s", getChartDirPath(downloadDirPath, chartdata), chartdata.Name)
tar := action.NewPackage()
tar.Destination = downloadDirPath
logrus.Info(versionedChartDir)
logrus.Info(chartDir)
tarname, err := tar.Run(versionedChartDir, nil)
// tarpath := fmt.Sprintf("%s/%s", versionedChartDir, tarname)
if err != nil {
return err
}
client := action.NewPushWithOpts(action.WithPushConfig(&action.Configuration{}))
logrus.Infof("trying to push: %s", tarname)
client.Settings = &cli.EnvSettings{}
smth, err := client.Run(tarname, fmt.Sprintf("%s/%s", server, prefix))
if err != nil {
return err
}
logrus.Info(smth)
return nil
}
func (h *Helm) RenderChart(workdirPath string, release *ReleaseData) error {
2023-10-13 16:02:11 +00:00
return nil
2023-12-20 16:36:14 +00:00
// downloadDirPath := getDownloadDirPath(workdirPath)
// chartDirPath := getChartDirPath(downloadDirPath, release)
// chartPath, err := getChartPathFromDir(chartDirPath)
// if err != nil {
// return err
// }
// logrus.Info(fmt.Sprintf("%s/%s", chartDirPath, chartPath))
// chartObj, err := loader.Load(fmt.Sprintf("%s/%s", chartDirPath, chartPath))
// if err != nil {
// return err
// }
// values := chartutil.Values{}
// values["Values"] = chartObj.Values
// values["Release"] = map[string]string{
// "Name": release.Name,
// "Namespace": release.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
// }
// logrus.Info(files)
// for file, data := range files {
// logrus.Infof("%s - %s", file, data)
// }
// logrus.Info("I'm here")
// return nil
2023-10-13 16:02:11 +00:00
}
2023-12-20 16:36:14 +00:00
2023-10-13 16:02:11 +00:00
func getChartPathFromDir(downloadDir string) (file string, err error) {
files, err := os.ReadDir(downloadDir)
2023-07-20 09:26:25 +00:00
if err != nil {
return "", err
} else if len(files) == 0 {
2023-10-13 16:02:11 +00:00
return "", fmt.Errorf("expected to have one file, got zero in a dir %s", downloadDir)
2023-07-20 09:26:25 +00:00
} else if len(files) > 1 {
2023-10-13 16:02:11 +00:00
return "", fmt.Errorf("expected to have only one file in a dir %s", downloadDir)
2023-07-20 09:26:25 +00:00
}
return files[0].Name(), nil
}
2023-12-20 16:36:14 +00:00
func chartFromString(info string) (*ChartData, error) {
releaseData := new(ChartData)
2023-10-20 11:31:30 +00:00
if err := yaml.Unmarshal([]byte(info), &releaseData); err != nil {
2023-07-20 09:26:25 +00:00
return nil, err
}
2023-10-20 11:31:30 +00:00
return releaseData, nil
2023-07-20 09:26:25 +00:00
}