-
Notifications
You must be signed in to change notification settings - Fork 5
/
dynamic_watch_single.go
83 lines (74 loc) · 2.76 KB
/
dynamic_watch_single.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"context"
"log"
"time"
"github.com/forbearing/k8s/deployment"
"github.com/forbearing/k8s/dynamic"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
func Dynamic_Watch_Single() {
filename1 := "../../testdata/examples/deployment.yaml"
filename2 := "../../testdata/examples/deployment-2.yaml"
filename3 := "../../testdata/examples/deployment-nolabel.yaml"
name1 := "mydep"
name2 := "mydep2"
name3 := "mydep-nolabel"
gvk := deployment.GVK
var (
addFunc = func(obj interface{}) {
unstructObj := obj.(*unstructured.Unstructured)
log.Printf(`added %s: "%s/%s".`, gvk.Kind, unstructObj.GetNamespace(), unstructObj.GetName())
}
modifyFunc = func(obj interface{}) {
unstructObj := obj.(*unstructured.Unstructured)
log.Printf(`modified %s: "%s/%s".`, gvk.Kind, unstructObj.GetNamespace(), unstructObj.GetName())
}
deleteFunc = func(obj interface{}) {
unstructObj := obj.(*unstructured.Unstructured)
log.Printf(`deleted %s: "%s/%s".`, gvk.Kind, unstructObj.GetNamespace(), unstructObj.GetName())
}
)
handler := dynamic.NewOrDie(context.TODO(), "", namespace)
ctx, cancel := context.WithCancel(context.TODO())
go func(ctx context.Context) {
time.Sleep(time.Second * 5)
handler.WithGVK(gvk).WatchByName(name1, addFunc, modifyFunc, deleteFunc)
}(ctx)
go func(ctx context.Context) {
for {
handler.Apply(filename1)
handler.Apply(filename2)
handler.Apply(filename3)
time.Sleep(time.Second * 20)
handler.WithGVK(gvk).Delete(name1)
handler.WithGVK(gvk).Delete(name2)
handler.WithGVK(gvk).Delete(name3)
}
}(ctx)
timer := time.NewTimer(time.Second * 60)
<-timer.C
cancel()
handler.WithGVK(gvk).Delete(name1)
handler.WithGVK(gvk).Delete(name2)
handler.WithGVK(gvk).Delete(name3)
// Output
//2022/09/07 21:41:32 added Deployment: "test/mydep".
//2022/09/07 21:41:44 modified Deployment: "test/mydep".
//2022/09/07 21:41:47 modified Deployment: "test/mydep".
//2022/09/07 21:41:47 deleted Deployment: "test/mydep".
//2022/09/07 21:41:47 added Deployment: "test/mydep".
//2022/09/07 21:41:47 modified Deployment: "test/mydep".
//2022/09/07 21:41:47 modified Deployment: "test/mydep".
//2022/09/07 21:41:47 modified Deployment: "test/mydep".
//2022/09/07 21:41:47 modified Deployment: "test/mydep".
//2022/09/07 21:41:54 modified Deployment: "test/mydep".
//2022/09/07 21:42:04 modified Deployment: "test/mydep".
//2022/09/07 21:42:07 deleted Deployment: "test/mydep".
//2022/09/07 21:42:07 added Deployment: "test/mydep".
//2022/09/07 21:42:07 modified Deployment: "test/mydep".
//2022/09/07 21:42:07 modified Deployment: "test/mydep".
//2022/09/07 21:42:07 modified Deployment: "test/mydep".
//2022/09/07 21:42:13 modified Deployment: "test/mydep".
//2022/09/07 21:42:17 modified Deployment: "test/mydep".
}