[ Agent ] [ APP ] - accomplish rke and dashboard
This commit is contained in:
@@ -1,8 +1,46 @@
|
||||
package executor
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
var configFilePath = "/root/.kube/config"
|
||||
"context"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
)
|
||||
|
||||
var k8sConfigFilePath = "/root/.kube/config"
|
||||
var k8sClient = newK8sClientInstance()
|
||||
|
||||
func newK8sClientInstance() *kubernetes.Clientset {
|
||||
once := sync.Once{}
|
||||
|
||||
if !BasicFileExistAndNotNull(k8sConfigFilePath) {
|
||||
log.WarnF("[newK8sClientInstance] - k8s config %s does not exist ! ", k8sConfigFilePath)
|
||||
return nil
|
||||
}
|
||||
|
||||
var client *kubernetes.Clientset
|
||||
once.Do(func() {
|
||||
// 使用kubeconfig文件初始化客户端
|
||||
config, err := clientcmd.BuildConfigFromFlags("", k8sConfigFilePath)
|
||||
if err != nil {
|
||||
log.ErrorF("[newK8sClientInstance] - load from %s error !", k8sConfigFilePath)
|
||||
|
||||
}
|
||||
|
||||
client, err = kubernetes.NewForConfig(config)
|
||||
if err != nil {
|
||||
log.Error("[newK8sClientInstance] - create k8s client error !")
|
||||
}
|
||||
})
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
func CheckPodStatus(specific string, supreme string) bool {
|
||||
|
||||
@@ -25,3 +63,119 @@ 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信息失败: %v\n", err)
|
||||
} else {
|
||||
log.DebugF("[CheckPodStatusTimeout] - 命名空间: %s, Pod名称: %s, 状态: %s\n", supreme, pod.Name, pod.Status.Phase)
|
||||
if pod.Status.Phase == "Running" {
|
||||
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
|
||||
if !BasicCommandExists("kubectl") {
|
||||
return false, []string{
|
||||
"[KubectlApplyExec] - kubectl command not exist !",
|
||||
}
|
||||
}
|
||||
|
||||
// check resourcesYamlFile
|
||||
if !BasicFileExistAndNotNull(resourcesYamlFile) {
|
||||
return false, []string{
|
||||
fmt.Sprintf("[KubectlApplyExec] - wrong resourcesYamlFile %s not exist !", resourcesYamlFile),
|
||||
}
|
||||
}
|
||||
|
||||
// apply -f
|
||||
ok, resultLog := AllCommandExecutor([]string{
|
||||
"kubectl",
|
||||
"apply",
|
||||
"-f",
|
||||
resourcesYamlFile,
|
||||
})
|
||||
if !ok {
|
||||
return false, resultLog
|
||||
}
|
||||
|
||||
return true, append(resultLog,
|
||||
fmt.Sprintf("[KubectlApplyExec] - %s apply success!", resourcesYamlFile))
|
||||
}
|
||||
|
||||
func KubectlDeleteExec(resourcesYamlFile string) (bool, []string) {
|
||||
|
||||
// check kubectl
|
||||
if !BasicCommandExists("kubectl") {
|
||||
return false, []string{
|
||||
"[KubectlDeleteExec] - kubectl command not exist !",
|
||||
}
|
||||
}
|
||||
|
||||
// check resourcesYamlFile
|
||||
if !BasicFileExistAndNotNull(resourcesYamlFile) {
|
||||
return false, []string{
|
||||
fmt.Sprintf("[KubectlDeleteExec] - wrong resourcesYamlFile %s not exist !", resourcesYamlFile),
|
||||
}
|
||||
}
|
||||
|
||||
// apply -f
|
||||
ok, resultLog := AllCommandExecutor([]string{
|
||||
"kubectl",
|
||||
"delete",
|
||||
"-f",
|
||||
resourcesYamlFile,
|
||||
})
|
||||
if !ok {
|
||||
return false, resultLog
|
||||
}
|
||||
|
||||
return true, append(resultLog,
|
||||
fmt.Sprintf("[KubectlDeleteExec] - %s delete success!", resourcesYamlFile))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user