1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
package main
import (
"flag"
"fmt"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/tools/clientcmd"
"os"
"path/filepath"
)
//获取家目录的环境变量值
func homeDir()string{
if h:= os.Getenv("HOME");h!= ""{
return h //linux家目录
}
return os.Getenv("USERPROFILE") //windows家目录
}
func main(){
var kubeconfig *string
// home是家目录,如果能取到家目录的值,就可以用来做默认值
if home := homeDir();home != ""{
//如果输入了kubeconfig参数,该参数的值就作为kubeconfig文件的路径
//否则就使用默认路径 ~/.kube/config
kubeconfig = flag.String("kubeconfig",filepath.Join(home,".kube","config"),"(optional) path to the kubeconfig file")
}else {
//如果取不到当前用户的家目录,就没办法设置kubeconfig的默认目录了,只能从入参中取
kubeconfig = flag.String("kubeconfig","","(optional) path to the kubeconfig file")
}
flag.Parse()
//从本机加载kubeconfig配置文件,因此第一个参数为空字符串
config , err := clientcmd.BuildConfigFromFlags("",*kubeconfig)
if err != nil{
fmt.Println("load kubeconfig failed!err:",err)
panic(err.Error())
}
//实例化一个discoveryClient对象
discoveryClient,err := discovery.NewDiscoveryClientForConfig(config)
if err != nil{
fmt.Println("init discoveryClient failed ! err: ",err)
panic(err.Error())
}
//获取所有分组的资源数据
APIGroup,APIResourceListSlice,err := discoveryClient.ServerGroupsAndResources()
if err != nil{
fmt.Println("get apigroup resource failed! err: ",err)
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)
}
}
}
输出结果:
C:\Users\xx\Desktop\git\client>go run main.go --kubeconfig inner-config
APIGroup :
[&APIGroup{Name:,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:v1,Version:v1,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:v1,Version:v1,},ServerAddressByClientCIDRs:[]ServerAddressByClientC
IDR{},} &APIGroup{Name:apiregistration.k8s.io,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:apiregistration.k8s.io/v1,Version:v1,},GroupVersionForDiscovery{GroupVersion:apiregistration.k8s.io/v1beta1,Vers
ion:v1beta1,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:apiregistration.k8s.io/v1,Version:v1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIGroup{Name:extensions,Versions:[]GroupVersionForDiscovery{G
roupVersionForDiscovery{GroupVersion:extensions/v1beta1,Version:v1beta1,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:extensions/v1beta1,Version:v1beta1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIG
roup{Name:apps,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:apps/v1,Version:v1,},GroupVersionForDiscovery{GroupVersion:apps/v1beta2,Version:v1beta2,},GroupVersionForDiscovery{GroupVersion:apps/v1beta1,Ve
rsion:v1beta1,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:apps/v1,Version:v1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIGroup{Name:events.k8s.io,Versions:[]GroupVersionForDiscovery{GroupVersionFo
rDiscovery{GroupVersion:events.k8s.io/v1beta1,Version:v1beta1,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:events.k8s.io/v1beta1,Version:v1beta1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIGroup{Na
me:authentication.k8s.io,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:authentication.k8s.io/v1,Version:v1,},GroupVersionForDiscovery{GroupVersion:authentication.k8s.io/v1beta1,Version:v1beta1,},},Preferr
edVersion:GroupVersionForDiscovery{GroupVersion:authentication.k8s.io/v1,Version:v1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIGroup{Name:authorization.k8s.io,Versions:[]GroupVersionForDiscovery{GroupVersionFor
Discovery{GroupVersion:authorization.k8s.io/v1,Version:v1,},GroupVersionForDiscovery{GroupVersion:authorization.k8s.io/v1beta1,Version:v1beta1,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:authorization.k8s.io/v1,Versio
n:v1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIGroup{Name:autoscaling,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:autoscaling/v1,Version:v1,},GroupVersionForDiscovery{GroupVersion
:autoscaling/v2beta1,Version:v2beta1,},GroupVersionForDiscovery{GroupVersion:autoscaling/v2beta2,Version:v2beta2,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:autoscaling/v1,Version:v1,},ServerAddressByClientCIDRs:[]Ser
verAddressByClientCIDR{},} &APIGroup{Name:batch,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:batch/v1,Version:v1,},GroupVersionForDiscovery{GroupVersion:batch/v1beta1,Version:v1beta1,},},PreferredVersion
:GroupVersionForDiscovery{GroupVersion:batch/v1,Version:v1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIGroup{Name:certificates.k8s.io,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:cer
tificates.k8s.io/v1beta1,Version:v1beta1,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:certificates.k8s.io/v1beta1,Version:v1beta1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIGroup{Name:networking.k
8s.io,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:networking.k8s.io/v1,Version:v1,},GroupVersionForDiscovery{GroupVersion:networking.k8s.io/v1beta1,Version:v1beta1,},},PreferredVersion:GroupVersionForDi
scovery{GroupVersion:networking.k8s.io/v1,Version:v1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIGroup{Name:policy,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:policy/v1beta1,Version
:v1beta1,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:policy/v1beta1,Version:v1beta1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIGroup{Name:rbac.authorization.k8s.io,Versions:[]GroupVersionForDisco
very{GroupVersionForDiscovery{GroupVersion:rbac.authorization.k8s.io/v1,Version:v1,},GroupVersionForDiscovery{GroupVersion:rbac.authorization.k8s.io/v1beta1,Version:v1beta1,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:
rbac.authorization.k8s.io/v1,Version:v1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIGroup{Name:storage.k8s.io,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:storage.k8s.io/v1,Version:v
1,},GroupVersionForDiscovery{GroupVersion:storage.k8s.io/v1beta1,Version:v1beta1,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:storage.k8s.io/v1,Version:v1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &A
PIGroup{Name:admissionregistration.k8s.io,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:admissionregistration.k8s.io/v1beta1,Version:v1beta1,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:admi
ssionregistration.k8s.io/v1beta1,Version:v1beta1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIGroup{Name:apiextensions.k8s.io,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:apiextension
s.k8s.io/v1beta1,Version:v1beta1,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:apiextensions.k8s.io/v1beta1,Version:v1beta1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIGroup{Name:scheduling.k8s.io,V
ersions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:scheduling.k8s.io/v1,Version:v1,},GroupVersionForDiscovery{GroupVersion:scheduling.k8s.io/v1beta1,Version:v1beta1,},},PreferredVersion:GroupVersionForDiscovery
{GroupVersion:scheduling.k8s.io/v1,Version:v1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIGroup{Name:coordination.k8s.io,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:coordination.k8s
.io/v1,Version:v1,},GroupVersionForDiscovery{GroupVersion:coordination.k8s.io/v1beta1,Version:v1beta1,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:coordination.k8s.io/v1,Version:v1,},ServerAddressByClientCIDRs:[]Server
AddressByClientCIDR{},} &APIGroup{Name:node.k8s.io,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:node.k8s.io/v1beta1,Version:v1beta1,},},PreferredVersion:GroupVersionForDiscovery{GroupVersion:node.k8s.io/
v1beta1,Version:v1beta1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},} &APIGroup{Name:metrics.k8s.io,Versions:[]GroupVersionForDiscovery{GroupVersionForDiscovery{GroupVersion:metrics.k8s.io/v1beta1,Version:v1beta1,},},
PreferredVersion:GroupVersionForDiscovery{GroupVersion:metrics.k8s.io/v1beta1,Version:v1beta1,},ServerAddressByClientCIDRs:[]ServerAddressByClientCIDR{},}]
*****************************************************************
GV string [v1]
GV struct [schema.GroupVersion{Group:"", Version:"v1"}]
resources :
bindings
componentstatuses
configmaps
endpoints
events
limitranges
namespaces
namespaces/finalize
namespaces/status
nodes
nodes/proxy
nodes/status
persistentvolumeclaims
persistentvolumeclaims/status
persistentvolumes
persistentvolumes/status
pods
pods/attach
pods/binding
pods/eviction
pods/exec
pods/log
pods/portforward
pods/proxy
pods/status
podtemplates
replicationcontrollers
replicationcontrollers/scale
replicationcontrollers/status
resourcequotas
resourcequotas/status
secrets
serviceaccounts
services
services/proxy
services/status
*****************************************************************
GV string [apiregistration.k8s.io/v1]
GV struct [schema.GroupVersion{Group:"apiregistration.k8s.io", Version:"v1"}]
resources :
apiservices
apiservices/status
*****************************************************************
GV string [apiregistration.k8s.io/v1beta1]
GV struct [schema.GroupVersion{Group:"apiregistration.k8s.io", Version:"v1beta1"}]
resources :
apiservices
apiservices/status
...
|