Skip to content

Commit

Permalink
refactor(kom): 更新 kom客户端使用方式并升级版本
Browse files Browse the repository at this point in the history
- 将 kom.Init()替换为 kom.DefaultCluster(),简化客户端初始化
- 更新导入路径,使用新的 kom 客户端版本
- 修复部分功能以适配新版本的 kom 客户端
  • Loading branch information
weibaohui committed Oct 25, 2024
1 parent fcaa55f commit a04bd50
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 39 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions pkg/callback/cb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/doc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,这里需要反向解析处理
Expand Down
13 changes: 6 additions & 7 deletions pkg/controller/dynamic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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 结构
Expand Down Expand Up @@ -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
Expand All @@ -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,
})
Expand All @@ -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,
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/ns/ns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
23 changes: 11 additions & 12 deletions pkg/controller/pod/pod_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions pkg/service/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,15 +28,15 @@ 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
}
return &deploy, nil
}
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
Expand All @@ -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
}

Expand All @@ -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{
Expand All @@ -85,15 +85,15 @@ 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
}
}

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
}
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions pkg/service/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"io"

"github.com/weibaohui/kom/kom/poder"
"github.com/weibaohui/kom/kom"
v1 "k8s.io/api/core/v1"
)

Expand All @@ -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
Expand Down

0 comments on commit a04bd50

Please sign in to comment.