Skip to content

Commit e992e39

Browse files
authored
Merge pull request #208 from gianlucam76/fix-tracking
(bug) Fix shard tracking: cluster map key used empty namespace/name
2 parents f522c50 + 6b07f52 commit e992e39

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

internal/controller/cluster_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ type ClusterReconciler struct {
4343

4444
func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
4545
logger := ctrl.LoggerFrom(ctx)
46-
logger.V(logs.LogInfo).Info("Reconciling Cluster")
46+
logger.V(logs.LogDebug).Info("Reconciling Cluster")
4747

4848
// Fecth the Cluster instance
4949
cluster := &clusterv1.Cluster{}
50-
addTypeInformationToObject(r.Scheme, cluster)
5150
return reconcile.Result{}, processCluster(ctx, r.Config, r.Client, r.AgentInMgmtCluster,
5251
cluster, req, logger)
5352
}

internal/controller/sveltoscluster_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ type SveltosClusterReconciler struct {
4343

4444
func (r *SveltosClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
4545
logger := ctrl.LoggerFrom(ctx)
46-
logger.V(logs.LogInfo).Info("Reconciling SveltosCluster")
46+
logger.V(logs.LogDebug).Info("Reconciling SveltosCluster")
4747

4848
// Fecth the SveltosCluster instance
4949
sveltosCluster := &libsveltosv1beta1.SveltosCluster{}
50-
addTypeInformationToObject(r.Scheme, sveltosCluster)
5150
return reconcile.Result{}, processCluster(ctx, r.Config, r.Client, r.AgentInMgmtCluster,
5251
sveltosCluster, req, logger)
5352
}

internal/controller/utils.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,18 @@ func InitScheme() (*runtime.Scheme, error) {
101101
func 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

Comments
 (0)