2024-09-02 13:44:44 +02:00
|
|
|
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 {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-09-03 15:18:20 +02:00
|
|
|
err := json.Unmarshal(stdout, &objmap)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this KubeContext) UseContext(newContext string) (error) {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (this KubeContext) Check() (error) {
|
|
|
|
|
2024-09-03 13:46:45 +02:00
|
|
|
cmd := exec.Command(this.Bin, "cluster-info")
|
|
|
|
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
|
|
|
}
|