generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 86
/
crd.go
59 lines (50 loc) · 1.35 KB
/
crd.go
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
package kubernetes
import (
"context"
"github.com/layer5io/meshkit/utils"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
)
type CRD struct {
Items []CRDItem `json:"items"`
}
type CRDItem struct {
Spec Spec `json:"spec"`
}
type Spec struct {
Names names `json:"names"`
Group string `json:"group"`
Versions []struct {
Name string `json:"name"`
} `json:"versions"`
}
type names struct {
ResourceName string `json:"plural"`
}
func GetAllCustomResourcesInCluster(ctx context.Context, client rest.Interface) ([]*schema.GroupVersionResource, error) {
crdresult, err := client.Get().RequestURI("/apis/apiextensions.k8s.io/v1/customresourcedefinitions").Do(context.Background()).Raw()
if err != nil {
return nil, err
}
var xcrd CRD
gvks := []*schema.GroupVersionResource{}
err = utils.Unmarshal(string(crdresult), &xcrd)
if err != nil {
return nil, err
}
for _, c := range xcrd.Items {
gvks = append(gvks, GetGVRForCustomResources(&c))
}
return gvks, nil
}
func GetGVRForCustomResources(crd *CRDItem) *schema.GroupVersionResource {
return &schema.GroupVersionResource{
Group: crd.Spec.Group,
Version: crd.Spec.Versions[0].Name,
Resource: crd.Spec.Names.ResourceName,
}
}
func IsCRD(manifest map[string]interface{}) bool {
kind, ok := manifest["kind"].(string)
return ok && kind == "CustomResourceDefinition"
}