diff --git a/pkg/fleet-manager/clusters.go b/pkg/fleet-manager/clusters.go index cc248afdc..78df3ed30 100644 --- a/pkg/fleet-manager/clusters.go +++ b/pkg/fleet-manager/clusters.go @@ -70,7 +70,7 @@ func (f *FleetManager) reconcileClusters(ctx context.Context, fleet *fleetapi.Fl for _, cluster := range fleet.Spec.Clusters { // cluster namespace can be not set, always use fleet namespace as a fleet can only include clusters in the same namespace. clusterKey := types.NamespacedName{Name: cluster.Name, Namespace: fleet.Namespace} - currentCluster, err := f.getFleetClusterInterface(ctx, cluster.Kind, clusterKey) + currentCluster, err := getFleetClusterInterface(ctx, f.Client, cluster.Kind, clusterKey) if err != nil { if !apierrors.IsNotFound(err) { log.Error(err, "unable to fetch cluster", "cluster", clusterKey, "kind", cluster.Kind) @@ -190,7 +190,7 @@ func (f *FleetManager) reconcileClustersOnDelete(ctx context.Context, fleet *fle for _, cluster := range fleet.Spec.Clusters { // cluster namespace can be not set, always use fleet namespace as a fleet can only include clusters in the same namespace. clusterKey := types.NamespacedName{Name: cluster.Name, Namespace: fleet.Namespace} - currentCluster, err := f.getFleetClusterInterface(ctx, cluster.Kind, clusterKey) + currentCluster, err := getFleetClusterInterface(ctx, f.Client, cluster.Kind, clusterKey) if err != nil { if !apierrors.IsNotFound(err) { log.Error(err, "unable to fetch cluster", "cluster", clusterKey) diff --git a/pkg/fleet-manager/fleet.go b/pkg/fleet-manager/fleet.go index 389602ff7..e27973de3 100644 --- a/pkg/fleet-manager/fleet.go +++ b/pkg/fleet-manager/fleet.go @@ -166,7 +166,7 @@ func (f *FleetManager) reconcile(ctx context.Context, fleet *fleetapi.Fleet) (ct return res, err } - fleetClusters, err := f.buildFleetClusters(ctx, fleet) + fleetClusters, err := buildFleetClusters(ctx, f.Client, fleet) if err != nil { return ctrl.Result{}, fmt.Errorf("failed to build cluster clients: %w", err) } diff --git a/pkg/fleet-manager/fleet_cluster.go b/pkg/fleet-manager/fleet_cluster.go index b4fe7fd13..7c87ae3eb 100644 --- a/pkg/fleet-manager/fleet_cluster.go +++ b/pkg/fleet-manager/fleet_cluster.go @@ -24,6 +24,7 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/clientcmd" ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" clusterv1alpha1 "kurator.dev/kurator/pkg/apis/cluster/v1alpha1" fleetapi "kurator.dev/kurator/pkg/apis/fleet/v1alpha1" @@ -41,13 +42,13 @@ type ClusterKey struct { Name string } -func (f *FleetManager) buildFleetClusters(ctx context.Context, fleet *fleetapi.Fleet) (map[ClusterKey]*fleetCluster, error) { +func buildFleetClusters(ctx context.Context, client client.Client, fleet *fleetapi.Fleet) (map[ClusterKey]*fleetCluster, error) { log := ctrl.LoggerFrom(ctx) res := make(map[ClusterKey]*fleetCluster, len(fleet.Spec.Clusters)) - for _, c := range fleet.Spec.Clusters { - clusterKey := types.NamespacedName{Namespace: fleet.Namespace, Name: c.Name} - clusterInterface, err := f.getFleetClusterInterface(ctx, c.Kind, clusterKey) + for _, cluster := range fleet.Spec.Clusters { + clusterKey := types.NamespacedName{Namespace: fleet.Namespace, Name: cluster.Name} + clusterInterface, err := getFleetClusterInterface(ctx, client, cluster.Kind, clusterKey) // TODO: should we make it work if err != nil { return nil, err @@ -58,11 +59,11 @@ func (f *FleetManager) buildFleetClusters(ctx context.Context, fleet *fleetapi.F continue } - kclient, err := f.clientForCluster(fleet.Namespace, clusterInterface) + kclient, err := clientForCluster(client, fleet.Namespace, clusterInterface) if err != nil { return nil, err } - res[ClusterKey{Kind: c.Kind, Name: c.Name}] = &fleetCluster{ + res[ClusterKey{Kind: cluster.Kind, Name: cluster.Name}] = &fleetCluster{ Secret: clusterInterface.GetSecretName(), SecretKey: clusterInterface.GetSecretKey(), client: kclient, @@ -72,17 +73,17 @@ func (f *FleetManager) buildFleetClusters(ctx context.Context, fleet *fleetapi.F return res, nil } -func (f *FleetManager) getFleetClusterInterface(ctx context.Context, kind string, nn types.NamespacedName) (ClusterInterface, error) { +func getFleetClusterInterface(ctx context.Context, client client.Client, kind string, nn types.NamespacedName) (ClusterInterface, error) { switch kind { case ClusterKind, "": cluster := &clusterv1alpha1.Cluster{} - if err := f.Get(ctx, nn, cluster); err != nil { + if err := client.Get(ctx, nn, cluster); err != nil { return nil, err } return cluster, nil case AttachedClusterKind: attachedCluster := &clusterv1alpha1.AttachedCluster{} - if err := f.Get(ctx, nn, attachedCluster); err != nil { + if err := client.Get(ctx, nn, attachedCluster); err != nil { return nil, err } return attachedCluster, nil @@ -91,10 +92,10 @@ func (f *FleetManager) getFleetClusterInterface(ctx context.Context, kind string } } -func (f *FleetManager) clientForCluster(ns string, cluster ClusterInterface) (*kclient.Client, error) { +func clientForCluster(client client.Client, ns string, cluster ClusterInterface) (*kclient.Client, error) { secret := &corev1.Secret{} nn := types.NamespacedName{Namespace: ns, Name: cluster.GetSecretName()} - if err := f.Client.Get(context.Background(), nn, secret); err != nil { + if err := client.Get(context.Background(), nn, secret); err != nil { return nil, err }