WIP: Trying to implement diff

This commit is contained in:
Nikolai Rodionov
2023-10-20 13:31:30 +02:00
parent 93ad3389b2
commit ed3d45a7c4
15 changed files with 197 additions and 136 deletions

View File

@ -73,24 +73,26 @@ func (g *Git) CloneRepo(workdir, gitURL string, dry bool) error {
return nil
}
func (g *Git) AddAllAndCommit(workdir, message string) error {
func (g *Git) AddAllAndCommit(workdir, message string) (string, error) {
r, err := git.PlainOpen(workdir)
if err != nil {
return err
return "", err
}
w, err := r.Worktree()
if err != nil {
return err
return "", err
}
if _, err := w.Add("."); err != nil {
return err
return "", err
}
if _, err := w.Commit(message, &git.CommitOptions{}); err != nil {
return err
sha, err := w.Commit(message, &git.CommitOptions{})
if err != nil {
return "", err
}
return nil
return sha.String(), nil
}
func (g *Git) Push(workdir string) error {

View File

@ -10,8 +10,8 @@ func (m *Mock) CloneRepo(workdir, gitURL string, dry bool) error {
return nil
}
func (g *Mock) AddAllAndCommit(workdir, message string) error {
return nil
func (g *Mock) AddAllAndCommit(workdir, message string) (string, error) {
return "HASH", nil
}
func (g *Mock) Push(workdir string) error {
return nil

View File

@ -2,6 +2,6 @@ package githelper
type Githelper interface {
CloneRepo(workdir, gitURL string, dry bool) error
AddAllAndCommit(workdir, message string) error
AddAllAndCommit(workdir, message string) (string, error)
Push(workdir string) error
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -128,11 +128,10 @@ func Generate(path string, gh githelper.Githelper) error {
Name: "helm-root",
Namespace: "flux-system",
},
Resources: kustomize.Files,
Resources: append(kustomize.Files, kustomize.ConfigMaps...),
GeneratorOptions: &kustomize_types.GeneratorOptions{
DisableNameSuffixHash: true,
},
ConfigMapGenerator: kustomize.CmGeneratorFromFiles(),
}
if len(kustomize.Secrets) > 0 {
@ -172,7 +171,7 @@ func Generate(path string, gh githelper.Githelper) error {
return nil
}
if err := gh.AddAllAndCommit(path, "Update the root kustomization"); err != nil {
if _, err := gh.AddAllAndCommit(path, "Update the root kustomization"); err != nil {
return err
}