Skip to content

Commit

Permalink
refactor(kubectl): 重构 kubectl 模块并移除未使用的代码
Browse files Browse the repository at this point in the history
- 删除了 kubectl 模块中的大量未使用代码
- 移除了 apply.go、client.go、dynamic.go 等多个文件
- 保留了 pod.go 中的 StreamPodLogs 函数
- 更新了相关引用和依赖
  • Loading branch information
weibaohui committed Oct 22, 2024
1 parent 4bcec41 commit b266170
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 1,193 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.4
github.com/weibaohui/kom v0.0.9
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.4 h1:lspdv9C+6znaXkgLI5MCMgRcIqwDrfzr8LU+C0c/c5o=
github.com/weibaohui/kom v0.0.4/go.mod h1:8sFtcytn9hW16Ta/vPpt7SrP7AOgK+hhK4xqXO6t0Os=
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/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
8 changes: 2 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/weibaohui/k8m/pkg/callback"
"github.com/weibaohui/k8m/pkg/comm/kubectl"
"github.com/weibaohui/k8m/pkg/controller/chat"
"github.com/weibaohui/k8m/pkg/controller/deploy"
"github.com/weibaohui/k8m/pkg/controller/doc"
"github.com/weibaohui/k8m/pkg/controller/dynamic"
"github.com/weibaohui/k8m/pkg/controller/ns"
"github.com/weibaohui/k8m/pkg/controller/pod"
"github.com/weibaohui/k8m/pkg/flag"
"github.com/weibaohui/kom/starter"
"github.com/weibaohui/kom/kom_starter"
"k8s.io/klog/v2"
)

Expand All @@ -36,10 +35,7 @@ func Init() {
klog.V(2).Infof("Git Commit: %s\n", GitCommit)

// 初始化kom
starter.Init()
// 初始化kubectl 连接
kubectl.InitConnection(cfg.KubeConfig)

kom_starter.InitWithConfig(cfg.KubeConfig)
// 初始化回调
callback.RegisterCallback()

Expand Down
9 changes: 3 additions & 6 deletions pkg/callback/cb.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package callback

import (
"context"

"github.com/weibaohui/kom/kom"
"k8s.io/klog/v2"
)
Expand All @@ -12,10 +10,9 @@ func RegisterCallback() {
_ = queryCallback.Register("k8m:get11", Get)
}

func Get(ctx context.Context, k8s *kom.Kom) error {
json := k8s.Statement.String()
func Get(k8s *kom.Kom) error {
// todo 在这里可以统一进行权限认证等操作,返回error即可阻断执行
u := ctx.Value("user")
klog.V(2).Infof("%s k8s Get stmt json:\n %s\n", u, json)
u := k8s.Statement.Context.Value("user")
klog.V(2).Infof("%s k8s Get \n", u)
return nil
}
143 changes: 0 additions & 143 deletions pkg/comm/kubectl/apply.go

This file was deleted.

87 changes: 1 addition & 86 deletions pkg/comm/kubectl/client.go
Original file line number Diff line number Diff line change
@@ -1,93 +1,8 @@
package kubectl

import (
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
)

var (
kubectl *Kubectl
)
var apiResources []metav1.APIResource

type Kubectl struct {
client *kubernetes.Clientset
config *rest.Config
dynamicClient dynamic.Interface
}

func Init() *Kubectl {
return kubectl
}

// InitConnection 在主入口处进行初始化
func InitConnection(path string) {
klog.V(2).Infof("k8s client init")
kubectl = &Kubectl{}

config, err := getKubeConfig(path)
if err != nil {
panic(err.Error())
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}

dynClient, err := dynamic.NewForConfig(config)
if err != nil {
panic(err.Error())
}

kubectl.client = client
kubectl.config = config
kubectl.dynamicClient = dynClient
_, lists, _ := kubectl.client.Discovery().ServerGroupsAndResources()
for _, list := range lists {

resources := list.APIResources
version := list.GroupVersionKind().Version
group := list.GroupVersionKind().Group
groupVersion := list.GroupVersion
gvs := strings.Split(groupVersion, "/")
if len(gvs) == 2 {
group = gvs[0]
version = gvs[1]
} else {
// 只有version的情况"v1"
version = groupVersion
}

for _, resource := range resources {
resource.Group = group
resource.Version = version
apiResources = append(apiResources, resource)
}
}

}

func getKubeConfig(path string) (*rest.Config, error) {
config, err := rest.InClusterConfig()

if err != nil {
klog.V(2).Infof("尝试读取集群内访问配置:%v\n", err)
klog.V(2).Infof("尝试读取本地配置%s", path)
// 不是在集群中,读取参数配置
config, err = clientcmd.BuildConfigFromFlags("", path)
if err != nil {
klog.Errorf(err.Error())
}

}
if config != nil {
klog.V(2).Infof("服务器地址:%s\n", config.Host)
}
return config, err
return &Kubectl{}
}
Loading

0 comments on commit b266170

Please sign in to comment.