250 lines
7.4 KiB
Go
250 lines
7.4 KiB
Go
package k8s_exec
|
|
|
|
import (
|
|
"agent-go/executor"
|
|
"agent-go/utils"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
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 {
|
|
|
|
//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 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
|
|
|
|
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") {
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func UpdateCmiiDeploymentImageTag(cmiiEnv, appName, newTag string) bool {
|
|
|
|
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 {
|
|
firstCol = utils.MaxInt(len(deploymentInterface.Name), firstCol)
|
|
secondCol = utils.MaxInt(len(deploymentInterface.ImageTag), secondCol)
|
|
thirdCol = utils.MaxInt(len(deploymentInterface.GitBranch), thirdCol)
|
|
fourthCol = utils.MaxInt(len(deploymentInterface.GitCommit), fourthCol)
|
|
}
|
|
|
|
firstCol += 2
|
|
secondCol += 2
|
|
secondCol += firstCol
|
|
thirdCol += 2
|
|
thirdCol += secondCol
|
|
fourthCol += 2
|
|
fourthCol += thirdCol
|
|
|
|
for _, deploymentInterface := range allInterface {
|
|
|
|
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"
|
|
|
|
if !executor.BasicAppendContentToFile(content, filePath) {
|
|
log.ErrorF("[BackupAllDeploymentFromEnv] - write to file %s error with contend %s", filePath, content)
|
|
return false
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
}
|