5
5
"fmt"
6
6
"log"
7
7
"strconv"
8
- "time"
9
8
10
9
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
10
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -92,27 +91,29 @@ func (w *Watcher) Start(notifyF WatchNotifyFunctions, gvr schema.GroupVersionRes
92
91
w .watcher = watcher
93
92
w .running = true
94
93
currentWatcherContext , cancelFunc := context .WithCancel (context .Background ())
94
+
95
+ // Function to restart the watcher
96
+ restartWatcher := func () {
97
+ if currentWatcherContext != nil && cancelFunc != nil {
98
+ cancelFunc ()
99
+ }
100
+ listOptions .ResourceVersion = resourceVersion
101
+ currentWatcherContext , cancelFunc = context .WithCancel (context .Background ())
102
+ w .watcher , err = w .client .Resource (gvr ).Namespace ("" ).Watch (currentWatcherContext , listOptions )
103
+ if err != nil {
104
+ log .Printf ("watcher restart error: %v, on object: %+v" , err , gvr )
105
+ }
106
+ watcher = w .watcher
107
+ }
108
+
95
109
go func () {
96
110
// Watch for events
97
-
98
111
for {
99
112
event , ok := <- watcher .ResultChan ()
100
113
if ! ok {
101
- if currentWatcherContext != nil && cancelFunc != nil {
102
- cancelFunc ()
103
- }
104
-
105
114
if w .running {
106
- // Need to restart the watcher: wait a bit and restart
107
- time .Sleep (5 * time .Second )
108
- listOptions .ResourceVersion = resourceVersion
109
- currentWatcherContext , cancelFunc = context .WithCancel (context .Background ())
110
- w .watcher , err = w .client .Resource (gvr ).Namespace ("" ).Watch (currentWatcherContext , listOptions )
111
- if err != nil {
112
- log .Printf ("watcher restart error: %v" , err )
113
- }
114
- watcher = w .watcher
115
- // Restart the loop
115
+ log .Printf ("Watcher channel closed on object %+v" , gvr )
116
+ restartWatcher ()
116
117
continue
117
118
} else {
118
119
// Stop the watcher
@@ -128,9 +129,7 @@ func (w *Watcher) Start(notifyF WatchNotifyFunctions, gvr schema.GroupVersionRes
128
129
continue
129
130
}
130
131
// Update the resourceVersion
131
- if isResourceVersionHigher (addedObject .GetResourceVersion (), resourceVersion ) {
132
- resourceVersion = addedObject .GetResourceVersion ()
133
- }
132
+ resourceVersion = addedObject .GetResourceVersion ()
134
133
notifyF .AddFunc (addedObject )
135
134
addedObject = nil // Make sure the item is scraped by the GC
136
135
case watch .Modified :
@@ -141,9 +140,7 @@ func (w *Watcher) Start(notifyF WatchNotifyFunctions, gvr schema.GroupVersionRes
141
140
continue
142
141
}
143
142
// Update the resourceVersion
144
- if isResourceVersionHigher (modifiedObject .GetResourceVersion (), resourceVersion ) {
145
- resourceVersion = modifiedObject .GetResourceVersion ()
146
- }
143
+ resourceVersion = modifiedObject .GetResourceVersion ()
147
144
notifyF .UpdateFunc (modifiedObject )
148
145
modifiedObject = nil // Make sure the item is scraped by the GC
149
146
case watch .Deleted :
@@ -154,9 +151,7 @@ func (w *Watcher) Start(notifyF WatchNotifyFunctions, gvr schema.GroupVersionRes
154
151
continue
155
152
}
156
153
// Update the resourceVersion
157
- if isResourceVersionHigher (deletedObject .GetResourceVersion (), resourceVersion ) {
158
- resourceVersion = deletedObject .GetResourceVersion ()
159
- }
154
+ resourceVersion = deletedObject .GetResourceVersion ()
160
155
notifyF .DeleteFunc (deletedObject )
161
156
deletedObject = nil // Make sure the item is scraped by the GC
162
157
@@ -171,7 +166,15 @@ func (w *Watcher) Start(notifyF WatchNotifyFunctions, gvr schema.GroupVersionRes
171
166
bookmarkObject = nil // Make sure the item is scraped by the GC
172
167
173
168
case watch .Error :
174
- log .Printf ("watcher error: %v" , event .Object )
169
+ // Convert the object to metav1.Status
170
+ watchError := event .Object .(* metav1.Status )
171
+ // Check if the object reason is "Expired" or "Gone" and restart the watcher
172
+ if watchError .Reason == "Expired" || watchError .Reason == "Gone" || watchError .Code == 410 {
173
+ restartWatcher ()
174
+ continue
175
+ } else {
176
+ log .Printf ("watcher error: %v, on object %+v" , event .Object , gvr )
177
+ }
175
178
}
176
179
}
177
180
}()
0 commit comments