Skip to content

Commit

Permalink
refactor(kubectl): 重构 kubectl 模块并移除未使用的代码
Browse files Browse the repository at this point in the history
- 删除了多个未使用的 kubectl 相关文件- 移除了 configmap、deploy、ingress、node、namespace、pod、pvc、secret、service account 和 svc 相关的冗余代码
- 更新了 deploy.go 中的 RestartDeploy 和 UpdateDeployImageTag 函数,使用 kom 库替代直接调用 k8s client
- 移除了 dynamic/example.go 中的示例代码
- 更新了 image_pull_secret.go 中的逻辑,使用 kom 库替代直接调用 k8s client
- 删除了 main.go 中的 /example路由
  • Loading branch information
weibaohui committed Oct 22, 2024
1 parent 9bf01e0 commit d3ce6fc
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 558 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.1
github.com/weibaohui/kom v0.0.3
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.1 h1:ypgf9g8pM9QNnpyKpPuHPjJ1B+7+3BmHsEEiXp9Bffw=
github.com/weibaohui/kom v0.0.1/go.mod h1:8sFtcytn9hW16Ta/vPpt7SrP7AOgK+hhK4xqXO6t0Os=
github.com/weibaohui/kom v0.0.3 h1:X6gszoRrVbCGxvoUU3E8l1G/xkTSyJGhZkdzG8ri79E=
github.com/weibaohui/kom v0.0.3/go.mod h1:8sFtcytn9hW16Ta/vPpt7SrP7AOgK+hhK4xqXO6t0Os=
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
3 changes: 0 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ func main() {
api.POST("/file/download", pod.DownloadFile)
api.POST("/file/upload", pod.UploadFile)

// Example
api.GET("/example", dynamic.Example)

}

err := r.Run(fmt.Sprintf(":%d", flag.Init().Port))
Expand Down
34 changes: 0 additions & 34 deletions pkg/comm/kubectl/configmap.go

This file was deleted.

34 changes: 14 additions & 20 deletions pkg/comm/kubectl/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,34 @@ import (
"strings"
"time"

"github.com/weibaohui/kom/kom"
"k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (k8s *Kubectl) GetDeploy(ctx context.Context, ns, name string) (*v1.Deployment, error) {
deployment, err := k8s.client.AppsV1().Deployments(ns).Get(ctx, name, metav1.GetOptions{})
return deployment, err
}

func (k8s *Kubectl) CreateDeploy(ctx context.Context, deploy *v1.Deployment) (*v1.Deployment, error) {
deployment, err := k8s.client.AppsV1().Deployments(deploy.Namespace).Create(ctx, deploy, metav1.CreateOptions{})
return deployment, err
}

func (k8s *Kubectl) RestartDeploy(ctx context.Context, ns string, name string) (*v1.Deployment, error) {
deployment, err := k8s.GetDeploy(ctx, ns, name)
var deploy v1.Deployment
err := kom.Init().WithContext(ctx).Resource(&deploy).Namespace(ns).Name(name).Get(&deploy).Error

if err != nil {
return nil, err
}
// 更新 Annotations,触发重启
if deployment.Spec.Template.Annotations == nil {
deployment.Spec.Template.Annotations = map[string]string{}
if deploy.Spec.Template.Annotations == nil {
deploy.Spec.Template.Annotations = map[string]string{}
}
deployment.Spec.Template.Annotations["kubectl.kubernetes.io/restartedAt"] = time.Now().Format(time.RFC3339)
deploy.Spec.Template.Annotations["kubectl.kubernetes.io/restartedAt"] = time.Now().Format(time.RFC3339)

// 更新 Deployment
updatedDeployment, err := k8s.client.AppsV1().Deployments(deployment.Namespace).Update(ctx, deployment, metav1.UpdateOptions{})
err = kom.Init().WithContext(ctx).Resource(&deploy).Namespace(ns).Name(name).Update(&deploy).Error
if err != nil {
return nil, err
}
return updatedDeployment, nil
return &deploy, nil
}
func (k8s *Kubectl) UpdateDeployImageTag(ctx context.Context, ns string, name string, containerName string, tag string) (*v1.Deployment, error) {
deploy, err := k8s.GetDeploy(ctx, ns, name)
var deploy v1.Deployment
err := kom.Init().WithContext(ctx).Resource(&deploy).Namespace(ns).Name(name).Get(&deploy).Error

if err != nil {
return nil, err
}
Expand All @@ -50,8 +44,8 @@ func (k8s *Kubectl) UpdateDeployImageTag(ctx context.Context, ns string, name st
c.Image = replaceImageTag(c.Image, tag)
}
}
deployment, err := k8s.client.AppsV1().Deployments(deploy.Namespace).Update(ctx, deploy, metav1.UpdateOptions{})
return deployment, err
err = kom.Init().WithContext(ctx).Resource(&deploy).Namespace(ns).Name(name).Update(&deploy).Error
return &deploy, err
}

// replaceImageTag 替换镜像的 tag
Expand Down
16 changes: 10 additions & 6 deletions pkg/comm/kubectl/image_pull_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"strings"

"github.com/weibaohui/kom/kom"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -13,9 +14,11 @@ func (k8s *Kubectl) CreateImagePullSecret(ctx context.Context, ns string, servic
secretName := "pull-secret"

// 先查查Secrets 有没有
_, err := k8s.GetSecret(ctx, ns, secretName)
secret := corev1.Secret{}
err := kom.Init().WithContext(ctx).Resource(&secret).Namespace(ns).Name(secretName).Get(&secret).Error
if err != nil && strings.Contains(err.Error(), "not found") {
secret := &corev1.Secret{
// 创建 secret
secret = corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: ns,
Expand All @@ -25,15 +28,15 @@ func (k8s *Kubectl) CreateImagePullSecret(ctx context.Context, ns string, servic
corev1.DockerConfigJsonKey: []byte(pullSecret),
},
}
// 创建 secret
_, err := k8s.CreateSecret(ctx, secret)
err = kom.Init().WithContext(ctx).Resource(&secret).Namespace(ns).Name(secretName).Create(&secret).Error
if err != nil {
return err
}
}

var sa corev1.ServiceAccount
// 将 secret 绑定到 ServiceAccount
sa, err := k8s.GetServiceAccount(ctx, ns, serviceAccount)
err = kom.Init().WithContext(ctx).Resource(&sa).Namespace(ns).Name(serviceAccount).Get(&sa).Error
if err != nil {
return err
}
Expand All @@ -47,6 +50,7 @@ func (k8s *Kubectl) CreateImagePullSecret(ctx context.Context, ns string, servic

// 绑定 imagePullSecret
sa.ImagePullSecrets = append(sa.ImagePullSecrets, corev1.LocalObjectReference{Name: secretName})
_, err = k8s.UpdateServiceAccount(ctx, sa)
err = kom.Init().WithContext(ctx).Resource(&sa).Namespace(ns).Name(serviceAccount).Update(&sa).Error

return err
}
34 changes: 0 additions & 34 deletions pkg/comm/kubectl/ingress.go

This file was deleted.

25 changes: 0 additions & 25 deletions pkg/comm/kubectl/node.go

This file was deleted.

35 changes: 0 additions & 35 deletions pkg/comm/kubectl/ns.go

This file was deleted.

17 changes: 0 additions & 17 deletions pkg/comm/kubectl/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (k8s *Kubectl) ListPod(ctx context.Context, ns string) ([]v1.Pod, error) {
list, err := k8s.client.CoreV1().Pods(ns).List(ctx, metav1.ListOptions{})
if err == nil && list != nil && list.Items != nil && len(list.Items) > 0 {
// 按创建时间倒序排序 Pods 列表
sort.Slice(list.Items, func(i, j int) bool {
return list.Items[i].CreationTimestamp.Time.After(list.Items[j].CreationTimestamp.Time)
})
return list.Items, nil
}
return nil, err
}

func (k8s *Kubectl) GetPod(ctx context.Context, ns, name string) (*v1.Pod, error) {
pod, err := k8s.client.CoreV1().Pods(ns).Get(ctx, name, metav1.GetOptions{})
return pod, err
}

func (k8s *Kubectl) StreamPodLogs(ctx context.Context, ns, name string, logOptions *v1.PodLogOptions) (io.ReadCloser, error) {

// 检查logOptions
Expand Down
31 changes: 0 additions & 31 deletions pkg/comm/kubectl/pvc.go

This file was deleted.

30 changes: 0 additions & 30 deletions pkg/comm/kubectl/secret.go

This file was deleted.

Loading

0 comments on commit d3ce6fc

Please sign in to comment.