[ 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

@@ -3,6 +3,7 @@ package k8s_exec
import (
"agent-go/executor"
"agent-go/utils"
"bufio"
"strings"
"time"
)
@@ -139,6 +140,44 @@ func FindPodNotHealthy(cmiiEnv string) (podList []CmiiPodInterface) {
return podList
}
func GetDeploymentGitInfoFromInnerEnv(cmiiEnv, appName string) (gitBranch, gitCommit string) {
// get app
podList := CmiiOperator.PodByAppName(cmiiEnv, appName)
// get pod
if podList == nil || len(podList) == 0 {
log.ErrorF("[GetDeploymentGitInfoFromInnerEnv] - get app pod error [%s] [%s]", cmiiEnv, appName)
return "", ""
}
// exec env
stdout, stderr := CmiiOperator.PodExec(cmiiEnv, podList[0], []string{"env"})
errLog := stderr.String()
if errLog != "" {
log.ErrorF("[GetDeploymentGitInfoFromInnerEnv] - pod Exec error %s", errLog)
return "", ""
}
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
// Get the current line
line := scanner.Text()
// Check if the current line contains the search term
if strings.HasPrefix(line, "GIT_BRANCH") {
gitBranch = strings.Split(line, "=")[1]
}
if strings.HasPrefix(line, "GIT_COMMIT") {
gitCommit = strings.Split(line, "=")[1]
}
}
// get out git info
return gitBranch, gitCommit
}
func FindCmiiMiddlewarePodInterface(cmiiEnv string) (podList []CmiiPodInterface) {
cmiiPodInterfaces := CmiiOperator.PodAllInterface(cmiiEnv)
@@ -316,6 +355,9 @@ func BackupAllDeploymentFromEnv(cmiiEnv string) bool {
allInterface := CmiiOperator.DeploymentAllInterface(cmiiEnv)
// must filter
allInterface = FilterAllCmiiAppSoft(allInterface)
filePath := "C:\\Users\\wddsh\\Documents\\IdeaProjects\\ProjectOctopus\\agent-go\\k8s_exec\\log\\all-" + CmiiOperator.CurrentNamespace + "-" + utils.TimeSplitFormatString() + ".txt"
log.InfoF("[BackupAllDeploymentFromEnv] - backup all image from %s => %s", CmiiOperator.CurrentNamespace, filePath)
@@ -342,6 +384,12 @@ func BackupAllDeploymentFromEnv(cmiiEnv string) bool {
for _, deploymentInterface := range allInterface {
if deploymentInterface.GitBranch == "" {
branch, commit := GetDeploymentGitInfoFromInnerEnv(deploymentInterface.Namespace, deploymentInterface.Name)
deploymentInterface.GitBranch = branch
deploymentInterface.GitCommit = commit
}
content := executor.BasicWordSpaceCompletion(deploymentInterface.Name, firstCol)
content = executor.BasicWordSpaceCompletion(content+deploymentInterface.ImageTag, secondCol)
content = executor.BasicWordSpaceCompletion(content+deploymentInterface.GitBranch, thirdCol)

View File

@@ -51,7 +51,7 @@ func TestFindCmiiMiddlewarePodInterface(t *testing.T) {
func TestBackupAllDeploymentFromEnv(t *testing.T) {
BackupAllDeploymentFromEnv("demo")
BackupAllDeploymentFromEnv(integration)
}

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

View File

@@ -2,6 +2,7 @@ package k8s_exec
import (
"agent-go/utils"
"bufio"
"fmt"
"github.com/magiconair/properties/assert"
"testing"
@@ -189,12 +190,6 @@ func TestCmiiK8sOperator_PodByAppName(t *testing.T) {
}
}
func TestCmiiDeploymentInterface_Convert(t *testing.T) {
log.DebugF("dadasdadasd")
}
func TestCmiiK8sOperator_PodFizz2(t *testing.T) {
start := time.Now()
@@ -232,6 +227,36 @@ func TestCmiiK8sOperator_PodByNodeName(t *testing.T) {
}
}
func TestCmiiK8sOperator_PodExec(t *testing.T) {
podList := CmiiOperator.PodByAppName(devFlight, "cmii-uav-gateway")
stdout, stderr := CmiiOperator.PodExec(devFlight, podList[0], []string{
"env",
})
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
// Get the current line
line := scanner.Text()
// Check if the current line contains the search term
fmt.Println(line)
}
utils.SplitLinePrint()
scanner = bufio.NewScanner(stderr)
for scanner.Scan() {
// Get the current line
line := scanner.Text()
// Check if the current line contains the search term
fmt.Println(line)
}
}
func TestCmiiK8sOperator_DeploymentStatusCheck(t *testing.T) {
cmiiEnv := "devflight"