-
Notifications
You must be signed in to change notification settings - Fork 5
/
deployment_watch_field.go
97 lines (88 loc) · 3.47 KB
/
deployment_watch_field.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
package main
import (
"context"
"log"
"time"
"github.com/forbearing/k8s/deployment"
appsv1 "k8s.io/api/apps/v1"
)
func Deployment_Watch_Field() {
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"
field := "metadata.namespace=test"
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.WatchByField(field, 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:11:14 added deployment: "test/mydep".
//2022/09/05 17:11:14 added deployment: "test/mydep2".
//2022/09/05 17:11:15 modified deployment: "test/mydep".
//2022/09/05 17:11:19 modified deployment: "test/mydep".
//2022/09/05 17:11:22 modified deployment: "test/mydep2".
//2022/09/05 17:11:25 modified deployment: "test/mydep2".
//2022/09/05 17:11:29 deleted deployment: "test/mydep".
//2022/09/05 17:11:29 deleted deployment: "test/mydep2".
//2022/09/05 17:11:29 added deployment: "test/mydep".
//2022/09/05 17:11:29 added deployment: "test/mydep2".
//2022/09/05 17:11:29 modified deployment: "test/mydep".
//2022/09/05 17:11:29 modified deployment: "test/mydep2".
//2022/09/05 17:11:29 modified deployment: "test/mydep".
//2022/09/05 17:11:29 modified deployment: "test/mydep2".
//2022/09/05 17:11:29 modified deployment: "test/mydep".
//2022/09/05 17:11:29 modified deployment: "test/mydep2".
//2022/09/05 17:11:29 modified deployment: "test/mydep".
//2022/09/05 17:11:29 modified deployment: "test/mydep2".
//2022/09/05 17:11:37 modified deployment: "test/mydep".
//2022/09/05 17:11:37 modified deployment: "test/mydep".
//2022/09/05 17:11:40 modified deployment: "test/mydep".
//2022/09/05 17:11:43 modified deployment: "test/mydep2".
//2022/09/05 17:11:47 modified deployment: "test/mydep2".
//2022/09/05 17:11:49 deleted deployment: "test/mydep".
//2022/09/05 17:11:49 deleted deployment: "test/mydep2".
//2022/09/05 17:11:49 added deployment: "test/mydep".
//2022/09/05 17:11:49 added deployment: "test/mydep2".
//2022/09/05 17:11:49 modified deployment: "test/mydep".
//2022/09/05 17:11:49 modified deployment: "test/mydep2".
//2022/09/05 17:11:49 modified deployment: "test/mydep".
//2022/09/05 17:11:49 modified deployment: "test/mydep2".
//2022/09/05 17:11:49 modified deployment: "test/mydep2".
//2022/09/05 17:11:49 modified deployment: "test/mydep".
//2022/09/05 17:11:57 modified deployment: "test/mydep".
//2022/09/05 17:11:58 modified deployment: "test/mydep".
//2022/09/05 17:12:01 modified deployment: "test/mydep".
//2022/09/05 17:12:09 deleted deployment: "test/mydep".
//2022/09/05 17:12:09 deleted deployment: "test/mydep2".
}