This commit is contained in:
admju
2024-09-02 07:09:46 +00:00
parent 00d92b73f8
commit 4ae5926b01
40 changed files with 1711 additions and 0 deletions

108
src/kubectl/context.go Normal file
View File

@@ -0,0 +1,108 @@
package kubectl
import (
// "fmt"
"strings"
"errors"
"os/exec"
"encoding/json"
log "oc-deploy/log_wrapper"
)
type KubeContext struct {
Bin string // Chemin vers le binaire
}
type kubeConfig struct {
CurrentContext string `json:"current-context"`
Contexts [] kubeConfigContexts `json:"contexts"`
Clusters [] kubeConfigClusters `json:"clusters"`
}
type kubeConfigContexts struct {
Name string `json:"name"`
Context kubeConfigContext `json:"context"`
}
type kubeConfigContext struct {
Cluster string `json:"cluster"`
User string `json:"user"`
Namespace string `json:"namespace"`
}
type kubeConfigCluster struct {
Server string `json:"server"`
}
type kubeConfigClusters struct {
Name string `json:"name"`
Cluster kubeConfigCluster `json:"cluster"`
}
func (this KubeContext) GetCurrentContext() (string, error) {
cmd := exec.Command(this.Bin, "config", "current-context")
stdout, err := cmd.CombinedOutput()
res := string(stdout)
res = strings.TrimSuffix(res, "\n")
return res, err
}
// Current Context
// namespace, server
func (this KubeContext) GetContext() (string, string, string, error) {
cmd := exec.Command(this.Bin, "config", "view", "-o", "json")
stdout, _ := cmd.CombinedOutput()
var objmap kubeConfig
json.Unmarshal(stdout, &objmap)
currentContext := objmap.CurrentContext
currentCluster := ""
currentNamespace := ""
for _, v := range objmap.Contexts {
if v.Name == currentContext {
currentNamespace = v.Context.Namespace
currentCluster = v.Context.Cluster
}
}
currentServer := ""
for _, v := range objmap.Clusters {
if v.Name == currentCluster {
currentServer = v.Cluster.Server
}
}
return currentContext, currentNamespace, currentServer, nil
}
func (this KubeContext) UseContext(newContext string) (error) {
cmd := exec.Command(this.Bin, "config", "use-context", newContext)
stdout, err := cmd.CombinedOutput()
if err != nil {
log.Log().Debug().Msg(string(stdout))
return errors.New(string(stdout))
}
return nil
}
func (this KubeContext) Check() (error) {
cmd := exec.Command(this.Bin, "cluster-info")
stdout, err := cmd.CombinedOutput()
if err != nil {
log.Log().Debug().Msg(string(stdout))
return errors.New("Kube non disponible")
}
return nil
}

82
src/kubectl/object.go Normal file
View File

@@ -0,0 +1,82 @@
package kubectl
import (
"fmt"
"strings"
"errors"
"time"
"os/exec"
"encoding/json"
log "oc-deploy/log_wrapper"
)
type KubeObject struct {
Bin string // Chemin vers le binaire
Name string
}
type getOutput struct {
Kind string `json:"kind"`
Status getStatusOutput `json:"status"`
}
type getStatusOutput struct {
Replicas int `json:"replicas"`
UnavailableReplicas int `json:"unavailableReplicas"`
}
func (this KubeObject) Get() (map[string]any, error) {
bin := this.Bin
name := this.Name
msg := fmt.Sprintf("%s get deployment %s -o json", bin, name)
log.Log().Debug().Msg(msg)
m := make(map[string]any)
cmd_args := strings.Split(msg, " ")
cmd := exec.Command(cmd_args[0], cmd_args[1:]...)
stdout, err := cmd.CombinedOutput()
if err != nil {
return m, errors.New(string(stdout))
}
var objmap getOutput
json.Unmarshal(stdout, &objmap)
kind := objmap.Kind
status := objmap.Status
m["name"] = name
m["kind"] = kind
m["replicas"] = status.Replicas
m["UnavailableReplicas"] = status.UnavailableReplicas
return m, nil
}
func (this KubeObject) Wait() (error) {
boucle := 10
sleep := 10000 * time.Millisecond
for _ = range boucle {
log.Log().Debug().Msg(fmt.Sprintf("Check Deployement %s", this.Name))
m, err := this.Get()
if err != nil {
return err
}
ko := m["UnavailableReplicas"].(int)
if ko == 0 {
return nil
}
time.Sleep(sleep)
}
return errors.New("Temps d'attente dépassé")
}

31
src/kubectl/version.go Normal file
View File

@@ -0,0 +1,31 @@
package kubectl
import (
"os/exec"
"encoding/json"
)
type toolClientVersion struct {
GitVersion string `json:"gitVersion"`
}
type toolVersion struct {
ClientVersion toolClientVersion `json:"clientVersion"`
}
func Version(path string) (string, error) {
cmd := exec.Command(path, "version", "-o", "json", "--client=true")
stdout, err := cmd.CombinedOutput()
if err != nil {
return "", err
}
var objmap toolVersion
json.Unmarshal(stdout, &objmap)
res := objmap.ClientVersion.GitVersion
return res, nil
}