@@ -134,8 +134,8 @@ type ClusterCache interface {
134
134
OnResourceUpdated (handler OnResourceUpdatedHandler ) Unsubscribe
135
135
// OnEvent register event handler that is executed every time when new K8S event received
136
136
OnEvent (handler OnEventHandler ) Unsubscribe
137
- // UpdateClusterConnectionStatus checks the watch errors periodically and updates the cluster connection status.
138
- UpdateClusterConnectionStatus (ctx context.Context )
137
+ // StartClusterConnectionStatusMonitoring starts a goroutine that checks the watch errors periodically and updates the cluster connection status.
138
+ StartClusterConnectionStatusMonitoring (ctx context.Context )
139
139
}
140
140
141
141
type WeightedSemaphore interface {
@@ -175,7 +175,7 @@ func NewClusterCache(config *rest.Config, opts ...UpdateSettingsFunc) *clusterCa
175
175
listRetryUseBackoff : false ,
176
176
listRetryFunc : ListRetryFuncNever ,
177
177
connectionStatus : ConnectionStatusUnknown ,
178
- watchFails : newWatchFaiures (),
178
+ watchFails : newWatchFailures (),
179
179
}
180
180
for i := range opts {
181
181
opts [i ](cache )
@@ -1231,10 +1231,10 @@ func skipAppRequeuing(key kube.ResourceKey) bool {
1231
1231
return ignoredRefreshResources [key .Group + "/" + key .Kind ]
1232
1232
}
1233
1233
1234
- // UpdateClusterConnectionStatus starts a goroutine that checks for watch failures.
1234
+ // StartClusterConnectionStatusMonitoring starts a goroutine that checks for watch failures.
1235
1235
// If there are any watch errors, it will periodically ping the remote cluster
1236
1236
// and update the cluster connection status.
1237
- func (c * clusterCache ) UpdateClusterConnectionStatus (ctx context.Context ) {
1237
+ func (c * clusterCache ) StartClusterConnectionStatusMonitoring (ctx context.Context ) {
1238
1238
go c .clusterConnectionService (ctx )
1239
1239
}
1240
1240
@@ -1249,15 +1249,23 @@ func (c *clusterCache) clusterConnectionService(ctx context.Context) {
1249
1249
watchErrors := c .watchFails .len ()
1250
1250
// Ping the cluster for connection verification if there are watch failures or
1251
1251
// if the cluster has recovered back from watch failures.
1252
- if watchErrors > 0 || (watchErrors == 0 && c .connectionStatus == ConnectionStatusFailed ) {
1252
+ watchesRecovered := false
1253
+ if watchErrors == 0 {
1254
+ // If there are no watch failures check if the status needs to be updated.
1255
+ c .lock .RLock ()
1256
+ if c .connectionStatus == ConnectionStatusFailed {
1257
+ watchesRecovered = true
1258
+ }
1259
+ c .lock .RUnlock ()
1260
+ }
1261
+
1262
+ if watchErrors > 0 || watchesRecovered {
1253
1263
c .log .V (1 ).Info ("verifying cluster connection" , "watches" , watchErrors )
1254
1264
1255
1265
_ , err := c .kubectl .GetServerVersion (c .config )
1256
1266
if err != nil {
1257
- if c .connectionStatus != ConnectionStatusFailed {
1258
- c .updateConnectionStatus (ConnectionStatusFailed )
1259
- }
1260
- } else if c .connectionStatus != ConnectionStatusSuccessful {
1267
+ c .updateConnectionStatus (ConnectionStatusFailed )
1268
+ } else {
1261
1269
c .updateConnectionStatus (ConnectionStatusSuccessful )
1262
1270
}
1263
1271
}
@@ -1270,14 +1278,18 @@ func (c *clusterCache) clusterConnectionService(ctx context.Context) {
1270
1278
}
1271
1279
1272
1280
func (c * clusterCache ) updateConnectionStatus (status ConnectionStatus ) {
1273
- if c .connectionStatus == status {
1274
- return
1275
- }
1276
-
1281
+ invalidateCache := false
1277
1282
c .lock .Lock ()
1278
- c .connectionStatus = status
1283
+ if c .connectionStatus != status {
1284
+ c .connectionStatus = status
1285
+ invalidateCache = true
1286
+ }
1279
1287
c .lock .Unlock ()
1280
1288
1289
+ if ! invalidateCache {
1290
+ return
1291
+ }
1292
+
1281
1293
c .log .V (1 ).Info ("updated cluster connection status" , "server" , c .config .Host , "status" , status )
1282
1294
1283
1295
c .Invalidate ()
@@ -1293,7 +1305,7 @@ type watchFailures struct {
1293
1305
mu sync.RWMutex
1294
1306
}
1295
1307
1296
- func newWatchFaiures () * watchFailures {
1308
+ func newWatchFailures () * watchFailures {
1297
1309
return & watchFailures {
1298
1310
watches : make (map [string ]bool ),
1299
1311
}
0 commit comments