Skip to content

Commit 2703ada

Browse files
authored
Fix the clusterStatus should reflect correct state of cluster (#976)
Currently, we are storing the CR in resource.Cluster object and changing this CR during the course of reconcile and at last call the update function. This causes the `object has been already modified, please apply your changes to the latest version and try again` With this change, we take the copy of the object before reconcile, run the changes on our CR, then patch will take diff for the before and after of object to find only the changed fields. This patch operation is more safer in terms of updating the CR.
1 parent 9d5c0c8 commit 2703ada

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

pkg/controller/cluster_controller.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request
118118

119119
cluster := resource.NewCluster(cr)
120120
cluster.Fetcher = fetcher
121+
cleanClusterObj := cluster.Unwrap()
121122
// on first run we need to save the status and exit to pass Openshift CI
122123
// we added a state called Starting for field ClusterStatus to accomplish this
123124
if cluster.Status().ClusterStatus == "" {
124125
cluster.SetClusterStatusOnFirstReconcile()
125-
if err := r.updateClusterStatus(ctx, log, &cluster); err != nil {
126+
if err := r.updateClusterStatus(ctx, log, &cluster, cleanClusterObj); err != nil {
126127
log.Error(err, "failed to update cluster status")
127128
return requeueIfError(err)
128129
}
@@ -133,7 +134,7 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request
133134
if cluster.True(api.CrdbVersionChecked) {
134135
if cluster.GetCockroachDBImageName() != cluster.Status().CrdbContainerImage {
135136
cluster.SetFalse(api.CrdbVersionChecked)
136-
if err := r.updateClusterStatus(ctx, log, &cluster); err != nil {
137+
if err := r.updateClusterStatus(ctx, log, &cluster, cleanClusterObj); err != nil {
137138
log.Error(err, "failed to update cluster status")
138139
return requeueIfError(err)
139140
}
@@ -156,7 +157,7 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request
156157
cluster.SetActionFailed(actorToExecute.GetActionType(), err.Error())
157158

158159
defer func(ctx context.Context, cluster *resource.Cluster) {
159-
if err := r.updateClusterStatus(ctx, log, cluster); err != nil {
160+
if err := r.updateClusterStatus(ctx, log, cluster, cleanClusterObj); err != nil {
160161
log.Error(err, "failed to update cluster status")
161162
}
162163
}(ctx, &cluster)
@@ -192,7 +193,7 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request
192193
cluster.SetActionFinished(actorToExecute.GetActionType())
193194
}
194195

195-
if err := r.updateClusterStatus(ctx, log, &cluster); err != nil {
196+
if err := r.updateClusterStatus(ctx, log, &cluster, cleanClusterObj); err != nil {
196197
log.Error(err, "failed to update cluster status")
197198
return requeueIfError(err)
198199
}
@@ -203,10 +204,11 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req reconcile.Request
203204

204205
// updateClusterStatus preprocesses a cluster's Status and then persists it to
205206
// the Kubernetes API. updateClusterStatus will retry on conflict errors.
206-
func (r *ClusterReconciler) updateClusterStatus(ctx context.Context, log logr.Logger, cluster *resource.Cluster) error {
207+
func (r *ClusterReconciler) updateClusterStatus(ctx context.Context, log logr.Logger, cluster *resource.Cluster,
208+
cleanObj *api.CrdbCluster) error {
207209
cluster.SetClusterStatus()
208210
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
209-
return r.Client.Status().Update(ctx, cluster.Unwrap())
211+
return r.Client.Status().Patch(ctx, cluster.Unwrap(), client.MergeFrom(cleanObj))
210212
})
211213
}
212214

pkg/resource/cluster.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (cluster Cluster) LookupSupportedVersion(version string) (string, bool) {
210210
return "", false
211211
}
212212

213-
//GetVersionAnnotation gets the current version of the cluster retrieved by version checker action
213+
// GetVersionAnnotation gets the current version of the cluster retrieved by version checker action
214214
func (cluster Cluster) GetVersionAnnotation() string {
215215
return cluster.getAnnotation(CrdbVersionAnnotation)
216216
}

0 commit comments

Comments
 (0)