Ressources
This commit is contained in:
@@ -2,6 +2,7 @@ package helm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
@@ -41,7 +42,6 @@ func (this HelmChart) Install() (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// existe := false
|
||||
|
||||
if existe {
|
||||
return "Existe déjà", nil
|
||||
@@ -137,6 +137,7 @@ func (this HelmChart) exists() (bool, error) {
|
||||
cmd := exec.Command(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))
|
||||
}
|
||||
|
||||
@@ -144,6 +145,15 @@ func (this HelmChart) exists() (bool, error) {
|
||||
res = strings.TrimSuffix(res, "\n")
|
||||
|
||||
log.Log().Debug().Msg(string(stdout))
|
||||
log.Log().Debug().Msg(strconv.FormatBool(res != ""))
|
||||
|
||||
return res != "", nil
|
||||
}
|
||||
|
||||
func (this HelmChart) GetRessources() (map[string]string, error) {
|
||||
hs := HelmStatus{Name: this.Name}
|
||||
hs.New(this.Bin)
|
||||
data, _ := hs.getRessources()
|
||||
|
||||
return data, nil
|
||||
}
|
||||
38
src/helm/command.go
Normal file
38
src/helm/command.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package helm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"errors"
|
||||
"os/exec"
|
||||
|
||||
log "oc-deploy/log_wrapper"
|
||||
)
|
||||
|
||||
type HelmCommandInterface interface {
|
||||
Status(string) (string, error)
|
||||
}
|
||||
|
||||
type HelmCommandData struct {
|
||||
bin string
|
||||
// name string
|
||||
}
|
||||
|
||||
type RealHelmCommandStatus HelmCommandData
|
||||
|
||||
func (this RealHelmCommandStatus) Status(name string) (string, error) {
|
||||
|
||||
msg := fmt.Sprintf("%s status %s --show-resources -o json", this.bin, name)
|
||||
log.Log().Debug().Msg(msg)
|
||||
|
||||
cmd_args := strings.Split(msg, " ")
|
||||
|
||||
cmd := exec.Command(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
|
||||
}
|
||||
84
src/helm/ressources.go
Normal file
84
src/helm/ressources.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package helm
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type HelmStatus struct {
|
||||
Name string // Nom
|
||||
command HelmCommandInterface
|
||||
}
|
||||
|
||||
func (this *HelmStatus) New(bin string) {
|
||||
this.command = RealHelmCommandStatus{bin: bin}
|
||||
}
|
||||
|
||||
func (this *HelmStatus) NewMock(mock HelmCommandInterface) {
|
||||
this.command = mock
|
||||
}
|
||||
|
||||
////
|
||||
|
||||
type parseStatusInfoResourcesMetadata struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// type parseStatusInfoResourcesPod struct {
|
||||
// Api string `json:"apiVersion"`
|
||||
// }
|
||||
|
||||
type parseStatusInfoResourcesStatefulSet struct {
|
||||
Api string `json:"apiVersion"`
|
||||
Kind string `json:"kind"`
|
||||
Metadata parseStatusInfoResourcesMetadata `json:"metadata"`
|
||||
}
|
||||
|
||||
type parseStatusInfoResourcesDeployment struct {
|
||||
Api string `json:"apiVersion"`
|
||||
Kind string `json:"kind"`
|
||||
Metadata parseStatusInfoResourcesMetadata `json:"metadata"`
|
||||
}
|
||||
|
||||
type parseStatusInfoResources struct {
|
||||
// Pod []parseStatusInfoResourcesPod `json:"v1/Pod(related)"`
|
||||
StatefulSet []parseStatusInfoResourcesStatefulSet `json:"v1/StatefulSet"`
|
||||
Deployment []parseStatusInfoResourcesDeployment `json:"v1/Deployment"`
|
||||
}
|
||||
|
||||
type parseStatusInfo struct {
|
||||
Status string `json:"status"`
|
||||
Resources parseStatusInfoResources `json:"Resources"`
|
||||
}
|
||||
|
||||
|
||||
type parseStatus struct {
|
||||
Name string `json:"name"`
|
||||
Info parseStatusInfo `json:"info"`
|
||||
}
|
||||
|
||||
func (this HelmStatus) getRessources() (map[string]string, error) {
|
||||
|
||||
res := make(map[string]string)
|
||||
|
||||
status, err := this.command.Status(this.Name)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
var objmap parseStatus
|
||||
|
||||
err = json.Unmarshal([]byte(status), &objmap)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
for _, ele := range objmap.Info.Resources.StatefulSet {
|
||||
res[ele.Metadata.Name] = ele.Kind
|
||||
}
|
||||
for _, ele := range objmap.Info.Resources.Deployment {
|
||||
res[ele.Metadata.Name] = ele.Kind
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
28
src/helm/ressources_test.go
Normal file
28
src/helm/ressources_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package helm
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type MockCommandStatus HelmCommandData
|
||||
|
||||
func TestHelmStatus(t *testing.T){
|
||||
|
||||
hs := HelmStatus{Name: "oc-catalog"}
|
||||
hs.NewMock(MockCommandStatus{})
|
||||
|
||||
data, _ := hs.getRessources()
|
||||
assert.Equal(t, "StatefulSet", data["oc-catalog-oc-catalog"], "TestHelmStatus error")
|
||||
}
|
||||
|
||||
// Mock
|
||||
func (this MockCommandStatus) Status(name string) (string, error) {
|
||||
fileName := filepath.Join(TEST_SRC_DIR, "helm_status.json")
|
||||
data, _ := os.ReadFile(fileName)
|
||||
return string(data), nil
|
||||
}
|
||||
Reference in New Issue
Block a user