oc-deploy/src/kubectl/context.go

131 lines
2.9 KiB
Go
Raw Normal View History

2024-09-02 13:44:44 +02:00
package kubectl
import (
2024-09-09 16:17:02 +02:00
"fmt"
2024-09-02 13:44:44 +02:00
"strings"
"errors"
"os/exec"
"encoding/json"
log "oc-deploy/log_wrapper"
)
2024-09-09 16:17:02 +02:00
// type KubeContext struct {
// Bin string // Chemin vers le binaire
// }
2024-09-02 13:44:44 +02:00
type kubeConfig struct {
CurrentContext string `json:"current-context"`
Contexts [] kubeConfigContexts `json:"contexts"`
Clusters [] kubeConfigClusters `json:"clusters"`
}
type kubeConfigContexts struct {
2024-09-03 13:46:45 +02:00
Name string `json:"name"`
Context kubeConfigContext `json:"context"`
2024-09-02 13:44:44 +02:00
}
type kubeConfigContext struct {
2024-09-03 13:46:45 +02:00
Cluster string `json:"cluster"`
User string `json:"user"`
Namespace string `json:"namespace"`
2024-09-02 13:44:44 +02:00
}
type kubeConfigCluster struct {
2024-09-03 13:46:45 +02:00
Server string `json:"server"`
2024-09-02 13:44:44 +02:00
}
type kubeConfigClusters struct {
2024-09-03 13:46:45 +02:00
Name string `json:"name"`
Cluster kubeConfigCluster `json:"cluster"`
2024-09-02 13:44:44 +02:00
}
2024-09-09 16:17:02 +02:00
func (this KubectlCommand) GetCurrentContext() (string, error) {
bin := this.Bin
msg := fmt.Sprintf("%s get config current-context", bin)
log.Log().Debug().Msg(msg)
2024-09-02 13:44:44 +02:00
2024-09-09 16:17:02 +02:00
cmd_args := strings.Split(msg, " ")
cmd := this.Exec(cmd_args[0], cmd_args[1:]...)
2024-09-02 13:44:44 +02:00
stdout, err := cmd.CombinedOutput()
res := string(stdout)
res = strings.TrimSuffix(res, "\n")
return res, err
}
// Current Context
// namespace, server
2024-09-09 16:17:02 +02:00
func (this KubectlCommand) GetContext() (string, string, string, error) {
bin := this.Bin
2024-09-02 13:44:44 +02:00
2024-09-09 16:17:02 +02:00
msg := fmt.Sprintf("%s config view -o json", bin)
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 {
return "", "", "", errors.New(string(stdout))
}
2024-09-02 13:44:44 +02:00
var objmap kubeConfig
2024-09-09 16:17:02 +02:00
err = json.Unmarshal(stdout, &objmap)
2024-09-03 15:18:20 +02:00
if err != nil {
return "", "", "", err
}
2024-09-02 13:44:44 +02:00
currentContext := objmap.CurrentContext
currentCluster := ""
currentNamespace := ""
for _, v := range objmap.Contexts {
if v.Name == currentContext {
currentNamespace = v.Context.Namespace
currentCluster = v.Context.Cluster
}
}
2024-09-03 13:46:45 +02:00
currentServer := ""
for _, v := range objmap.Clusters {
if v.Name == currentCluster {
currentServer = v.Cluster.Server
}
}
2024-09-02 13:44:44 +02:00
return currentContext, currentNamespace, currentServer, nil
}
2024-09-09 16:17:02 +02:00
func (this KubectlCommand) UseContext(newContext string) (error) {
2024-09-02 13:44:44 +02:00
2024-09-03 13:46:45 +02:00
cmd := exec.Command(this.Bin, "config", "use-context", newContext)
stdout, err := cmd.CombinedOutput()
2024-09-02 13:44:44 +02:00
2024-09-03 13:46:45 +02:00
if err != nil {
2024-09-02 13:44:44 +02:00
log.Log().Debug().Msg(string(stdout))
2024-09-03 13:46:45 +02:00
return errors.New(string(stdout))
}
2024-09-02 13:44:44 +02:00
2024-09-03 13:46:45 +02:00
return nil
2024-09-02 13:44:44 +02:00
}
2024-09-09 16:17:02 +02:00
func (this KubectlCommand) Check() (error) {
bin := this.Bin
msg := fmt.Sprintf("%s cluster-info", bin)
log.Log().Debug().Msg(msg)
2024-09-02 13:44:44 +02:00
2024-09-09 16:17:02 +02:00
cmd_args := strings.Split(msg, " ")
cmd := this.Exec(cmd_args[0], cmd_args[1:]...)
2024-09-03 13:46:45 +02:00
stdout, err := cmd.CombinedOutput()
2024-09-02 13:44:44 +02:00
if err != nil {
log.Log().Debug().Msg(string(stdout))
return errors.New("Kube non disponible")
}
2024-09-03 13:46:45 +02:00
return nil
2024-09-02 13:44:44 +02:00
}