This commit is contained in:
admju 2024-09-19 12:10:03 +00:00
parent 981ee7dce4
commit 1b9b79c67f
6 changed files with 234 additions and 200 deletions

View File

@ -25,8 +25,13 @@ type repoData struct {
Opts string `yaml:"opts"` Opts string `yaml:"opts"`
} }
type ociData struct {
Url string `yaml:"url"`
}
type ChartRepoData struct { type ChartRepoData struct {
Repository repoData `yaml:"repository"` Repository repoData `yaml:"repository"`
Oci ociData `yaml:"oci"`
Charts []ChartData `yaml:"charts"` Charts []ChartData `yaml:"charts"`
} }

View File

@ -39,7 +39,7 @@ func _TestReadConfChart(t *testing.T) {
assert.Equal(t, "https://zzzz/myfirstchart-0.1.0.tgz", myfirstrelease.Url, "FromConfigFile error") 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") src := filepath.Join(TEST_SRC_DIR, "oc_overwrite.yml")
assert.FileExists(t, src, "FromConfigFile error") assert.FileExists(t, src, "FromConfigFile error")
@ -47,4 +47,17 @@ func TestReadConfChartOverwrite(t *testing.T){
data, _ := FromConfigFile(src) data, _ := FromConfigFile(src)
// Nombre de lettres // Nombre de lettres
assert.Equal(t, 70, len(data[0].Charts[0].Overwrite), "TestReadConfChartOverwrite error") assert.Equal(t, 70, len(data[0].Charts[0].Overwrite), "TestReadConfChartOverwrite error")
} }
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")
}

View File

@ -1,165 +1,169 @@
package helm package helm
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"os" "os"
"strings" "strings"
"errors" "errors"
"path/filepath" "path/filepath"
"encoding/json" "encoding/json"
log "oc-deploy/log_wrapper" log "oc-deploy/log_wrapper"
) )
type HelmChart struct { type HelmChart struct {
Bin string Bin string
Name string Name string
Chart string Chart string
Version string Version string
Url string Url string
Workspace string Workspace string
Opts string Opts string
Values map[string]string Values map[string]string
FileValues []string FileValues []string
} }
type installInfoOutput struct { type installInfoOutput struct {
Description string `json:"description"` Description string `json:"description"`
Notes string `json:"notes"` Notes string `json:"notes"`
Status string `json:"status"` Status string `json:"status"`
} }
type installOutput struct { type installOutput struct {
Info installInfoOutput `json:"info"` Info installInfoOutput `json:"info"`
} }
func (this HelmCommand) ChartInstall(data HelmChart) (string, error) { func (this HelmCommand) ChartInstall(data HelmChart) (string, error) {
bin := this.Bin bin := this.Bin
existe, err := this.chartExists(data) existe, err := this.chartExists(data)
if err != nil { if err != nil {
return "", err return "", err
} }
if existe { if existe {
return "Existe déjà", nil return "Existe déjà", nil
} }
ficChart := data.Chart ficChart := data.Chart
// Recherche locale // Recherche locale
if _, err := os.Stat(ficChart); err != nil { if _, err := os.Stat(ficChart); err != nil {
} else { } else {
// Recherche voa le Workspace // Recherche voa le Workspace
ficChart := filepath.Join(data.Workspace, data.Chart) ficChart := filepath.Join(data.Workspace, data.Chart)
if _, err := os.Stat(ficChart); err == nil { if _, err := os.Stat(ficChart); err == nil {
} else { } else {
if data.Url != "" { if data.Url != "" {
fmt.Println("============ 52 Télechargement", 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) msg := fmt.Sprintf("%s install %s %s %s --output json", bin, data.Name, ficChart, data.Opts)
if data.Version != "" { if data.Version != "" {
msg = fmt.Sprintf("%s --version %s", msg, data.Version) msg = fmt.Sprintf("%s --version %s", msg, data.Version)
} }
for key, value := range data.Values { for key, value := range data.Values {
msg = fmt.Sprintf("%s --set %s=%s", msg, key, value) msg = fmt.Sprintf("%s --set %s=%s", msg, key, value)
} }
ficoverwrite := filepath.Join(data.Workspace, fmt.Sprintf("value-%s.yml", data.Name)) ficoverwrite := filepath.Join(data.Workspace, fmt.Sprintf("value-%s.yml", data.Name))
if _, err := os.Stat(ficoverwrite); err != nil { if _, err := os.Stat(ficoverwrite); err != nil {
log.Log().Warn().Msg(ficoverwrite) log.Log().Warn().Msg(ficoverwrite)
} else { } else {
msg = fmt.Sprintf("%s --values %s", msg, ficoverwrite) msg = fmt.Sprintf("%s --values %s", msg, ficoverwrite)
} }
for _, valuefilename := range data.FileValues { for _, valuefilename := range data.FileValues {
fic := filepath.Join(data.Workspace, valuefilename) fic := filepath.Join(data.Workspace, valuefilename)
if _, err := os.Stat(fic); err != nil { if _, err := os.Stat(fic); err != nil {
log.Log().Warn().Msg(fic) log.Log().Warn().Msg(fic)
} else { } else {
msg = fmt.Sprintf("%s --values %s", msg, fic) msg = fmt.Sprintf("%s --values %s", msg, fic)
} }
} }
msg = strings.Replace(msg, " ", " ", -1) msg = strings.Replace(msg, " ", " ", -1)
log.Log().Debug().Msg(msg) log.Log().Debug().Msg(msg)
cmd_args := strings.Split(msg, " ") cmd_args := strings.Split(msg, " ")
cmd := this.Exec(cmd_args[0], cmd_args[1:]...) cmd := this.Exec(cmd_args[0], cmd_args[1:]...)
stdout, err := cmd.CombinedOutput() stdout, err := cmd.CombinedOutput()
if err != nil { if err != nil {
res := string(stdout) res := string(stdout)
res = strings.TrimSuffix(res, "\n") res = strings.TrimSuffix(res, "\n")
return "", errors.New(res) return "", errors.New(res)
} }
var objmap installOutput var objmap installOutput
err = json.Unmarshal(stdout, &objmap) i := strings.Index(string(stdout), "{")
if err != nil { stdout2 := string(stdout)[i:]
return "", err
} err = json.Unmarshal([]byte(stdout2), &objmap)
if err != nil {
res := objmap.Info.Status return "", err
}
return res, nil
} res := objmap.Info.Status
func (this HelmCommand) ChartUninstall(data HelmChart) (string, error) { return res, nil
bin := this.Bin }
log.Log().Info().Msg(" >> Chart : " + data.Name) func (this HelmCommand) ChartUninstall(data HelmChart) (string, error) {
bin := this.Bin
existe, err := this.chartExists(data)
if err != nil { log.Log().Info().Msg(" >> Chart : " + data.Name)
return "", err
} existe, err := this.chartExists(data)
if ! existe { if err != nil {
return "Non présent", nil return "", err
} }
if ! existe {
msg := fmt.Sprintf("%s uninstall %s", bin, data.Name) return "Non présent", nil
log.Log().Debug().Msg(msg) }
cmd_args := strings.Split(msg, " ") msg := fmt.Sprintf("%s uninstall %s", bin, data.Name)
cmd := this.Exec(cmd_args[0], cmd_args[1:]...) log.Log().Debug().Msg(msg)
stdout, err := cmd.CombinedOutput()
cmd_args := strings.Split(msg, " ")
res := string(stdout) cmd := this.Exec(cmd_args[0], cmd_args[1:]...)
res = strings.TrimSuffix(res, "\n") stdout, err := cmd.CombinedOutput()
log.Log().Debug().Msg(res) res := string(stdout)
res = strings.TrimSuffix(res, "\n")
return res, err
} log.Log().Debug().Msg(res)
// ../bin/helm list --filter phpmyadminm --short return res, err
func (this HelmCommand) chartExists(data HelmChart) (bool, error) { }
bin := this.Bin
// ../bin/helm list --filter phpmyadminm --short
msg := fmt.Sprintf("%s list --filter %s --no-headers", bin, data.Name) func (this HelmCommand) chartExists(data HelmChart) (bool, error) {
log.Log().Debug().Msg(msg)
bin := this.Bin
cmd_args := strings.Split(msg, " ")
cmd := this.Exec(cmd_args[0], cmd_args[1:]...) msg := fmt.Sprintf("%s list --filter %s --no-headers", bin, data.Name)
stdout, err := cmd.CombinedOutput() log.Log().Debug().Msg(msg)
if err != nil {
log.Log().Debug().Msg(string(stdout)) cmd_args := strings.Split(msg, " ")
return false, errors.New(string(stdout)) cmd := this.Exec(cmd_args[0], cmd_args[1:]...)
} stdout, err := cmd.CombinedOutput()
if err != nil {
res := string(stdout) log.Log().Debug().Msg(string(stdout))
res = strings.TrimSuffix(res, "\n") return false, errors.New(string(stdout))
}
log.Log().Debug().Msg(string(stdout))
log.Log().Debug().Msg(strconv.FormatBool(res != "")) res := string(stdout)
res = strings.TrimSuffix(res, "\n")
return res != "", nil
} log.Log().Debug().Msg(string(stdout))
log.Log().Debug().Msg(strconv.FormatBool(res != ""))
return res != "", nil
}

View File

@ -1,32 +1,28 @@
package helm package helm
import ( import (
"fmt" "fmt"
"strings" "strings"
"errors" "errors"
log "oc-deploy/log_wrapper" log "oc-deploy/log_wrapper"
) )
// type HelmData struct { func (this HelmCommand) Status(data HelmChart) (string, error) {
// Name string
// } helm_bin := this.Bin
func (this HelmCommand) Status(data HelmChart) (string, error) { msg := fmt.Sprintf("%s status %s --show-resources -o json", helm_bin, data.Name)
log.Log().Debug().Msg(msg)
helm_bin := this.Bin
cmd_args := strings.Split(msg, " ")
msg := fmt.Sprintf("%s status %s --show-resources -o json", helm_bin, data.Name)
log.Log().Debug().Msg(msg) cmd := this.Exec(cmd_args[0], cmd_args[1:]...)
stdout, err := cmd.CombinedOutput()
cmd_args := strings.Split(msg, " ") if err != nil {
log.Log().Debug().Msg(string(stdout))
cmd := this.Exec(cmd_args[0], cmd_args[1:]...) return "", errors.New(string(stdout))
stdout, err := cmd.CombinedOutput() }
if err != nil {
log.Log().Debug().Msg(string(stdout)) return string(stdout), nil
return "", errors.New(string(stdout)) }
}
return string(stdout), nil
}

View File

@ -60,7 +60,14 @@ func (this *InstallClass) InstallCharts(modules []string) (error) {
var wg sync.WaitGroup var wg sync.WaitGroup
for _, v := range this.charts { 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 { for _, v1 := range v.Charts {
if len(modules) == 0 || utils.StringInSlice(v1.Name, modules) { if len(modules) == 0 || utils.StringInSlice(v1.Name, modules) {

9
test/chart/oc_oci.yml Normal file
View File

@ -0,0 +1,9 @@
---
opencloud:
- oci:
url: oci://harbor.dtf/dev
charts:
- name: oc-catalog
chart: oc-catalog
version: 0.1.0