-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(kubectl): 重构 kubectl 模块并移除未使用的代码
- 删除了 kubectl 模块中的大量未使用代码 - 移除了 apply.go、client.go、dynamic.go 等多个文件 - 保留了 pod.go 中的 StreamPodLogs 函数 - 更新了相关引用和依赖
- Loading branch information
Showing
15 changed files
with
51 additions
and
1,193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
} |
Oops, something went wrong.