[ CMII ] [ Operator ] - get pod GitBranch from cmiiEnv

This commit is contained in:
zeaslity
2024-01-22 11:43:36 +08:00
parent 42dee262cf
commit f1fb7e8309
8 changed files with 186 additions and 20 deletions

View File

@@ -4,13 +4,18 @@ import (
"agent-go/g"
"agent-go/logger"
"agent-go/utils"
"bytes"
"context"
v1 "k8s.io/api/apps/v1"
autoscalingv1 "k8s.io/api/autoscaling/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
"os"
"strings"
"sync"
"time"
@@ -24,6 +29,9 @@ type CmiiK8sOperator struct {
CoreClient *kubernetes.Clientset
CurrentNamespace string
CurrentClient *kubernetes.Clientset
DevConfig *restclient.Config
CoreConfig *restclient.Config
CurrentConfig *restclient.Config
}
const (
@@ -48,6 +56,7 @@ func (op *CmiiK8sOperator) checkAndBuildCmiiK8sOperator() {
log.Error(msg)
panic(msg)
}
op.DevConfig = devConfig
op.DevClient, err = kubernetes.NewForConfig(devConfig)
if err != nil {
panic(err.Error())
@@ -62,6 +71,7 @@ func (op *CmiiK8sOperator) checkAndBuildCmiiK8sOperator() {
log.Error(msg)
panic(msg)
}
op.CoreConfig = coreConfig
op.CoreClient, err = kubernetes.NewForConfig(coreConfig)
if err != nil {
panic(err.Error())
@@ -77,8 +87,10 @@ func (op *CmiiK8sOperator) changeOperatorEnv(cmiiEnv string) {
if strings.Contains(cmiiEnv, "dev") {
op.CurrentClient = op.DevClient
op.CurrentConfig = op.DevConfig
} else {
op.CurrentClient = op.CoreClient
op.CurrentConfig = op.CoreConfig
}
if strings.Contains(cmiiEnv, "dev") {
@@ -713,6 +725,51 @@ func (op *CmiiK8sOperator) PodDelete(cmiiEnv, podName string) bool {
}
func (op *CmiiK8sOperator) PodExec(cmiiEnv string, podInterface CmiiPodInterface, commandList []string) (stdout, stderr *bytes.Buffer) {
op.changeOperatorEnv(cmiiEnv)
client := op.CurrentClient
execRequest := client.CoreV1().RESTClient().
Post().
Resource("pods").
Name(podInterface.Name).
Namespace(op.CurrentNamespace).
SubResource("exec").
VersionedParams(&corev1.PodExecOptions{
Stdin: false,
Stdout: true,
Stderr: true,
TTY: false,
Container: podInterface.ContainerName,
Command: commandList,
}, scheme.ParameterCodec)
stdout = &bytes.Buffer{}
stderr = &bytes.Buffer{}
log.DebugF("PodExec] - [%s] [%s] exec %s, url %s", cmiiEnv, podInterface.Name, commandList, execRequest.URL())
exec, err := remotecommand.NewSPDYExecutor(op.CurrentConfig, "POST", execRequest.URL())
if err != nil {
log.ErrorF("[PodExec] - NewSPDYExecutor error => %s", err.Error())
return stdout, stderr
}
err = exec.Stream(remotecommand.StreamOptions{
Stdin: nil,
Stdout: stdout,
Stderr: stderr,
Tty: false,
TerminalSizeQueue: nil,
})
if err != nil {
log.ErrorF("[PodExec] - exec.Stream error => %s", err.Error())
os.Exit(1)
}
return stdout, stderr
}
func (op *CmiiK8sOperator) NodeAll(cmiiEnv string) (nodeListR []corev1.Node) {
op.changeOperatorEnv(cmiiEnv)
client := op.CurrentClient