Skip to content

Commit

Permalink
refactor(platform): reduce provider method (#1684)
Browse files Browse the repository at this point in the history
  • Loading branch information
leoryu authored Nov 30, 2021
1 parent 3d1cbba commit 6c473aa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 32 deletions.
9 changes: 7 additions & 2 deletions pkg/platform/provider/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
28 changes: 2 additions & 26 deletions pkg/platform/provider/cluster/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/platform/registry/cluster/storage/tappcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 14 additions & 2 deletions pkg/platform/util/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/platform/util/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
Expand Down

0 comments on commit 6c473aa

Please sign in to comment.