-
Notifications
You must be signed in to change notification settings - Fork 5
/
deployment_watch_all.go
100 lines (91 loc) · 3.67 KB
/
deployment_watch_all.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"context"
"log"
"time"
"github.com/forbearing/k8s/deployment"
appsv1 "k8s.io/api/apps/v1"
)
func Deployment_Watch_All() {
var (
addFunc = func(obj interface{}) {
deploy := obj.(*appsv1.Deployment)
log.Printf(`added deployment: "%s/%s".`, deploy.Namespace, deploy.Name)
}
modifyFunc = func(obj interface{}) {
deploy := obj.(*appsv1.Deployment)
log.Printf(`modified deployment: "%s/%s".`, deploy.Namespace, deploy.Name)
}
deleteFunc = func(obj interface{}) {
deploy := obj.(*appsv1.Deployment)
log.Printf(`deleted deployment: "%s/%s".`, deploy.Namespace, deploy.Name)
}
)
filename := "../../testdata/examples/deployment.yaml"
filename2 := "../../testdata/examples/deployment-2.yaml"
name := "mydep"
name2 := "mydep2"
handler := deployment.NewOrDie(ctx, "", namespace)
ctx, cancel := context.WithCancel(ctx)
go func(ctx context.Context) {
time.Sleep(time.Second * 5)
handler.Watch(addFunc, modifyFunc, deleteFunc)
}(ctx)
go func(ctx context.Context) {
for {
handler.Apply(filename)
handler.Apply(filename2)
time.Sleep(time.Second * 20)
handler.Delete(name)
handler.Delete(name2)
}
}(ctx)
timer := time.NewTimer(time.Second * 60)
<-timer.C
cancel()
handler.Delete(name)
handler.Delete(name2)
// Outputs:
//2022/09/05 17:13:14 added deployment: "kube-system/coredns".
//2022/09/05 17:13:14 added deployment: "local-path-storage/local-path-provisioner".
//2022/09/05 17:13:14 added deployment: "test/mydep".
//2022/09/05 17:13:14 added deployment: "test/mydep2".
//2022/09/05 17:13:14 added deployment: "default/nginx".
//2022/09/05 17:13:15 deleted deployment: "test/mydep2".
//2022/09/05 17:13:18 deleted deployment: "test/mydep".
//2022/09/05 17:13:21 deleted deployment: "test/mydep2".
//2022/09/05 17:13:24 deleted deployment: "test/mydep2".
//2022/09/05 17:13:28 deleted deployment: "test/mydep".
//2022/09/05 17:13:29 modified deployment: "test/mydep".
//2022/09/05 17:13:29 modified deployment: "test/mydep2".
//2022/09/05 17:13:29 added deployment: "test/mydep".
//2022/09/05 17:13:29 added deployment: "test/mydep2".
//2022/09/05 17:13:29 deleted deployment: "test/mydep".
//2022/09/05 17:13:29 deleted deployment: "test/mydep2".
//2022/09/05 17:13:29 deleted deployment: "test/mydep".
//2022/09/05 17:13:29 deleted deployment: "test/mydep2".
//2022/09/05 17:13:29 deleted deployment: "test/mydep".
//2022/09/05 17:13:29 deleted deployment: "test/mydep2".
//2022/09/05 17:13:29 deleted deployment: "test/mydep2".
//2022/09/05 17:13:29 deleted deployment: "test/mydep".
//2022/09/05 17:13:37 deleted deployment: "test/mydep2".
//2022/09/05 17:13:39 deleted deployment: "test/mydep".
//2022/09/05 17:13:44 deleted deployment: "test/mydep".
//2022/09/05 17:13:49 modified deployment: "test/mydep".
//2022/09/05 17:13:49 modified deployment: "test/mydep2".
//2022/09/05 17:13:49 added deployment: "test/mydep".
//2022/09/05 17:13:49 added deployment: "test/mydep2".
//2022/09/05 17:13:49 deleted deployment: "test/mydep".
//2022/09/05 17:13:49 deleted deployment: "test/mydep2".
//2022/09/05 17:13:49 deleted deployment: "test/mydep".
//2022/09/05 17:13:49 deleted deployment: "test/mydep2".
//2022/09/05 17:13:49 deleted deployment: "test/mydep".
//2022/09/05 17:13:49 deleted deployment: "test/mydep2".
//2022/09/05 17:13:57 deleted deployment: "test/mydep".
//2022/09/05 17:13:58 deleted deployment: "test/mydep2".
//2022/09/05 17:14:01 deleted deployment: "test/mydep".
//2022/09/05 17:14:04 deleted deployment: "test/mydep".
//2022/09/05 17:14:07 deleted deployment: "test/mydep2".
//2022/09/05 17:14:09 modified deployment: "test/mydep".
//2022/09/05 17:14:09 modified deployment: "test/mydep2".
}