Skip to content

Commit

Permalink
Merge pull request #379 from Xieql/t95
Browse files Browse the repository at this point in the history
fleet: Refactor for broader usage by decoupling from FleetManager
  • Loading branch information
kurator-bot authored Sep 7, 2023
2 parents eff94b1 + da954af commit 96bb2ce
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pkg/fleet-manager/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/fleet-manager/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
23 changes: 12 additions & 11 deletions pkg/fleet-manager/fleet_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit 96bb2ce

Please sign in to comment.