From a04bd509ec6781427eb95fc7a5df573094f03e55 Mon Sep 17 00:00:00 2001 From: weibaohui Date: Fri, 25 Oct 2024 22:23:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor(kom):=20=E6=9B=B4=E6=96=B0=20kom?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=E4=BD=BF=E7=94=A8=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E5=B9=B6=E5=8D=87=E7=BA=A7=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 kom.Init()替换为 kom.DefaultCluster(),简化客户端初始化 - 更新导入路径,使用新的 kom 客户端版本 - 修复部分功能以适配新版本的 kom 客户端 --- go.mod | 2 +- go.sum | 4 ++-- pkg/callback/cb.go | 4 ++-- pkg/controller/doc/doc.go | 4 ++-- pkg/controller/dynamic/dynamic.go | 13 ++++++------- pkg/controller/ns/ns.go | 2 +- pkg/controller/pod/pod.go | 4 ++-- pkg/controller/pod/pod_file.go | 23 +++++++++++------------ pkg/service/deploy.go | 16 ++++++++-------- pkg/service/pod.go | 4 ++-- 10 files changed, 37 insertions(+), 39 deletions(-) diff --git a/go.mod b/go.mod index 0773cdb..f5a4f19 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/gin-gonic/gin v1.10.0 github.com/sashabaranov/go-openai v1.32.2 github.com/spf13/pflag v1.0.5 - github.com/weibaohui/kom v0.0.9 + github.com/weibaohui/kom v0.0.10 k8s.io/api v0.31.1 k8s.io/apimachinery v0.31.1 k8s.io/client-go v0.31.1 diff --git a/go.sum b/go.sum index ba25a84..280bdd9 100644 --- a/go.sum +++ b/go.sum @@ -133,8 +133,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= -github.com/weibaohui/kom v0.0.9 h1:Mvmgrh9YEP3iufqgi6yuZKY8PGM2YVr+5YrOmwf/pd8= -github.com/weibaohui/kom v0.0.9/go.mod h1:OauDGb387+wW44uGraJRVw+MYBWVjYrnAwdAX/5tqkM= +github.com/weibaohui/kom v0.0.10 h1:oz29Sy8lVSrZMFtXpbavW4cwAnA89/hA6DvV+G7wpyY= +github.com/weibaohui/kom v0.0.10/go.mod h1:OauDGb387+wW44uGraJRVw+MYBWVjYrnAwdAX/5tqkM= github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= diff --git a/pkg/callback/cb.go b/pkg/callback/cb.go index ba9b203..5be82f5 100644 --- a/pkg/callback/cb.go +++ b/pkg/callback/cb.go @@ -6,11 +6,11 @@ import ( ) func RegisterCallback() { - queryCallback := kom.Init().Callback().Get() + queryCallback := kom.DefaultCluster().Callback().Get() _ = queryCallback.Register("k8m:get11", Get) } -func Get(k8s *kom.Kom) error { +func Get(k8s *kom.Kubectl) error { // todo 在这里可以统一进行权限认证等操作,返回error即可阻断执行 u := k8s.Statement.Context.Value("user") klog.V(2).Infof("%s k8s Get \n", u) diff --git a/pkg/controller/doc/doc.go b/pkg/controller/doc/doc.go index 18e40fe..076f61d 100644 --- a/pkg/controller/doc/doc.go +++ b/pkg/controller/doc/doc.go @@ -7,13 +7,13 @@ import ( "github.com/weibaohui/k8m/pkg/comm/utils" "github.com/weibaohui/k8m/pkg/comm/utils/amis" "github.com/weibaohui/k8m/pkg/service" - "github.com/weibaohui/kom/kom/doc" + "github.com/weibaohui/kom/kom" ) func Doc(c *gin.Context) { kind := c.Param("kind") apiVersion := c.Param("api_version") - docs := doc.Instance() + docs := kom.DefaultCluster().Status().Docs() // apiVersion 有可能包含xxx.com/v1 类似,所以需要处理 // 前端使用了base64Encode,这里需要反向解析处理 diff --git a/pkg/controller/dynamic/dynamic.go b/pkg/controller/dynamic/dynamic.go index 063c1e0..3c620a0 100644 --- a/pkg/controller/dynamic/dynamic.go +++ b/pkg/controller/dynamic/dynamic.go @@ -6,7 +6,6 @@ import ( "github.com/gin-gonic/gin" "github.com/weibaohui/k8m/pkg/comm/utils/amis" "github.com/weibaohui/kom/kom" - "github.com/weibaohui/kom/kom/applier" "github.com/weibaohui/kom/utils" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "sigs.k8s.io/yaml" @@ -18,7 +17,7 @@ func List(c *gin.Context) { kind := c.Param("kind") ctx := c.Request.Context() var list []unstructured.Unstructured - err := kom.Init().WithContext(ctx).Namespace(ns).CRD(group, "v1", kind).List(&list).Error + err := kom.DefaultCluster().WithContext(ctx).Namespace(ns).CRD(group, "v1", kind).List(&list).Error amis.WriteJsonListWithError(c, list, err) } func Fetch(c *gin.Context) { @@ -30,7 +29,7 @@ func Fetch(c *gin.Context) { var obj *unstructured.Unstructured - err := kom.Init().WithContext(ctx).Name(name).Namespace(ns).CRD(group, "v1", kind).Get(&obj).Error + err := kom.DefaultCluster().WithContext(ctx).Name(name).Namespace(ns).CRD(group, "v1", kind).Get(&obj).Error if err != nil { amis.WriteJsonError(c, err) return @@ -61,7 +60,7 @@ func Remove(c *gin.Context) { } func removeSingle(ctx context.Context, kind, group, ns, name string) error { - return kom.Init().WithContext(ctx).Name(name).Namespace(ns).CRD(group, "v1", kind).Delete().Error + return kom.DefaultCluster().WithContext(ctx).Name(name).Namespace(ns).CRD(group, "v1", kind).Delete().Error } // NamesPayload 定义结构体以匹配批量删除 JSON 结构 @@ -117,7 +116,7 @@ func Save(c *gin.Context) { } obj.SetName(name) obj.SetNamespace(ns) - err := kom.Init().WithContext(ctx).Name(name).Namespace(ns).CRD(group, "v1", kind).Update(&obj).Error + err := kom.DefaultCluster().WithContext(ctx).Name(name).Namespace(ns).CRD(group, "v1", kind).Update(&obj).Error if err != nil { amis.WriteJsonError(c, err) return @@ -134,7 +133,7 @@ func Apply(c *gin.Context) { return } yamlStr := req.YAML - result := applier.Instance().WithContext(ctx).Apply(yamlStr) + result := kom.DefaultCluster().WithContext(ctx).Applier().Apply(yamlStr) amis.WriteJsonData(c, gin.H{ "result": result, }) @@ -150,7 +149,7 @@ func Delete(c *gin.Context) { return } yamlStr := req.YAML - result := applier.Instance().WithContext(ctx).Delete(yamlStr) + result := kom.DefaultCluster().WithContext(ctx).Applier().Delete(yamlStr) amis.WriteJsonData(c, gin.H{ "result": result, }) diff --git a/pkg/controller/ns/ns.go b/pkg/controller/ns/ns.go index 1c23e57..03bdd6a 100644 --- a/pkg/controller/ns/ns.go +++ b/pkg/controller/ns/ns.go @@ -10,7 +10,7 @@ import ( func OptionList(c *gin.Context) { ctx := c.Request.Context() var ns []v1.Namespace - err := kom.Init().WithContext(ctx).Resource(&v1.Namespace{}).List(&ns).Error + err := kom.DefaultCluster().WithContext(ctx).Resource(&v1.Namespace{}).List(&ns).Error if err != nil { amis.WriteJsonError(c, err) return diff --git a/pkg/controller/pod/pod.go b/pkg/controller/pod/pod.go index a433055..1c226f6 100644 --- a/pkg/controller/pod/pod.go +++ b/pkg/controller/pod/pod.go @@ -27,7 +27,7 @@ func StreamPodLogsBySelector(c *gin.Context, ns string, containerName string, op ctx := c.Request.Context() var pods []v1.Pod - err := kom.Init().Resource(&v1.Pod{}).Namespace(ns).List(&pods, options).Error + err := kom.DefaultCluster().Resource(&v1.Pod{}).Namespace(ns).List(&pods, options).Error if err != nil { amis.WriteJsonError(c, err) return @@ -63,7 +63,7 @@ func DownloadLogs(c *gin.Context) { func DownloadPodLogsBySelector(c *gin.Context, ns string, containerName string, options metav1.ListOptions) { ctx := c.Request.Context() var pods []v1.Pod - err := kom.Init().Resource(&v1.Pod{}).Namespace(ns).List(&pods, options).Error + err := kom.DefaultCluster().Resource(&v1.Pod{}).Namespace(ns).List(&pods, options).Error if err != nil { amis.WriteJsonError(c, err) return diff --git a/pkg/controller/pod/pod_file.go b/pkg/controller/pod/pod_file.go index 53a2de6..0ec8f9e 100644 --- a/pkg/controller/pod/pod_file.go +++ b/pkg/controller/pod/pod_file.go @@ -8,7 +8,7 @@ import ( "github.com/gin-gonic/gin" "github.com/weibaohui/k8m/pkg/comm/utils" "github.com/weibaohui/k8m/pkg/comm/utils/amis" - "github.com/weibaohui/kom/kom/poder" + "github.com/weibaohui/kom/kom" "k8s.io/klog/v2" ) @@ -32,9 +32,8 @@ func FileList(c *gin.Context) { amis.WriteJsonError(c, err) return } - - poder := poder.Instance(). - WithContext(c.Request.Context()). + ctx := c.Request.Context() + poder := kom.DefaultCluster().WithContext(ctx).Poder(). Namespace(info.Namespace). Name(info.PodName). ContainerName(info.ContainerName) @@ -60,8 +59,8 @@ func ShowFile(c *gin.Context) { return } - poder := poder.Instance(). - WithContext(c.Request.Context()). + ctx := c.Request.Context() + poder := kom.DefaultCluster().WithContext(ctx).Poder(). Namespace(info.Namespace). Name(info.PodName). ContainerName(info.ContainerName) @@ -106,8 +105,8 @@ func SaveFile(c *gin.Context) { return } - poder := poder.Instance(). - WithContext(c.Request.Context()). + ctx := c.Request.Context() + poder := kom.DefaultCluster().WithContext(ctx).Poder(). Namespace(info.Namespace). Name(info.PodName). ContainerName(info.ContainerName) @@ -145,8 +144,8 @@ func DownloadFile(c *gin.Context) { return } - poder := poder.Instance(). - WithContext(c.Request.Context()). + ctx := c.Request.Context() + poder := kom.DefaultCluster().WithContext(ctx).Poder(). Namespace(info.Namespace). Name(info.PodName). ContainerName(info.ContainerName) @@ -184,8 +183,8 @@ func UploadFile(c *gin.Context) { // 替换FileName中非法字符 info.FileName = utils.SanitizeFileName(info.FileName) - poder := poder.Instance(). - WithContext(c.Request.Context()). + ctx := c.Request.Context() + poder := kom.DefaultCluster().WithContext(ctx).Poder(). Namespace(info.Namespace). Name(info.PodName). ContainerName(info.ContainerName) diff --git a/pkg/service/deploy.go b/pkg/service/deploy.go index 3cf83bd..f64fee1 100644 --- a/pkg/service/deploy.go +++ b/pkg/service/deploy.go @@ -16,7 +16,7 @@ type DeployService struct { func (d *DeployService) RestartDeploy(ctx context.Context, ns string, name string) (*v1.Deployment, error) { var deploy v1.Deployment - err := kom.Init().WithContext(ctx).Resource(&deploy).Namespace(ns).Name(name).Get(&deploy).Error + err := kom.DefaultCluster().WithContext(ctx).Resource(&deploy).Namespace(ns).Name(name).Get(&deploy).Error if err != nil { return nil, err @@ -28,7 +28,7 @@ func (d *DeployService) RestartDeploy(ctx context.Context, ns string, name strin deploy.Spec.Template.Annotations["kubectl.kubernetes.io/restartedAt"] = time.Now().Format(time.RFC3339) // 更新 Deployment - err = kom.Init().WithContext(ctx).Resource(&deploy).Namespace(ns).Name(name).Update(&deploy).Error + err = kom.DefaultCluster().WithContext(ctx).Resource(&deploy).Namespace(ns).Name(name).Update(&deploy).Error if err != nil { return nil, err } @@ -36,7 +36,7 @@ func (d *DeployService) RestartDeploy(ctx context.Context, ns string, name strin } func (d *DeployService) UpdateDeployImageTag(ctx context.Context, ns string, name string, containerName string, tag string) (*v1.Deployment, error) { var deploy v1.Deployment - err := kom.Init().WithContext(ctx).Resource(&deploy).Namespace(ns).Name(name).Get(&deploy).Error + err := kom.DefaultCluster().WithContext(ctx).Resource(&deploy).Namespace(ns).Name(name).Get(&deploy).Error if err != nil { return nil, err @@ -49,7 +49,7 @@ func (d *DeployService) UpdateDeployImageTag(ctx context.Context, ns string, nam c.Image = replaceImageTag(c.Image, tag) } } - err = kom.Init().WithContext(ctx).Resource(&deploy).Namespace(ns).Name(name).Update(&deploy).Error + err = kom.DefaultCluster().WithContext(ctx).Resource(&deploy).Namespace(ns).Name(name).Update(&deploy).Error return &deploy, err } @@ -72,7 +72,7 @@ func (d *DeployService) CreateImagePullSecret(ctx context.Context, ns string, se // 先查查Secrets 有没有 secret := corev1.Secret{} - err := kom.Init().WithContext(ctx).Resource(&secret).Namespace(ns).Name(secretName).Get(&secret).Error + err := kom.DefaultCluster().WithContext(ctx).Resource(&secret).Namespace(ns).Name(secretName).Get(&secret).Error if err != nil && strings.Contains(err.Error(), "not found") { // 创建 secret secret = corev1.Secret{ @@ -85,7 +85,7 @@ func (d *DeployService) CreateImagePullSecret(ctx context.Context, ns string, se corev1.DockerConfigJsonKey: []byte(pullSecret), }, } - err = kom.Init().WithContext(ctx).Resource(&secret).Namespace(ns).Name(secretName).Create(&secret).Error + err = kom.DefaultCluster().WithContext(ctx).Resource(&secret).Namespace(ns).Name(secretName).Create(&secret).Error if err != nil { return err } @@ -93,7 +93,7 @@ func (d *DeployService) CreateImagePullSecret(ctx context.Context, ns string, se var sa corev1.ServiceAccount // 将 secret 绑定到 ServiceAccount - err = kom.Init().WithContext(ctx).Resource(&sa).Namespace(ns).Name(serviceAccount).Get(&sa).Error + err = kom.DefaultCluster().WithContext(ctx).Resource(&sa).Namespace(ns).Name(serviceAccount).Get(&sa).Error if err != nil { return err } @@ -107,7 +107,7 @@ func (d *DeployService) CreateImagePullSecret(ctx context.Context, ns string, se // 绑定 imagePullSecret sa.ImagePullSecrets = append(sa.ImagePullSecrets, corev1.LocalObjectReference{Name: secretName}) - err = kom.Init().WithContext(ctx).Resource(&sa).Namespace(ns).Name(serviceAccount).Update(&sa).Error + err = kom.DefaultCluster().WithContext(ctx).Resource(&sa).Namespace(ns).Name(serviceAccount).Update(&sa).Error return err } diff --git a/pkg/service/pod.go b/pkg/service/pod.go index 099448a..7e4b1db 100644 --- a/pkg/service/pod.go +++ b/pkg/service/pod.go @@ -4,7 +4,7 @@ import ( "context" "io" - "github.com/weibaohui/kom/kom/poder" + "github.com/weibaohui/kom/kom" v1 "k8s.io/api/core/v1" ) @@ -23,7 +23,7 @@ func (p *PodService) StreamPodLogs(ctx context.Context, ns, name string, logOpti logOptions.SinceSeconds = nil } - podLogs := poder.Instance().WithContext(ctx).Namespace(ns).Name(name).GetLogs(name, logOptions) + podLogs := kom.DefaultCluster().WithContext(ctx).Poder().Namespace(ns).Name(name).GetLogs(name, logOptions) logStream, err := podLogs.Stream(ctx) return logStream, err