@@ -24,18 +24,13 @@ package deployment
24
24
25
25
import (
26
26
"k8s.io/api/core/v1"
27
- "k8s.io/apimachinery/pkg/fields"
28
27
"k8s.io/client-go/tools/cache"
28
+
29
+ "github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
29
30
)
30
31
31
32
// listenForPodEvents keep listening for changes in pod until the given channel is closed.
32
33
func (d * Deployment ) listenForPodEvents (stopCh <- chan struct {}) {
33
- source := cache .NewListWatchFromClient (
34
- d .deps .KubeCli .CoreV1 ().RESTClient (),
35
- "pods" ,
36
- d .apiObject .GetNamespace (),
37
- fields .Everything ())
38
-
39
34
getPod := func (obj interface {}) (* v1.Pod , bool ) {
40
35
pod , ok := obj .(* v1.Pod )
41
36
if ! ok {
@@ -49,35 +44,35 @@ func (d *Deployment) listenForPodEvents(stopCh <-chan struct{}) {
49
44
return pod , true
50
45
}
51
46
52
- _ , informer := cache .NewIndexerInformer (source , & v1.Pod {}, 0 , cache.ResourceEventHandlerFuncs {
53
- AddFunc : func (obj interface {}) {
54
- if p , ok := getPod (obj ); ok && d .isOwnerOf (p ) {
55
- d .triggerInspection ()
56
- }
57
- },
58
- UpdateFunc : func (oldObj , newObj interface {}) {
59
- if p , ok := getPod (newObj ); ok && d .isOwnerOf (p ) {
60
- d .triggerInspection ()
61
- }
62
- },
63
- DeleteFunc : func (obj interface {}) {
64
- if p , ok := getPod (obj ); ok && d .isOwnerOf (p ) {
65
- d .triggerInspection ()
66
- }
67
- },
68
- }, cache.Indexers {})
69
-
70
- informer .Run (stopCh )
47
+ rw := k8sutil .NewResourceWatcher (
48
+ d .deps .Log ,
49
+ d .deps .KubeCli .CoreV1 ().RESTClient (),
50
+ "pods" ,
51
+ d .apiObject .GetNamespace (),
52
+ & v1.Pod {},
53
+ cache.ResourceEventHandlerFuncs {
54
+ AddFunc : func (obj interface {}) {
55
+ if p , ok := getPod (obj ); ok && d .isOwnerOf (p ) {
56
+ d .triggerInspection ()
57
+ }
58
+ },
59
+ UpdateFunc : func (oldObj , newObj interface {}) {
60
+ if p , ok := getPod (newObj ); ok && d .isOwnerOf (p ) {
61
+ d .triggerInspection ()
62
+ }
63
+ },
64
+ DeleteFunc : func (obj interface {}) {
65
+ if p , ok := getPod (obj ); ok && d .isOwnerOf (p ) {
66
+ d .triggerInspection ()
67
+ }
68
+ },
69
+ })
70
+
71
+ rw .Run (stopCh )
71
72
}
72
73
73
74
// listenForPVCEvents keep listening for changes in PVC's until the given channel is closed.
74
75
func (d * Deployment ) listenForPVCEvents (stopCh <- chan struct {}) {
75
- source := cache .NewListWatchFromClient (
76
- d .deps .KubeCli .CoreV1 ().RESTClient (),
77
- "persistentvolumeclaims" ,
78
- d .apiObject .GetNamespace (),
79
- fields .Everything ())
80
-
81
76
getPVC := func (obj interface {}) (* v1.PersistentVolumeClaim , bool ) {
82
77
pvc , ok := obj .(* v1.PersistentVolumeClaim )
83
78
if ! ok {
@@ -91,35 +86,35 @@ func (d *Deployment) listenForPVCEvents(stopCh <-chan struct{}) {
91
86
return pvc , true
92
87
}
93
88
94
- _ , informer := cache .NewIndexerInformer (source , & v1.PersistentVolumeClaim {}, 0 , cache.ResourceEventHandlerFuncs {
95
- AddFunc : func (obj interface {}) {
96
- if p , ok := getPVC (obj ); ok && d .isOwnerOf (p ) {
97
- d .triggerInspection ()
98
- }
99
- },
100
- UpdateFunc : func (oldObj , newObj interface {}) {
101
- if p , ok := getPVC (newObj ); ok && d .isOwnerOf (p ) {
102
- d .triggerInspection ()
103
- }
104
- },
105
- DeleteFunc : func (obj interface {}) {
106
- if p , ok := getPVC (obj ); ok && d .isOwnerOf (p ) {
107
- d .triggerInspection ()
108
- }
109
- },
110
- }, cache.Indexers {})
111
-
112
- informer .Run (stopCh )
89
+ rw := k8sutil .NewResourceWatcher (
90
+ d .deps .Log ,
91
+ d .deps .KubeCli .CoreV1 ().RESTClient (),
92
+ "persistentvolumeclaims" ,
93
+ d .apiObject .GetNamespace (),
94
+ & v1.PersistentVolumeClaim {},
95
+ cache.ResourceEventHandlerFuncs {
96
+ AddFunc : func (obj interface {}) {
97
+ if p , ok := getPVC (obj ); ok && d .isOwnerOf (p ) {
98
+ d .triggerInspection ()
99
+ }
100
+ },
101
+ UpdateFunc : func (oldObj , newObj interface {}) {
102
+ if p , ok := getPVC (newObj ); ok && d .isOwnerOf (p ) {
103
+ d .triggerInspection ()
104
+ }
105
+ },
106
+ DeleteFunc : func (obj interface {}) {
107
+ if p , ok := getPVC (obj ); ok && d .isOwnerOf (p ) {
108
+ d .triggerInspection ()
109
+ }
110
+ },
111
+ })
112
+
113
+ rw .Run (stopCh )
113
114
}
114
115
115
116
// listenForSecretEvents keep listening for changes in Secrets's until the given channel is closed.
116
117
func (d * Deployment ) listenForSecretEvents (stopCh <- chan struct {}) {
117
- source := cache .NewListWatchFromClient (
118
- d .deps .KubeCli .CoreV1 ().RESTClient (),
119
- "secrets" ,
120
- d .apiObject .GetNamespace (),
121
- fields .Everything ())
122
-
123
118
getSecret := func (obj interface {}) (* v1.Secret , bool ) {
124
119
secret , ok := obj .(* v1.Secret )
125
120
if ! ok {
@@ -133,36 +128,36 @@ func (d *Deployment) listenForSecretEvents(stopCh <-chan struct{}) {
133
128
return secret , true
134
129
}
135
130
136
- _ , informer := cache .NewIndexerInformer (source , & v1.Secret {}, 0 , cache.ResourceEventHandlerFuncs {
137
- // Note: For secrets we look at all of them because they do not have to be owned by this deployment.
138
- AddFunc : func (obj interface {}) {
139
- if _ , ok := getSecret (obj ); ok {
140
- d .triggerInspection ()
141
- }
142
- },
143
- UpdateFunc : func (oldObj , newObj interface {}) {
144
- if _ , ok := getSecret (newObj ); ok {
145
- d .triggerInspection ()
146
- }
147
- },
148
- DeleteFunc : func (obj interface {}) {
149
- if _ , ok := getSecret (obj ); ok {
150
- d .triggerInspection ()
151
- }
152
- },
153
- }, cache.Indexers {})
154
-
155
- informer .Run (stopCh )
131
+ rw := k8sutil .NewResourceWatcher (
132
+ d .deps .Log ,
133
+ d .deps .KubeCli .CoreV1 ().RESTClient (),
134
+ "secrets" ,
135
+ d .apiObject .GetNamespace (),
136
+ & v1.Secret {},
137
+ cache.ResourceEventHandlerFuncs {
138
+ // Note: For secrets we look at all of them because they do not have to be owned by this deployment.
139
+ AddFunc : func (obj interface {}) {
140
+ if _ , ok := getSecret (obj ); ok {
141
+ d .triggerInspection ()
142
+ }
143
+ },
144
+ UpdateFunc : func (oldObj , newObj interface {}) {
145
+ if _ , ok := getSecret (newObj ); ok {
146
+ d .triggerInspection ()
147
+ }
148
+ },
149
+ DeleteFunc : func (obj interface {}) {
150
+ if _ , ok := getSecret (obj ); ok {
151
+ d .triggerInspection ()
152
+ }
153
+ },
154
+ })
155
+
156
+ rw .Run (stopCh )
156
157
}
157
158
158
159
// listenForServiceEvents keep listening for changes in Service's until the given channel is closed.
159
160
func (d * Deployment ) listenForServiceEvents (stopCh <- chan struct {}) {
160
- source := cache .NewListWatchFromClient (
161
- d .deps .KubeCli .CoreV1 ().RESTClient (),
162
- "services" ,
163
- d .apiObject .GetNamespace (),
164
- fields .Everything ())
165
-
166
161
getService := func (obj interface {}) (* v1.Service , bool ) {
167
162
service , ok := obj .(* v1.Service )
168
163
if ! ok {
@@ -176,23 +171,29 @@ func (d *Deployment) listenForServiceEvents(stopCh <-chan struct{}) {
176
171
return service , true
177
172
}
178
173
179
- _ , informer := cache .NewIndexerInformer (source , & v1.Service {}, 0 , cache.ResourceEventHandlerFuncs {
180
- AddFunc : func (obj interface {}) {
181
- if s , ok := getService (obj ); ok && d .isOwnerOf (s ) {
182
- d .triggerInspection ()
183
- }
184
- },
185
- UpdateFunc : func (oldObj , newObj interface {}) {
186
- if s , ok := getService (newObj ); ok && d .isOwnerOf (s ) {
187
- d .triggerInspection ()
188
- }
189
- },
190
- DeleteFunc : func (obj interface {}) {
191
- if s , ok := getService (obj ); ok && d .isOwnerOf (s ) {
192
- d .triggerInspection ()
193
- }
194
- },
195
- }, cache.Indexers {})
196
-
197
- informer .Run (stopCh )
174
+ rw := k8sutil .NewResourceWatcher (
175
+ d .deps .Log ,
176
+ d .deps .KubeCli .CoreV1 ().RESTClient (),
177
+ "services" ,
178
+ d .apiObject .GetNamespace (),
179
+ & v1.Service {},
180
+ cache.ResourceEventHandlerFuncs {
181
+ AddFunc : func (obj interface {}) {
182
+ if s , ok := getService (obj ); ok && d .isOwnerOf (s ) {
183
+ d .triggerInspection ()
184
+ }
185
+ },
186
+ UpdateFunc : func (oldObj , newObj interface {}) {
187
+ if s , ok := getService (newObj ); ok && d .isOwnerOf (s ) {
188
+ d .triggerInspection ()
189
+ }
190
+ },
191
+ DeleteFunc : func (obj interface {}) {
192
+ if s , ok := getService (obj ); ok && d .isOwnerOf (s ) {
193
+ d .triggerInspection ()
194
+ }
195
+ },
196
+ })
197
+
198
+ rw .Run (stopCh )
198
199
}
0 commit comments