From 6c473aa9520b41b6bbdf4165705838265733fcb0 Mon Sep 17 00:00:00 2001 From: Leo Ryu Date: Tue, 30 Nov 2021 11:16:08 +0800 Subject: [PATCH] refactor(platform): reduce provider method (#1684) --- pkg/platform/provider/cluster/cluster.go | 9 ++++-- pkg/platform/provider/cluster/interface.go | 28 ++----------------- .../cluster/storage/tappcontroller.go | 7 ++++- pkg/platform/util/client.go | 16 +++++++++-- pkg/platform/util/location.go | 9 +++++- 5 files changed, 37 insertions(+), 32 deletions(-) diff --git a/pkg/platform/provider/cluster/cluster.go b/pkg/platform/provider/cluster/cluster.go index 260ed3042..a1af90a00 100644 --- a/pkg/platform/provider/cluster/cluster.go +++ b/pkg/platform/provider/cluster/cluster.go @@ -123,7 +123,12 @@ func GetCluster(ctx context.Context, platformClient internalversion.PlatformInte if err != nil && !apierrors.IsNotFound(err) { return result, err } - restConfig, err := provider.GetRestConfig(ctx, cluster, username) + clusterv1 := &platformv1.Cluster{} + err = platformv1.Convert_platform_Cluster_To_v1_Cluster(cluster, clusterv1, nil) + if err != nil { + return nil, err + } + restConfig, err := provider.GetRestConfig(ctx, clusterv1, username) if err != nil && !apierrors.IsNotFound(err) { return result, err } @@ -153,7 +158,7 @@ func GetV1Cluster(ctx context.Context, platformClient platformversionedclient.Pl if err != nil && !apierrors.IsNotFound(err) { return result, err } - restConfig, err := provider.GetRestConfigV1(ctx, cluster, username) + restConfig, err := provider.GetRestConfig(ctx, cluster, username) if err != nil && !apierrors.IsNotFound(err) { return result, err } diff --git a/pkg/platform/provider/cluster/interface.go b/pkg/platform/provider/cluster/interface.go index b666eae2e..ea55e5343 100644 --- a/pkg/platform/provider/cluster/interface.go +++ b/pkg/platform/provider/cluster/interface.go @@ -34,7 +34,6 @@ import ( "k8s.io/apiserver/pkg/server/mux" "k8s.io/client-go/rest" platformv1client "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1" - "tkestack.io/tke/api/platform" platformv1 "tkestack.io/tke/api/platform/v1" "tkestack.io/tke/pkg/platform/types" v1 "tkestack.io/tke/pkg/platform/types/v1" @@ -82,9 +81,7 @@ type ControllerProvider interface { } type RestConfigProvider interface { - GetRestConfig(ctx context.Context, cluster *platform.Cluster, username string) (*rest.Config, error) - // remove this method in future - GetRestConfigV1(ctx context.Context, cluster *platformv1.Cluster, username string) (*rest.Config, error) + GetRestConfig(ctx context.Context, cluster *platformv1.Cluster, username string) (*rest.Config, error) } // Provider defines a set of response interfaces for specific cluster @@ -436,29 +433,8 @@ func (p *DelegateProvider) getCurrentCondition(c *v1.Cluster, phase platformv1.C return nil, errors.New("no condition need process") } -// GetRestConfig returns the cluster's rest config -func (p *DelegateProvider) GetRestConfig(ctx context.Context, cluster *platform.Cluster, username string) (*rest.Config, error) { - if p.PlatformClient == nil { - return nil, fmt.Errorf("provider platform client is nil") - } - clusterv1 := &platformv1.Cluster{} - err := platformv1.Convert_platform_Cluster_To_v1_Cluster(cluster, clusterv1, nil) - if err != nil { - return nil, err - } - cc, err := credential.GetClusterCredentialV1(ctx, p.PlatformClient, clusterv1, username) - if err != nil { - return nil, err - } - config := &rest.Config{} - if cc != nil { - config = cc.RESTConfig(clusterv1) - } - return config, nil -} - // GetRestConfigV1 returns the cluster's rest config -func (p *DelegateProvider) GetRestConfigV1(ctx context.Context, cluster *platformv1.Cluster, username string) (*rest.Config, error) { +func (p *DelegateProvider) GetRestConfig(ctx context.Context, cluster *platformv1.Cluster, username string) (*rest.Config, error) { if p.PlatformClient == nil { return nil, fmt.Errorf("provider platform client is nil") } diff --git a/pkg/platform/registry/cluster/storage/tappcontroller.go b/pkg/platform/registry/cluster/storage/tappcontroller.go index 3e92c4aee..b88e76337 100644 --- a/pkg/platform/registry/cluster/storage/tappcontroller.go +++ b/pkg/platform/registry/cluster/storage/tappcontroller.go @@ -103,7 +103,12 @@ func (r *TappControllerREST) Connect(ctx context.Context, clusterName string, op } username, _ := authentication.UsernameAndTenantID(ctx) - config, err := provider.GetRestConfig(ctx, cluster, username) + clusterv1 := &platformv1.Cluster{} + err = platformv1.Convert_platform_Cluster_To_v1_Cluster(cluster, clusterv1, nil) + if err != nil { + return nil, err + } + config, err := provider.GetRestConfig(ctx, clusterv1, username) if err != nil { return nil, err } diff --git a/pkg/platform/util/client.go b/pkg/platform/util/client.go index 02b8debfb..f459737ae 100644 --- a/pkg/platform/util/client.go +++ b/pkg/platform/util/client.go @@ -52,7 +52,13 @@ func DynamicClientByCluster(ctx context.Context, cluster *platform.Cluster, plat return nil, err } - restConfig, err := provider.GetRestConfig(ctx, cluster, username) + clusterv1 := &platformv1.Cluster{} + err = platformv1.Convert_platform_Cluster_To_v1_Cluster(cluster, clusterv1, nil) + if err != nil { + return nil, err + } + + restConfig, err := provider.GetRestConfig(ctx, clusterv1, username) if err != nil { return nil, err } @@ -79,7 +85,13 @@ func ClientSetByCluster(ctx context.Context, cluster *platform.Cluster, platform return nil, err } - restConfig, err := provider.GetRestConfig(ctx, cluster, username) + clusterv1 := &platformv1.Cluster{} + err = platformv1.Convert_platform_Cluster_To_v1_Cluster(cluster, clusterv1, nil) + if err != nil { + return nil, err + } + + restConfig, err := provider.GetRestConfig(ctx, clusterv1, username) if err != nil { return nil, err } diff --git a/pkg/platform/util/location.go b/pkg/platform/util/location.go index 828ab3332..5599905cf 100644 --- a/pkg/platform/util/location.go +++ b/pkg/platform/util/location.go @@ -34,6 +34,7 @@ import ( restclient "k8s.io/client-go/rest" platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion" "tkestack.io/tke/api/platform" + platformv1 "tkestack.io/tke/api/platform/v1" clusterprovider "tkestack.io/tke/pkg/platform/provider/cluster" ) @@ -57,7 +58,13 @@ func APIServerLocationByCluster(ctx context.Context, cluster *platform.Cluster, return nil, nil, "", errors.NewInternalError(err) } - restconfig, err := provider.GetRestConfig(ctx, cluster, username) + clusterv1 := &platformv1.Cluster{} + err = platformv1.Convert_platform_Cluster_To_v1_Cluster(cluster, clusterv1, nil) + if err != nil { + return nil, nil, "", errors.NewInternalError(err) + } + + restconfig, err := provider.GetRestConfig(ctx, clusterv1, username) if err != nil { return nil, nil, "", errors.NewInternalError(err) }