182 lines
4.6 KiB
Go
182 lines
4.6 KiB
Go
package executor
|
||
|
||
import (
|
||
"fmt"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
|
||
"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 {
|
||
|
||
if !BasicCommandExists("kubectl") {
|
||
log.Error("kubectl命令不存在,无法查看Pod状态,请查看!")
|
||
}
|
||
|
||
ok, resultLog := AllCommandExecutor([]string{
|
||
"kubectl", "-n", supreme, "get", "pod", specific, "-o", "jsonpath='{.status.phase}'",
|
||
})
|
||
if !ok {
|
||
return false
|
||
}
|
||
|
||
for _, resultLine := range resultLog {
|
||
if strings.HasPrefix(resultLine, "Running") {
|
||
return true
|
||
}
|
||
}
|
||
|
||
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 !BasicCommandExistByPath("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{
|
||
"/usr/local/bin/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 !BasicCommandExistByPath("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{
|
||
"/usr/local/bin/kubectl",
|
||
"delete",
|
||
"-f",
|
||
resourcesYamlFile,
|
||
})
|
||
if !ok {
|
||
return false, resultLog
|
||
}
|
||
|
||
return true, append(resultLog,
|
||
fmt.Sprintf("[KubectlDeleteExec] - %s delete success!", resourcesYamlFile))
|
||
}
|