fix: issue when cluster is moved to a different shard #25126
      
        
          +11
        
        
          −0
        
        
          
        
      
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Fixes: http://github.com/argoproj/argo-cd/issues/19854
When using any sharding algorithm that moves shards with some frequency this issue becomes more apparent.
Simple steps to reproduce:
1. When a cluster is moved for any reason (new cluster added, new app added, etc) you will see in the logs something like
Cluster https://XYZ.gr7.us-east-1.eks.amazonaws.com has changed shard from 4 to 62.1 handleModEvent() will be triggered on shard 4:
It will go into
if !c.canHandleCluster(newCluster) {and delete the cluster from the local cache: https://github.com/argoproj/argo-cd/blob/master/controller/cache/cache.go#L8332.2 handleModEvent() will be triggered on shard 6:
As shard 6 hasn't had
newCluster.Serverin its cache before, ok is set tofalseand the entire logic of handleModEvent is skipped and the cluster is never added to this shard's cache.This fix checks if the cluster is not in local cache, wether it should be added. The call to
getSyncedClusterpopulates the local cache as it callsgetClusterthat sets the entry in the cache: https://github.com/argoproj/argo-cd/blob/master/controller/cache/cache.go#L479I don't have the time to look into adding tests for a few days so if someone can guide me to the right place to add them it will save me a lot of time.
Checklist: