-
Notifications
You must be signed in to change notification settings - Fork 5
/
dynamic_get.go
60 lines (50 loc) · 1.7 KB
/
dynamic_get.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
60
package main
import (
"context"
"github.com/forbearing/k8s/dynamic"
"github.com/forbearing/k8s/pod"
)
func Dynamic_Get() {
handler := dynamic.NewOrDie(context.TODO(), "", namespace)
defer cleanup(handler)
// get deployment
if _, err := handler.Apply(deployUnstructData); err != nil {
panic(err)
}
// if the Get() parameter is []byte which containing the resource defination.
// it's not necessarily to provides GroupVersionKind with WithGVK() method.
u1, err := handler.Get(deployUnstructData)
checkErr("get deployment", u1.GetName(), err)
// get pod
if _, err := handler.Apply(podUnstructData); err != nil {
panic(err)
}
// if the Get() parameter is resource name, you should call WithGVK()
// to provides the GroupVersionkind of this resource.
u2, err := handler.WithGVK(pod.GVK).Get(podUnstructName)
checkErr("get pod", u2.GetName(), err)
// get namespace
if _, err := handler.Apply(nsUnstructData); err != nil {
panic(err)
}
u3, err := handler.Get(nsUnstructData)
checkErr("get namespace", u3.GetName(), err)
// get persistentvolume
if _, err := handler.Apply(pvUnstructData); err != nil {
panic(err)
}
u4, err := handler.Get(pvUnstructData)
checkErr("get persistentvolume", u4.GetName(), err)
// get clusterrole
if _, err := handler.Apply(crUnstructData); err != nil {
panic(err)
}
u5, err := handler.Get(crUnstructData)
checkErr("get clusterrole", u5.GetName(), err)
// Output:
//2022/09/03 22:04:10 get deployment success: mydep-unstruct
//2022/09/03 22:04:10 get pod success: pod-unstruct
//2022/09/03 22:04:10 get namespace success: ns-unstruct
//2022/09/03 22:04:11 get persistentvolume success: pv-unstruct
//2022/09/03 22:04:11 get clusterrole success: cr-unstruct
}