Files
oc-deploy/src/kubectl/context.go

131 lines
3.0 KiB
Go
Raw Normal View History

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