-
Notifications
You must be signed in to change notification settings - Fork 5
/
pod_watch.go
119 lines (110 loc) · 5.44 KB
/
pod_watch.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"context"
"log"
"time"
"github.com/forbearing/k8s/deployment"
"github.com/forbearing/k8s/pod"
corev1 "k8s.io/api/core/v1"
)
func Pod_Watch() {
var (
addFunc = func(obj interface{}) {
pod := obj.(*corev1.Pod)
log.Printf(`added pod: "%s/%s".`, pod.Namespace, pod.Name)
}
modifyFunc = func(obj interface{}) {
pod := obj.(*corev1.Pod)
log.Printf(`modified pod: "%s/%s".`, pod.Namespace, pod.Name)
}
deleteFunc = func(obj interface{}) {
pod := obj.(*corev1.Pod)
log.Printf(`deleted pod: "%s/%s".`, pod.Namespace, pod.Name)
}
)
filename := "../../testdata/examples/deployment.yaml"
name := "mydep"
podHandler := pod.NewOrDie(ctx, "", namespace)
deployHandler := deployment.NewOrDie(ctx, "", namespace)
ctx, cancel := context.WithCancel(ctx)
go func(ctx context.Context) {
time.Sleep(time.Second * 5)
podHandler.Watch(addFunc, modifyFunc, deleteFunc)
}(ctx)
go func(ctx context.Context) {
for {
deployHandler.Apply(filename)
time.Sleep(time.Second * 20)
deployHandler.Delete(name)
}
}(ctx)
timer := time.NewTimer(time.Second * 60)
<-timer.C
cancel()
deployHandler.Delete(name)
// Output:
//2022/09/08 22:47:01 added pod: "kube-system/etcd-operator-control-plane".
//2022/09/08 22:47:01 added pod: "kube-system/kube-apiserver-operator-control-plane".
//2022/09/08 22:47:01 added pod: "kube-system/kube-controller-manager-operator-control-plane".
//2022/09/08 22:47:01 added pod: "kube-system/kube-scheduler-operator-control-plane".
//2022/09/08 22:47:01 added pod: "test/mydep-859646944d-72nqr".
//2022/09/08 22:47:01 added pod: "local-path-storage/local-path-provisioner-66b445c94-k8frj".
//2022/09/08 22:47:01 added pod: "test/mydep-859646944d-ls4mk".
//2022/09/08 22:47:01 added pod: "test/mydep-859646944d-scr5v".
//2022/09/08 22:47:01 added pod: "default/nginx-85b98978db-tf4w7".
//2022/09/08 22:47:01 added pod: "kube-system/coredns-64897985d-467q7".
//2022/09/08 22:47:01 added pod: "kube-system/coredns-64897985d-tgjph".
//2022/09/08 22:47:01 added pod: "kube-system/kindnet-pg7xh".
//2022/09/08 22:47:01 added pod: "kube-system/kube-proxy-p6k42".
//2022/09/08 22:47:04 deleted pod: "test/mydep-859646944d-ls4mk".
//2022/09/08 22:47:09 deleted pod: "test/mydep-859646944d-72nqr".
//2022/09/08 22:47:11 deleted pod: "test/mydep-859646944d-scr5v".
//2022/09/08 22:47:16 added pod: "test/mydep-67fcc784fc-hfjhx".
//2022/09/08 22:47:16 added pod: "test/mydep-67fcc784fc-r6b5g".
//2022/09/08 22:47:16 deleted pod: "test/mydep-67fcc784fc-hfjhx".
//2022/09/08 22:47:16 added pod: "test/mydep-67fcc784fc-jlccm".
//2022/09/08 22:47:16 deleted pod: "test/mydep-859646944d-72nqr".
//2022/09/08 22:47:16 deleted pod: "test/mydep-859646944d-scr5v".
//2022/09/08 22:47:16 deleted pod: "test/mydep-859646944d-ls4mk".
//2022/09/08 22:47:16 deleted pod: "test/mydep-67fcc784fc-r6b5g".
//2022/09/08 22:47:16 deleted pod: "test/mydep-67fcc784fc-jlccm".
//2022/09/08 22:47:16 deleted pod: "test/mydep-67fcc784fc-hfjhx".
//2022/09/08 22:47:16 deleted pod: "test/mydep-859646944d-scr5v".
//2022/09/08 22:47:16 deleted pod: "test/mydep-67fcc784fc-r6b5g".
//2022/09/08 22:47:16 deleted pod: "test/mydep-67fcc784fc-jlccm".
//2022/09/08 22:47:17 deleted pod: "test/mydep-859646944d-ls4mk".
//2022/09/08 22:47:17 deleted pod: "test/mydep-859646944d-ls4mk".
//2022/09/08 22:47:17 modified pod: "test/mydep-859646944d-ls4mk".
//2022/09/08 22:47:18 deleted pod: "test/mydep-859646944d-72nqr".
//2022/09/08 22:47:18 deleted pod: "test/mydep-859646944d-72nqr".
//2022/09/08 22:47:18 modified pod: "test/mydep-859646944d-72nqr".
//2022/09/08 22:47:19 deleted pod: "test/mydep-859646944d-scr5v".
//2022/09/08 22:47:19 modified pod: "test/mydep-859646944d-scr5v".
//2022/09/08 22:47:22 deleted pod: "test/mydep-67fcc784fc-hfjhx".
//2022/09/08 22:47:27 deleted pod: "test/mydep-67fcc784fc-jlccm".
//2022/09/08 22:47:30 deleted pod: "test/mydep-67fcc784fc-r6b5g".
//2022/09/08 22:47:36 deleted pod: "test/mydep-67fcc784fc-hfjhx".
//2022/09/08 22:47:36 deleted pod: "test/mydep-67fcc784fc-r6b5g".
//2022/09/08 22:47:36 added pod: "test/mydep-859646944d-xl7rb".
//2022/09/08 22:47:36 deleted pod: "test/mydep-67fcc784fc-jlccm".
//2022/09/08 22:47:36 added pod: "test/mydep-859646944d-glwh6".
//2022/09/08 22:47:36 deleted pod: "test/mydep-859646944d-xl7rb".
//2022/09/08 22:47:36 added pod: "test/mydep-859646944d-z55cl".
//2022/09/08 22:47:36 deleted pod: "test/mydep-859646944d-glwh6".
//2022/09/08 22:47:36 deleted pod: "test/mydep-859646944d-z55cl".
//2022/09/08 22:47:36 deleted pod: "test/mydep-859646944d-xl7rb".
//2022/09/08 22:47:36 deleted pod: "test/mydep-859646944d-z55cl".
//2022/09/08 22:47:36 deleted pod: "test/mydep-859646944d-glwh6".
//2022/09/08 22:47:37 deleted pod: "test/mydep-67fcc784fc-jlccm".
//2022/09/08 22:47:37 deleted pod: "test/mydep-67fcc784fc-jlccm".
//2022/09/08 22:47:37 modified pod: "test/mydep-67fcc784fc-jlccm".
//2022/09/08 22:47:38 deleted pod: "test/mydep-67fcc784fc-hfjhx".
//2022/09/08 22:47:38 deleted pod: "test/mydep-67fcc784fc-hfjhx".
//2022/09/08 22:47:38 modified pod: "test/mydep-67fcc784fc-hfjhx".
//2022/09/08 22:47:39 deleted pod: "test/mydep-67fcc784fc-r6b5g".
//2022/09/08 22:47:39 deleted pod: "test/mydep-67fcc784fc-r6b5g".
//2022/09/08 22:47:39 modified pod: "test/mydep-67fcc784fc-r6b5g".
//2022/09/08 22:47:41 deleted pod: "test/mydep-859646944d-xl7rb".
//2022/09/08 22:47:45 deleted pod: "test/mydep-859646944d-glwh6".
//2022/09/08 22:47:48 deleted pod: "test/mydep-859646944d-z55cl".
}