@@ -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 )
@@ -1233,10 +1233,10 @@ func skipAppRequeuing(key kube.ResourceKey) bool {
1233
1233
return ignoredRefreshResources [key .Group + "/" + key .Kind ]
1234
1234
}
1235
1235
1236
- // UpdateClusterConnectionStatus starts a goroutine that checks for watch failures.
1236
+ // StartClusterConnectionStatusMonitoring starts a goroutine that checks for watch failures.
1237
1237
// If there are any watch errors, it will periodically ping the remote cluster
1238
1238
// and update the cluster connection status.
1239
- func (c * clusterCache ) UpdateClusterConnectionStatus (ctx context.Context ) {
1239
+ func (c * clusterCache ) StartClusterConnectionStatusMonitoring (ctx context.Context ) {
1240
1240
go c .clusterConnectionService (ctx )
1241
1241
}
1242
1242
@@ -1251,15 +1251,23 @@ func (c *clusterCache) clusterConnectionService(ctx context.Context) {
1251
1251
watchErrors := c .watchFails .len ()
1252
1252
// Ping the cluster for connection verification if there are watch failures or
1253
1253
// if the cluster has recovered back from watch failures.
1254
- if watchErrors > 0 || (watchErrors == 0 && c .connectionStatus == ConnectionStatusFailed ) {
1254
+ watchesRecovered := false
1255
+ if watchErrors == 0 {
1256
+ // If there are no watch failures check if the status needs to be updated.
1257
+ c .lock .RLock ()
1258
+ if c .connectionStatus == ConnectionStatusFailed {
1259
+ watchesRecovered = true
1260
+ }
1261
+ c .lock .RUnlock ()
1262
+ }
1263
+
1264
+ if watchErrors > 0 || watchesRecovered {
1255
1265
c .log .V (1 ).Info ("verifying cluster connection" , "watches" , watchErrors )
1256
1266
1257
1267
_ , err := c .kubectl .GetServerVersion (c .config )
1258
1268
if err != nil {
1259
- if c .connectionStatus != ConnectionStatusFailed {
1260
- c .updateConnectionStatus (ConnectionStatusFailed )
1261
- }
1262
- } else if c .connectionStatus != ConnectionStatusSuccessful {
1269
+ c .updateConnectionStatus (ConnectionStatusFailed )
1270
+ } else {
1263
1271
c .updateConnectionStatus (ConnectionStatusSuccessful )
1264
1272
}
1265
1273
}
@@ -1272,14 +1280,18 @@ func (c *clusterCache) clusterConnectionService(ctx context.Context) {
1272
1280
}
1273
1281
1274
1282
func (c * clusterCache ) updateConnectionStatus (status ConnectionStatus ) {
1275
- if c .connectionStatus == status {
1276
- return
1277
- }
1278
-
1283
+ invalidateCache := false
1279
1284
c .lock .Lock ()
1280
- c .connectionStatus = status
1285
+ if c .connectionStatus != status {
1286
+ c .connectionStatus = status
1287
+ invalidateCache = true
1288
+ }
1281
1289
c .lock .Unlock ()
1282
1290
1291
+ if ! invalidateCache {
1292
+ return
1293
+ }
1294
+
1283
1295
c .log .V (1 ).Info ("updated cluster connection status" , "server" , c .config .Host , "status" , status )
1284
1296
1285
1297
c .Invalidate ()
@@ -1295,7 +1307,7 @@ type watchFailures struct {
1295
1307
mu sync.RWMutex
1296
1308
}
1297
1309
1298
- func newWatchFaiures () * watchFailures {
1310
+ func newWatchFailures () * watchFailures {
1299
1311
return & watchFailures {
1300
1312
watches : make (map [string ]bool ),
1301
1313
}
0 commit comments