You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.3 KiB

package main
import (
"ClientGo/utils"
"fmt"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"log"
)
/*
*
@author: sre
@date: 2022/8/10 0010
@desc: DiscoveryClient 是针对于资源的。用于查看当前 Kubernetes 集群支持那些资源组、资源版本、资源信息。
*
*/
func GetDiscoveryClient() (*discovery.DiscoveryClient, error) {
config, err := utils.GetConfig()
if err != nil {
log.Println("GetConfig 失败: ", err.Error())
return nil, err
}
// 实例化 DiscoveryClient
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
log.Println("NewDiscoveryClientForConfig 失败: ", err.Error())
return nil, err
}
return discoveryClient, nil
}
func GetDiscoveryClientRes() {
discoveryClient, err := GetDiscoveryClient()
if err != nil {
log.Println("GetDiscoveryClient 失败: ", err.Error())
return
}
_, apiResources, err := discoveryClient.ServerGroupsAndResources()
if err != nil {
panic(err.Error())
}
for _, list := range apiResources {
gv, err := schema.ParseGroupVersion(list.GroupVersion)
if err != nil {
panic(err.Error())
}
for _, resource := range list.APIResources {
fmt.Printf("name: %v, group: %v, version: %v\n", resource.Name, gv.Group, gv.Version)
}
}
}