From 756638fe2112c01157cb8df6340ec0cd8886aa97 Mon Sep 17 00:00:00 2001 From: admju Date: Tue, 3 Sep 2024 15:18:11 +0000 Subject: [PATCH] Charts locaux --- src/chart/conf.go | 9 +++++++-- src/chart/conf_test.go | 25 +++++++++++++++++-------- src/cmd/args.go | 5 ++++- src/cmd/installCmd.go | 5 ++++- src/helm/chart.go | 19 ++++++++++++++++++- src/install/install.go | 26 ++++++++++++++------------ test/chart/oc.yml | 10 ++++++++-- 7 files changed, 72 insertions(+), 27 deletions(-) diff --git a/src/chart/conf.go b/src/chart/conf.go index 86fd9d6..f23bb83 100644 --- a/src/chart/conf.go +++ b/src/chart/conf.go @@ -8,17 +8,22 @@ import ( type ChartData struct { Name string `yaml:"name"` Chart string `yaml:"chart"` + Url string `yaml:"url"` Version string `yaml:"version"` Opts string `yaml:"helm_opts"` Values string `yaml:"helm_values"` FileValues string `yaml:"helm_filevalues"` +} +type repoData struct { + Name string `yaml:"name"` + Url string `yaml:"url"` + ForceUpdate bool `yaml:"forceupdate"` } type ChartRepoData struct { - Name string `yaml:"name"` - Repository string `yaml:"repository"` + Repository repoData `yaml:"repository"` Charts []ChartData `yaml:"charts"` } diff --git a/src/chart/conf_test.go b/src/chart/conf_test.go index 20384d9..3f3273a 100644 --- a/src/chart/conf_test.go +++ b/src/chart/conf_test.go @@ -15,16 +15,25 @@ func TestReadConfChart(t *testing.T){ assert.FileExists(t, src, "FromConfigFile error") data, _ := FromConfigFile(src) - assert.Equal(t, data[0].Name, "bitnami", "FromConfigFile error") - assert.Equal(t, data[0].Repository, "https://charts.bitnami.com/bitnami", "FromConfigFile error") + assert.Equal(t, "bitnami", data[0].Repository.Name, "FromConfigFile error") + assert.Equal(t, "https://charts.bitnami.com/bitnami", data[0].Repository.Url, "FromConfigFile error") wordpress := data[0].Charts[0] - assert.Equal(t, wordpress.Name, "wordpress", "FromConfigFile error") - assert.Equal(t, wordpress.Chart, "bitnami/wordpress", "FromConfigFile error") - assert.Equal(t, wordpress.Version, "23.1.0", "FromConfigFile error") + assert.Equal(t, "wordpress", wordpress.Name, "FromConfigFile error") + assert.Equal(t, "bitnami/wordpress", wordpress.Chart, "FromConfigFile error") + assert.Equal(t, "23.1.0", wordpress.Version, "FromConfigFile error") phpmyadmin := data[0].Charts[1] - assert.Equal(t, phpmyadmin.Name, "phpmyadmin", "FromConfigFile error") - assert.Equal(t, phpmyadmin.Chart, "bitnami/phpmyadmin", "FromConfigFile error") - assert.Equal(t, phpmyadmin.Version, "17.0.4", "FromConfigFile error") + assert.Equal(t, "phpmyadmin", phpmyadmin.Name, "FromConfigFile error") + assert.Equal(t, "bitnami/phpmyadmin", phpmyadmin.Chart,"FromConfigFile error") + assert.Equal(t, "17.0.4", phpmyadmin.Version, "FromConfigFile error") + + data1 := data[1] + assert.Equal(t, "", data1.Repository.Name, "FromConfigFile error") + assert.Equal(t, "", data1.Repository.Url, "FromConfigFile error") + + myfirstrelease := data1.Charts[0] + assert.Equal(t, "myfirstrelease", myfirstrelease.Name, "FromConfigFile error") + assert.Equal(t, "https://zzzz/myfirstchart-0.1.0.tgz", myfirstrelease.Url, "FromConfigFile error") + } diff --git a/src/cmd/args.go b/src/cmd/args.go index ff7412f..0c00bc3 100644 --- a/src/cmd/args.go +++ b/src/cmd/args.go @@ -5,6 +5,7 @@ package cmd import ( + "strings" "github.com/spf13/cobra" log "oc-deploy/log_wrapper" @@ -13,6 +14,7 @@ import ( var ( context string version string + modules []string ) func Execute() { @@ -27,7 +29,7 @@ func Execute() { Long: `deploy Charts`, Args: cobra.MaximumNArgs(0), Run: func(cmd *cobra.Command, args []string) { - InstallCmd(context, version) + InstallCmd(context, version, strings.Split(modules, ",")) }, Example: "oc-deploy install --version 1.0 --context ex1", } @@ -56,6 +58,7 @@ func Execute() { cmdInstall.Flags().StringVarP(&context, "context", "p", "opencloud", "Nom du projet") cmdInstall.Flags().StringVarP(&version, "version", "v", "latest", "Version") + cmdInstall.Flags().StringArrayVarP(&modules, "modules", "m", "", "modules, ...") cmdUninstall.Flags().StringVarP(&context, "context", "p", "opencloud", "Nom du projet") diff --git a/src/cmd/installCmd.go b/src/cmd/installCmd.go index f39710a..9910fab 100644 --- a/src/cmd/installCmd.go +++ b/src/cmd/installCmd.go @@ -7,10 +7,13 @@ import ( "oc-deploy/install" ) -func InstallCmd(context string, version string) { +func InstallCmd(context string, version string, modules []string) { log.Log().Info().Msg("Install >> ") log.Log().Info().Msg(" << Contexte : " + context) + if len(modules) > 0 { + log.Log().Info().Msg(fmt.Sprintf(" << Modules : |%s| %d", modules, len(modules))) + } workspace := fmt.Sprintf("workspace_%s", context) obj := install.InstallClass{Workspace: workspace, Version: version} diff --git a/src/helm/chart.go b/src/helm/chart.go index f727ba0..b5b6697 100644 --- a/src/helm/chart.go +++ b/src/helm/chart.go @@ -16,6 +16,8 @@ type HelmChart struct { Name string Chart string Version string + Url string + Workspace string Opts string Values string @@ -39,12 +41,27 @@ func (this HelmChart) Install() (string, error) { if err != nil { return "", err } + // existe := false if existe { return "Existe déjà", nil } - msg := fmt.Sprintf("%s install %s %s %s --output json", bin, this.Name, this.Chart, this.Opts) + ficChart := this.Chart + // Recherche locale + if _, err := os.Stat(ficChart); err != nil { + } else { + // Recherche voa le Workspace + ficChart := filepath.Join(this.Workspace, this.Chart) + if _, err := os.Stat(ficChart); err == nil { + } else { + if this.Url != "" { + fmt.Println("============ 52 Télechargement", this.Url) + } + } + } + + msg := fmt.Sprintf("%s install %s %s %s --output json", bin, this.Name, ficChart, this.Opts) if this.Version != "" { msg = fmt.Sprintf("%s --version %s", msg, this.Version) diff --git a/src/install/install.go b/src/install/install.go index 2a2c42c..90904b2 100644 --- a/src/install/install.go +++ b/src/install/install.go @@ -71,18 +71,19 @@ func (this *InstallClass) ChartRepo() (error) { bin_path, _ := this.getToolBin("helm") for _, v := range this.charts { - log.Log().Info().Msg(fmt.Sprintf(" >> Helm Repo : %s", v.Name)) - repo := helm.HelmRepo{Bin: bin_path, - Name: v.Name, - Repository: v.Repository, - ForceUpdate: true} - res, err := repo.AddRepository() - if err != nil { - log.Log().Info().Msg(fmt.Sprintf(" << %s ", err)) - return err + if v.Repository.Name != "" { + log.Log().Info().Msg(fmt.Sprintf(" >> Helm Repo : %s", v.Repository.Name)) + repo := helm.HelmRepo{Bin: bin_path, + Name: v.Repository.Name, + Repository: v.Repository.Url, + ForceUpdate: v.Repository.ForceUpdate} + res, err := repo.AddRepository() + if err != nil { + log.Log().Info().Msg(fmt.Sprintf(" << %s ", err)) + return err + } + log.Log().Info().Msg(fmt.Sprintf(" << %s ", res)) } - log.Log().Info().Msg(fmt.Sprintf(" << %s ", res)) - } return nil } @@ -116,6 +117,7 @@ func (this *InstallClass) installChart(helm_bin string, kubectl_bin string, char helmchart := helm.HelmChart{Bin: helm_bin, Name: chart.Name, Chart: chart.Chart, + Url: chart.Url, Version: chart.Version, Workspace: this.Workspace, Opts: chart.Opts, @@ -127,7 +129,7 @@ func (this *InstallClass) installChart(helm_bin string, kubectl_bin string, char res, err := helmchart.Install() if err != nil { - log.Log().Error().Msg(fmt.Sprintf(" >> %s %s (%s)", helmchart.Name, "KO", err)) + log.Log().Error().Msg(fmt.Sprintf(" >> %s %s (%s)", helmchart.Name, "KO", err)) return } log.Log().Info().Msg(fmt.Sprintf(" >> %s (%s)", helmchart.Name, res)) diff --git a/test/chart/oc.yml b/test/chart/oc.yml index 446cec5..1220faa 100644 --- a/test/chart/oc.yml +++ b/test/chart/oc.yml @@ -1,8 +1,9 @@ --- opencloud: - - name: bitnami - repository: https://charts.bitnami.com/bitnami # Repository des Charts + - repository: + name: bitnami + url: https://charts.bitnami.com/bitnami # Repository des Charts charts: - name: wordpress chart: bitnami/wordpress @@ -13,3 +14,8 @@ opencloud: chart: bitnami/phpmyadmin version: 17.0.4 values: {} + + - charts: + - name: myfirstrelease + chart: myfirstchart-0.1.0.tgz + url: https://zzzz/myfirstchart-0.1.0.tgz