Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.

Commit 4782ae3

Browse files
authored
fix: overwriting return error value (#29)
1 parent 013c4a7 commit 4782ae3

File tree

2 files changed

+50
-58
lines changed

2 files changed

+50
-58
lines changed

controllers/delivery/sync_controller.go

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,11 @@ type SyncReconciler struct {
5151

5252
// Reconcile is part of the main kubernetes reconciliation loop which aims to
5353
// move the current state of the cluster closer to the desired state.
54-
func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
55-
var (
56-
result ctrl.Result
57-
retErr error
58-
)
59-
54+
func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {
6055
log := log.FromContext(ctx)
6156

6257
obj := &v1alpha1.Sync{}
63-
if err := r.Get(ctx, req.NamespacedName, obj); err != nil {
58+
if err = r.Get(ctx, req.NamespacedName, obj); err != nil {
6459
if apierrors.IsNotFound(err) {
6560
return ctrl.Result{}, nil
6661
}
@@ -70,12 +65,12 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
7065

7166
// The replication controller doesn't need a shouldReconcile, because it should always reconcile,
7267
// that is its purpose.
73-
patchHelper, err := patch.NewHelper(obj, r.Client)
68+
var patchHelper *patch.Helper
69+
patchHelper, err = patch.NewHelper(obj, r.Client)
7470
if err != nil {
75-
retErr = errors.Join(retErr, err)
7671
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.PatchFailedReason, err.Error())
7772

78-
return ctrl.Result{}, retErr
73+
return ctrl.Result{}, fmt.Errorf("failed to create patch helper: %w", err)
7974
}
8075

8176
// Always attempt to patch the object and status after each reconciliation.
@@ -91,13 +86,13 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
9186

9287
// Check if it's a successful reconciliation.
9388
// We don't set Requeue in case of error, so we can safely check for Requeue.
94-
if result.RequeueAfter == obj.GetRequeueAfter() && !result.Requeue && retErr == nil {
89+
if result.RequeueAfter == obj.GetRequeueAfter() && !result.Requeue && err == nil {
9590
// Remove the reconciling condition if it's set.
9691
conditions.Delete(obj, meta.ReconcilingCondition)
9792

9893
// Set the return err as the ready failure message if the resource is not ready, but also not reconciling or stalled.
9994
if ready := conditions.Get(obj, meta.ReadyCondition); ready != nil && ready.Status == metav1.ConditionFalse && !conditions.IsStalled(obj) {
100-
retErr = errors.New(conditions.GetMessage(obj, meta.ReadyCondition))
95+
err = errors.New(conditions.GetMessage(obj, meta.ReadyCondition))
10196
}
10297
}
10398

@@ -112,7 +107,7 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
112107
// If not reconciling or stalled than mark Ready=True
113108
if !conditions.IsReconciling(obj) &&
114109
!conditions.IsStalled(obj) &&
115-
retErr == nil {
110+
err == nil {
116111
conditions.MarkTrue(obj, meta.ReadyCondition, meta.SucceededReason, "Reconciliation success")
117112
}
118113

@@ -122,8 +117,8 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
122117
}
123118

124119
// Update the object.
125-
if err := patchHelper.Patch(ctx, obj); err != nil {
126-
retErr = errors.Join(retErr, err)
120+
if perr := patchHelper.Patch(ctx, obj); perr != nil {
121+
err = errors.Join(err, perr)
127122
}
128123
}()
129124

@@ -134,40 +129,40 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
134129
}
135130

136131
snapshot := &ocmv1.Snapshot{}
137-
if err := r.Get(ctx, types.NamespacedName{
132+
if err = r.Get(ctx, types.NamespacedName{
138133
Namespace: obj.Namespace,
139134
Name: obj.Spec.SnapshotRef.Name,
140135
}, snapshot); err != nil {
141-
retErr = fmt.Errorf("failed to find snapshot: %w", err)
142-
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.SnapshotGetFailedReason, retErr.Error())
136+
err = fmt.Errorf("failed to find snapshot: %w", err)
137+
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.SnapshotGetFailedReason, err.Error())
143138

144-
return ctrl.Result{}, retErr
139+
return ctrl.Result{}, err
145140
}
146141

147142
log.V(4).Info("found target snapshot")
148143

149144
repository := &mpasv1alpha1.Repository{}
150-
if err := r.Get(ctx, types.NamespacedName{
145+
if err = r.Get(ctx, types.NamespacedName{
151146
Namespace: obj.Namespace,
152147
Name: obj.Spec.RepositoryRef.Name,
153148
}, repository); err != nil {
154-
retErr = fmt.Errorf("failed to find repository: %w", err)
155-
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.RepositoryGetFailedReason, retErr.Error())
149+
err = fmt.Errorf("failed to find repository: %w", err)
150+
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.RepositoryGetFailedReason, err.Error())
156151

157-
return ctrl.Result{}, retErr
152+
return ctrl.Result{}, err
158153
}
159154

160155
log.V(4).Info("found target repository")
161156

162157
authSecret := &corev1.Secret{}
163-
if err := r.Get(ctx, types.NamespacedName{
158+
if err = r.Get(ctx, types.NamespacedName{
164159
Namespace: obj.Namespace,
165160
Name: repository.Spec.Credentials.SecretRef.Name,
166161
}, authSecret); err != nil {
167-
retErr = fmt.Errorf("failed to find authentication secret: %w", err)
168-
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.CredentialsNotFoundReason, retErr.Error())
162+
err = fmt.Errorf("failed to find authentication secret: %w", err)
163+
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.CredentialsNotFoundReason, err.Error())
169164

170-
return ctrl.Result{}, retErr
165+
return ctrl.Result{}, err
171166
}
172167

173168
log.V(4).Info("found authentication secret")
@@ -176,14 +171,15 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
176171
if baseBranch == "" {
177172
baseBranch = "main"
178173
}
174+
179175
targetBranch := obj.Spec.CommitTemplate.TargetBranch
180176
if targetBranch == "" && obj.Spec.AutomaticPullRequestCreation {
181177
targetBranch = fmt.Sprintf("branch-%d", time.Now().Unix())
182178
} else if targetBranch == "" && !obj.Spec.AutomaticPullRequestCreation {
183-
retErr = fmt.Errorf("branch cannot be empty if automatic pull request creation is not enabled")
184-
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.GitRepositoryPushFailedReason, retErr.Error())
179+
err = fmt.Errorf("branch cannot be empty if automatic pull request creation is not enabled")
180+
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.GitRepositoryPushFailedReason, err.Error())
185181

186-
return ctrl.Result{}, retErr
182+
return ctrl.Result{}, err
187183
}
188184

189185
log.Info("preparing to push snapshot content", "base", baseBranch, "target", targetBranch)
@@ -203,12 +199,13 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
203199

204200
r.parseAuthSecret(authSecret, opts)
205201

206-
digest, err := r.Git.Push(ctx, opts)
202+
var digest string
203+
digest, err = r.Git.Push(ctx, opts)
207204
if err != nil {
208-
retErr = fmt.Errorf("failed to push to git repository: %w", err)
209-
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.GitRepositoryPushFailedReason, retErr.Error())
205+
err = fmt.Errorf("failed to push to git repository: %w", err)
206+
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.GitRepositoryPushFailedReason, err.Error())
210207

211-
return ctrl.Result{}, retErr
208+
return ctrl.Result{}, err
212209
}
213210

214211
log.Info("target content pushed with digest", "base", baseBranch, "target", targetBranch, "digest", digest)
@@ -218,11 +215,11 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
218215
if obj.Spec.AutomaticPullRequestCreation {
219216
log.Info("automatic pull-request creation is enabled, preparing to create a pull request")
220217

221-
if err := r.Provider.CreatePullRequest(ctx, targetBranch, *obj, *repository); err != nil {
222-
retErr = fmt.Errorf("failed to create pull request: %w", err)
223-
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.CreatePullRequestFailedReason, retErr.Error())
218+
if err = r.Provider.CreatePullRequest(ctx, targetBranch, *obj, *repository); err != nil {
219+
err = fmt.Errorf("failed to create pull request: %w", err)
220+
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.CreatePullRequestFailedReason, err.Error())
224221

225-
return ctrl.Result{}, retErr
222+
return ctrl.Result{}, err
226223
}
227224
}
228225

@@ -233,7 +230,7 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
233230

234231
log.Info("successfully reconciled sync object")
235232

236-
return result, retErr
233+
return ctrl.Result{}, nil
237234
}
238235

239236
// SetupWithManager sets up the controller with the Manager.

controllers/mpas/repository_controller.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,24 @@ type RepositoryReconciler struct {
3838

3939
// Reconcile is part of the main kubernetes reconciliation loop which aims to
4040
// move the current state of the cluster closer to the desired state.
41-
func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
42-
var (
43-
retErr error
44-
result ctrl.Result
45-
)
46-
41+
func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {
4742
logger := log.FromContext(ctx).WithName("repository")
4843
logger.V(4).Info("entering repository loop...")
4944

5045
obj := &mpasv1alpha1.Repository{}
5146

5247
if err := r.Get(ctx, req.NamespacedName, obj); err != nil {
5348
if apierrors.IsNotFound(err) {
54-
return result, nil
49+
return ctrl.Result{}, nil
5550
}
56-
retErr = fmt.Errorf("failed to get component object: %w", err)
57-
return result, retErr
51+
52+
return ctrl.Result{}, fmt.Errorf("failed to get component object: %w", err)
5853
}
5954

60-
patchHelper, err := patch.NewHelper(obj, r.Client)
55+
var patchHelper *patch.Helper
56+
patchHelper, err = patch.NewHelper(obj, r.Client)
6157
if err != nil {
62-
retErr = errors.Join(retErr, err)
63-
return result, retErr
58+
return ctrl.Result{}, fmt.Errorf("failed to create patch helper: %w", err)
6459
}
6560

6661
// Always attempt to patch the object and status after each reconciliation.
@@ -76,13 +71,13 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request)
7671

7772
// Check if it's a successful reconciliation.
7873
// We don't set Requeue in case of error, so we can safely check for Requeue.
79-
if result.RequeueAfter == obj.GetRequeueAfter() && !result.Requeue && retErr == nil {
74+
if result.RequeueAfter == obj.GetRequeueAfter() && !result.Requeue && err == nil {
8075
// Remove the reconciling condition if it's set.
8176
conditions.Delete(obj, meta.ReconcilingCondition)
8277

8378
// Set the return err as the ready failure message if the resource is not ready, but also not reconciling or stalled.
8479
if ready := conditions.Get(obj, meta.ReadyCondition); ready != nil && ready.Status == metav1.ConditionFalse && !conditions.IsStalled(obj) {
85-
retErr = errors.New(conditions.GetMessage(obj, meta.ReadyCondition))
80+
err = errors.New(conditions.GetMessage(obj, meta.ReadyCondition))
8681
}
8782
}
8883

@@ -97,7 +92,7 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request)
9792
// If not reconciling or stalled than mark Ready=True
9893
if !conditions.IsReconciling(obj) &&
9994
!conditions.IsStalled(obj) &&
100-
retErr == nil {
95+
err == nil {
10196
conditions.MarkTrue(obj, meta.ReadyCondition, meta.SucceededReason, "Reconciliation success")
10297
}
10398

@@ -107,8 +102,8 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request)
107102
}
108103

109104
// Update the object.
110-
if err := patchHelper.Patch(ctx, obj); err != nil {
111-
retErr = errors.Join(retErr, err)
105+
if perr := patchHelper.Patch(ctx, obj); perr != nil {
106+
err = errors.Join(err, perr)
112107
}
113108
}()
114109

@@ -117,8 +112,7 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request)
117112
// block at the very end.
118113
conditions.Delete(obj, meta.ReadyCondition)
119114

120-
result, retErr = r.reconcile(ctx, obj)
121-
return result, retErr
115+
return r.reconcile(ctx, obj)
122116
}
123117

124118
// SetupWithManager sets up the controller with the Manager.
@@ -131,6 +125,7 @@ func (r *RepositoryReconciler) SetupWithManager(mgr ctrl.Manager) error {
131125
func (r *RepositoryReconciler) reconcile(ctx context.Context, obj *mpasv1alpha1.Repository) (ctrl.Result, error) {
132126
if err := r.Provider.CreateRepository(ctx, *obj); err != nil {
133127
conditions.MarkFalse(obj, meta.ReadyCondition, mpasv1alpha1.RepositoryCreateFailedReason, err.Error())
128+
134129
return ctrl.Result{}, fmt.Errorf("failed to create repository: %w", err)
135130
}
136131

0 commit comments

Comments
 (0)