Skip to content

Commit

Permalink
Compare artifact <> Kustomizations in enqueuers
Browse files Browse the repository at this point in the history
The reason for this is that the `EnqueueRequestsFromMapFunc` calls the
enqueuer for _both_ the old and the new object, and we only want to act
on the ones that contain a revision different from the one that we have
recorded in the status object of the `Kustomization`.

Signed-off-by: Hidde Beydals <hello@hidde.co>
  • Loading branch information
hiddeco committed Oct 28, 2020
1 parent 9da37f7 commit db57728
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions controllers/kustomization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ func (r *KustomizationReconciler) SetupWithManager(mgr ctrl.Manager, opts Kustom
For(&kustomizev1.Kustomization{}, builder.WithPredicates(predicates.ChangePredicate{})).
Watches(
&source.Kind{Type: &sourcev1.GitRepository{}},
&handler.EnqueueRequestsFromMapFunc{ToRequests: handler.ToRequestsFunc(r.kustomizationsForGitRepository)},
&handler.EnqueueRequestsFromMapFunc{ToRequests: handler.ToRequestsFunc(r.requestsForGitRepositoryRevisionChange)},
builder.WithPredicates(SourceRevisionChangePredicate{}),
).
Watches(
&source.Kind{Type: &sourcev1.Bucket{}},
&handler.EnqueueRequestsFromMapFunc{ToRequests: handler.ToRequestsFunc(r.kustomizationsForBucket)},
&handler.EnqueueRequestsFromMapFunc{ToRequests: handler.ToRequestsFunc(r.requestsForBucketRevisionChange)},
builder.WithPredicates(SourceRevisionChangePredicate{}),
).
WithOptions(controller.Options{MaxConcurrentReconciles: opts.MaxConcurrentReconciles}).
Expand Down Expand Up @@ -855,7 +855,16 @@ func (r *KustomizationReconciler) checkDependencies(kustomization kustomizev1.Ku
return nil
}

func (r *KustomizationReconciler) kustomizationsForGitRepository(obj handler.MapObject) []reconcile.Request {
func (r *KustomizationReconciler) requestsForGitRepositoryRevisionChange(obj handler.MapObject) []reconcile.Request {
repo, ok := obj.Object.(*sourcev1.GitRepository)
if !ok {
panic(fmt.Sprintf("Expected a GitRepository but got a %T", obj))
}
// If we do not have an artifact, we have no requests to make
if repo.GetArtifact() == nil {
return nil
}

ctx := context.Background()
var list kustomizev1.KustomizationList
if err := r.List(ctx, &list, client.MatchingFields{
Expand All @@ -866,6 +875,11 @@ func (r *KustomizationReconciler) kustomizationsForGitRepository(obj handler.Map
}
var dd []dependency.Dependent
for _, d := range list.Items {
// If the revision of the artifact equals to the last attempted revision,
// we should not make a request for this Kustomization
if repo.GetArtifact().Revision == d.Status.LastAttemptedRevision {
continue
}
dd = append(dd, d)
}
sorted, err := dependency.Sort(dd)
Expand All @@ -878,22 +892,38 @@ func (r *KustomizationReconciler) kustomizationsForGitRepository(obj handler.Map
reqs[i].NamespacedName.Name = sorted[i].Name
reqs[i].NamespacedName.Namespace = sorted[i].Namespace

r.Log.Info("requesting reconciliation", kustomizev1.KustomizationKind, reqs[i].NamespacedName)
r.Log.Info("requesting reconciliation due to GitRepository revision change",
strings.ToLower(kustomizev1.KustomizationKind), &reqs[i].NamespacedName,
"revision", repo.GetArtifact().Revision)
}
return reqs
}

func (r *KustomizationReconciler) kustomizationsForBucket(obj handler.MapObject) []reconcile.Request {
func (r *KustomizationReconciler) requestsForBucketRevisionChange(obj handler.MapObject) []reconcile.Request {
bucket, ok := obj.Object.(*sourcev1.Bucket)
if !ok {
panic(fmt.Sprintf("Expected a Bucket but got a %T", obj))
}
// If we do not have an artifact, we have no requests to make
if bucket.GetArtifact() == nil {
return nil
}

ctx := context.Background()
var list kustomizev1.KustomizationList
if err := r.List(ctx, &list, client.MatchingFields{
kustomizev1.BucketIndexKey: fmt.Sprintf("%s/%s", obj.Meta.GetNamespace(), obj.Meta.GetName()),
}); err != nil {
r.Log.Error(err, "failed to list Kustomizations for GitRepository")
r.Log.Error(err, "failed to list Kustomizations for Bucket")
return nil
}
var dd []dependency.Dependent
for _, d := range list.Items {
// If the revision of the artifact equals to the last attempted revision,
// we should not make a request for this Kustomization
if bucket.GetArtifact().Revision == d.Status.LastAttemptedRevision {
continue
}
dd = append(dd, d)
}
sorted, err := dependency.Sort(dd)
Expand All @@ -906,7 +936,9 @@ func (r *KustomizationReconciler) kustomizationsForBucket(obj handler.MapObject)
reqs[i].NamespacedName.Name = sorted[i].Name
reqs[i].NamespacedName.Namespace = sorted[i].Namespace

r.Log.Info("requesting reconciliation", kustomizev1.KustomizationKind, reqs[i].NamespacedName)
r.Log.Info("requesting reconciliation due to Bucket revision change",
strings.ToLower(kustomizev1.KustomizationKind), &reqs[i].NamespacedName,
"revision", bucket.GetArtifact().Revision)
}
return reqs
}
Expand Down

0 comments on commit db57728

Please sign in to comment.