HAOJX

获取资源api-reasources

字数统计: 179阅读时长: 1 min
2021/11/25 Share

方法1:

1
2
3
4
5
6
7
8
9
10
11
12
13
func (this *ResourcesCtl) ListResources(c *gin.Context) goft.Json{
_,res,err:=this.Client.ServerGroupsAndResources()
goft.Error(err)
gRes:= make([]*GroupResources,0)
for _,r := range res{
group,version:=this.GetGroupVersion(r.GroupVersion)
gr:= &GroupResources{Group: group,Version: version,Resources: make([]*Resources,0)}
for _,rr:= range r.APIResources{
res:=&Resources{Name: rr.Name,Verbs: rr.Verbs}
gr.Resources=append(gr.Resources,res)
}
gRes=append(gRes,gr)
}

其中GetGroupVersion函数是

1
2
3
4
5
6
7
8
9
func (c *ResourcesCtl) GetGroupVersion(str string) (group,version string) {
list:= strings.Split(str,"/")
if len(list)==1 {
return "core", list[0]
}else if len(list)==2 {
return list[0], list[1]
}
panic("error groupversion"+str)
}

方法2:

用restmapper也可以

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import(
"k8s.io/client-go/restmapper"
)
.....

gr, err := restmapper.GetAPIGroupResources(client.Discovery())

if err != nil {
log.Fatal(err)
}

for _, resources := range gr {
fmt.Println("这个是resources", resources.Group, "resources结束")
}
.....

这种也可以

CATALOG