[ Agent ] [ App ] - k8s-pvc
This commit is contained in:
@@ -45,7 +45,117 @@ func newK8sClientInstance() *kubernetes.Clientset {
|
||||
return client
|
||||
}
|
||||
|
||||
func CheckPodStatus(specific string, supreme string) bool {
|
||||
func K8sCheckPodStatusTimeout(specificPod string, supreme string, waitTimeOut int) bool {
|
||||
|
||||
// 设置超时时间和时间间隔
|
||||
timeout := time.After(time.Duration(waitTimeOut) * time.Second)
|
||||
tick := time.Tick(5 * time.Second)
|
||||
|
||||
// 监控Pod状态
|
||||
for {
|
||||
select {
|
||||
case <-timeout:
|
||||
log.ErrorF("[K8sCheckPodStatusTimeout] - 命名空间: [%s], Pod名称: [%s], 状态: 失败!", supreme, specificPod)
|
||||
return false
|
||||
case <-tick:
|
||||
pod, err := k8sClient.CoreV1().Pods(supreme).Get(context.TODO(), specificPod, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.ErrorF("[K8sCheckPodStatusTimeout] - 获取Pod信息失败: %s", err.Error())
|
||||
} else {
|
||||
log.DebugF("[K8sCheckPodStatusTimeout] - 命名空间: [%s], Pod名称: [%s], 状态: [%s]", supreme, pod.Name, pod.Status.Phase)
|
||||
if pod.Status.Phase == corev1.PodRunning || pod.Status.Phase == corev1.PodSucceeded {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func K8sCheckDeploymentStatusTimeout(specificDeployment string, supreme string, waitTimeOut int) bool {
|
||||
|
||||
// 设置超时时间和时间间隔
|
||||
timeout := time.After(time.Duration(waitTimeOut) * time.Second)
|
||||
tick := time.Tick(5 * time.Second)
|
||||
|
||||
// 监控Pod状态
|
||||
for {
|
||||
select {
|
||||
case <-timeout:
|
||||
log.ErrorF("[K8sCheckDeploymentStatusTimeout] - 命名空间: %s, Deployment名称: %s, 状态: 失败!\n", supreme, specificDeployment)
|
||||
return false
|
||||
case <-tick:
|
||||
deployment, err := k8sClient.AppsV1().Deployments(supreme).Get(context.TODO(), specificDeployment, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.ErrorF("[K8sCheckDeploymentStatusTimeout] - 获取deployment信息失败: %v ", err)
|
||||
} else {
|
||||
log.DebugF("[K8sCheckDeploymentStatusTimeout] - 命名空间: [ %s ], Deployment: [ %s ] 还有Pods未处于Running状态 (Ready: %d, Total: %d)\n", supreme, deployment.Name, deployment.Status.ReadyReplicas, deployment.Status.Replicas)
|
||||
|
||||
if deployment.Status.ReadyReplicas == deployment.Status.Replicas {
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func K8sListPVCInNamespace(supreme string) (bool, []string) {
|
||||
|
||||
if k8sClient == nil {
|
||||
log.ErrorF("k8s client is nil, run k8s function error !")
|
||||
return false, nil
|
||||
}
|
||||
|
||||
pvcs, err := k8sClient.CoreV1().PersistentVolumeClaims(supreme).List(context.TODO(), metav1.ListOptions{})
|
||||
if err != nil {
|
||||
log.ErrorF("[K8sListPVCInNamespace] - error list pvc list in namespace %s", supreme)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
var pvcList []string
|
||||
for _, pvc := range pvcs.Items {
|
||||
pvcList = append(pvcList, pvc.Name)
|
||||
}
|
||||
|
||||
log.DebugF("K8sListPVCInNamespace - all pvc in namespace of [ %s ] are %v", supreme, pvcList)
|
||||
return true, pvcList
|
||||
}
|
||||
|
||||
func K8sCheckPVCStatusTimeOut(specificPvcName string, supreme string, waitTimeOut int) bool {
|
||||
|
||||
if k8sClient == nil {
|
||||
log.ErrorF("k8s client is nil, run k8s function error !")
|
||||
return false
|
||||
}
|
||||
|
||||
// 设置超时时间和时间间隔
|
||||
timeout := time.After(time.Duration(waitTimeOut) * time.Second)
|
||||
tick := time.Tick(5 * time.Second)
|
||||
|
||||
// 监控Pod状态
|
||||
for {
|
||||
select {
|
||||
case <-timeout:
|
||||
log.ErrorF("[K8sCheckPVCStatusTimeOut] - 命名空间: %s, PVC 名称: %s, 状态: 失败! ", supreme, specificPvcName)
|
||||
return false
|
||||
case <-tick:
|
||||
pvc, err := k8sClient.CoreV1().PersistentVolumeClaims(supreme).Get(context.TODO(), specificPvcName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.ErrorF("[K8sCheckPVCStatusTimeOut] - 获取 PVC 信息失败: %v ", err)
|
||||
}
|
||||
|
||||
if pvc.Status.Phase == "Running" {
|
||||
log.DebugF("K8sCheckPVCStatusTimeOut] - PVC %s in namespace %s is running", specificPvcName, supreme)
|
||||
return true
|
||||
} else {
|
||||
log.WarnF("K8sCheckPVCStatusTimeOut] - PVC %s in namespace %s run failed !", specificPvcName, supreme)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func KubectlCheckPodStatus(specific string, supreme string) bool {
|
||||
|
||||
if !BasicCommandExists("kubectl") {
|
||||
log.Error("kubectl命令不存在,无法查看Pod状态,请查看!")
|
||||
@@ -67,60 +177,6 @@ func CheckPodStatus(specific string, supreme string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func CheckPodStatusTimeout(specificPod string, supreme string, waitTimeOut int) bool {
|
||||
|
||||
// 设置超时时间和时间间隔
|
||||
timeout := time.After(time.Duration(waitTimeOut) * time.Second)
|
||||
tick := time.Tick(5 * time.Second)
|
||||
|
||||
// 监控Pod状态
|
||||
for {
|
||||
select {
|
||||
case <-timeout:
|
||||
log.ErrorF("[CheckPodStatusTimeout] - 命名空间: [%s], Pod名称: [%s], 状态: 失败!\n", supreme, specificPod)
|
||||
return false
|
||||
case <-tick:
|
||||
pod, err := k8sClient.CoreV1().Pods(supreme).Get(context.TODO(), specificPod, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.ErrorF("[CheckPodStatusTimeout] - 获取Pod信息失败: %s", err.Error())
|
||||
} else {
|
||||
log.DebugF("[CheckPodStatusTimeout] - 命名空间: [%s], Pod名称: [%s], 状态: [%s]\n", supreme, pod.Name, pod.Status.Phase)
|
||||
if pod.Status.Phase == corev1.PodRunning || pod.Status.Phase == corev1.PodSucceeded {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CheckDeploymentStatusTimeout(specificDeployment string, supreme string, waitTimeOut int) bool {
|
||||
|
||||
// 设置超时时间和时间间隔
|
||||
timeout := time.After(time.Duration(waitTimeOut) * time.Second)
|
||||
tick := time.Tick(5 * time.Second)
|
||||
|
||||
// 监控Pod状态
|
||||
for {
|
||||
select {
|
||||
case <-timeout:
|
||||
log.ErrorF("[CheckDeploymentStatusTimeout] - 命名空间: %s, Deployment名称: %s, 状态: 失败!\n", supreme, specificDeployment)
|
||||
return false
|
||||
case <-tick:
|
||||
deployment, err := k8sClient.AppsV1().Deployments(supreme).Get(context.TODO(), specificDeployment, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.ErrorF("[CheckDeploymentStatusTimeout] - 获取deployment信息失败: %v\n", err)
|
||||
} else {
|
||||
log.DebugF("[CheckDeploymentStatusTimeout] - 命名空间: [ %s ], Deployment: [ %s ] 还有Pods未处于Running状态 (Ready: %d, Total: %d)\n", supreme, deployment.Name, deployment.Status.ReadyReplicas, deployment.Status.Replicas)
|
||||
|
||||
if deployment.Status.ReadyReplicas == deployment.Status.Replicas {
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func KubectlApplyExec(resourcesYamlFile string) (bool, []string) {
|
||||
|
||||
// check kubectl
|
||||
@@ -183,23 +239,33 @@ func KubectlDeleteExec(resourcesYamlFile string) (bool, []string) {
|
||||
fmt.Sprintf("[KubectlDeleteExec] - %s delete success!", resourcesYamlFile))
|
||||
}
|
||||
|
||||
func CreateK8sNamespace(namespaceName string) bool {
|
||||
func K8sCreateNamespace(namespaceName string) bool {
|
||||
if k8sClient == nil {
|
||||
log.ErrorF("k8s client is nil, run k8s function error !")
|
||||
return false
|
||||
}
|
||||
|
||||
namespace, err := k8sClient.CoreV1().Namespaces().Get(context.TODO(), namespaceName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.ErrorF("Error getting namespace: %s ", err.Error())
|
||||
return false
|
||||
}
|
||||
if namespace != nil {
|
||||
log.InfoF("[K8sCreateNamespace] - namespace of [%s] already exists!", namespaceName)
|
||||
return true
|
||||
}
|
||||
|
||||
// create namespace
|
||||
// 创建命名空间对象
|
||||
namespace := &corev1.Namespace{
|
||||
namespace = &corev1.Namespace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: namespaceName,
|
||||
},
|
||||
}
|
||||
// 使用客户端创建命名空间
|
||||
_, err := k8sClient.CoreV1().Namespaces().Create(context.TODO(), namespace, metav1.CreateOptions{})
|
||||
namespace, err = k8sClient.CoreV1().Namespaces().Create(context.TODO(), namespace, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
log.ErrorF("Error creating namespace: %s \n", err.Error())
|
||||
log.ErrorF("Error getting namespace: %s ", err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -222,7 +288,7 @@ func CreateK8sNamespace(namespaceName string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func GetK8sDashBoardAuthKey() {
|
||||
func K8sGetDashBoardAuthKey() {
|
||||
|
||||
// 获取 kube-system 命名空间的 secrets 列表
|
||||
secrets, err := k8sClient.CoreV1().Secrets("kube-system").List(context.TODO(), metav1.ListOptions{})
|
||||
|
||||
Reference in New Issue
Block a user