-
Notifications
You must be signed in to change notification settings - Fork 5
/
dynamic_list.go
60 lines (51 loc) · 1.69 KB
/
dynamic_list.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"
"fmt"
"log"
"github.com/forbearing/k8s/deployment"
"github.com/forbearing/k8s/dynamic"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
func Dynamic_List() {
filename := "../../testdata/examples/deployment.yaml"
handler := dynamic.NewOrDie(context.TODO(), "", namespace)
defer cleanup(handler)
if _, err := handler.Apply(deployUnstructData); err != nil {
log.Fatal(err)
}
if _, err := handler.Apply(filename); err != nil {
log.Fatal(err)
}
gvk := deployment.GVK
label := "type=deployment"
field := fmt.Sprintf("metadata.namespace=%s", namespace)
_ = label
_ = field
ul, err := handler.WithGVK(gvk).List()
checkErr("List()", output(ul), err)
ul2, err := handler.WithGVK(gvk).ListAll()
checkErr("ListAll()", output(ul2), err)
ul3, err := handler.WithGVK(gvk).ListByLabel(label)
checkErr("ListByLabel()", output(ul3), err)
ul4, err := handler.WithGVK(gvk).ListByField(field)
checkErr("ListByField()", output(ul4), err)
ul5, err := handler.WithGVK(gvk).ListByNamespace(namespace)
checkErr("ListByNamespace()", output(ul5), err)
// Output:
//2022/09/07 18:15:19 List() success: [nginx coredns local-path-provisioner mydep mydep-unstruct]
//2022/09/07 18:15:19 ListAll() success: [nginx coredns local-path-provisioner mydep mydep-unstruct]
//2022/09/07 18:15:19 ListByLabel() success: [mydep mydep-unstruct]
//2022/09/07 18:15:19 ListByField() success: [mydep mydep-unstruct]
//2022/09/07 18:15:19 ListByNamespace() success: [mydep mydep-unstruct]
}
func output(unstructList []*unstructured.Unstructured) []string {
if unstructList == nil {
return nil
}
var ul []string
for _, u := range unstructList {
ul = append(ul, u.GetName())
}
return ul
}