WIP: Trying to implement diff
This commit is contained in:
@ -4,11 +4,11 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.badhouseplants.net/allanger/shoebill/pkg/repository"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gopkg.in/yaml.v2"
|
||||
"helm.sh/helm/v3/pkg/action"
|
||||
"helm.sh/helm/v3/pkg/chart/loader"
|
||||
"helm.sh/helm/v3/pkg/chartutil"
|
||||
"helm.sh/helm/v3/pkg/cli"
|
||||
"helm.sh/helm/v3/pkg/engine"
|
||||
"helm.sh/helm/v3/pkg/getter"
|
||||
@ -22,20 +22,16 @@ func NewHelm() Helmhelper {
|
||||
return &Helm{}
|
||||
}
|
||||
|
||||
type ChartData struct {
|
||||
Version string
|
||||
}
|
||||
|
||||
func getDownloadDirPath(workdirPath string) string {
|
||||
return fmt.Sprintf("%s/.charts", workdirPath)
|
||||
}
|
||||
|
||||
func getChartDirPath(downloadDirPath, repository, chart, version string) string {
|
||||
return fmt.Sprintf("%s/%s-%s-%s", downloadDirPath, repository, chart, version)
|
||||
func getChartDirPath(downloadDirPath string, release *ReleaseData) string {
|
||||
return fmt.Sprintf("%s/%s-%s-%s", downloadDirPath, release.RepositoryName, release.Chart, release.Version)
|
||||
|
||||
}
|
||||
|
||||
func (h *Helm) PullChart(workdirPath string, repository repository.Repository, chart, version string) (path string, err error) {
|
||||
func (h *Helm) PullChart(workdirPath string, release *ReleaseData) (path string, err error) {
|
||||
downloadDirPath := getDownloadDirPath(workdirPath)
|
||||
if err := os.MkdirAll(downloadDirPath, 0777); err != nil {
|
||||
return "", err
|
||||
@ -43,7 +39,7 @@ func (h *Helm) PullChart(workdirPath string, repository repository.Repository, c
|
||||
|
||||
config := new(action.Configuration)
|
||||
cl := cli.New()
|
||||
chartDir := getChartDirPath(downloadDirPath, repository.Name, chart, version)
|
||||
chartDir := getChartDirPath(downloadDirPath, release)
|
||||
_, err = os.Stat(chartDir)
|
||||
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
@ -59,10 +55,10 @@ func (h *Helm) PullChart(workdirPath string, repository repository.Repository, c
|
||||
|
||||
var path string
|
||||
// Download the chart to the workdir
|
||||
if repository.Kind != "oci" {
|
||||
if release.RepositoryKind != "oci" {
|
||||
r, err := repo.NewChartRepository(&repo.Entry{
|
||||
Name: repository.Name,
|
||||
URL: repository.URL,
|
||||
Name: release.RepositoryName,
|
||||
URL: release.RepositoryURL,
|
||||
}, getter.All(cl))
|
||||
if err != nil {
|
||||
return "", err
|
||||
@ -70,7 +66,7 @@ func (h *Helm) PullChart(workdirPath string, repository repository.Repository, c
|
||||
path = r.Config.Name
|
||||
|
||||
} else {
|
||||
path = repository.URL
|
||||
path = release.RepositoryURL
|
||||
}
|
||||
|
||||
client := action.NewPullWithOpts(action.WithConfig(config))
|
||||
@ -78,7 +74,7 @@ func (h *Helm) PullChart(workdirPath string, repository repository.Repository, c
|
||||
client.DestDir = chartDir
|
||||
client.Settings = cl
|
||||
|
||||
chartRemote := fmt.Sprintf("%s/%s", path, chart)
|
||||
chartRemote := fmt.Sprintf("%s/%s", path, release.Chart)
|
||||
logrus.Infof("trying to pull: %s", chartRemote)
|
||||
if _, err = client.Run(chartRemote); err != nil {
|
||||
return "", err
|
||||
@ -92,7 +88,7 @@ func (h *Helm) PullChart(workdirPath string, repository repository.Repository, c
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func (h *Helm) FindLatestVersion(workdirPath, chart string, repository repository.Repository) (version string, err error) {
|
||||
func (h *Helm) FindLatestVersion(workdirPath string, release *ReleaseData) (version string, err error) {
|
||||
downloadDirPath := getDownloadDirPath(workdirPath)
|
||||
if err := os.MkdirAll(downloadDirPath, 0777); err != nil {
|
||||
return "", err
|
||||
@ -100,14 +96,13 @@ func (h *Helm) FindLatestVersion(workdirPath, chart string, repository repositor
|
||||
|
||||
config := new(action.Configuration)
|
||||
cl := cli.New()
|
||||
chartDir := getChartDirPath(downloadDirPath, repository.Name, chart, "latest")
|
||||
chartPath, err := h.PullChart(workdirPath, repository, chart, "latest")
|
||||
chartDir := getChartDirPath(downloadDirPath, release)
|
||||
chartPath, err := h.PullChart(workdirPath, release)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
showAction := action.NewShowWithConfig(action.ShowChart, config)
|
||||
action.New
|
||||
|
||||
res, err := showAction.LocateChart(fmt.Sprintf("%s/%s", chartDir, chartPath), cl)
|
||||
if err != nil {
|
||||
@ -122,34 +117,45 @@ func (h *Helm) FindLatestVersion(workdirPath, chart string, repository repositor
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
logrus.Infof("the latest version of %s is %s", chart, chartData.Version)
|
||||
versionedChartDir := getChartDirPath(downloadDirPath, repository.Name, chart, chartData.Version)
|
||||
logrus.Infof("the latest version of %s is %s", release.Chart, chartData.Version)
|
||||
versionedChartDir := getChartDirPath(downloadDirPath, release)
|
||||
os.Rename(chartDir, versionedChartDir)
|
||||
return chartData.Version, err
|
||||
}
|
||||
|
||||
func (h *Helm) RenderChart(workdirPath string, repository repository.Repository, ch2art, version string) error {
|
||||
func (h *Helm) RenderChart(workdirPath string, release *ReleaseData) error {
|
||||
downloadDirPath := getDownloadDirPath(workdirPath)
|
||||
chartDirPath := getChartDirPath(downloadDirPath, repository.Name, ch2art, version)
|
||||
chartDirPath := getChartDirPath(downloadDirPath, release)
|
||||
chartPath, err := getChartPathFromDir(chartDirPath)
|
||||
if err != nil {
|
||||
return 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
|
||||
}
|
||||
config := new(action.Configuration)
|
||||
client := action.NewInstall(config)
|
||||
client.RepoURL = repository.URL
|
||||
client.ReleaseName = ch2art
|
||||
|
||||
res, err := engine.Render(chartObj, nil)
|
||||
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.Infof("%v", res)
|
||||
logrus.Info(files)
|
||||
for file, data := range files {
|
||||
logrus.Infof("%s - %s", file, data)
|
||||
}
|
||||
logrus.Info("I'm here")
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -165,10 +171,10 @@ func getChartPathFromDir(downloadDir string) (file string, err error) {
|
||||
return files[0].Name(), nil
|
||||
}
|
||||
|
||||
func chartFromString(info string) (*ChartData, error) {
|
||||
chartData := new(ChartData)
|
||||
if err := yaml.Unmarshal([]byte(info), &chartData); err != nil {
|
||||
func chartFromString(info string) (*ReleaseData, error) {
|
||||
releaseData := new(ReleaseData)
|
||||
if err := yaml.Unmarshal([]byte(info), &releaseData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return chartData, nil
|
||||
return releaseData, nil
|
||||
}
|
||||
|
@ -1,9 +1,5 @@
|
||||
package helmhelper
|
||||
|
||||
import (
|
||||
"git.badhouseplants.net/allanger/shoebill/pkg/repository"
|
||||
)
|
||||
|
||||
const (
|
||||
MOCK_LATEST_VERSION = "v1.12.1"
|
||||
MOCK_CHART_PATH = ".charts/repo-release-latest/release-latest.gz"
|
||||
@ -15,14 +11,14 @@ func NewHelmMock() Helmhelper {
|
||||
return &Mock{}
|
||||
}
|
||||
|
||||
func (h *Mock) FindLatestVersion(dir, chart string, repository repository.Repository) (version string, err error) {
|
||||
func (h *Mock) FindLatestVersion(workdir string, release *ReleaseData) (version string, err error) {
|
||||
return MOCK_LATEST_VERSION, nil
|
||||
}
|
||||
|
||||
func (h *Mock) PullChart(workdirPath string, repository repository.Repository, chart, version string) (path string, err error) {
|
||||
func (h *Mock) PullChart(workdirPath string, release *ReleaseData) (path string, err error) {
|
||||
return MOCK_CHART_PATH, nil
|
||||
}
|
||||
|
||||
func (h *Mock) RenderChart(workdirPath string, repository repository.Repository, ch2art, version string) error {
|
||||
func (h *Mock) RenderChart(workdirPath string, release *ReleaseData) error {
|
||||
return nil
|
||||
}
|
||||
|
@ -1,9 +1,18 @@
|
||||
package helmhelper
|
||||
|
||||
import "git.badhouseplants.net/allanger/shoebill/pkg/repository"
|
||||
|
||||
type Helmhelper interface {
|
||||
FindLatestVersion(dir, chart string, repository repository.Repository) (string, error)
|
||||
PullChart(workdirPath string, repository repository.Repository, chart, version string) (string, error)
|
||||
RenderChart(workdirPath string, repository repository.Repository, ch2art, version string) error
|
||||
FindLatestVersion(workdirPath string, release *ReleaseData) (string, error)
|
||||
PullChart(workdirPath string, release *ReleaseData) (string, error)
|
||||
RenderChart(workdirPath string, release *ReleaseData) error
|
||||
}
|
||||
|
||||
type ReleaseData struct {
|
||||
Name string
|
||||
Chart string
|
||||
Namespace string
|
||||
Version string
|
||||
RepositoryName string
|
||||
RepositoryURL string
|
||||
RepositoryKind string
|
||||
ValuesData string
|
||||
}
|
||||
|
Reference in New Issue
Block a user