Skip to content

Commit

Permalink
Replace logrus with logger when querying targets
Browse files Browse the repository at this point in the history
Maybe rely on logger from context?
  • Loading branch information
manno committed Jul 17, 2024
1 parent 3f1e043 commit cf5a2bc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
5 changes: 3 additions & 2 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, *fleet.Cluster) ([]*fleet.Bundle, []*fleet.Bundle, error)
BundlesForCluster(context.Context, logr.Logger, *fleet.Cluster) ([]*fleet.Bundle, []*fleet.Bundle, error)
}

type Store interface {
Expand Down Expand Up @@ -94,7 +94,8 @@ 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)
bundlesToRefresh, _, err := r.Query.BundlesForCluster(ctx, cluster)
logger := log.FromContext(ctx).WithName("bundle enqueuer")
bundlesToRefresh, _, err := r.Query.BundlesForCluster(ctx, logger, 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, cluster)
_, cleanup, err := r.Query.BundlesForCluster(ctx, logger, cluster)
if err != nil {
return ctrl.Result{}, r.updateErrorStatus(ctx, req.NamespacedName, cluster.Status, err)
}
Expand Down
20 changes: 10 additions & 10 deletions internal/cmd/controller/target/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package target
import (
"context"

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

fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
Expand All @@ -13,20 +13,20 @@ import (
"k8s.io/apimachinery/pkg/labels"
)

func (m *Manager) BundlesForCluster(ctx context.Context, cluster *fleet.Cluster) (bundlesToRefresh, bundlesToCleanup []*fleet.Bundle, err error) {
bundles, err := m.getBundlesInScopeForCluster(ctx, cluster)
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)
if err != nil {
return nil, nil, err
}

for _, bundle := range bundles {
bm, err := matcher.New(bundle)
if err != nil {
logrus.Errorf("ignore bad app %s/%s: %v", bundle.Namespace, bundle.Name, err)
logger.Error(err, "ignore bad app bundle", "namespace", bundle.Namespace, "name", bundle.Name)
continue
}

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

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

// all bundles in the cluster namespace are in scope
Expand Down Expand Up @@ -72,7 +72,7 @@ func (m *Manager) getBundlesInScopeForCluster(ctx context.Context, cluster *flee
mapping := mapping
matcher, err := newBundleMapping(&mapping)
if err != nil {
logrus.Errorf("invalid BundleNamespaceMapping %s/%s skipping: %v", mapping.Namespace, mapping.Name, err)
logger.Error(err, "invalid BundleNamespaceMapping, skipping", "namespace", mapping.Namespace, "name", mapping.Name)
continue
}
if !matcher.MatchesNamespace(ctx, m.client, cluster.Namespace) {
Expand All @@ -86,7 +86,7 @@ func (m *Manager) getBundlesInScopeForCluster(ctx context.Context, cluster *flee
return bundleSet.bundles(), nil
}

func (m *Manager) clusterGroupsForCluster(ctx context.Context, cluster *fleet.Cluster) (result []*fleet.ClusterGroup, _ error) {
func (m *Manager) clusterGroupsForCluster(ctx context.Context, logger logr.Logger, cluster *fleet.Cluster) (result []*fleet.ClusterGroup, _ error) {
cgs := &fleet.ClusterGroupList{}
err := m.client.List(ctx, cgs, client.InNamespace(cluster.Namespace))
if err != nil {
Expand All @@ -100,8 +100,8 @@ func (m *Manager) clusterGroupsForCluster(ctx context.Context, cluster *fleet.Cl
}
sel, err := metav1.LabelSelectorAsSelector(cg.Spec.Selector)
if err != nil {
logrus.Errorf("invalid selector on clusterGroup %s/%s [%v]: %v", cg.Namespace, cg.Name,
cg.Spec.Selector, err)
logger.Error(err, "invalid selector on clusterGroup", "namespace", cg.Namespace, "name", cg.Name,
"selector", cg.Spec.Selector)
continue
}
if sel.Matches(labels.Set(cluster.Labels)) {
Expand Down

0 comments on commit cf5a2bc

Please sign in to comment.