[ Agent ] [ CMII ] - 新增pod的条件查询,根据主机查询,根据deployment查询
This commit is contained in:
@@ -49,12 +49,16 @@ func TestBackupAllCmiiDeploymentToMap(t *testing.T) {
|
||||
|
||||
func TestUpdateCmiiDeploymentImageTag(t *testing.T) {
|
||||
|
||||
//tag := UpdateCmiiDeploymentImageTag("devflight", "cmii-uav-depotautoreturn", "123sdsa45678")
|
||||
//tag := UpdateCmiiDeploymentImageTag("demo", "cmii-uav-platform", "5.2.0-011101")
|
||||
tag := UpdateCmiiDeploymentImageTag("demo", "cmii-uav-waypoint", "5.2.0-011102")
|
||||
//tag := UpdateCmiiDeploymentImageTag("uavms", "uavms-lowaltitude-platform", "5.1.0-011103")
|
||||
cmiiEnv := "uavms"
|
||||
appName := "uavms-lowaltitude-platform"
|
||||
newTag := "5.2.0-011202"
|
||||
|
||||
tag := UpdateCmiiDeploymentImageTag(cmiiEnv, appName, newTag)
|
||||
assert.Equal(t, tag, true, "update image tag failed !")
|
||||
|
||||
check := CmiiOperator.DeploymentStatusCheck(cmiiEnv, appName, 180)
|
||||
assert.Equal(t, check, true, "deployment run failed!")
|
||||
|
||||
}
|
||||
|
||||
func TestRollBackCmiiDeploymentFromUpdateLog(t *testing.T) {
|
||||
@@ -65,7 +69,7 @@ func TestRollBackCmiiDeploymentFromUpdateLog(t *testing.T) {
|
||||
|
||||
func TestRestartCmiiBackendDeployment(t *testing.T) {
|
||||
|
||||
RestartCmiiBackendDeployment("devflight")
|
||||
RestartCmiiBackendDeployment("test")
|
||||
}
|
||||
|
||||
func TestRestartCmiiFrontendDeployment(t *testing.T) {
|
||||
@@ -96,7 +100,7 @@ func TestFindAllNodeNotHealthy(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFindPodNotHealthy(t *testing.T) {
|
||||
podNotHealthy := FindPodNotHealthy("int")
|
||||
podNotHealthy := FindPodNotHealthy("valida")
|
||||
podNotHealthy = FilterAllCmiiPodSoft(podNotHealthy)
|
||||
|
||||
for _, podInterface := range podNotHealthy {
|
||||
@@ -105,7 +109,7 @@ func TestFindPodNotHealthy(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFindPodNotHealthy_And_Delete(t *testing.T) {
|
||||
podNotHealthy := FindPodNotHealthy("int")
|
||||
podNotHealthy := FindPodNotHealthy("uavms")
|
||||
podNotHealthy = FilterAllCmiiPodSoft(podNotHealthy)
|
||||
|
||||
for _, podInterface := range podNotHealthy {
|
||||
|
||||
@@ -301,39 +301,37 @@ func (op *CmiiK8sOperator) DeploymentOneInterface(cmiiEnv, appName string) (depl
|
||||
return &convert
|
||||
}
|
||||
|
||||
func (op *CmiiK8sOperator) DeploymentScale(cmiiEnv, appFizz string, scaleCount int32) bool {
|
||||
func (op *CmiiK8sOperator) DeploymentScale(cmiiEnv, appName string, scaleCount int32) bool {
|
||||
|
||||
deploymentFizz := op.DeploymentFizz(cmiiEnv, appFizz)
|
||||
deployment := op.DeploymentOneInterface(cmiiEnv, appName)
|
||||
|
||||
client := op.CurrentClient
|
||||
|
||||
for _, deployment := range deploymentFizz {
|
||||
log.DebugF("[DeploymentScale] - start to scale [%s] [%s] to %d", deployment.Namespace, deployment.Name, scaleCount)
|
||||
log.DebugF("[DeploymentScale] - start to scale [%s] [%s] to %d", deployment.Namespace, deployment.Name, scaleCount)
|
||||
|
||||
scale := &autoscalingv1.Scale{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: deployment.Name,
|
||||
Namespace: deployment.Namespace,
|
||||
},
|
||||
Spec: autoscalingv1.ScaleSpec{
|
||||
Replicas: scaleCount,
|
||||
},
|
||||
}
|
||||
|
||||
updateScale, err := client.AppsV1().Deployments(deployment.Namespace).UpdateScale(
|
||||
context.TODO(),
|
||||
deployment.Name,
|
||||
scale,
|
||||
metav1.UpdateOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
log.ErrorF("[DeploymentScale] - scale error %s", err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
log.InfoF("[DeploymentScale] - scale of [%s] [%s] to %d success !", updateScale.Namespace, updateScale.Name, scaleCount)
|
||||
scale := &autoscalingv1.Scale{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: deployment.Name,
|
||||
Namespace: deployment.Namespace,
|
||||
},
|
||||
Spec: autoscalingv1.ScaleSpec{
|
||||
Replicas: scaleCount,
|
||||
},
|
||||
}
|
||||
|
||||
updateScale, err := client.AppsV1().Deployments(deployment.Namespace).UpdateScale(
|
||||
context.TODO(),
|
||||
deployment.Name,
|
||||
scale,
|
||||
metav1.UpdateOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
log.ErrorF("[DeploymentScale] - scale error %s", err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
log.InfoF("[DeploymentScale] - scale of [%s] [%s] to %d success !", updateScale.Namespace, updateScale.Name, scaleCount)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -396,20 +394,76 @@ func (op *CmiiK8sOperator) DeploymentRestart(cmiiEnv, appName string) bool {
|
||||
return result
|
||||
}
|
||||
|
||||
func (op *CmiiK8sOperator) ReplicaSet(cmiiEnv, replicaSetName string) *v1.ReplicaSet {
|
||||
func (op *CmiiK8sOperator) DeploymentStatusCheck(cmiiEnv, appName string, waitTimeOut int) bool {
|
||||
|
||||
op.changeOperatorEnv(cmiiEnv)
|
||||
|
||||
// 设置超时时间和时间间隔
|
||||
timeout := time.After(time.Duration(waitTimeOut) * time.Second)
|
||||
tick := time.Tick(2 * time.Second)
|
||||
|
||||
// 监控Pod状态
|
||||
for {
|
||||
select {
|
||||
case <-timeout:
|
||||
log.ErrorF("[DeploymentStatusCheck] - [%s] [%s] 状态: 失败!", cmiiEnv, appName)
|
||||
return false
|
||||
case <-tick:
|
||||
// check deployment exists
|
||||
deployment := op.DeploymentOneInterface(cmiiEnv, appName)
|
||||
if deployment == nil {
|
||||
log.ErrorF("[DeploymentStatusCheck] - [%s] [%s] not exists !", cmiiEnv, appName)
|
||||
return false
|
||||
}
|
||||
if deployment.AvailableReplicas == deployment.Replicas {
|
||||
log.InfoF("[DeploymentStatusCheck] - [%s] [%s] Available: %d, Total: %d success !", cmiiEnv, appName, deployment.AvailableReplicas, deployment.Replicas)
|
||||
return true
|
||||
}
|
||||
|
||||
log.DebugF("[DeploymentStatusCheck] - [%s] [%s] Available: %d, Total: %d waiting !", cmiiEnv, appName, deployment.AvailableReplicas, deployment.Replicas)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (op *CmiiK8sOperator) ReplicaSetExists(cmiiEnv, replicaSetName string) *v1.ReplicaSet {
|
||||
|
||||
op.changeOperatorEnv(cmiiEnv)
|
||||
client := op.CurrentClient
|
||||
|
||||
replicaSet, err := client.AppsV1().ReplicaSets(op.CurrentNamespace).Get(context.TODO(), replicaSetName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.ErrorF("[ReplicaSetExists] - [%s] [%s] not exists !", cmiiEnv, replicaSetName)
|
||||
return nil
|
||||
}
|
||||
|
||||
return replicaSet
|
||||
}
|
||||
|
||||
func (op *CmiiK8sOperator) ReplicaSetByAppName(cmiiEnv, appName string) (replicaList []v1.ReplicaSet) {
|
||||
deploy := op.DeploymentExist(cmiiEnv, appName)
|
||||
if deploy == nil {
|
||||
log.ErrorF("[ReplicaSetByAppName] - [%s] [%s] app not exists !", cmiiEnv, appName)
|
||||
return nil
|
||||
}
|
||||
|
||||
labelSelector := metav1.FormatLabelSelector(deploy.Spec.Selector)
|
||||
//Get the replica sets that belong to the deployment.
|
||||
replicaSets, err := op.CurrentClient.AppsV1().ReplicaSets(op.CurrentNamespace).List(context.TODO(), metav1.ListOptions{
|
||||
LabelSelector: labelSelector,
|
||||
})
|
||||
if err != nil {
|
||||
log.ErrorF("[ReplicaSetByAppName] - [%s] [%s] list replicaset error %s", cmiiEnv, appName, err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, replicaSet := range replicaSets.Items {
|
||||
replicaSet.SetManagedFields(nil)
|
||||
replicaList = append(replicaList, replicaSet)
|
||||
}
|
||||
|
||||
return replicaList
|
||||
}
|
||||
|
||||
func (op *CmiiK8sOperator) PodAll(cmiiEnv string) []corev1.Pod {
|
||||
op.changeOperatorEnv(cmiiEnv)
|
||||
|
||||
@@ -533,10 +587,57 @@ func (op *CmiiK8sOperator) PodAllInterface(cmiiEnv string) []CmiiPodInterface {
|
||||
return results[:index]
|
||||
}
|
||||
|
||||
// PodAllInSpecificNode cmiiEnv aims to specific k8s cluster
|
||||
func PodAllInSpecificNode(cmiiEnv, nodeName string) (podList []CmiiPodInterface) {
|
||||
func (op *CmiiK8sOperator) PodByAppName(cmiiEnv, appName string) (podList []CmiiPodInterface) {
|
||||
|
||||
return nil
|
||||
deploy := op.DeploymentExist(cmiiEnv, appName)
|
||||
if deploy == nil {
|
||||
log.ErrorF("[PodByAppName] - [%s] [%s] app not exists !", cmiiEnv, appName)
|
||||
return nil
|
||||
}
|
||||
|
||||
labelSelector := metav1.FormatLabelSelector(deploy.Spec.Selector)
|
||||
//Get the replica sets that belong to the deployment.
|
||||
pods, err := op.CurrentClient.CoreV1().Pods(op.CurrentNamespace).List(context.TODO(), metav1.ListOptions{
|
||||
LabelSelector: labelSelector,
|
||||
})
|
||||
if err != nil {
|
||||
log.ErrorF("[PodByAppName] - [%s] [%s] list pods error %s", cmiiEnv, appName, err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
cmiiPodInterface := CmiiPodInterface{}
|
||||
for _, pod := range pods.Items {
|
||||
pod.SetManagedFields(nil)
|
||||
podInterface := cmiiPodInterface.Convert(pod)
|
||||
podList = append(podList, podInterface)
|
||||
}
|
||||
|
||||
return podList
|
||||
}
|
||||
|
||||
func (op *CmiiK8sOperator) PodByNodeName(cmiiEnv, nodeName string) (podList []CmiiPodInterface) {
|
||||
|
||||
node := op.NodeExists(cmiiEnv, nodeName)
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
list, err := op.CurrentClient.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{
|
||||
FieldSelector: "spec.nodeName=" + nodeName,
|
||||
})
|
||||
if err != nil {
|
||||
log.ErrorF("[PodByNodeName] - [%s] [%s] list pod error %s !", cmiiEnv, nodeName, err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
podInterface := CmiiPodInterface{}
|
||||
for _, pod := range list.Items {
|
||||
cmiiPodInterface := podInterface.Convert(pod)
|
||||
|
||||
podList = append(podList, cmiiPodInterface)
|
||||
}
|
||||
|
||||
return podList
|
||||
}
|
||||
|
||||
func (op *CmiiK8sOperator) PodFizz(cmiiEnv, appFizz string) (fizzPod []corev1.Pod) {
|
||||
@@ -608,3 +709,21 @@ func (op *CmiiK8sOperator) NodeAllInterface(cmiiEnv string) (nodeList []CmiiNode
|
||||
|
||||
return nodeList
|
||||
}
|
||||
|
||||
func (op *CmiiK8sOperator) NodeExists(cmiiEnv, nodeName string) (node *CmiiNodeInterface) {
|
||||
|
||||
op.changeOperatorEnv(cmiiEnv)
|
||||
client := op.CurrentClient
|
||||
|
||||
nodeInterface := CmiiNodeInterface{}
|
||||
|
||||
nodeList, err := client.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.ErrorF("[NodeExists] - [%s] [%s] not exists !", cmiiEnv, nodeName)
|
||||
return nil
|
||||
}
|
||||
|
||||
convert := nodeInterface.Convert(*nodeList)
|
||||
|
||||
return &convert
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package k8s_exec
|
||||
import (
|
||||
"agent-go/utils"
|
||||
"fmt"
|
||||
"github.com/magiconair/properties/assert"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -100,6 +101,28 @@ func TestCmiiK8sOperator_DeploymentOneInterface(t *testing.T) {
|
||||
utils.BeautifulPrint(*deploy)
|
||||
}
|
||||
|
||||
func TestCmiiK8sOperator_ReplicaSetExists(t *testing.T) {
|
||||
|
||||
cmiiEnv := "uavcloud-devflight"
|
||||
appName := "cmii-admin-data-bf8f87cb7"
|
||||
|
||||
exists := CmiiOperator.ReplicaSetExists(cmiiEnv, appName)
|
||||
|
||||
utils.BeautifulPrint(*exists)
|
||||
}
|
||||
|
||||
func TestCmiiK8sOperator_ReplicaSetByAppName(t *testing.T) {
|
||||
|
||||
cmiiEnv := "uavcloud-devflight"
|
||||
appName := "cmii-admin-data"
|
||||
|
||||
exists := CmiiOperator.ReplicaSetByAppName(cmiiEnv, appName)
|
||||
|
||||
for _, replicaSet := range exists {
|
||||
utils.BeautifulPrint(replicaSet)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCmiiK8sOperator_PodAll(t *testing.T) {
|
||||
start := time.Now()
|
||||
podList := CmiiOperator.PodAll("devflight")
|
||||
@@ -132,6 +155,17 @@ func TestCmiiK8sOperator_PodFizz(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestCmiiK8sOperator_PodByAppName(t *testing.T) {
|
||||
cmiiEnv := "uavcloud-devflight"
|
||||
nodeName := "dev-01.ecs.io"
|
||||
|
||||
exists := CmiiOperator.PodByNodeName(cmiiEnv, nodeName)
|
||||
|
||||
for _, podInterface := range exists {
|
||||
utils.BeautifulPrint(podInterface)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCmiiDeploymentInterface_Convert(t *testing.T) {
|
||||
|
||||
log.DebugF("dadasdadasd")
|
||||
@@ -158,9 +192,30 @@ func TestCmiiK8sOperator_PodFizz2(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestCmiiK8sOperator_PodByNodeName(t *testing.T) {
|
||||
cmiiEnv := "uavcloud-devflight"
|
||||
appName := "cmii-admin-data"
|
||||
|
||||
exists := CmiiOperator.PodByAppName(cmiiEnv, appName)
|
||||
|
||||
for _, podInterface := range exists {
|
||||
utils.BeautifulPrint(podInterface)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCmiiK8sOperator_DeploymentStatusCheck(t *testing.T) {
|
||||
|
||||
cmiiEnv := "devflight"
|
||||
appName := "cmii-uav-gateway"
|
||||
|
||||
check := CmiiOperator.DeploymentStatusCheck(cmiiEnv, appName, 180)
|
||||
assert.Equal(t, check, true, "deployment run failed!")
|
||||
|
||||
}
|
||||
|
||||
func TestCmiiK8sOperator_NodeAll(t *testing.T) {
|
||||
start := time.Now()
|
||||
nodeList := CmiiOperator.NodeAll("uat")
|
||||
nodeList := CmiiOperator.NodeAll("dev")
|
||||
elapsed := time.Since(start).Milliseconds()
|
||||
fmt.Printf("执行耗时: %d ms\n", elapsed)
|
||||
|
||||
|
||||
@@ -7,3 +7,10 @@
|
||||
2024-01-11-11-58-15 uavcloud-uavms uavms-lowaltitude-platform 5.1.0 5.2.0-011101
|
||||
2024-01-11-14-00-34 uavcloud-uavms uavms-lowaltitude-platform 5.1.0-011102 5.1.0-011103
|
||||
2024-01-11-14-49-53 uavcloud-demo cmii-uav-waypoint 5.2.0-011101 5.2.0-011102
|
||||
2024-01-11-15-32-28 uavcloud-demo cmii-uav-platform 5.2.0-011101 5.2.0-011102
|
||||
2024-01-11-17-09-44 uavcloud-feature cmii-uav-platform 5.2.0-validation 5.2.0-011102
|
||||
2024-01-11-17-35-47 uavcloud-uavms cmii-uav-surveillance 5.1.0-LAIN05A 5.1.0-011101
|
||||
2024-01-11-17-38-06 uavcloud-uavms uavms-lowaltitude-platform 5.1.0-011103 5.1.0-011102
|
||||
2024-01-11-17-49-09 uavcloud-uavms uavms-lowaltitude-platform 5.1.0-011102 5.1.0-011104
|
||||
2024-01-12-10-38-30 uavcloud-uavms uavms-lowaltitude-platform 5.1.0-011105 5.2.0-011201
|
||||
2024-01-12-12-09-59 uavcloud-uavms uavms-lowaltitude-platform 5.2.0-011201 5.2.0-011202
|
||||
|
||||
Reference in New Issue
Block a user