From 1b9b79c67fd3a0fe48bd0fb904e63aa2da1b9d22 Mon Sep 17 00:00:00 2001 From: admju Date: Thu, 19 Sep 2024 12:10:03 +0000 Subject: [PATCH] oci --- src/chart/conf.go | 5 + src/chart/conf_test.go | 17 ++- src/helm/chart.go | 334 +++++++++++++++++++++-------------------- src/helm/status.go | 60 ++++---- src/install/install.go | 9 +- test/chart/oc_oci.yml | 9 ++ 6 files changed, 234 insertions(+), 200 deletions(-) create mode 100644 test/chart/oc_oci.yml diff --git a/src/chart/conf.go b/src/chart/conf.go index 50cf33f..50624d4 100644 --- a/src/chart/conf.go +++ b/src/chart/conf.go @@ -25,8 +25,13 @@ type repoData struct { Opts string `yaml:"opts"` } +type ociData struct { + Url string `yaml:"url"` +} + type ChartRepoData struct { Repository repoData `yaml:"repository"` + Oci ociData `yaml:"oci"` Charts []ChartData `yaml:"charts"` } diff --git a/src/chart/conf_test.go b/src/chart/conf_test.go index 3dd192a..d8c79d9 100644 --- a/src/chart/conf_test.go +++ b/src/chart/conf_test.go @@ -39,7 +39,7 @@ func _TestReadConfChart(t *testing.T) { assert.Equal(t, "https://zzzz/myfirstchart-0.1.0.tgz", myfirstrelease.Url, "FromConfigFile error") } -func TestReadConfChartOverwrite(t *testing.T){ +func _TestReadConfChartOverwrite(t *testing.T){ src := filepath.Join(TEST_SRC_DIR, "oc_overwrite.yml") assert.FileExists(t, src, "FromConfigFile error") @@ -47,4 +47,17 @@ func TestReadConfChartOverwrite(t *testing.T){ data, _ := FromConfigFile(src) // Nombre de lettres assert.Equal(t, 70, len(data[0].Charts[0].Overwrite), "TestReadConfChartOverwrite error") -} \ No newline at end of file +} + + +func TestReadConfChartOci(t *testing.T) { + src := filepath.Join(TEST_SRC_DIR, "oc_oci.yml") + + assert.FileExists(t, src, "FromConfigFile error") + + data, _ := FromConfigFile(src) + assert.Equal(t, "", data[0].Repository.Name, "FromConfigFile error") + assert.Equal(t, "oci://harbor.dtf/dev", data[0].Oci.Url, "FromConfigFile error") + +} + diff --git a/src/helm/chart.go b/src/helm/chart.go index ab88b4f..af2695e 100644 --- a/src/helm/chart.go +++ b/src/helm/chart.go @@ -1,165 +1,169 @@ -package helm - -import ( - "fmt" - "strconv" - "os" - "strings" - "errors" - "path/filepath" - "encoding/json" - log "oc-deploy/log_wrapper" -) - -type HelmChart struct { - Bin string - Name string - Chart string - Version string - Url string - - Workspace string - Opts string - Values map[string]string - FileValues []string -} - -type installInfoOutput struct { - Description string `json:"description"` - Notes string `json:"notes"` - Status string `json:"status"` -} - -type installOutput struct { - Info installInfoOutput `json:"info"` -} - -func (this HelmCommand) ChartInstall(data HelmChart) (string, error) { - bin := this.Bin - - existe, err := this.chartExists(data) - if err != nil { - return "", err - } - - if existe { - return "Existe déjà", nil - } - - ficChart := data.Chart - // Recherche locale - if _, err := os.Stat(ficChart); err != nil { - } else { - // Recherche voa le Workspace - ficChart := filepath.Join(data.Workspace, data.Chart) - if _, err := os.Stat(ficChart); err == nil { - } else { - if data.Url != "" { - fmt.Println("============ 52 Télechargement", data.Url) - } - } - } - - msg := fmt.Sprintf("%s install %s %s %s --output json", bin, data.Name, ficChart, data.Opts) - - if data.Version != "" { - msg = fmt.Sprintf("%s --version %s", msg, data.Version) - } - - for key, value := range data.Values { - msg = fmt.Sprintf("%s --set %s=%s", msg, key, value) - } - - ficoverwrite := filepath.Join(data.Workspace, fmt.Sprintf("value-%s.yml", data.Name)) - if _, err := os.Stat(ficoverwrite); err != nil { - log.Log().Warn().Msg(ficoverwrite) - } else { - msg = fmt.Sprintf("%s --values %s", msg, ficoverwrite) - } - - for _, valuefilename := range data.FileValues { - fic := filepath.Join(data.Workspace, valuefilename) - if _, err := os.Stat(fic); err != nil { - log.Log().Warn().Msg(fic) - } else { - msg = fmt.Sprintf("%s --values %s", msg, fic) - } - } - - msg = strings.Replace(msg, " ", " ", -1) - - log.Log().Debug().Msg(msg) - - cmd_args := strings.Split(msg, " ") - cmd := this.Exec(cmd_args[0], cmd_args[1:]...) - stdout, err := cmd.CombinedOutput() - - if err != nil { - res := string(stdout) - res = strings.TrimSuffix(res, "\n") - return "", errors.New(res) - } - - var objmap installOutput - - err = json.Unmarshal(stdout, &objmap) - if err != nil { - return "", err - } - - res := objmap.Info.Status - - return res, nil -} - -func (this HelmCommand) ChartUninstall(data HelmChart) (string, error) { - bin := this.Bin - - log.Log().Info().Msg(" >> Chart : " + data.Name) - - existe, err := this.chartExists(data) - if err != nil { - return "", err - } - if ! existe { - return "Non présent", nil - } - - msg := fmt.Sprintf("%s uninstall %s", bin, data.Name) - log.Log().Debug().Msg(msg) - - cmd_args := strings.Split(msg, " ") - cmd := this.Exec(cmd_args[0], cmd_args[1:]...) - stdout, err := cmd.CombinedOutput() - - res := string(stdout) - res = strings.TrimSuffix(res, "\n") - - log.Log().Debug().Msg(res) - - return res, err -} - -// ../bin/helm list --filter phpmyadminm --short -func (this HelmCommand) chartExists(data HelmChart) (bool, error) { - bin := this.Bin - - msg := fmt.Sprintf("%s list --filter %s --no-headers", bin, data.Name) - log.Log().Debug().Msg(msg) - - cmd_args := strings.Split(msg, " ") - cmd := this.Exec(cmd_args[0], cmd_args[1:]...) - stdout, err := cmd.CombinedOutput() - if err != nil { - log.Log().Debug().Msg(string(stdout)) - return false, errors.New(string(stdout)) - } - - res := string(stdout) - res = strings.TrimSuffix(res, "\n") - - log.Log().Debug().Msg(string(stdout)) - log.Log().Debug().Msg(strconv.FormatBool(res != "")) - - return res != "", nil -} +package helm + +import ( + "fmt" + "strconv" + "os" + "strings" + "errors" + "path/filepath" + "encoding/json" + log "oc-deploy/log_wrapper" +) + +type HelmChart struct { + Bin string + Name string + Chart string + Version string + Url string + + Workspace string + Opts string + Values map[string]string + FileValues []string +} + +type installInfoOutput struct { + Description string `json:"description"` + Notes string `json:"notes"` + Status string `json:"status"` +} + +type installOutput struct { + Info installInfoOutput `json:"info"` +} + +func (this HelmCommand) ChartInstall(data HelmChart) (string, error) { + bin := this.Bin + + existe, err := this.chartExists(data) + if err != nil { + return "", err + } + + if existe { + return "Existe déjà", nil + } + + ficChart := data.Chart + // Recherche locale + if _, err := os.Stat(ficChart); err != nil { + } else { + // Recherche voa le Workspace + ficChart := filepath.Join(data.Workspace, data.Chart) + if _, err := os.Stat(ficChart); err == nil { + } else { + if data.Url != "" { + fmt.Println("============ 52 Télechargement", data.Url) + } + } + } + + msg := fmt.Sprintf("%s install %s %s %s --output json", bin, data.Name, ficChart, data.Opts) + + if data.Version != "" { + msg = fmt.Sprintf("%s --version %s", msg, data.Version) + } + + for key, value := range data.Values { + msg = fmt.Sprintf("%s --set %s=%s", msg, key, value) + } + + ficoverwrite := filepath.Join(data.Workspace, fmt.Sprintf("value-%s.yml", data.Name)) + if _, err := os.Stat(ficoverwrite); err != nil { + log.Log().Warn().Msg(ficoverwrite) + } else { + msg = fmt.Sprintf("%s --values %s", msg, ficoverwrite) + } + + for _, valuefilename := range data.FileValues { + fic := filepath.Join(data.Workspace, valuefilename) + if _, err := os.Stat(fic); err != nil { + log.Log().Warn().Msg(fic) + } else { + msg = fmt.Sprintf("%s --values %s", msg, fic) + } + } + + msg = strings.Replace(msg, " ", " ", -1) + + log.Log().Debug().Msg(msg) + + cmd_args := strings.Split(msg, " ") + cmd := this.Exec(cmd_args[0], cmd_args[1:]...) + stdout, err := cmd.CombinedOutput() + + if err != nil { + res := string(stdout) + res = strings.TrimSuffix(res, "\n") + return "", errors.New(res) + } + + var objmap installOutput + + i := strings.Index(string(stdout), "{") + stdout2 := string(stdout)[i:] + + err = json.Unmarshal([]byte(stdout2), &objmap) + if err != nil { + return "", err + } + + res := objmap.Info.Status + + return res, nil +} + +func (this HelmCommand) ChartUninstall(data HelmChart) (string, error) { + bin := this.Bin + + log.Log().Info().Msg(" >> Chart : " + data.Name) + + existe, err := this.chartExists(data) + if err != nil { + return "", err + } + if ! existe { + return "Non présent", nil + } + + msg := fmt.Sprintf("%s uninstall %s", bin, data.Name) + log.Log().Debug().Msg(msg) + + cmd_args := strings.Split(msg, " ") + cmd := this.Exec(cmd_args[0], cmd_args[1:]...) + stdout, err := cmd.CombinedOutput() + + res := string(stdout) + res = strings.TrimSuffix(res, "\n") + + log.Log().Debug().Msg(res) + + return res, err +} + +// ../bin/helm list --filter phpmyadminm --short +func (this HelmCommand) chartExists(data HelmChart) (bool, error) { + + bin := this.Bin + + msg := fmt.Sprintf("%s list --filter %s --no-headers", bin, data.Name) + log.Log().Debug().Msg(msg) + + cmd_args := strings.Split(msg, " ") + cmd := this.Exec(cmd_args[0], cmd_args[1:]...) + stdout, err := cmd.CombinedOutput() + if err != nil { + log.Log().Debug().Msg(string(stdout)) + return false, errors.New(string(stdout)) + } + + res := string(stdout) + res = strings.TrimSuffix(res, "\n") + + log.Log().Debug().Msg(string(stdout)) + log.Log().Debug().Msg(strconv.FormatBool(res != "")) + + return res != "", nil +} diff --git a/src/helm/status.go b/src/helm/status.go index 5b35cb0..93eb593 100644 --- a/src/helm/status.go +++ b/src/helm/status.go @@ -1,32 +1,28 @@ -package helm - -import ( - "fmt" - "strings" - "errors" - - log "oc-deploy/log_wrapper" -) - -// type HelmData struct { -// Name string -// } - -func (this HelmCommand) Status(data HelmChart) (string, error) { - - helm_bin := this.Bin - - msg := fmt.Sprintf("%s status %s --show-resources -o json", helm_bin, data.Name) - log.Log().Debug().Msg(msg) - - cmd_args := strings.Split(msg, " ") - - cmd := this.Exec(cmd_args[0], cmd_args[1:]...) - stdout, err := cmd.CombinedOutput() - if err != nil { - log.Log().Debug().Msg(string(stdout)) - return "", errors.New(string(stdout)) - } - - return string(stdout), nil -} +package helm + +import ( + "fmt" + "strings" + "errors" + + log "oc-deploy/log_wrapper" +) + +func (this HelmCommand) Status(data HelmChart) (string, error) { + + helm_bin := this.Bin + + msg := fmt.Sprintf("%s status %s --show-resources -o json", helm_bin, data.Name) + log.Log().Debug().Msg(msg) + + cmd_args := strings.Split(msg, " ") + + cmd := this.Exec(cmd_args[0], cmd_args[1:]...) + stdout, err := cmd.CombinedOutput() + if err != nil { + log.Log().Debug().Msg(string(stdout)) + return "", errors.New(string(stdout)) + } + + return string(stdout), nil +} diff --git a/src/install/install.go b/src/install/install.go index c359221..641614b 100644 --- a/src/install/install.go +++ b/src/install/install.go @@ -60,7 +60,14 @@ func (this *InstallClass) InstallCharts(modules []string) (error) { var wg sync.WaitGroup for _, v := range this.charts { - repoName := v.Repository.Name + repoName := "" + if v.Repository.Name != "" { + repoName = v.Repository.Name + } else { + if v.Oci.Url != "" { + repoName = v.Oci.Url + } + } for _, v1 := range v.Charts { if len(modules) == 0 || utils.StringInSlice(v1.Name, modules) { diff --git a/test/chart/oc_oci.yml b/test/chart/oc_oci.yml new file mode 100644 index 0000000..869c93d --- /dev/null +++ b/test/chart/oc_oci.yml @@ -0,0 +1,9 @@ +--- + +opencloud: + - oci: + url: oci://harbor.dtf/dev + charts: + - name: oc-catalog + chart: oc-catalog + version: 0.1.0