@@ -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.
0 commit comments