HAOJX

如何通过grk来找gvr,或者gvr找gvk

字数统计: 988阅读时长: 5 min
2021/12/29 Share

简单的一个例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
....

gr, err := restmapper.GetAPIGroupResources(client.Discovery())
if err != nil {
log.Fatal(err)
}

mapper := restmapper.NewDiscoveryRESTMapper(gr)
mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
log.Fatal(err)
}

gvr := schema.GroupVersionResource{
Group: mapping.Resource.Group,
Version: mapping.Resource.Version,
Resource: mapping.Resource.Resource,
}

....

gvr找gvk

1
2
3
4
5
6
....
gvk1, err := mapper.KindFor(gvr)
if err != nil {
log.Fatal(err)
}
....

附录

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
package main

import (
"bytes"
"fmt"
"io"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/restmapper"
"kube-base3/lib/kubeConfig"
"log"
"os/exec"
)

func kust(path string) []byte {
cmd := exec.Command("kustomize", "build", path)
var ret bytes.Buffer
cmd.Stdout = &ret
if err := cmd.Run(); err != nil {
fmt.Println(err)
return nil
}
return ret.Bytes()
}
func main() {
//dc := kubeConfig.InitDynamicClient()
client := kubeConfig.InitClient()
b := kust("pkg/deploy")
decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewReader(b), len(b))

for {
var rawObj runtime.RawExtension
err := decoder.Decode(&rawObj)

if err != nil {
if err == io.EOF {
break
} else {
log.Fatal(err)
}

}
fmt.Println("这个是raw", rawObj, "raw结束")

obj, gvk, err := syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).
Decode(rawObj.Raw, nil, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("这个是obj", obj, "obj结束")
fmt.Println("这个是gvk", gvk, "gvk结束")

unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
log.Fatal(err)
}
fmt.Println("这个是unstructuredMap", unstructuredMap, "unstructured结束")

unstructuredObj := &unstructured.Unstructured{Object: unstructuredMap}
fmt.Println("这个是unstructuredobj", unstructuredObj, "unstructuredobj结束")

gr, err := restmapper.GetAPIGroupResources(client.Discovery())
if err != nil {
log.Fatal(err)
}

mapper := restmapper.NewDiscoveryRESTMapper(gr)
mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
log.Fatal(err)
}

gvr := schema.GroupVersionResource{
Group: mapping.Resource.Group,
Version: mapping.Resource.Version,
Resource: mapping.Resource.Resource,
}

//_, err = dc.Resource(gvr).Namespace(unstructuredObj.GetNamespace()).
// Create(context.Background(), unstructuredObj, metav1.CreateOptions{})
//
//if err != nil {
// log.Fatal(err)
//}
gvk1, err := mapper.KindFor(gvr)
if err != nil {
log.Fatal(err)
}
fmt.Println("这是gvk1", gvk1, "gvk1结束")

}
}

输出结果

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
这个是raw {[123 34 97 112 105 86 101 114 115 105 111 110 34 58 34 118 49 34 44 34 107 105 110 100 34 58 34 83 101 114 118 105 99 101 34 44 34 109 101 116 97 100 97 116 97 34 58 123 
34 110 97 109 101 34 58 34 109 121 103 111 116 116 45 115 118 99 34 44 34 110 97 109 101 115 112 97 99 101 34 58 34 100 101 102 97 117 108 116 34 125 44 34 115 112 101 99 34 58 123
34 112 111 114 116 115 34 58 91 123 34 112 111 114 116 34 58 56 48 44 34 116 97 114 103 101 116 80 111 114 116 34 58 56 48 125 93 44 34 115 101 108 101 99 116 111 114 34 58 123 34 9
7 112 112 34 58 34 109 121 103 111 116 116 34 125 44 34 116 121 112 101 34 58 34 67 108 117 115 116 101 114 73 80 34 125 125] <nil>} raw结束
这个是obj &{map[apiVersion:v1 kind:Service metadata:map[name:mygott-svc namespace:default] spec:map[ports:[map[port:80 targetPort:80]] selector:map[app:mygott] type:ClusterIP]]} obj
结束

这个是gvk /v1, Kind=Service gvk结束

这个是unstructuredMap map[apiVersion:v1 kind:Service metadata:map[name:mygott-svc namespace:default] spec:map[ports:[map[port:80 targetPort:80]] selector:map[app:mygott] type:Cluste
rIP]] unstructured结束

这个是unstructuredobj &{map[apiVersion:v1 kind:Service metadata:map[name:mygott-svc namespace:default] spec:map[ports:[map[port:80 targetPort:80]] selector:map[app:mygott] type:Clus
terIP]]} unstructuredobj结束

这是gvk1 /v1, Kind=Service gvk1结束

这个是raw {[123 34 97 112 105 86 101 114 115 105 111 110 34 58 34 97 112 112 115 47 118 49 34 44 34 107 105 110 100 34 58 34 68 101 112 108 111 121 109 101 110 116 34 44 34 109 101
116 97 100 97 116 97 34 58 123 34 110 97 109 101 34 58 34 109 121 103 111 116 116 34 44 34 110 97 109 101 115 112 97 99 101 34 58 34 100 101 102 97 117 108 116 34 125 44 34 115 112
101 99 34 58 123 34 114 101 112 108 105 99 97 115 34 58 49 44 34 115 101 108 101 99 116 111 114 34 58 123 34 109 97 116 99 104 76 97 98 101 108 115 34 58 123 34 97 112 112 34 58 34
etadata:map[labels:map[app:mygott]] spec:map[containers:[map[image:registry.cn-beijing.aliyuncs.com/jtthinkweb/mygo:v1 imagePullPolicy:IfNotPresent name:jtproxy ports:[map[container
Port:80]]]] imagePullSecrets:[map[name:img-secret]]]]]] unstructured结束

这个是unstructuredobj &{map[apiVersion:apps/v1 kind:Deployment metadata:map[name:mygott namespace:default] spec:map[replicas:1 selector:map[matchLabels:map[app:mygott]] template:map
[metadata:map[labels:map[app:mygott]] spec:map[containers:[map[image:registry.cn-beijing.aliyuncs.com/jtthinkweb/mygo:v1 imagePullPolicy:IfNotPresent name:jtproxy ports:[map[contain
erPort:80]]]] imagePullSecrets:[map[name:img-secret]]]]]]} unstructuredobj结束

这是gvk1 apps/v1, Kind=Deployment gvk1结束
CATALOG