@@ -20,8 +20,10 @@ import (
2020 "k8s.io/apimachinery/pkg/runtime"
2121 "k8s.io/apimachinery/pkg/types"
2222 ctrl "sigs.k8s.io/controller-runtime"
23+ "sigs.k8s.io/controller-runtime/pkg/builder"
2324 "sigs.k8s.io/controller-runtime/pkg/client"
2425 "sigs.k8s.io/controller-runtime/pkg/log"
26+ "sigs.k8s.io/controller-runtime/pkg/predicate"
2527
2628 ocmv1 "github.com/open-component-model/ocm-controller/api/v1alpha1"
2729
@@ -56,7 +58,7 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
5658 )
5759
5860 log := log .FromContext (ctx )
59- log . V ( 4 ). Info ( "starting reconcile loop for snapshot" )
61+
6062 obj := & v1alpha1.Sync {}
6163 if err := r .Get (ctx , req .NamespacedName , obj ); err != nil {
6264 if apierrors .IsNotFound (err ) {
@@ -142,6 +144,8 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
142144 return ctrl.Result {}, retErr
143145 }
144146
147+ log .V (4 ).Info ("found target snapshot" )
148+
145149 repository := & mpasv1alpha1.Repository {}
146150 if err := r .Get (ctx , types.NamespacedName {
147151 Namespace : obj .Namespace ,
@@ -153,6 +157,8 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
153157 return ctrl.Result {}, retErr
154158 }
155159
160+ log .V (4 ).Info ("found target repository" )
161+
156162 authSecret := & corev1.Secret {}
157163 if err := r .Get (ctx , types.NamespacedName {
158164 Namespace : obj .Namespace ,
@@ -164,26 +170,35 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
164170 return ctrl.Result {}, retErr
165171 }
166172
167- branch := obj .Spec .Branch
168- if branch == "" && obj .Spec .AutomaticPullRequestCreation {
169- branch = fmt .Sprintf ("branch-%d" , time .Now ().Unix ())
170- } else if branch == "" && ! obj .Spec .AutomaticPullRequestCreation {
173+ log .V (4 ).Info ("found authentication secret" )
174+
175+ baseBranch := obj .Spec .BaseBranch
176+ if baseBranch == "" {
177+ baseBranch = "main"
178+ }
179+ targetBranch := obj .Spec .TargetBranch
180+ if targetBranch == "" && obj .Spec .AutomaticPullRequestCreation {
181+ targetBranch = fmt .Sprintf ("branch-%d" , time .Now ().Unix ())
182+ } else if targetBranch == "" && ! obj .Spec .AutomaticPullRequestCreation {
171183 retErr = fmt .Errorf ("branch cannot be empty if automatic pull request creation is not enabled" )
172184 conditions .MarkFalse (obj , meta .ReadyCondition , v1alpha1 .GitRepositoryPushFailedReason , retErr .Error ())
173185
174186 return ctrl.Result {}, retErr
175187 }
176188
189+ log .Info ("preparing to push snapshot content" , "base" , baseBranch , "target" , targetBranch )
190+
177191 // trim any trailing `/` and then just add.
178192 log .V (4 ).Info ("crafting artifact URL to download from" , "url" , snapshot .Status .RepositoryURL )
179193 opts := & pkg.PushOptions {
180- URL : repository .GetRepositoryURL (),
181- Message : obj .Spec .CommitTemplate .Message ,
182- Name : obj .Spec .CommitTemplate .Name ,
183- Email : obj .Spec .CommitTemplate .Email ,
184- Snapshot : snapshot ,
185- Branch : branch ,
186- SubPath : obj .Spec .SubPath ,
194+ URL : repository .GetRepositoryURL (),
195+ Message : obj .Spec .CommitTemplate .Message ,
196+ Name : obj .Spec .CommitTemplate .Name ,
197+ Email : obj .Spec .CommitTemplate .Email ,
198+ Snapshot : snapshot ,
199+ BaseBranch : baseBranch ,
200+ TargetBranch : targetBranch ,
201+ SubPath : obj .Spec .SubPath ,
187202 }
188203
189204 r .parseAuthSecret (authSecret , opts )
@@ -196,10 +211,14 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
196211 return ctrl.Result {}, retErr
197212 }
198213
214+ log .Info ("target content pushed with digest" , "base" , baseBranch , "target" , targetBranch , "digest" , digest )
215+
199216 obj .Status .Digest = digest
200217
201218 if obj .Spec .AutomaticPullRequestCreation {
202- if err := r .Provider .CreatePullRequest (ctx , branch , * obj , * repository ); err != nil {
219+ log .Info ("automatic pull-request creation is enabled, preparing to create a pull request" )
220+
221+ if err := r .Provider .CreatePullRequest (ctx , targetBranch , * obj , * repository ); err != nil {
203222 retErr = fmt .Errorf ("failed to create pull request: %w" , err )
204223 conditions .MarkFalse (obj , meta .ReadyCondition , v1alpha1 .CreatePullRequestFailedReason , retErr .Error ())
205224
@@ -212,13 +231,15 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
212231 // block at the very end.
213232 conditions .Delete (obj , meta .ReadyCondition )
214233
234+ log .Info ("successfully reconciled sync object" )
235+
215236 return result , retErr
216237}
217238
218239// SetupWithManager sets up the controller with the Manager.
219240func (r * SyncReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
220241 return ctrl .NewControllerManagedBy (mgr ).
221- For (& v1alpha1.Sync {}).
242+ For (& v1alpha1.Sync {}, builder . WithPredicates (predicate. GenerationChangedPredicate {}) ).
222243 Complete (r )
223244}
224245
0 commit comments