Skip to content

Commit

Permalink
filter entries by kind in name
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWeindel committed Jan 16, 2025
1 parent 59b08dc commit 0cf9d3d
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/dns/source/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func DNSSourceController(source DNSSourceType, reconcilerType controller.Reconci
Cluster(cluster.DEFAULT). // first one used as MAIN cluster
DefaultWorkerPool(2, 120*time.Second).
MainResource(gk.Group, gk.Kind).
Reconciler(reconcilers.SlaveReconcilerTypeByFunction(SlaveReconcilerType, SlaveAccessSpecCreatorForSource(source)), "entries").
Reconciler(reconcilerTypeFilterByKind(gk.Kind, reconcilers.SlaveReconcilerTypeByFunction(SlaveReconcilerType, SlaveAccessSpecCreatorForSource(source))), "entries").
Reconciler(OwnerReconciler, "owner").
Cluster(TARGET_CLUSTER, cluster.DEFAULT).
CustomResourceDefinitions(entryGroupKind).
Expand Down
91 changes: 91 additions & 0 deletions pkg/dns/source/filterbykindreonciler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0

package source

import (
"fmt"
"strings"

"github.com/gardener/controller-manager-library/pkg/controllermanager/controller"
"github.com/gardener/controller-manager-library/pkg/controllermanager/controller/reconcile"
"github.com/gardener/controller-manager-library/pkg/logger"
"github.com/gardener/controller-manager-library/pkg/resources"
)

func reconcilerTypeFilterByKind(kind string, reconcilerType controller.ReconcilerType) controller.ReconcilerType {
return func(c controller.Interface) (reconcile.Interface, error) {
reconciler, err := reconcilerType(c)
if err != nil {
return nil, err
}
return &filterByKindReconciler{
nested: reconciler,
kindSubstring: fmt.Sprintf("-%s-", strings.ToLower(kind)),
}, nil
}
}

// filterByKindReconciler is a reconciler that filters out entries by kind.
type filterByKindReconciler struct {
nested reconcile.Interface
kindSubstring string
}

var _ reconcile.Interface = &filterByKindReconciler{}
var _ reconcile.StartInterface = &filterByKindReconciler{}
var _ reconcile.SetupInterface = &filterByKindReconciler{}
var _ reconcile.CleanupInterface = &filterByKindReconciler{}

func (f filterByKindReconciler) Start() error {
if itf, ok := f.nested.(reconcile.StartInterface); ok {
return itf.Start()
}
return nil
}

func (f filterByKindReconciler) Setup() error {
if itf, ok := f.nested.(reconcile.SetupInterface); ok {
return itf.Setup()
}
return nil
}

func (f filterByKindReconciler) Cleanup() error {
if itf, ok := f.nested.(reconcile.CleanupInterface); ok {
return itf.Cleanup()
}
return nil
}

func (f filterByKindReconciler) Reconcile(logger logger.LogContext, object resources.Object) reconcile.Status {
if !f.isRelevantByHeuristic(object.GetName()) {
return reconcile.Succeeded(logger)
}
return f.nested.Reconcile(logger, object)
}

func (f filterByKindReconciler) Delete(logger logger.LogContext, object resources.Object) reconcile.Status {
if !f.isRelevantByHeuristic(object.GetName()) {
return reconcile.Succeeded(logger)
}
return f.nested.Delete(logger, object)
}

func (f filterByKindReconciler) Deleted(logger logger.LogContext, key resources.ClusterObjectKey) reconcile.Status {
if !f.isRelevantByHeuristic(key.Name()) {
return reconcile.Succeeded(logger)
}
return f.nested.Deleted(logger, key)
}

func (f filterByKindReconciler) Command(logger logger.LogContext, cmd string) reconcile.Status {
return f.nested.Command(logger, cmd)
}

// isRelevantByHeuristic returns true if the entry name contains the kind substring.
// This is a heuristic which relies on the fact that the SourceReconciler adds the kind to the entry name.
func (this *filterByKindReconciler) isRelevantByHeuristic(objectName string) bool {
return strings.Contains(objectName, this.kindSubstring)
}

0 comments on commit 0cf9d3d

Please sign in to comment.