Opts pour helm repo

This commit is contained in:
admju 2024-09-13 10:53:28 +00:00
parent dfa9fe3f1e
commit 9f0218a4da
4 changed files with 108 additions and 102 deletions

View File

@ -1,3 +1,3 @@
--- ---
version: 1.0 version: 0.1.0

View File

@ -22,6 +22,7 @@ type repoData struct {
Name string `yaml:"name"` Name string `yaml:"name"`
Url string `yaml:"url"` Url string `yaml:"url"`
ForceUpdate bool `yaml:"forceupdate"` ForceUpdate bool `yaml:"forceupdate"`
Opts string `yaml:"opts"`
} }
type ChartRepoData struct { type ChartRepoData struct {
@ -29,14 +30,14 @@ type ChartRepoData struct {
Charts []ChartData `yaml:"charts"` Charts []ChartData `yaml:"charts"`
} }
type chartsRepoData struct { type chartsRepoParse struct {
Charts []ChartRepoData `yaml:"opencloud"` Charts []ChartRepoData `yaml:"opencloud"`
} }
func FromConfigFile(filename string) ([]ChartRepoData, error) { func FromConfigFile(filename string) ([]ChartRepoData, error) {
yamlFile, _ := os.ReadFile(filename) yamlFile, _ := os.ReadFile(filename)
var data chartsRepoData var data chartsRepoParse
err := yaml.Unmarshal(yamlFile, &data) err := yaml.Unmarshal(yamlFile, &data)
if err != nil { if err != nil {
return data.Charts, err return data.Charts, err

View File

@ -1,98 +1,102 @@
package helm package helm
import ( import (
"fmt" "fmt"
"strings" "strings"
"encoding/json" "encoding/json"
log "oc-deploy/log_wrapper" log "oc-deploy/log_wrapper"
"oc-deploy/utils" "oc-deploy/utils"
) )
type HelmRepo struct { type HelmRepo struct {
Name string Name string
Repository string // Url du dépôt Repository string // Url du dépôt
ForceUpdate bool ForceUpdate bool
Opts string Opts string
} }
func (this HelmCommand) AddRepository(repo HelmRepo) (string, error) { func (this HelmCommand) AddRepository(repo HelmRepo) (string, error) {
helm_bin := this.Bin helm_bin := this.Bin
force_update := "--force-update=false" force_update := "--force-update=false"
if repo.ForceUpdate { if repo.ForceUpdate {
force_update = "--force-update=true" force_update = "--force-update=true"
} else { } else {
list, _ := this.ListRepository() list, _ := this.ListRepository()
if utils.StringInSlice(repo.Name, list) { if utils.StringInSlice(repo.Name, list) {
return "Existe déjà", nil return "Existe déjà", nil
} }
} }
msg := fmt.Sprintf("%s repo add %s %s %s %s", helm_bin, repo.Name, repo.Repository, force_update, repo.Opts) msg := fmt.Sprintf("%s repo add %s %s %s %s", helm_bin, repo.Name, repo.Repository, force_update, repo.Opts)
log.Log().Debug().Msg(msg)
msg = strings.TrimSuffix(msg, " ")
msg = strings.TrimSuffix(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 {
res := string(stdout) return "", fmt.Errorf(string(stdout))
res = strings.TrimSuffix(res, "\n") }
return res, err res := string(stdout)
} res = strings.TrimSuffix(res, "\n")
log.Log().Debug().Msg(res)
type parseList struct {
Name string `json:"name"` return res, nil
} }
func (this HelmCommand) ListRepository() ([]string, error) { type parseList struct {
Name string `json:"name"`
helm_bin := this.Bin }
res := make([]string, 0, 0)
func (this HelmCommand) ListRepository() ([]string, error) {
msg := fmt.Sprintf("%s repo list -o json", helm_bin)
log.Log().Debug().Msg(msg) helm_bin := this.Bin
res := make([]string, 0, 0)
cmd_args := strings.Split(msg, " ")
msg := fmt.Sprintf("%s repo list -o json", helm_bin)
cmd := this.Exec(cmd_args[0], cmd_args[1:]...) log.Log().Debug().Msg(msg)
stdout, err := cmd.CombinedOutput()
if err != nil { cmd_args := strings.Split(msg, " ")
return res, err
} cmd := this.Exec(cmd_args[0], cmd_args[1:]...)
stdout, err := cmd.CombinedOutput()
var objmap []parseList if err != nil {
return res, err
err = json.Unmarshal(stdout, &objmap) }
if err != nil {
return res, err var objmap []parseList
}
err = json.Unmarshal(stdout, &objmap)
for _, ele := range objmap { if err != nil {
res = append(res, ele.Name) return res, err
} }
return res, err for _, ele := range objmap {
} res = append(res, ele.Name)
}
// helm repo remove [NAME]
func (this HelmCommand) RemoveRepository(repo HelmRepo) (string, error) { return res, err
helm_bin := this.Bin }
msg := fmt.Sprintf("%s repo remove %s", helm_bin, repo.Name) // helm repo remove [NAME]
log.Log().Debug().Msg(msg) func (this HelmCommand) RemoveRepository(repo HelmRepo) (string, error) {
helm_bin := this.Bin
cmd_args := strings.Split(msg, " ")
msg := fmt.Sprintf("%s repo remove %s", helm_bin, repo.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)
res = strings.TrimSuffix(res, "\n") cmd := this.Exec(cmd_args[0], cmd_args[1:]...)
stdout, err := cmd.CombinedOutput()
return res, err
} res := string(stdout)
res = strings.TrimSuffix(res, "\n")
return res, err
}

View File

@ -41,7 +41,8 @@ func (this *InstallClass) ChartRepo() (error) {
log.Log().Info().Msg(fmt.Sprintf(" >> Helm Repo : %s", v.Repository.Name)) log.Log().Info().Msg(fmt.Sprintf(" >> Helm Repo : %s", v.Repository.Name))
repo := helm.HelmRepo{Name: v.Repository.Name, repo := helm.HelmRepo{Name: v.Repository.Name,
Repository: v.Repository.Url, Repository: v.Repository.Url,
ForceUpdate: v.Repository.ForceUpdate} ForceUpdate: v.Repository.ForceUpdate,
Opts: v.Repository.Opts}
res, err := this.commandHelm.AddRepository(repo) res, err := this.commandHelm.AddRepository(repo)
if err != nil { if err != nil {
log.Log().Info().Msg(fmt.Sprintf(" << %s ", err)) log.Log().Info().Msg(fmt.Sprintf(" << %s ", err))