Skip to content

Commit

Permalink
fixup! Replace logrus with logger when querying targets
Browse files Browse the repository at this point in the history
  • Loading branch information
manno committed Jul 17, 2024
1 parent cf5a2bc commit 14d7f44
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
5 changes: 2 additions & 3 deletions internal/cmd/controller/reconciler/bundle_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const bundleFinalizer = "fleet.cattle.io/bundle-finalizer"

type BundleQuery interface {
// BundlesForCluster is used to map from a cluster to bundles
BundlesForCluster(context.Context, logr.Logger, *fleet.Cluster) ([]*fleet.Bundle, []*fleet.Bundle, error)
BundlesForCluster(context.Context, *fleet.Cluster) ([]*fleet.Bundle, []*fleet.Bundle, error)
}

type Store interface {
Expand Down Expand Up @@ -94,8 +94,7 @@ func (r *BundleReconciler) SetupWithManager(mgr ctrl.Manager) error {
&fleet.Cluster{},
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, a client.Object) []ctrl.Request {
cluster := a.(*fleet.Cluster)
logger := log.FromContext(ctx).WithName("bundle enqueuer")
bundlesToRefresh, _, err := r.Query.BundlesForCluster(ctx, logger, cluster)
bundlesToRefresh, _, err := r.Query.BundlesForCluster(ctx, cluster)
if err != nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/controller/reconciler/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}

// Clean up old bundledeployments
_, cleanup, err := r.Query.BundlesForCluster(ctx, logger, cluster)
_, cleanup, err := r.Query.BundlesForCluster(ctx, cluster)
if err != nil {
return ctrl.Result{}, r.updateErrorStatus(ctx, req.NamespacedName, cluster.Status, err)
}
Expand Down
17 changes: 10 additions & 7 deletions internal/cmd/controller/target/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@ package target
import (
"context"

"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/rancher/fleet/internal/cmd/controller/target/matcher"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"

"github.com/rancher/fleet/internal/cmd/controller/target/matcher"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)

func (m *Manager) BundlesForCluster(ctx context.Context, logger logr.Logger, cluster *fleet.Cluster) (bundlesToRefresh, bundlesToCleanup []*fleet.Bundle, err error) {
bundles, err := m.getBundlesInScopeForCluster(ctx, logger, cluster)
func (m *Manager) BundlesForCluster(ctx context.Context, cluster *fleet.Cluster) (bundlesToRefresh, bundlesToCleanup []*fleet.Bundle, err error) {
bundles, err := m.getBundlesInScopeForCluster(ctx, cluster)
if err != nil {
return nil, nil, err
}

logger := log.FromContext(ctx).WithName("target")
for _, bundle := range bundles {
bm, err := matcher.New(bundle)
if err != nil {
logger.Error(err, "ignore bad app bundle", "namespace", bundle.Namespace, "name", bundle.Name)
continue
}

cgs, err := m.clusterGroupsForCluster(ctx, logger, cluster)
cgs, err := m.clusterGroupsForCluster(ctx, cluster)
if err != nil {
return nil, nil, err
}
Expand All @@ -42,7 +43,7 @@ func (m *Manager) BundlesForCluster(ctx context.Context, logger logr.Logger, clu
return
}

func (m *Manager) getBundlesInScopeForCluster(ctx context.Context, logger logr.Logger, cluster *fleet.Cluster) ([]*fleet.Bundle, error) {
func (m *Manager) getBundlesInScopeForCluster(ctx context.Context, cluster *fleet.Cluster) ([]*fleet.Bundle, error) {
bundleSet := newBundleSet()

// all bundles in the cluster namespace are in scope
Expand All @@ -68,6 +69,7 @@ func (m *Manager) getBundlesInScopeForCluster(ctx context.Context, logger logr.L
return nil, err
}

logger := log.FromContext(ctx).WithName("target")
for _, mapping := range mappings.Items {
mapping := mapping
matcher, err := newBundleMapping(&mapping)
Expand All @@ -86,13 +88,14 @@ func (m *Manager) getBundlesInScopeForCluster(ctx context.Context, logger logr.L
return bundleSet.bundles(), nil
}

func (m *Manager) clusterGroupsForCluster(ctx context.Context, logger logr.Logger, cluster *fleet.Cluster) (result []*fleet.ClusterGroup, _ error) {
func (m *Manager) clusterGroupsForCluster(ctx context.Context, cluster *fleet.Cluster) (result []*fleet.ClusterGroup, _ error) {
cgs := &fleet.ClusterGroupList{}
err := m.client.List(ctx, cgs, client.InNamespace(cluster.Namespace))
if err != nil {
return nil, err
}

logger := log.FromContext(ctx).WithName("target")
for _, cg := range cgs.Items {
cg := cg
if cg.Spec.Selector == nil {
Expand Down

0 comments on commit 14d7f44

Please sign in to comment.