@@ -119,31 +119,81 @@ type KustomizationReconcilerOptions struct {
119119 HTTPRetry int
120120 DependencyRequeueInterval time.Duration
121121 RateLimiter workqueue.TypedRateLimiter [reconcile.Request ]
122+ WatchConfigsPredicate predicate.Predicate
122123}
123124
124125func (r * KustomizationReconciler ) SetupWithManager (ctx context.Context , mgr ctrl.Manager , opts KustomizationReconcilerOptions ) error {
125126 const (
126- ociRepositoryIndexKey string = ".metadata.ociRepository"
127- gitRepositoryIndexKey string = ".metadata.gitRepository"
128- bucketIndexKey string = ".metadata.bucket"
127+ indexOCIRepository = ".metadata.ociRepository"
128+ indexGitRepository = ".metadata.gitRepository"
129+ indexBucket = ".metadata.bucket"
130+ indexConfigMap = ".metadata.configMap"
131+ indexSecret = ".metadata.secret"
129132 )
130133
131134 // Index the Kustomizations by the OCIRepository references they (may) point at.
132- if err := mgr .GetCache ().IndexField (ctx , & kustomizev1.Kustomization {}, ociRepositoryIndexKey ,
135+ if err := mgr .GetCache ().IndexField (ctx , & kustomizev1.Kustomization {}, indexOCIRepository ,
133136 r .indexBy (sourcev1 .OCIRepositoryKind )); err != nil {
134- return fmt .Errorf ("failed setting index fields : %w" , err )
137+ return fmt .Errorf ("failed creating index %s : %w" , indexOCIRepository , err )
135138 }
136139
137140 // Index the Kustomizations by the GitRepository references they (may) point at.
138- if err := mgr .GetCache ().IndexField (ctx , & kustomizev1.Kustomization {}, gitRepositoryIndexKey ,
141+ if err := mgr .GetCache ().IndexField (ctx , & kustomizev1.Kustomization {}, indexGitRepository ,
139142 r .indexBy (sourcev1 .GitRepositoryKind )); err != nil {
140- return fmt .Errorf ("failed setting index fields : %w" , err )
143+ return fmt .Errorf ("failed creating index %s : %w" , indexGitRepository , err )
141144 }
142145
143146 // Index the Kustomizations by the Bucket references they (may) point at.
144- if err := mgr .GetCache ().IndexField (ctx , & kustomizev1.Kustomization {}, bucketIndexKey ,
147+ if err := mgr .GetCache ().IndexField (ctx , & kustomizev1.Kustomization {}, indexBucket ,
145148 r .indexBy (sourcev1 .BucketKind )); err != nil {
146- return fmt .Errorf ("failed setting index fields: %w" , err )
149+ return fmt .Errorf ("failed creating index %s: %w" , indexBucket , err )
150+ }
151+
152+ // Index the Kustomization by the ConfigMap references they point to.
153+ if err := mgr .GetFieldIndexer ().IndexField (ctx , & kustomizev1.Kustomization {}, indexConfigMap ,
154+ func (o client.Object ) []string {
155+ obj := o .(* kustomizev1.Kustomization )
156+ namespace := obj .GetNamespace ()
157+ var keys []string
158+ if kc := obj .Spec .KubeConfig ; kc != nil && kc .ConfigMapRef != nil {
159+ keys = append (keys , fmt .Sprintf ("%s/%s" , namespace , kc .ConfigMapRef .Name ))
160+ }
161+ if pb := obj .Spec .PostBuild ; pb != nil {
162+ for _ , ref := range pb .SubstituteFrom {
163+ if ref .Kind == "ConfigMap" {
164+ keys = append (keys , fmt .Sprintf ("%s/%s" , namespace , ref .Name ))
165+ }
166+ }
167+ }
168+ return keys
169+ },
170+ ); err != nil {
171+ return fmt .Errorf ("failed creating index %s: %w" , indexConfigMap , err )
172+ }
173+
174+ // Index the Kustomization by the Secret references they point to.
175+ if err := mgr .GetFieldIndexer ().IndexField (ctx , & kustomizev1.Kustomization {}, indexSecret ,
176+ func (o client.Object ) []string {
177+ obj := o .(* kustomizev1.Kustomization )
178+ namespace := obj .GetNamespace ()
179+ var keys []string
180+ if dec := obj .Spec .Decryption ; dec != nil && dec .SecretRef != nil {
181+ keys = append (keys , fmt .Sprintf ("%s/%s" , namespace , dec .SecretRef .Name ))
182+ }
183+ if kc := obj .Spec .KubeConfig ; kc != nil && kc .SecretRef != nil {
184+ keys = append (keys , fmt .Sprintf ("%s/%s" , namespace , kc .SecretRef .Name ))
185+ }
186+ if pb := obj .Spec .PostBuild ; pb != nil {
187+ for _ , ref := range pb .SubstituteFrom {
188+ if ref .Kind == "Secret" {
189+ keys = append (keys , fmt .Sprintf ("%s/%s" , namespace , ref .Name ))
190+ }
191+ }
192+ }
193+ return keys
194+ },
195+ ); err != nil {
196+ return fmt .Errorf ("failed creating index %s: %w" , indexSecret , err )
147197 }
148198
149199 r .requeueDependency = opts .DependencyRequeueInterval
@@ -156,19 +206,29 @@ func (r *KustomizationReconciler) SetupWithManager(ctx context.Context, mgr ctrl
156206 )).
157207 Watches (
158208 & sourcev1.OCIRepository {},
159- handler .EnqueueRequestsFromMapFunc (r .requestsForRevisionChangeOf (ociRepositoryIndexKey )),
209+ handler .EnqueueRequestsFromMapFunc (r .requestsForRevisionChangeOf (indexOCIRepository )),
160210 builder .WithPredicates (SourceRevisionChangePredicate {}),
161211 ).
162212 Watches (
163213 & sourcev1.GitRepository {},
164- handler .EnqueueRequestsFromMapFunc (r .requestsForRevisionChangeOf (gitRepositoryIndexKey )),
214+ handler .EnqueueRequestsFromMapFunc (r .requestsForRevisionChangeOf (indexGitRepository )),
165215 builder .WithPredicates (SourceRevisionChangePredicate {}),
166216 ).
167217 Watches (
168218 & sourcev1.Bucket {},
169- handler .EnqueueRequestsFromMapFunc (r .requestsForRevisionChangeOf (bucketIndexKey )),
219+ handler .EnqueueRequestsFromMapFunc (r .requestsForRevisionChangeOf (indexBucket )),
170220 builder .WithPredicates (SourceRevisionChangePredicate {}),
171221 ).
222+ WatchesMetadata (
223+ & corev1.ConfigMap {},
224+ handler .EnqueueRequestsFromMapFunc (r .requestsForConfigDependency (indexConfigMap )),
225+ builder .WithPredicates (predicate.ResourceVersionChangedPredicate {}, opts .WatchConfigsPredicate ),
226+ ).
227+ WatchesMetadata (
228+ & corev1.Secret {},
229+ handler .EnqueueRequestsFromMapFunc (r .requestsForConfigDependency (indexSecret )),
230+ builder .WithPredicates (predicate.ResourceVersionChangedPredicate {}, opts .WatchConfigsPredicate ),
231+ ).
172232 WithOptions (controller.Options {
173233 RateLimiter : opts .RateLimiter ,
174234 }).
0 commit comments