Skip to content

Commit 147df26

Browse files
committed
replace redundant indexers code with high-order functions
Signed-off-by: Chanwit Kaewkasi <[email protected]>
1 parent 80ebb79 commit 147df26

File tree

2 files changed

+53
-102
lines changed

2 files changed

+53
-102
lines changed

controllers/kustomization_controller.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ type KustomizationReconcilerOptions struct {
8888
func (r *KustomizationReconciler) SetupWithManager(mgr ctrl.Manager, opts KustomizationReconcilerOptions) error {
8989
// Index the Kustomizations by the GitRepository references they (may) point at.
9090
if err := mgr.GetCache().IndexField(context.TODO(), &kustomizev1.Kustomization{}, kustomizev1.GitRepositoryIndexKey,
91-
r.indexByGitRepository); err != nil {
91+
r.indexBy(sourcev1.GitRepositoryKind)); err != nil {
9292
return fmt.Errorf("failed setting index fields: %w", err)
9393
}
9494

9595
// Index the Kustomizations by the Bucket references they (may) point at.
9696
if err := mgr.GetCache().IndexField(context.TODO(), &kustomizev1.Kustomization{}, kustomizev1.BucketIndexKey,
97-
r.indexByBucket); err != nil {
97+
r.indexBy(sourcev1.BucketKind)); err != nil {
9898
return fmt.Errorf("failed setting index fields: %w", err)
9999
}
100100

@@ -115,12 +115,12 @@ func (r *KustomizationReconciler) SetupWithManager(mgr ctrl.Manager, opts Kustom
115115
)).
116116
Watches(
117117
&source.Kind{Type: &sourcev1.GitRepository{}},
118-
handler.EnqueueRequestsFromMapFunc(r.requestsForGitRepositoryRevisionChange),
118+
handler.EnqueueRequestsFromMapFunc(r.requestsForRevisionChangeOf(kustomizev1.GitRepositoryIndexKey)),
119119
builder.WithPredicates(SourceRevisionChangePredicate{}),
120120
).
121121
Watches(
122122
&source.Kind{Type: &sourcev1.Bucket{}},
123-
handler.EnqueueRequestsFromMapFunc(r.requestsForBucketRevisionChange),
123+
handler.EnqueueRequestsFromMapFunc(r.requestsForRevisionChangeOf(kustomizev1.BucketIndexKey)),
124124
builder.WithPredicates(SourceRevisionChangePredicate{}),
125125
).
126126
WithOptions(controller.Options{MaxConcurrentReconciles: opts.MaxConcurrentReconciles}).

controllers/kustomization_indexers.go

+49-98
Original file line numberDiff line numberDiff line change
@@ -28,112 +28,63 @@ import (
2828
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
2929
)
3030

31-
func (r *KustomizationReconciler) requestsForGitRepositoryRevisionChange(obj client.Object) []reconcile.Request {
32-
repo, ok := obj.(*sourcev1.GitRepository)
33-
if !ok {
34-
panic(fmt.Sprintf("Expected a GitRepository but got a %T", obj))
35-
}
36-
// If we do not have an artifact, we have no requests to make
37-
if repo.GetArtifact() == nil {
38-
return nil
39-
}
40-
41-
ctx := context.Background()
42-
var list kustomizev1.KustomizationList
43-
if err := r.List(ctx, &list, client.MatchingFields{
44-
kustomizev1.GitRepositoryIndexKey: ObjectKey(obj).String(),
45-
}); err != nil {
46-
return nil
47-
}
48-
var dd []dependency.Dependent
49-
for _, d := range list.Items {
50-
// If the revision of the artifact equals to the last attempted revision,
51-
// we should not make a request for this Kustomization
52-
if repo.GetArtifact().Revision == d.Status.LastAttemptedRevision {
53-
continue
31+
func (r *KustomizationReconciler) requestsForRevisionChangeOf(indexKey string) func(obj client.Object) []reconcile.Request {
32+
return func(obj client.Object) []reconcile.Request {
33+
repo, ok := obj.(interface {
34+
GetArtifact() *sourcev1.Artifact
35+
})
36+
if !ok {
37+
panic(fmt.Sprintf("Expected an object conformed with GetArtifact() method, but got a %T", obj))
5438
}
55-
dd = append(dd, d)
56-
}
57-
sorted, err := dependency.Sort(dd)
58-
if err != nil {
59-
return nil
60-
}
61-
reqs := make([]reconcile.Request, len(sorted), len(sorted))
62-
for i := range sorted {
63-
reqs[i].NamespacedName.Name = sorted[i].Name
64-
reqs[i].NamespacedName.Namespace = sorted[i].Namespace
65-
}
66-
return reqs
67-
}
68-
69-
func (r *KustomizationReconciler) indexByGitRepository(o client.Object) []string {
70-
k, ok := o.(*kustomizev1.Kustomization)
71-
if !ok {
72-
panic(fmt.Sprintf("Expected a Kustomization, got %T", o))
73-
}
74-
75-
if k.Spec.SourceRef.Kind == sourcev1.GitRepositoryKind {
76-
namespace := k.GetNamespace()
77-
if k.Spec.SourceRef.Namespace != "" {
78-
namespace = k.Spec.SourceRef.Namespace
39+
// If we do not have an artifact, we have no requests to make
40+
if repo.GetArtifact() == nil {
41+
return nil
7942
}
80-
return []string{fmt.Sprintf("%s/%s", namespace, k.Spec.SourceRef.Name)}
81-
}
82-
83-
return nil
84-
}
8543

86-
func (r *KustomizationReconciler) requestsForBucketRevisionChange(obj client.Object) []reconcile.Request {
87-
bucket, ok := obj.(*sourcev1.Bucket)
88-
if !ok {
89-
panic(fmt.Sprintf("Expected a Bucket but got a %T", obj))
90-
}
91-
// If we do not have an artifact, we have no requests to make
92-
if bucket.GetArtifact() == nil {
93-
return nil
94-
}
95-
96-
ctx := context.Background()
97-
var list kustomizev1.KustomizationList
98-
if err := r.List(ctx, &list, client.MatchingFields{
99-
kustomizev1.BucketIndexKey: ObjectKey(obj).String(),
100-
}); err != nil {
101-
return nil
102-
}
103-
var dd []dependency.Dependent
104-
for _, d := range list.Items {
105-
// If the revision of the artifact equals to the last attempted revision,
106-
// we should not make a request for this Kustomization
107-
if bucket.GetArtifact().Revision == d.Status.LastAttemptedRevision {
108-
continue
44+
ctx := context.Background()
45+
var list kustomizev1.KustomizationList
46+
if err := r.List(ctx, &list, client.MatchingFields{
47+
indexKey: ObjectKey(obj).String(),
48+
}); err != nil {
49+
return nil
10950
}
110-
dd = append(dd, d)
111-
}
112-
sorted, err := dependency.Sort(dd)
113-
if err != nil {
114-
return nil
115-
}
116-
reqs := make([]reconcile.Request, len(sorted), len(sorted))
117-
for i := range sorted {
118-
reqs[i].NamespacedName.Name = sorted[i].Name
119-
reqs[i].NamespacedName.Namespace = sorted[i].Namespace
51+
var dd []dependency.Dependent
52+
for _, d := range list.Items {
53+
// If the revision of the artifact equals to the last attempted revision,
54+
// we should not make a request for this Kustomization
55+
if repo.GetArtifact().Revision == d.Status.LastAttemptedRevision {
56+
continue
57+
}
58+
dd = append(dd, d)
59+
}
60+
sorted, err := dependency.Sort(dd)
61+
if err != nil {
62+
return nil
63+
}
64+
reqs := make([]reconcile.Request, len(sorted), len(sorted))
65+
for i := range sorted {
66+
reqs[i].NamespacedName.Name = sorted[i].Name
67+
reqs[i].NamespacedName.Namespace = sorted[i].Namespace
68+
}
69+
return reqs
12070
}
121-
return reqs
12271
}
12372

124-
func (r *KustomizationReconciler) indexByBucket(o client.Object) []string {
125-
k, ok := o.(*kustomizev1.Kustomization)
126-
if !ok {
127-
panic(fmt.Sprintf("Expected a Kustomization, got %T", o))
128-
}
73+
func (r *KustomizationReconciler) indexBy(kind string) func(o client.Object) []string {
74+
return func(o client.Object) []string {
75+
k, ok := o.(*kustomizev1.Kustomization)
76+
if !ok {
77+
panic(fmt.Sprintf("Expected a Kustomization, got %T", o))
78+
}
12979

130-
if k.Spec.SourceRef.Kind == sourcev1.BucketKind {
131-
namespace := k.GetNamespace()
132-
if k.Spec.SourceRef.Namespace != "" {
133-
namespace = k.Spec.SourceRef.Namespace
80+
if k.Spec.SourceRef.Kind == kind {
81+
namespace := k.GetNamespace()
82+
if k.Spec.SourceRef.Namespace != "" {
83+
namespace = k.Spec.SourceRef.Namespace
84+
}
85+
return []string{fmt.Sprintf("%s/%s", namespace, k.Spec.SourceRef.Name)}
13486
}
135-
return []string{fmt.Sprintf("%s/%s", namespace, k.Spec.SourceRef.Name)}
136-
}
13787

138-
return nil
88+
return nil
89+
}
13990
}

0 commit comments

Comments
 (0)