github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;
名称 | 链接 | 备注 |
---|---|---|
项目主页 | github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
go mod init discoveryclientdemo
go get k8s.io/api@v0.20.0 go get k8s.io/client-go@v0.20.0
package main import ( "flag" "fmt" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/discovery" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/util/homedir" "path/filepath" ) func main() { var kubeconfig *string // home是家目录,如果能取得家目录的值,就可以用来做默认值 if home:=homedir.HomeDir(); home != "" { // 如果输入了kubeconfig参数,该参数的值就是kubeconfig文件的绝对路径, // 如果没有输入kubeconfig参数,就用默认路径~/.kube/config kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") } else { // 如果取不到当前用户的家目录,就没办法设置kubeconfig的默认目录了,只能从入参中取 kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file") } flag.Parse() // 从本机加载kubeconfig配置文件,因此第一个参数为空字符串 config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) // kubeconfig加载失败就直接退出了 if err != nil { panic(err.Error()) } // 新建discoveryClient实例 discoveryClient, err := discovery.NewDiscoveryClientForConfig(config) if err != nil { panic(err.Error()) } // 获取所有分组和资源数据 APIGroup, APIResourceListSlice, err := discoveryClient.ServerGroupsAndResources() if err != nil { panic(err.Error()) } // 先看Group信息 fmt.Printf("APIGroup :\n\n %v\n\n\n\n",APIGroup) // APIResourceListSlice是个切片,里面的每个元素代表一个GroupVersion及其资源 for _, singleAPIResourceList := range APIResourceListSlice { // GroupVersion是个字符串,例如"apps/v1" groupVerionStr := singleAPIResourceList.GroupVersion // ParseGroupVersion方法将字符串转成数据结构 gv, err := schema.ParseGroupVersion(groupVerionStr) if err != nil { panic(err.Error()) } fmt.Println("*****************************************************************") fmt.Printf("GV string [%v]\nGV struct [%#v]\nresources :\n\n", groupVerionStr, gv) // APIResources字段是个切片,里面是当前GroupVersion下的所有资源 for _, singleAPIResource := range singleAPIResourceList.APIResources { fmt.Printf("%v\n", singleAPIResource.Name) } } }
... ***************************************************************** GV string [discovery.k8s.io/v1beta1] GV struct [schema.GroupVersion{Group:"discovery.k8s.io", Version:"v1beta1"}] resources : endpointslices ***************************************************************** GV string [flowcontrol.apiserver.k8s.io/v1beta1] GV struct [schema.GroupVersion{Group:"flowcontrol.apiserver.k8s.io", Version:"v1beta1"}] resources : flowschemas flowschemas/status prioritylevelconfigurations prioritylevelconfigurations/status
zhaoqin@zhaoqindeMBP-2 discoveryclientdemo % kubectl api-versions admissionregistration.k8s.io/v1 admissionregistration.k8s.io/v1beta1 apiextensions.k8s.io/v1 apiextensions.k8s.io/v1beta1 apiregistration.k8s.io/v1 apiregistration.k8s.io/v1beta1 apps/v1 authentication.k8s.io/v1 ...
type APIVersionsOptions struct { discoveryClient discovery.CachedDiscoveryInterface genericclioptions.IOStreams }
从名称CachedDiscoveryInterface来看,kubectl对GVR数据是做了本地缓存的,想想也是,GVR不经常变化,没必要每次都去API Server拉取,关于缓存的细节请参考:staging/src/k8s.io/client-go/discovery/cached/disk/cached_discovery.go ,这里就不展开了;
至此,client-go的四种客户端工具实战以及相关源码的浅层次分析就全部完成了,在您做client-go开发的时候,希望这些内容能给您提供一些参考;
我是欣宸,期待与您一同畅游Java世界…