[ 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

@@ -1986,15 +1986,6 @@ func (op *AgentOsOperator) chronyToMaster(args []string) [][]string {
func (op *AgentOsOperator) chronyToMasterExec(args []string) (bool, []string) {
if op.IsOsTypeCentOS {
// todo
return false, []string{
"[chronyToMasterExec] - do not support none ubuntu os !",
}
}
parseIP := net.ParseIP(args[0])
if parseIP == nil {
return false, []string{
@@ -2002,6 +1993,38 @@ func (op *AgentOsOperator) chronyToMasterExec(args []string) (bool, []string) {
}
}
if op.IsOsTypeCentOS {
if !op.CanAccessInternet {
return op.chronyToMasterCentOsOfflineExec(args)
}
// install ntp
installSoftwares, i := BasicInstallSoftwares(op.InstallCommandPrefix, true, "ntp")
if !installSoftwares {
msg := "[chronyToMasterExec] - centos install ntp error!"
log.ErrorF(msg)
return false, append(i, msg)
}
BasicSystemdUp("ntpd")
HardCodeCommandExecutor("ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime")
ok, resultLog := AllCommandExecutor([]string{
"ntpdate",
args[0],
})
if !ok {
sprintf := fmt.Sprintf("[chronyToMasterExec] - centos ntpdate to %s error", args[0])
log.Error(sprintf)
return false, append(resultLog, sprintf)
}
return true, []string{
"[chronyToMasterExec] - chrony to master success !",
}
}
// modify the systemd-timesync.service
ntpFile := "/etc/systemd/timesyncd.conf"
AllCommandExecutor([]string{
@@ -2051,6 +2074,13 @@ func (op *AgentOsOperator) chronyToMasterExec(args []string) (bool, []string) {
}
}
func (op *AgentOsOperator) chronyToMasterCentOsOfflineExec(args []string) (bool, []string) {
// todo
return true, nil
}
func (op *AgentOsOperator) installZSH() [][]string {
installZSHFunc := [][]string{

View File

@@ -11,10 +11,12 @@ require (
github.com/spf13/viper v1.15.0
github.com/streadway/amqp v1.1.0
go.uber.org/zap v1.24.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.25.2
k8s.io/apimachinery v0.26.2
k8s.io/client-go v0.25.2
sigs.k8s.io/yaml v1.3.0
)
require (
@@ -47,6 +49,7 @@ require (
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
@@ -79,11 +82,9 @@ require (
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

View File

@@ -40,6 +40,7 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
@@ -57,6 +58,7 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153 h1:yUdfgN0XgIJw7foRItutHYUIhlcKzcSf5vDpdhQAKTc=
github.com/emicklei/go-restful/v3 v3.8.0 h1:eCZ8ulSerjdAiaNpF7GxXIE7ZCMo1moN1qX+S609eVw=
github.com/emicklei/go-restful/v3 v3.8.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@@ -213,6 +215,7 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
@@ -262,6 +265,8 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mittwald/goharbor-client/v5 v5.4.2 h1:1wH49SsBU9wc5czO5s1yEtnMODCbqJMk6ed0Byu0cpQ=
github.com/mittwald/goharbor-client/v5 v5.4.2/go.mod h1:fuOkU/lHZI441jF39mH6bqWMVlVYWJIYZAxpP5B2XzU=
github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8=
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

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"

View File

@@ -44,8 +44,8 @@ func GetDockerInfo() {
utils.BeautifulPrint(cpuDocker)
usageDocker, _ := docker.CgroupCPUUsageDocker(dockerStat.ContainerID)
utils.BeautifulPrint(usageDocker)
//usageDocker, _ := docker.CgroupCPUUsageDocker(dockerStat.ContainerID)
//utils.BeautifulPrint(usageDocker)
mem, _ := docker.CgroupMemDocker(dockerStat.ContainerID)
utils.BeautifulPrint(mem)