@@ -25,7 +25,6 @@ import (
2525 corev1 "k8s.io/api/core/v1"
2626 apierrors "k8s.io/apimachinery/pkg/api/errors"
2727 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28- "k8s.io/apimachinery/pkg/labels"
2928 "k8s.io/apimachinery/pkg/types"
3029 utilerrors "k8s.io/apimachinery/pkg/util/errors"
3130 "k8s.io/client-go/tools/record"
@@ -169,9 +168,9 @@ func (b *bundle) reconcileBundle(ctx context.Context, req ctrl.Request) (result
169168 return ctrl.Result {}, statusPatch , nil
170169 }
171170
172- targetResources := map [ target.Resource ] struct {}{}
171+ var targetResources [] target.Resource
173172
174- namespaceSelector , err := b . bundleTargetNamespaceSelector (& bundle )
173+ namespaceSelector , err := target . NamespaceSelector (& bundle )
175174 if err != nil {
176175 b .recorder .Eventf (& bundle , corev1 .EventTypeWarning , "NamespaceSelectorError" , "Failed to build namespace match labels selector: %s" , err )
177176 return ctrl.Result {}, nil , fmt .Errorf ("failed to build NamespaceSelector: %w" , err )
@@ -202,87 +201,30 @@ func (b *bundle) reconcileBundle(ctx context.Context, req ctrl.Request) (result
202201 }
203202
204203 if bundle .Spec .Target .Secret != nil {
205- targetResources [ target.Resource {Kind : target .KindSecret , NamespacedName : namespacedName }] = struct {}{}
204+ targetResources = append ( targetResources , target.Resource {Kind : target .KindSecret , NamespacedName : namespacedName })
206205 }
207206 if bundle .Spec .Target .ConfigMap != nil {
208- targetResources [target.Resource {Kind : target .KindConfigMap , NamespacedName : namespacedName }] = struct {}{}
209- }
210- }
211- }
212-
213- // Find all old existing target resources.
214- targetKinds := []target.Kind {target .KindConfigMap }
215- if b .Options .SecretTargetsEnabled {
216- targetKinds = append (targetKinds , target .KindSecret )
217- }
218- for _ , kind := range targetKinds {
219- targetList := & metav1.PartialObjectMetadataList {
220- TypeMeta : metav1.TypeMeta {
221- APIVersion : "v1" ,
222- Kind : string (kind ),
223- },
224- }
225- err := b .targetReconciler .Cache .List (ctx , targetList , & client.ListOptions {
226- LabelSelector : labels .SelectorFromSet (map [string ]string {
227- trustapi .BundleLabelKey : bundle .Name ,
228- }),
229- })
230- if err != nil {
231- log .Error (err , "failed to list targets" , "kind" , kind )
232- b .recorder .Eventf (& bundle , corev1 .EventTypeWarning , fmt .Sprintf ("%sListError" , kind ), "Failed to list %ss: %s" , strings .ToLower (string (kind )), err )
233- return ctrl.Result {}, nil , fmt .Errorf ("failed to list %ss: %w" , kind , err )
234- }
235-
236- for _ , t := range targetList .Items {
237- key := target.Resource {
238- Kind : kind ,
239- NamespacedName : types.NamespacedName {
240- Name : t .Name ,
241- Namespace : t .Namespace ,
242- },
243- }
244-
245- targetLog := log .WithValues ("target" , key )
246-
247- if _ , ok := targetResources [key ]; ok {
248- // This target is still a target, so we don't need to remove it.
249- continue
250- }
251-
252- // Don't reconcile target for targets that are being deleted.
253- if t .GetDeletionTimestamp () != nil {
254- targetLog .V (2 ).WithValues ("deletionTimestamp" , t .GetDeletionTimestamp ()).Info ("skipping sync for target as it is being deleted" )
255- continue
256- }
257-
258- if ! metav1 .IsControlledBy (& t , & bundle ) /* #nosec G601 -- False positive. See https://github.com/golang/go/discussions/56010 */ {
259- targetLog .V (2 ).Info ("skipping sync for target as it is not controlled by bundle" )
260- continue
261- }
262-
263- if _ , err := b .targetReconciler .CleanupTarget (ctx , key , & bundle ); err != nil {
264- // Failing target cleanup is not considered critical, log error and continue.
265- targetLog .Error (err , "failed to cleanup bundle target" )
207+ targetResources = append (targetResources , target.Resource {Kind : target .KindConfigMap , NamespacedName : namespacedName })
266208 }
267209 }
268210 }
269211
270212 var needsUpdate bool
271213
272- for t := range targetResources {
214+ for _ , t := range targetResources {
273215 targetLog := log .WithValues ("target" , t )
274216 synced , err := b .targetReconciler .ApplyTarget (logf .IntoContext (ctx , targetLog ), t , & bundle , resolvedBundle )
275217 if err != nil {
276218 targetLog .Error (err , "failed sync bundle to target namespace" )
277- b .recorder .Eventf (& bundle , corev1 .EventTypeWarning , fmt .Sprintf ("Sync %sTargetFailed" , t .Kind ), "Failed to sync target %s in Namespace %q: %s" , t .Kind , t .Namespace , err )
219+ b .recorder .Eventf (& bundle , corev1 .EventTypeWarning , fmt .Sprintf ("ApplyTarget %sTargetFailed" , t .Kind ), "Failed to sync target %s in Namespace %q: %s" , t .Kind , t .Namespace , err )
278220
279221 b .setBundleCondition (
280222 bundle .Status .Conditions ,
281223 & statusPatch .Conditions ,
282224 metav1.Condition {
283225 Type : trustapi .BundleConditionSynced ,
284226 Status : metav1 .ConditionFalse ,
285- Reason : fmt .Sprintf ("Sync %sTargetFailed" , t .Kind ),
227+ Reason : fmt .Sprintf ("ApplyTarget %sTargetFailed" , t .Kind ),
286228 Message : fmt .Sprintf ("Failed to sync bundle %s to namespace %q: %s" , t .Kind , t .Namespace , err ),
287229 ObservedGeneration : bundle .Generation ,
288230 },
@@ -330,16 +272,3 @@ func (b *bundle) reconcileBundle(ctx context.Context, req ctrl.Request) (result
330272
331273 return ctrl.Result {}, statusPatch , nil
332274}
333-
334- func (b * bundle ) bundleTargetNamespaceSelector (bundleObj * trustapi.Bundle ) (labels.Selector , error ) {
335- nsSelector := bundleObj .Spec .Target .NamespaceSelector
336-
337- // LabelSelectorAsSelector returns a Selector selecting nothing if LabelSelector is nil,
338- // while our current default is to select everything. But this is subject to change.
339- // See https://github.com/cert-manager/trust-manager/issues/39
340- if nsSelector == nil {
341- return labels .Everything (), nil
342- }
343-
344- return metav1 .LabelSelectorAsSelector (nsSelector )
345- }
0 commit comments