[ Agent ] [ CMII ] - 完成工具类的代码

This commit is contained in:
zeaslity
2024-01-09 16:42:27 +08:00
parent 36b8e36392
commit 22faf15665
18 changed files with 1307 additions and 13 deletions

View File

@@ -0,0 +1,135 @@
package k8s_exec
import (
"agent-go/executor"
"agent-go/utils"
"strings"
"time"
)
var CmiiOperator = CmiiK8sOperator{}
// FindDeploymentRestartCountGreaterThanN 重启次数大于N的所有Deployment
func FindDeploymentRestartCountGreaterThanN(cmiiEnv string, restartCount int32) []CmiiDeploymentInterface {
//podInterface := CmiiPodInterface{}
// get all pods
podAll := CmiiOperator.PodAll(cmiiEnv)
// restart map
restartMap := make(map[string]int32, len(podAll))
// restart count
for _, pod := range podAll {
for _, containerStatus := range pod.Status.ContainerStatuses {
if containerStatus.RestartCount > restartCount {
restart, exists := restartMap[containerStatus.Name]
if exists {
restartMap[containerStatus.Name] = utils.MaxInt32(containerStatus.RestartCount, restart)
} else {
restartMap[containerStatus.Name] = containerStatus.RestartCount
}
}
}
}
result := make([]CmiiDeploymentInterface, len(restartMap))
cmiiDeploymentInterface := CmiiDeploymentInterface{}
index := 0
//log.DebugF("[FindDeploymentRestartCountGreaterThanN] - restart map is => %v", restartMap)
// find deployment convert to interface
for key, value := range restartMap {
deployment := CmiiOperator.DeploymentExist(cmiiEnv, key)
if deployment != nil {
// deployment exists
log.DebugF("[FindDeploymentRestartCountGreaterThanN] - restart [%s] [%s] is [%d]", cmiiEnv, key, value)
convert := cmiiDeploymentInterface.Convert(*deployment)
result[index] = convert
index++
}
}
return result[:index]
}
func RestartDeploymentFromList(deploymentList []CmiiDeploymentInterface) bool {
result := true
for _, deployment := range deploymentList {
result = CmiiOperator.DeploymentScale(deployment.Namespace, deployment.Name, 0)
if !result {
return result
}
time.Sleep(time.Second)
result = CmiiOperator.DeploymentScale(deployment.Namespace, deployment.Name, deployment.Replicas)
if !result {
return result
}
}
return result
}
func RestartCmiiBackendDeployment(cmiiEnv string) {
cmiiDeploymentInterfaces := CmiiOperator.DeploymentAllInterface(cmiiEnv)
for _, deploymentInterface := range cmiiDeploymentInterfaces {
if strings.Contains(deploymentInterface.Name, "platform") {
}
}
}
// GetCmiiAllDeploymentFromDemo 从DEMO提取全部的CMII的应用
func GetCmiiAllDeploymentFromDemo() {
allInterface := CmiiOperator.DeploymentAllInterface("demo")
filePath := "C:\\Users\\wddsh\\Documents\\IdeaProjects\\ProjectOctopus\\agent-go\\k8s_exec\\all-cmii-image.txt"
firstCol := 0
secondCol := 0
thirdCol := 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)
}
}
firstCol += 4
secondCol += 4
secondCol += firstCol
thirdCol += 4
thirdCol += secondCol
for _, deploymentInterface := range allInterface {
if strings.Contains(deploymentInterface.Name, "proxy") {
continue
}
content := executor.BasicWordSpaceCompletion(deploymentInterface.Name, firstCol)
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("[GetCmiiAllDeploymentFromDemo] - write to file %s error with contend %s", filePath, content)
}
}
}
}