@@ -101,11 +101,18 @@ func InitScheme() (*runtime.Scheme, error) {
101101func processCluster (ctx context.Context , config * rest.Config , c client.Client ,
102102 agentInMgmtCluster bool , cluster client.Object , req ctrl.Request , logger logr.Logger ) error {
103103
104- clusterRef := getObjectReferenceFromObject (c .Scheme (), cluster )
105-
106104 if err := c .Get (ctx , req .NamespacedName , cluster ); err != nil {
107105 if apierrors .IsNotFound (err ) {
108- return stopTrackingCluster (ctx , config , clusterRef , logger )
106+ // cluster is empty here; derive the GVK from the scheme so the ref is complete.
107+ addTypeInformationToObject (c .Scheme (), cluster )
108+ apiVersion , kind := cluster .GetObjectKind ().GroupVersionKind ().ToAPIVersionAndKind ()
109+ notFoundRef := & corev1.ObjectReference {
110+ Namespace : req .Namespace ,
111+ Name : req .Name ,
112+ Kind : kind ,
113+ APIVersion : apiVersion ,
114+ }
115+ return stopTrackingCluster (ctx , config , notFoundRef , logger )
109116 }
110117 logger .Error (err , "Failed to fetch cluster" )
111118 return errors .Wrapf (
@@ -115,6 +122,9 @@ func processCluster(ctx context.Context, config *rest.Config, c client.Client,
115122 )
116123 }
117124
125+ // clusterRef is computed after Get so namespace/name are populated.
126+ clusterRef := getObjectReferenceFromObject (c .Scheme (), cluster )
127+
118128 // Handle deleted cluster
119129 if ! cluster .GetDeletionTimestamp ().IsZero () {
120130 return stopTrackingCluster (ctx , config , clusterRef , logger )
@@ -145,13 +155,13 @@ func trackCluster(ctx context.Context, config *rest.Config, c client.Client, age
145155 mux .Lock ()
146156 defer mux .Unlock ()
147157
148- oldShard , ok := clusterMap [* cluster ]
149- if ok && oldShard == currentShardKey {
158+ oldShard , alreadyTracked := clusterMap [* cluster ]
159+ if alreadyTracked && oldShard == currentShardKey {
150160 // Cluster is already tracked. And cluster shard has not changed.
151161 return nil
152162 }
153163
154- if ok {
164+ if alreadyTracked {
155165 if shardMap [oldShard ].Has (cluster ) &&
156166 shardMap [oldShard ].Len () == 1 {
157167 // By removing cluster, no more clusters will match oldShard.
@@ -167,6 +177,8 @@ func trackCluster(ctx context.Context, config *rest.Config, c client.Client, age
167177 clusterPerShard := shardMap [oldShard ]
168178 clusterPerShard .Erase (cluster )
169179 shardMap [oldShard ] = clusterPerShard
180+ logger .V (logs .LogInfo ).Info (fmt .Sprintf ("removed cluster from shard %q: %d cluster(s) remaining" ,
181+ oldShard , clusterPerShard .Len ()))
170182 }
171183
172184 // Update Cluster shard (key: cluster; value: cluster current shard)
@@ -189,6 +201,8 @@ func trackCluster(ctx context.Context, config *rest.Config, c client.Client, age
189201 }
190202 clusterPerShard .Insert (cluster )
191203 shardMap [currentShardKey ] = clusterPerShard
204+ logger .V (logs .LogInfo ).Info (fmt .Sprintf ("added cluster to shard %q: %d cluster(s) total" ,
205+ currentShardKey , clusterPerShard .Len ()))
192206
193207 return nil
194208}
@@ -217,6 +231,8 @@ func stopTrackingCluster(ctx context.Context, config *rest.Config, cluster *core
217231 fmt .Sprintf ("no more clusters matching shard %s. Removing controllers" , oldShardKey ))
218232 return undeployControllers (ctx , config , oldShardKey , logger )
219233 }
234+ logger .V (logs .LogInfo ).Info (fmt .Sprintf ("removed cluster from shard %q: %d cluster(s) remaining" ,
235+ oldShardKey , shardMap [oldShardKey ].Len ()))
220236 }
221237 }
222238
0 commit comments