diff --git a/agent-go/executor/BasicFunction.go b/agent-go/executor/BasicFunction.go index 3a49b13..26499df 100644 --- a/agent-go/executor/BasicFunction.go +++ b/agent-go/executor/BasicFunction.go @@ -1,6 +1,7 @@ package executor import ( + "bufio" "bytes" "fmt" "io" @@ -115,6 +116,40 @@ func BasicGrepItemInFile(item string, fileName string) bool { return false } +func BasicFindContentInFile(content string, fileName string) bool { + // Open the file + file, err := os.Open(fileName) + if err != nil { + log.ErrorF("[BasicFindContentInFile] - file not exits !") + return false + } + defer file.Close() + + // Create a scanner to read the file line by line + scanner := bufio.NewScanner(file) + + // Set the split function for the scanner + scanner.Split(bufio.ScanLines) + + // Iterate over the lines of the file + for scanner.Scan() { + // Get the current line + line := scanner.Text() + + // Check if the current line contains the search term + if strings.Contains(line, content) { + return true + } + } + + // Check for any errors that occurred during scanning + if err := scanner.Err(); err != nil { + log.ErrorF("[BasicFindContentInFile] - scanner error ! %s", err.Error()) + } + + return false +} + func BasicDockerImageExists(imageName, imageVersion string) bool { if !BasicCommandExistByPath("docker") { diff --git a/agent-go/k8s_exec/CmiiK8sConfig.go b/agent-go/k8s_exec/CmiiK8sConfig.go index dceb96e..8da957d 100644 --- a/agent-go/k8s_exec/CmiiK8sConfig.go +++ b/agent-go/k8s_exec/CmiiK8sConfig.go @@ -23,6 +23,77 @@ var CmiiFrontendAppName = []string{ "cmii-uav-platform-visualization", } +var CmiiBackendAppMap = map[string]int32{ + "cmii-admin-data": 2, + "cmii-admin-gateway": 2, + "cmii-admin-user": 2, + "cmii-app-release": 2, + "cmii-open-gateway": 2, + "cmii-suav-supervision": 2, + "cmii-uav-airspace": 2, + "cmii-uav-alarm": 2, + "cmii-uav-autowaypoint": 1, + "cmii-uav-brain": 2, + "cmii-uav-cloud-live": 2, + "cmii-uav-clusters": 0, + "cmii-uav-cms": 2, + "cmii-uav-data-post-process": 2, + "cmii-uav-depotautoreturn": 1, + "cmii-uav-developer": 2, + "cmii-uav-device": 2, + "cmii-uav-emergency": 2, + "cmii-uav-gateway": 2, + "cmii-uav-gis-server": 1, + "cmii-uav-grid-datasource": 2, + "cmii-uav-grid-engine": 2, + "cmii-uav-grid-manage": 2, + "cmii-uav-industrial-portfolio": 2, + "cmii-uav-integration": 2, + "cmii-uav-kpi-monitor": 2, + "cmii-uav-logger": 2, + "cmii-uav-material-warehouse": 2, + "cmii-uav-mission": 2, + "cmii-uav-mqtthandler": 2, + "cmii-uav-notice": 2, + "cmii-uav-oauth": 2, + "cmii-uav-process": 2, + "cmii-uav-surveillance": 2, + "cmii-uav-threedsimulation": 2, + "cmii-uav-tower": 2, + "cmii-uav-user": 2, + "cmii-uav-waypoint": 2, + "helm-live-op-v2": 1, +} + +var CmiiFrontendAppMap = map[string]int32{ + "cmii-suav-platform-supervision": 1, + "cmii-suav-platform-supervisionh5": 1, + "cmii-uav-platform": 1, + "cmii-uav-platform-ai-brain": 1, + "cmii-uav-platform-armypeople": 1, + "cmii-uav-platform-base": 1, + "cmii-uav-platform-cms-portal": 1, + "cmii-uav-platform-detection": 1, + "cmii-uav-platform-emergency-rescue": 0, + "cmii-uav-platform-hljtt": 1, + "cmii-uav-platform-jiangsuwenlv": 1, + "cmii-uav-platform-logistics": 1, + "cmii-uav-platform-media": 1, + "cmii-uav-platform-multiterminal": 1, + "cmii-uav-platform-mws": 1, + "cmii-uav-platform-oms": 1, + "cmii-uav-platform-open": 1, + "cmii-uav-platform-qingdao": 1, + "cmii-uav-platform-qinghaitourism": 2, + "cmii-uav-platform-security": 1, + "cmii-uav-platform-securityh5": 1, + "cmii-uav-platform-seniclive": 0, + "cmii-uav-platform-share": 1, + "cmii-uav-platform-splice": 1, + "cmii-uav-platform-threedsimulation": 1, + "cmii-uav-platform-visualization": 0, +} + var CmiiBackendAppName = []string{ "cmii-uav-gateway", "cmii-uav-oauth", diff --git a/agent-go/k8s_exec/CmiiK8sOperator.go b/agent-go/k8s_exec/CmiiK8sOperator.go index 65242a4..8ddfd54 100644 --- a/agent-go/k8s_exec/CmiiK8sOperator.go +++ b/agent-go/k8s_exec/CmiiK8sOperator.go @@ -8,6 +8,7 @@ import ( ) var CmiiOperator = CmiiK8sOperator{} +var updateLogPath = "C:\\Users\\wddsh\\Documents\\IdeaProjects\\ProjectOctopus\\agent-go\\k8s_exec\\log\\cmii-update-log.txt" // FindDeploymentRestartCountGreaterThanN 重启次数大于N的所有Deployment func FindDeploymentRestartCountGreaterThanN(cmiiEnv string, restartCount int32) []CmiiDeploymentInterface { @@ -38,7 +39,7 @@ func FindDeploymentRestartCountGreaterThanN(cmiiEnv string, restartCount int32) cmiiDeploymentInterface := CmiiDeploymentInterface{} index := 0 - //log.DebugF("[FindDeploymentRestartCountGreaterThanN] - restart map is => %v", restartMap) + log.DebugF("[FindDeploymentRestartCountGreaterThanN] - restart map is => %v", restartMap) // find deployment convert to interface for key, value := range restartMap { deployment := CmiiOperator.DeploymentExist(cmiiEnv, key) @@ -55,6 +56,22 @@ func FindDeploymentRestartCountGreaterThanN(cmiiEnv string, restartCount int32) return result[:index] } +func FindDeploymentReplicasSmallerThanN(cmiiEnv string, replicasMin int32) (deploymentList []CmiiDeploymentInterface) { + + // get all deployments + cmiiDeploymentInterfaces := CmiiOperator.DeploymentAllInterface(cmiiEnv) + + // filter + for _, deploymentInterface := range cmiiDeploymentInterfaces { + if deploymentInterface.Replicas <= replicasMin { + deploymentList = append(deploymentList, deploymentInterface) + } + } + + // convert + return deploymentList +} + func RestartDeploymentFromList(deploymentList []CmiiDeploymentInterface) bool { result := true @@ -86,50 +103,147 @@ func RestartCmiiBackendDeployment(cmiiEnv string) { } -// GetCmiiAllDeploymentFromDemo 从DEMO提取全部的CMII的应用 -func GetCmiiAllDeploymentFromDemo() { - allInterface := CmiiOperator.DeploymentAllInterface("demo") +func UpdateCmiiDeploymentImageTag(cmiiEnv, appName, newTag string) bool { - filePath := "C:\\Users\\wddsh\\Documents\\IdeaProjects\\ProjectOctopus\\agent-go\\k8s_exec\\all-cmii-image.txt" + deployment := CmiiOperator.DeploymentExist(cmiiEnv, appName) + if deployment == nil { + log.ErrorF("[UpdateCmiiDeploymentImageTag] - [%s] [%s] not exists !", cmiiEnv, appName) + return false + } + + deploymentInterface := CmiiDeploymentInterface{} + cmiiDeploymentInterface := deploymentInterface.Convert(*deployment) + // check if need to update + if cmiiDeploymentInterface.ImageTag == newTag { + log.DebugF("[UpdateCmiiDeploymentImageTag] - [%s] [%s] image tag are the same ! no need to update !", cmiiEnv, appName) + return true + } + + content := executor.BasicWordSpaceCompletion(utils.TimeSplitFormatString()+" "+cmiiDeploymentInterface.Namespace, 45) + content = executor.BasicWordSpaceCompletion(content+cmiiDeploymentInterface.Name, 85) + content = executor.BasicWordSpaceCompletion(content+cmiiDeploymentInterface.ImageTag, 105) + content = content + newTag + "\n" + + log.DebugF("[UpdateCmiiDeploymentImageTag] - prepare to update [%s]!", content) + + // update + tag := CmiiOperator.DeploymentUpdateTag(cmiiEnv, appName, newTag) + if !tag { + log.ErrorF("[UpdateCmiiDeploymentImageTag] - [%s] update failed !", content) + return false + } + + // append log + executor.BasicAppendContentToFile(content, updateLogPath) + + // re-get from env + time.Sleep(time.Second) + deploy := CmiiOperator.DeploymentOneInterface(cmiiEnv, appName) + if deploy == nil { + log.ErrorF("[UpdateCmiiDeploymentImageTag] - unknown error happened ! [%s] [%s] not exists !", cmiiEnv, appName) + return false + } + + // log + log.InfoF("[UpdateCmiiDeploymentImageTag] - [%s] success ! real image tag are [%s] ", content, deploy.Image) + return true +} + +func RollBackCmiiDeploymentFromUpdateLog(updateLog string) bool { + + if !executor.BasicFindContentInFile(updateLog, updateLogPath) { + log.ErrorF("[RollBackCmiiDeploymentFromUpdateLog] - [%s] no this update log ! use update instead ! => ", updateLog) + return false + } + + split := strings.Split(updateLog, " ") + index := 0 + cmiiEnv := "" + appName := "" + fromTag := "" + newTag := "" + for _, s := range split { + if s != "" { + if index == 1 { + cmiiEnv = s + } else if index == 2 { + appName = s + } else if index == 3 { + fromTag = s + } else if index == 4 { + newTag = s + } + index++ + } + } + + log.InfoF("[RollBackCmiiDeploymentFromUpdateLog] - rollback [%s] [%s] from [%s] to [%s]", cmiiEnv, appName, newTag, fromTag) + rollback := UpdateCmiiDeploymentImageTag(cmiiEnv, appName, fromTag) + + return rollback +} + +// BackupAllDeploymentFromEnv 从DEMO提取全部的CMII的应用 +func BackupAllDeploymentFromEnv(cmiiEnv string) bool { + + allInterface := CmiiOperator.DeploymentAllInterface(cmiiEnv) + + 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) firstCol := 0 secondCol := 0 thirdCol := 0 + fourthCol := 0 for _, deploymentInterface := range allInterface { - if strings.Contains(deploymentInterface.Name, "proxy") { - continue - } firstCol = utils.MaxInt(len(deploymentInterface.Name), firstCol) - - for name, image := range deploymentInterface.ContainerImageMap { - secondCol = utils.MaxInt(len(name), secondCol) - thirdCol = utils.MaxInt(len(image), thirdCol) - } + secondCol = utils.MaxInt(len(deploymentInterface.ImageTag), secondCol) + thirdCol = utils.MaxInt(len(deploymentInterface.GitBranch), thirdCol) + fourthCol = utils.MaxInt(len(deploymentInterface.GitCommit), fourthCol) } - firstCol += 4 - secondCol += 4 + firstCol += 2 + secondCol += 2 secondCol += firstCol - thirdCol += 4 + thirdCol += 2 thirdCol += secondCol + fourthCol += 2 + fourthCol += thirdCol for _, deploymentInterface := range allInterface { - if strings.Contains(deploymentInterface.Name, "proxy") { - continue - } content := executor.BasicWordSpaceCompletion(deploymentInterface.Name, firstCol) + content = executor.BasicWordSpaceCompletion(content+deploymentInterface.ImageTag, secondCol) + content = executor.BasicWordSpaceCompletion(content+deploymentInterface.GitBranch, thirdCol) + content = executor.BasicWordSpaceCompletion(content+deploymentInterface.GitCommit, fourthCol) + content += "\n" - for name, image := range deploymentInterface.ContainerImageMap { - content = executor.BasicWordSpaceCompletion(content+name, secondCol) - content = executor.BasicWordSpaceCompletion(content+image, thirdCol) - content += "\n" + if !executor.BasicAppendContentToFile(content, filePath) { + log.ErrorF("[BackupAllDeploymentFromEnv] - write to file %s error with contend %s", filePath, content) + return false + } - if !executor.BasicAppendContentToFile(content, filePath) { - log.ErrorF("[GetCmiiAllDeploymentFromDemo] - write to file %s error with contend %s", filePath, content) - } + } + + return true +} + +func BackupAllCmiiDeploymentToMap(cmiiEnv string) (backendMap, frontendMap map[string]int32) { + + allInterface := CmiiOperator.DeploymentAllInterface(cmiiEnv) + + backendMap = make(map[string]int32, len(allInterface)) + frontendMap = make(map[string]int32, len(allInterface)) + + for _, deploymentInterface := range allInterface { + if strings.Contains(deploymentInterface.Name, "platform") { + frontendMap[deploymentInterface.Name] = deploymentInterface.Replicas + } else { + backendMap[deploymentInterface.Name] = deploymentInterface.Replicas } } + return backendMap, frontendMap } diff --git a/agent-go/k8s_exec/CmiiK8sOperator_test.go b/agent-go/k8s_exec/CmiiK8sOperator_test.go index 0007143..78141b9 100644 --- a/agent-go/k8s_exec/CmiiK8sOperator_test.go +++ b/agent-go/k8s_exec/CmiiK8sOperator_test.go @@ -2,6 +2,7 @@ package k8s_exec import ( "agent-go/utils" + "github.com/magiconair/properties/assert" "testing" ) @@ -17,7 +18,42 @@ func TestFindDeploymentRestartCountGreaterThanN(t *testing.T) { } -func TestGetCmiiAllDeploymentFromDemo(t *testing.T) { +func TestFindDeploymentReplicasSmallerThanN(t *testing.T) { + + deploymentReplicasSmallerThanN := FindDeploymentReplicasSmallerThanN("devflight", 0) + + for _, deploymentInterface := range deploymentReplicasSmallerThanN { + println() + utils.BeautifulPrint(deploymentInterface) + println() + } - GetCmiiAllDeploymentFromDemo() +} + +func TestGetCmiiAllDeploymentFromEnv(t *testing.T) { + + BackupAllDeploymentFromEnv("uat") + +} + +func TestBackupAllCmiiDeploymentToMap(t *testing.T) { + backendMap, frontendMap := BackupAllCmiiDeploymentToMap("demo") + + utils.BeautifulPrint(backendMap) + utils.BeautifulPrint(frontendMap) + +} + +func TestUpdateCmiiDeploymentImageTag(t *testing.T) { + + //tag := UpdateCmiiDeploymentImageTag("devflight", "cmii-uav-depotautoreturn", "123sdsa45678") + tag := UpdateCmiiDeploymentImageTag("demo", "cmii-uav-platform", "5.2.0-011004") + + assert.Equal(t, tag, true, "update image tag failed !") +} + +func TestRollBackCmiiDeploymentFromUpdateLog(t *testing.T) { + updateLog := RollBackCmiiDeploymentFromUpdateLog("2024-01-10-14-37-07 uavcloud-devflight cmii-uav-depotautoreturn 12345678 123sdsa45678") + + assert.Equal(t, updateLog, true, "roll back from update log failed !") } diff --git a/agent-go/k8s_exec/CmiiStatus.go b/agent-go/k8s_exec/CmiiStatus.go index f213e11..6046d2d 100644 --- a/agent-go/k8s_exec/CmiiStatus.go +++ b/agent-go/k8s_exec/CmiiStatus.go @@ -3,6 +3,7 @@ package k8s_exec import ( v1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" + "strings" ) type CmiiInterfaceConvert interface { @@ -17,6 +18,11 @@ type CmiiPodInterface struct { HostIP string NodeName string RestartCountMap map[string]int32 + ContainerName string + Image string + ImageTag string + GitBranch string + GitCommit string } type CmiiDeploymentInterface struct { @@ -25,6 +31,11 @@ type CmiiDeploymentInterface struct { AvailableReplicas int32 Replicas int32 ContainerImageMap map[string]string + ContainerName string + Image string + ImageTag string + GitBranch string + GitCommit string StatusOk bool } @@ -33,8 +44,24 @@ func (deploy CmiiDeploymentInterface) Convert(deployment v1.Deployment) CmiiDepl containers := deployment.Spec.Template.Spec.Containers containerImageMap := make(map[string]string, len(containers)) + if len(containers) > 1 { + log.WarnF("[CmiiDeploymentInterface Convert] - deployment [%s] [%s] container greater than one !", deployment.Namespace, deployment.Name) + } for _, container := range containers { containerImageMap[container.Name] = container.Image + deploy.Image = container.Image + deploy.ContainerName = container.Name + deploy.ImageTag = strings.Split(container.Image, ":")[1] + + for _, envVar := range container.Env { + if strings.HasPrefix(envVar.Name, "GIT_BRANCH") { + deploy.GitBranch = envVar.Value + } + + if strings.HasPrefix(envVar.Name, "GIT_COMMIT") { + deploy.GitCommit = envVar.Value + } + } } deploy.Name = deployment.Name @@ -51,12 +78,30 @@ func (pod CmiiPodInterface) Convert(podDetail corev1.Pod) CmiiPodInterface { containers := podDetail.Spec.Containers containerImageMap := make(map[string]string, len(containers)) + if len(containers) > 1 { + log.WarnF("[CmiiDeploymentInterface Convert] - pod [%s] [%s] container greater than one !", podDetail.Namespace, podDetail.Name) + } for _, container := range containers { containerImageMap[container.Name] = container.Image + + pod.Image = container.Image + pod.ContainerName = container.Name + pod.ImageTag = strings.Split(container.Image, ":")[1] + + for _, envVar := range container.Env { + if strings.HasPrefix(envVar.Name, "GIT_BRANCH") { + pod.GitBranch = envVar.Value + } + + if strings.HasPrefix(envVar.Name, "GIT_COMMIT") { + pod.GitCommit = envVar.Value + } + } } containerStatuses := podDetail.Status.ContainerStatuses containerStatusMap := make(map[string]int32, len(containerStatuses)) + for _, containerStatus := range containerStatuses { containerStatusMap[containerStatus.Name] = containerStatus.RestartCount } diff --git a/agent-go/k8s_exec/K8sOperator.go b/agent-go/k8s_exec/K8sOperator.go index 7a5e731..21854e3 100644 --- a/agent-go/k8s_exec/K8sOperator.go +++ b/agent-go/k8s_exec/K8sOperator.go @@ -31,6 +31,7 @@ const ( devOperation = "uavcloud-devoperation" validation = "uavcloud-feature" integration = "uavcloud-test" + uat = "uavcloud-uat" demo = "uavcloud-demo" workerThread = 4 ) @@ -96,6 +97,10 @@ func (op *CmiiK8sOperator) changeOperatorEnv(cmiiEnv string) { op.CurrentNamespace = validation } + if strings.Contains(cmiiEnv, "uat") { + op.CurrentNamespace = uat + } + if strings.Contains(cmiiEnv, "demo") { op.CurrentNamespace = demo } @@ -236,11 +241,24 @@ func (op *CmiiK8sOperator) DeploymentAllInterface(cmiiEnv string) []CmiiDeployme index := 0 for deployment := range ccc { + // 过滤所有非CMII的内容 + if strings.Contains(deployment.Name, "proxy") { + continue + } + if strings.Contains(deployment.Name, "minio") { + continue + } + if strings.HasPrefix(deployment.Name, "helm-live-rtsp") { + continue + } + if strings.Contains(deployment.Name, "nfs") { + continue + } results[index] = deployment index++ } - return results + return results[:index] } func (op *CmiiK8sOperator) DeploymentFizz(cmiiEnv, appFizz string) (fizzDeployment []v1.Deployment) { @@ -262,7 +280,7 @@ func (op *CmiiK8sOperator) DeploymentFizz(cmiiEnv, appFizz string) (fizzDeployme } func (op *CmiiK8sOperator) DeploymentExist(cmiiEnv, appName string) (exists *v1.Deployment) { - + op.changeOperatorEnv(cmiiEnv) client := op.CurrentClient deployment, err := client.AppsV1().Deployments(op.CurrentNamespace).Get(context.TODO(), appName, metav1.GetOptions{}) @@ -273,6 +291,23 @@ func (op *CmiiK8sOperator) DeploymentExist(cmiiEnv, appName string) (exists *v1. return deployment } + +func (op *CmiiK8sOperator) DeploymentOneInterface(cmiiEnv, appName string) (deploy *CmiiDeploymentInterface) { + op.changeOperatorEnv(cmiiEnv) + client := op.CurrentClient + deploymentInterface := CmiiDeploymentInterface{} + + deployment, err := client.AppsV1().Deployments(op.CurrentNamespace).Get(context.TODO(), appName, metav1.GetOptions{}) + if err != nil { + log.ErrorF("[DeploymentExist] - deployments [%s] [%s] not exists ! %s", cmiiEnv, appName, err.Error()) + return nil + } + + convert := deploymentInterface.Convert(*deployment) + + return &convert +} + func (op *CmiiK8sOperator) DeploymentScale(cmiiEnv, appFizz string, scaleCount int32) bool { deploymentFizz := op.DeploymentFizz(cmiiEnv, appFizz) @@ -309,6 +344,41 @@ func (op *CmiiK8sOperator) DeploymentScale(cmiiEnv, appFizz string, scaleCount i return true } +func (op *CmiiK8sOperator) DeploymentUpdateTag(cmiiEnv, appName, newTag string) bool { + + if newTag == "" { + log.WarnF("[DeploymentUpdateTag] - can not update image tag to null!") + return false + } + + deployment := op.DeploymentExist(cmiiEnv, appName) + if deployment == nil { + return false + } + + containers := deployment.Spec.Template.Spec.Containers + if len(containers) == 1 { + // only update this kind + container := containers[0] + split := strings.Split(container.Image, ":") + + container.Image = split[0] + ":" + newTag + log.InfoF("[DeploymentUpdateTag] - update [%s] [%s] from [%s] to [%s]", op.CurrentNamespace, appName, split[1], container.Image) + + // re assign + deployment.Spec.Template.Spec.Containers[0] = container + + // update + _, err := op.CurrentClient.AppsV1().Deployments(deployment.Namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{}) + if err != nil { + log.ErrorF("[DeploymentUpdateTag] - update [%s] [%s] from [%s] to [%s] error ! %s", op.CurrentNamespace, appName, split[1], container.Image, err.Error()) + return false + } + } + + return true +} + func (op *CmiiK8sOperator) ReplicaSet(cmiiEnv, replicaSetName string) *v1.ReplicaSet { op.changeOperatorEnv(cmiiEnv) diff --git a/agent-go/k8s_exec/K8sOperator_test.go b/agent-go/k8s_exec/K8sOperator_test.go index e44ada7..e752cb6 100644 --- a/agent-go/k8s_exec/K8sOperator_test.go +++ b/agent-go/k8s_exec/K8sOperator_test.go @@ -52,15 +52,17 @@ func TestCmiiK8sOperator_DeploymentAllInterface(t *testing.T) { func TestCmiiK8sOperator_DeploymentFizz(t *testing.T) { start := time.Now() - deploymentFizz := CmiiOperator.DeploymentFizz("devflight", "") + deploymentFizz := CmiiOperator.DeploymentFizz("demo", "cmii-uav-platform") elapsed := time.Since(start).Milliseconds() fmt.Printf("执行耗时: %d ms\n", elapsed) t.Logf("deployment list lenght is => %d", len(deploymentFizz)) - deploymentInterface := CmiiDeploymentInterface{} + cmiiDeploymentInterface := CmiiDeploymentInterface{} for _, pod := range deploymentFizz { - convert := deploymentInterface.Convert(pod) + utils.BeautifulPrint(pod) + + convert := cmiiDeploymentInterface.Convert(pod) utils.BeautifulPrint(convert) } @@ -71,12 +73,29 @@ func TestCmiiK8sOperator_DeploymentFizz(t *testing.T) { func TestCmiiK8sOperator_DeploymentScale(t *testing.T) { start := time.Now() - CmiiOperator.DeploymentScale("devflight", "cmii-uav-gis-server", 0) + CmiiOperator.DeploymentScale("devflight", "cmii-uav-gis-server", 1) elapsed := time.Since(start).Milliseconds() fmt.Printf("执行耗时: %d ms\n", elapsed) } +func TestCmiiK8sOperator_DeploymentUpdateTag(t *testing.T) { + + start := time.Now() + CmiiOperator.DeploymentUpdateTag("demo", "cmii-uav-platform", "5.2.0-011001") + elapsed := time.Since(start).Milliseconds() + fmt.Printf("执行耗时: %d ms\n", elapsed) +} + +func TestCmiiK8sOperator_DeploymentOneInterface(t *testing.T) { + start := time.Now() + deploy := CmiiOperator.DeploymentOneInterface("devflight", "cmii-uav-depotautoreturn") + elapsed := time.Since(start).Milliseconds() + fmt.Printf("执行耗时: %d ms\n", elapsed) + + utils.BeautifulPrint(*deploy) +} + func TestCmiiK8sOperator_PodAll(t *testing.T) { start := time.Now() podList := CmiiOperator.PodAll("devflight") @@ -93,16 +112,18 @@ func TestCmiiK8sOperator_PodAll(t *testing.T) { func TestCmiiK8sOperator_PodFizz(t *testing.T) { start := time.Now() - podList := CmiiOperator.PodFizz("devflight", "notice") + podList := CmiiOperator.PodFizz("devflight", "cmii-uav-depotautoreturn") elapsed := time.Since(start).Milliseconds() fmt.Printf("执行耗时: %d ms\n", elapsed) t.Logf("pod list lenght is => %d", len(podList)) + cmiiPodInterface := CmiiPodInterface{} for _, pod := range podList { println() utils.BeautifulPrint(pod) println() - break + convert := cmiiPodInterface.Convert(pod) + utils.BeautifulPrint(convert) } } diff --git a/agent-go/tmp/cmii-frontend-app.yaml b/agent-go/tmp/cmii-frontend-app.yaml new file mode 100644 index 0000000..e500946 --- /dev/null +++ b/agent-go/tmp/cmii-frontend-app.yaml @@ -0,0 +1,202 @@ +{ + "metadata": { + "name": "cmii-uav-platform-splice", + "namespace": "uavcloud-devflight", + "selfLink": "/apis/apps/v1/namespaces/uavcloud-devflight/deployments/cmii-uav-platform-splice", + "uid": "c6c53d74-adbf-4507-a642-06fa695d9139", + "resourceVersion": "468805453", + "generation": 2, + "creationTimestamp": "2023-04-23T08:00:32Z", + "labels": { + "app.kubernetes.io/managed-by": "Helm", + "app.kubernetes.io/version": "4.1.2-snapshot", + "cmii.app": "cmii-uav-platform-splice", + "cmii.modules": "3dvisual", + "cmii.type": "frontend", + "helm.sh/chart": "cmii-uav-platform-splice-4.1.2-snapshot" + } + }, + "spec": { + "replicas": 2, + "selector": { + "matchLabels": { + "cmii.app": "cmii-uav-platform-splice", + "cmii.type": "frontend" + } + }, + "template": { + "metadata": { + "creationTimestamp": null, + "labels": { + "cmii.app": "cmii-uav-platform-splice", + "cmii.type": "frontend" + } + }, + "spec": { + "volumes": [ + { + "name": "nginx-conf", + "configMap": { + "name": "nginx-cm", + "items": [ + { + "key": "nginx.conf", + "path": "nginx.conf" + } + ], + "defaultMode": 420 + } + }, + { + "name": "default-nginx-conf", + "configMap": { + "name": "default-nginx-cm", + "items": [ + { + "key": "default.conf", + "path": "default.conf" + } + ], + "defaultMode": 420 + } + }, + { + "name": "tenant-prefix", + + "configMap": { + "name": "tenant-prefix-splice", + "items": [ + { + "key": "ingress-config.js", + "path": "ingress-config.js" + } + ], + "defaultMode": 420 + } + } + ], + "containers": [ + { + "name": "cmii-uav-platform-splice", + "image": "harbor.cdcyy.com.cn/cmii/cmii-uav-platform-splice:4.1.2-snapshot", + "env": [ + { + "name": "GIT_BRANCH", + + "value": "feature/UAVCLOUD-20939" + }, + { + "name": "GIT_COMMIT", + + "value": "c235350212bf2359d550faecab78ff02047bca37" + }, + { + "name": "K8S_NAMESPACE", + "value": "uavcloud-devflight" + }, + { + "name": "APPLICATION_NAME", + "value": "cmii-uav-platform-splice" + } + ], + "resources": { + "limits": { + "cpu": "2", + "memory": "2Gi" + }, + "requests": { + "cpu": "100m", + "memory": "200Mi" + } + }, + "volumeMounts": [ + { + "name": "nginx-conf", + + "mountPath": "/usr/local/nginx/conf/nginx.conf", + "subPath": "nginx.conf" + }, + { + "name": "default-nginx-conf", + "mountPath": "/etc/nginx/conf.d/default.conf", + "subPath": "default.conf" + }, + { + "name": "tenant-prefix", + "mountPath": "/home/cmii-platform/dist/ingress-config.js", + "subPath": "ingress-config.js" + } + ], + "terminationMessagePath": "/dev/termination-log", + "terminationMessagePolicy": "File", + "imagePullPolicy": "Always" + } + ], + "restartPolicy": "Always", + "terminationGracePeriodSeconds": 30, + "dnsPolicy": "ClusterFirst", + + "securityContext": { }, + "imagePullSecrets": [ + { + "name": "harborsecret" + } + ], + "affinity": { + "nodeAffinity": { + "requiredDuringSchedulingIgnoredDuringExecution": { + "nodeSelectorTerms": [ + { + "matchExpressions": [ + { + "key": "uavcloud.affinity", + "operator": "In", + "values": [ + "common" + ] + } + ] + } + ] + } + } + }, + "schedulerName": "default-scheduler" + } + }, + "strategy": { + "type": "RollingUpdate", + "rollingUpdate": { + "maxUnavailable": 1, + "maxSurge": "25%" + } + }, + "revisionHistoryLimit": 10, + "progressDeadlineSeconds": 600 + }, + "status": { + "observedGeneration": 2, + "replicas": 2, + "updatedReplicas": 2, + "readyReplicas": 2, + "availableReplicas": 2, + "conditions": [ + { + "type": "Available", + "status": "True", + "lastUpdateTime": "2023-04-23T08:00:38Z", + "lastTransitionTime": "2023-04-23T08:00:38Z", + "reason": "MinimumReplicasAvailable", + "message": "Deployment has minimum availability." + }, + { + "type": "Progressing", + "status": "True", + "lastUpdateTime": "2023-04-23T08:00:38Z", + "lastTransitionTime": "2023-04-23T08:00:32Z", + "reason": "NewReplicaSetAvailable", + "message": "ReplicaSet \"cmii-uav-platform-splice-5bd5c65b97\" has successfully progressed." + } + ] + } +} diff --git a/agent-go/tmp/cmii-frontend-pod.yaml b/agent-go/tmp/cmii-frontend-pod.yaml new file mode 100644 index 0000000..5abdd7e --- /dev/null +++ b/agent-go/tmp/cmii-frontend-pod.yaml @@ -0,0 +1,242 @@ +{ + "metadata": { + "name": "cmii-uav-platform-splice-5bd5c65b97-2fqrt", + "generateName": "cmii-uav-platform-splice-5bd5c65b97-", + "namespace": "uavcloud-devflight", + "selfLink": "/api/v1/namespaces/uavcloud-devflight/pods/cmii-uav-platform-splice-5bd5c65b97-2fqrt", + "uid": "613b0ccc-9f27-4a90-a6c3-0c7e7a43a979", + "resourceVersion": "341073286", + "creationTimestamp": "2023-04-23T08:00:32Z", + "labels": { + "cmii.app": "cmii-uav-platform-splice", + "cmii.type": "frontend", + "pod-template-hash": "5bd5c65b97" + }, + "ownerReferences": [ + { + "apiVersion": "apps/v1", + "kind": "ReplicaSet", + "name": "cmii-uav-platform-splice-5bd5c65b97", + "uid": "1dc4c64b-bffe-4358-be0a-fe7e0ecc71c0", + "controller": true, + "blockOwnerDeletion": true + } + ] + }, + "spec": { + "volumes": [ + { + "name": "nginx-conf", + "configMap": { + "name": "nginx-cm", + "items": [ + { + "key": "nginx.conf", + "path": "nginx.conf" + } + ], + "defaultMode": 420 + } + }, + { + "name": "default-nginx-conf", + "configMap": { + "name": "default-nginx-cm", + "items": [ + { + "key": "default.conf", + "path": "default.conf" + } + ], + "defaultMode": 420 + } + }, + { + "name": "tenant-prefix", + "configMap": { + "name": "tenant-prefix-splice", + "items": [ + { + "key": "ingress-config.js", + "path": "ingress-config.js" + } + ], + "defaultMode": 420 + } + }, + { + "name": "default-token-hzddz", + "secret": { + "secretName": "default-token-hzddz", + "defaultMode": 420 + } + } + ], + "containers": [ + { + "name": "cmii-uav-platform-splice", + + "image": "harbor.cdcyy.com.cn/cmii/cmii-uav-platform-splice:4.1.2-snapshot", + "env": [ + { + "name": "GIT_BRANCH", + "value": "feature/UAVCLOUD-20939" + }, + { + "name": "GIT_COMMIT", + "value": "c235350212bf2359d550faecab78ff02047bca37" + }, + { + "name": "K8S_NAMESPACE", + "value": "uavcloud-devflight" + }, + { + "name": "APPLICATION_NAME", + "value": "cmii-uav-platform-splice" + } + ], + "resources": { + "limits": { + "cpu": "2", + "memory": "2Gi" + }, + "requests": { + "cpu": "100m", + "memory": "200Mi" + } + }, + "volumeMounts": [ + { + "name": "nginx-conf", + "mountPath": "/usr/local/nginx/conf/nginx.conf", + "subPath": "nginx.conf" + }, + { + "name": "default-nginx-conf", + "mountPath": "/etc/nginx/conf.d/default.conf", + "subPath": "default.conf" + }, + { + "name": "tenant-prefix", + "mountPath": "/home/cmii-platform/dist/ingress-config.js", + "subPath": "ingress-config.js" + }, + { + "name": "default-token-hzddz", + "readOnly": true, + "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount" + } + ], + "terminationMessagePath": "/dev/termination-log", + "terminationMessagePolicy": "File", + + "imagePullPolicy": "Always" + } + ], + "restartPolicy": "Always", + "terminationGracePeriodSeconds": 30, + "dnsPolicy": "ClusterFirst", + "serviceAccountName": "default", + "serviceAccount": "default", + "nodeName": "dev-08.ecs.io", + "securityContext": { }, + "imagePullSecrets": [ + { + "name": "harborsecret" + } + ], + "affinity": { + "nodeAffinity": { + "requiredDuringSchedulingIgnoredDuringExecution": { + "nodeSelectorTerms": [ + { + "matchExpressions": [ + { + "key": "uavcloud.affinity", + "operator": "In", + "values": [ + "common" + ] + } + ] + } + ] + } + } + }, + "schedulerName": "default-scheduler", + "tolerations": [ + { + "key": "node.kubernetes.io/not-ready", + "operator": "Exists", + "effect": "NoExecute", + "tolerationSeconds": 300 + }, + { + "key": "node.kubernetes.io/unreachable", + "operator": "Exists", + "effect": "NoExecute", + "tolerationSeconds": 300 + } + ], + "priority": 0, + "enableServiceLinks": true + }, + "status": { + "phase": "Running", + "conditions": [ + { + "type": "Initialized", + "status": "True", + "lastProbeTime": null, + "lastTransitionTime": "2023-04-23T08:00:32Z" + }, + { + "type": "Ready", + "status": "True", + "lastProbeTime": null, + "lastTransitionTime": "2023-04-23T08:00:38Z" + }, + { + "type": "ContainersReady", + "status": "True", + "lastProbeTime": null, + "lastTransitionTime": "2023-04-23T08:00:38Z" + }, + { + "type": "PodScheduled", + "status": "True", + "lastProbeTime": null, + "lastTransitionTime": "2023-04-23T08:00:32Z" + } + ], + "hostIP": "192.168.11.162", + "podIP": "10.244.106.210", + "podIPs": [ + { + "ip": "10.244.106.210" + } + ], + "startTime": "2023-04-23T08:00:32Z", + "containerStatuses": [ + { + "name": "cmii-uav-platform-splice", + + "state": { + "running": { + "startedAt": "2023-04-23T08:00:37Z" + } + }, + "lastState": { }, + "ready": true, + "restartCount": 0, + "image": "harbor.cdcyy.com.cn/cmii/cmii-uav-platform-splice:4.1.2-snapshot", + "imageID": "docker-pullable://harbor.cdcyy.com.cn/cmii/cmii-uav-platform-splice@sha256:86264f6cfa36f828a829afe061b6cbd9854ff3ff509d287210e93873a78e28e1", + "containerID": "docker://dcc7cbff3b399fb8ded6abb0350162e1d42c0a9e169228431b98e3adc97f0ed6", + "started": true + } + ], + "qosClass": "Burstable" + } +} + diff --git a/agent-go/utils/TimeUtils.go b/agent-go/utils/TimeUtils.go index 4dfb872..2a388f2 100644 --- a/agent-go/utils/TimeUtils.go +++ b/agent-go/utils/TimeUtils.go @@ -20,3 +20,10 @@ func ParseISOLocalDateTime() string { now := time.Now() return now.Format(time.RFC3339) } + +func TimeSplitFormatString() string { + now := time.Now() + formattedTime := now.Format("2006-01-02-15-04-05") + + return formattedTime +}