drone-kubernetes/k8s/client.go
谢作欢 f0bd8013fa
All checks were successful
continuous-integration/drone/tag Build is passing
🎨
2023-11-03 16:36:52 +08:00

118 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package k8s
import (
"context"
"drone-kubernetes/utils"
"errors"
"fmt"
appv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
kindv1 "k8s.io/client-go/kubernetes/typed/apps/v1"
"time"
)
type client struct {
kubernetes.Interface
}
// Client
// @description: 获取客户端
// @receiver c
// @return *client
// @return error
func (c *config) Client() (*client, error) {
kubeClient, err := kubernetes.NewForConfig(c.resetConf)
if err != nil {
return nil, err
}
return &client{kubeClient}, nil
}
// Do
// @description: 执行
// @receiver c
// @param namespace
// @param kind
// @param resourceName
// @param actionMethod
// @param repo
// @return err
func (c client) Do(namespace, kind, resourceName, actionMethod, repo string) (err error) {
var kindType any // 应用类型
var resource any // 资源详情
switch kind {
case "Deployment":
kindType = c.AppsV1().Deployments(namespace)
// 获取一下资源
resource, err = kindType.(kindv1.DeploymentInterface).Get(context.Background(), resourceName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("get deployment error: %v", err.Error())
}
switch actionMethod {
case "create":
case "update": // 更新目前仅支持更新镜像和tag
if err != nil && resource == nil {
return fmt.Errorf("get deployment error: %v", err.Error())
}
if repo == "" {
return fmt.Errorf("repo not empty")
}
repo = utils.ParseImage(repo)
resource.(*appv1.Deployment).Spec.Template.Spec.Containers[0].Image = repo
_, err = kindType.(kindv1.DeploymentInterface).Update(context.Background(), resource.(*appv1.Deployment), metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("update deployment error: %v", err.Error())
}
case "delete": // 删除应用
if err != nil && resource == nil {
return fmt.Errorf("get deployment error: %v", err.Error())
}
err = kindType.(kindv1.DeploymentInterface).Delete(context.Background(), resourceName, metav1.DeleteOptions{})
if err != nil {
return err
}
case "restart": // 重启应用
if err != nil && resource == nil {
return fmt.Errorf("get deployment error: %v", err.Error())
}
resource.(*appv1.Deployment).Spec.Template.ObjectMeta.Annotations["kubectl.kubernetes.io/restartedAt"] = time.Now().Local().String()
_, err = kindType.(kindv1.DeploymentInterface).Update(context.Background(), resource.(*appv1.Deployment), metav1.UpdateOptions{})
if err != nil {
return err
}
}
default:
return errors.New("not support")
}
fmt.Println("命名空间: ", namespace)
fmt.Println("资源类型: ", kind)
fmt.Println("资源名称: ", resourceName)
fmt.Println("操作类型: ", actionMethod)
fmt.Println("镜像地址: ", repo)
return nil
}