Skip to content

Commit

Permalink
refactor(platform): remove provider client input (tkestack#1683)
Browse files Browse the repository at this point in the history
  • Loading branch information
leoryu committed Nov 30, 2021
1 parent 76efe48 commit 3d1cbba
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 28 deletions.
6 changes: 3 additions & 3 deletions pkg/platform/provider/baremetal/cluster/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,22 @@ func (p *Provider) EnsureRemoveNode(ctx context.Context, c *v1.Cluster) error {
func (p *Provider) EnsureRemoveMachine(ctx context.Context, c *v1.Cluster) error {
log.FromContext(ctx).Info("delete machine start")
fieldSelector := fields.OneTermEqualSelector("spec.clusterName", c.Name).String()
machineList, err := p.platformClient.Machines().List(ctx, metav1.ListOptions{FieldSelector: fieldSelector})
machineList, err := p.PlatformClient.Machines().List(ctx, metav1.ListOptions{FieldSelector: fieldSelector})
if err != nil {
return err
}
if len(machineList.Items) == 0 {
return nil
}
for _, machine := range machineList.Items {
if err := p.platformClient.Machines().Delete(ctx, machine.Name, metav1.DeleteOptions{}); err != nil {
if err := p.PlatformClient.Machines().Delete(ctx, machine.Name, metav1.DeleteOptions{}); err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}

if err = wait.PollImmediate(5*time.Second, 5*time.Minute, waitForMachineDelete(ctx, p.platformClient, machine.Name)); err != nil {
if err = wait.PollImmediate(5*time.Second, 5*time.Minute, waitForMachineDelete(ctx, p.PlatformClient, machine.Name)); err != nil {
return err
}
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/platform/provider/baremetal/cluster/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func RegisterProvider() {
type Provider struct {
*clusterprovider.DelegateProvider

config *config.Config
platformClient platformv1client.PlatformV1Interface
config *config.Config
}

var _ clusterprovider.Provider = &Provider{}
Expand Down Expand Up @@ -175,7 +174,7 @@ func NewProvider() (*Provider, error) {
if err != nil {
log.Errorf("read PlatformAPIClientConfig error: %w", err)
} else {
p.platformClient, err = platformv1client.NewForConfig(restConfig)
p.PlatformClient, err = platformv1client.NewForConfig(restConfig)
if err != nil {
return nil, err
}
Expand All @@ -191,11 +190,11 @@ func (p *Provider) RegisterHandler(mux *mux.PathRecorderMux) {
}

func (p *Provider) Validate(cluster *types.Cluster) field.ErrorList {
return validation.ValidateCluster(p.platformClient, cluster)
return validation.ValidateCluster(p.PlatformClient, cluster)
}

func (p *Provider) ValidateUpdate(cluster *types.Cluster, oldCluster *types.Cluster) field.ErrorList {
return validation.ValidateClusterUpdate(p.platformClient, cluster, oldCluster)
return validation.ValidateClusterUpdate(p.PlatformClient, cluster, oldCluster)
}

func (p *Provider) PreCreate(cluster *types.Cluster) error {
Expand Down
8 changes: 4 additions & 4 deletions pkg/platform/provider/baremetal/cluster/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (p *Provider) EnsureUpgradeControlPlaneNode(ctx context.Context, c *v1.Clus
if err != nil {
return err
}
machines, err := p.platformClient.Machines().List(context.TODO(), metav1.ListOptions{
machines, err := p.PlatformClient.Machines().List(context.TODO(), metav1.ListOptions{
LabelSelector: requirement.String(),
FieldSelector: fields.OneTermEqualSelector(platformv1.MachineClusterField, c.Name).String(),
})
Expand Down Expand Up @@ -228,7 +228,7 @@ func (p *Provider) EnsureUpgradeControlPlaneNode(ctx context.Context, c *v1.Clus
if err != nil {
return err
}
upgraded, err := kubeadm.UpgradeNode(s, client, p.platformClient, logger, c, option)
upgraded, err := kubeadm.UpgradeNode(s, client, p.PlatformClient, logger, c, option)
if err != nil {
return err
}
Expand All @@ -239,10 +239,10 @@ func (p *Provider) EnsureUpgradeControlPlaneNode(ctx context.Context, c *v1.Clus
// set willUpgrade value to all worker node when upgraded all master nodes and upgrade mode is auto.
labelValue = kubeadm.WillUpgrade
}
if err := kubeadm.AddNeedUpgradeLabel(p.platformClient, c.Name, labelValue); err != nil {
if err := kubeadm.AddNeedUpgradeLabel(p.PlatformClient, c.Name, labelValue); err != nil {
return err
}
err = kubeadm.MarkNextUpgradeWorkerNode(client, p.platformClient, option.Version, c.Name)
err = kubeadm.MarkNextUpgradeWorkerNode(client, p.PlatformClient, option.Version, c.Name)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/platform/provider/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func GetCluster(ctx context.Context, platformClient internalversion.PlatformInte
if err != nil && !apierrors.IsNotFound(err) {
return result, err
}
restConfig, err := provider.GetRestConfig(ctx, platformClient, cluster, username)
restConfig, err := provider.GetRestConfig(ctx, cluster, username)
if err != nil && !apierrors.IsNotFound(err) {
return result, err
}
Expand Down Expand Up @@ -153,7 +153,7 @@ func GetV1Cluster(ctx context.Context, platformClient platformversionedclient.Pl
if err != nil && !apierrors.IsNotFound(err) {
return result, err
}
restConfig, err := provider.GetRestConfigV1(ctx, platformClient, cluster, username)
restConfig, err := provider.GetRestConfigV1(ctx, cluster, username)
if err != nil && !apierrors.IsNotFound(err) {
return result, err
}
Expand Down
29 changes: 20 additions & 9 deletions pkg/platform/provider/cluster/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/server/mux"
"k8s.io/client-go/rest"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
platformversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1"
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"
Expand Down Expand Up @@ -83,9 +82,9 @@ type ControllerProvider interface {
}

type RestConfigProvider interface {
GetRestConfig(ctx context.Context, client platforminternalclient.PlatformInterface, cluster *platform.Cluster, username string) (*rest.Config, error)
GetRestConfig(ctx context.Context, cluster *platform.Cluster, username string) (*rest.Config, error)
// remove this method in future
GetRestConfigV1(ctx context.Context, client platformversionedclient.PlatformV1Interface, cluster *platformv1.Cluster, username string) (*rest.Config, error)
GetRestConfigV1(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 @@ -125,6 +124,7 @@ type DelegateProvider struct {
UpgradeHandlers []Handler
ScaleUpHandlers []Handler
ScaleDownHandlers []Handler
PlatformClient platformv1client.PlatformV1Interface
}

func (p *DelegateProvider) Name() string {
Expand Down Expand Up @@ -437,21 +437,32 @@ func (p *DelegateProvider) getCurrentCondition(c *v1.Cluster, phase platformv1.C
}

// GetRestConfig returns the cluster's rest config
func (p *DelegateProvider) GetRestConfig(ctx context.Context, client platforminternalclient.PlatformInterface, cluster *platform.Cluster, username string) (*rest.Config, error) {
cc, err := credential.GetClusterCredential(ctx, client, cluster, username)
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(cluster)
config = cc.RESTConfig(clusterv1)
}
return config, nil
}

// GetRestConfigV1 returns the cluster's rest config
func (p *DelegateProvider) GetRestConfigV1(ctx context.Context, client platformversionedclient.PlatformV1Interface, cluster *platformv1.Cluster, username string) (*rest.Config, error) {
cc, err := credential.GetClusterCredentialV1(ctx, client, cluster, username)
func (p *DelegateProvider) GetRestConfigV1(ctx context.Context, cluster *platformv1.Cluster, username string) (*rest.Config, error) {
if p.PlatformClient == nil {
return nil, fmt.Errorf("provider platform client is nil")
}
cc, err := credential.GetClusterCredentialV1(ctx, p.PlatformClient, cluster, username)
if err != nil {
return nil, err
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/platform/provider/imported/cluster/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import (
"context"

"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/client-go/tools/clientcmd"
platformv1client "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1"
"tkestack.io/tke/pkg/platform/provider/baremetal/constants"
clusterprovider "tkestack.io/tke/pkg/platform/provider/cluster"
"tkestack.io/tke/pkg/platform/provider/imported/config"
"tkestack.io/tke/pkg/platform/provider/imported/validation"
"tkestack.io/tke/pkg/platform/types"
"tkestack.io/tke/pkg/util/log"
Expand All @@ -39,6 +43,7 @@ func RegisterProvider() {

type Provider struct {
*clusterprovider.DelegateProvider
config *config.Config
}

var _ clusterprovider.Provider = &Provider{}
Expand All @@ -55,6 +60,22 @@ func NewProvider() (*Provider, error) {
p.EnsureCleanClusterMark,
},
}
cfg, err := config.New(constants.ConfigFile)
if err != nil {
return nil, err
}
p.config = cfg
if cfg.PlatformAPIClientConfig != "" {
restConfig, err := clientcmd.BuildConfigFromFlags("", cfg.PlatformAPIClientConfig)
if err != nil {
log.Errorf("read PlatformAPIClientConfig error: %w", err)
} else {
p.PlatformClient, err = platformv1client.NewForConfig(restConfig)
if err != nil {
return nil, err
}
}
}
return p, nil
}

Expand Down
51 changes: 51 additions & 0 deletions pkg/platform/provider/imported/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2019 Tencent. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://opensource.org/licenses/Apache-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package config

import (
"os"

"gopkg.in/yaml.v2"

"github.com/jinzhu/configor"
)

func New(filename string) (*Config, error) {
config := &Config{}
if err := configor.Load(config, filename); err != nil {
return nil, err
}

return config, nil
}

type Config struct {
PlatformAPIClientConfig string `yaml:"platformAPIClientConfig"`
}

func (c *Config) Save(filename string) error {
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()

y := yaml.NewEncoder(f)
return y.Encode(c)
}
2 changes: 1 addition & 1 deletion pkg/platform/registry/cluster/storage/tappcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (r *TappControllerREST) Connect(ctx context.Context, clusterName string, op
}

username, _ := authentication.UsernameAndTenantID(ctx)
config, err := provider.GetRestConfig(ctx, r.platformClient, cluster, username)
config, err := provider.GetRestConfig(ctx, cluster, username)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/platform/util/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func DynamicClientByCluster(ctx context.Context, cluster *platform.Cluster, plat
return nil, err
}

restConfig, err := provider.GetRestConfig(ctx, platformClient, cluster, username)
restConfig, err := provider.GetRestConfig(ctx, cluster, username)
if err != nil {
return nil, err
}
Expand All @@ -79,7 +79,7 @@ func ClientSetByCluster(ctx context.Context, cluster *platform.Cluster, platform
return nil, err
}

restConfig, err := provider.GetRestConfig(ctx, platformClient, cluster, username)
restConfig, err := provider.GetRestConfig(ctx, cluster, username)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/platform/util/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func APIServerLocationByCluster(ctx context.Context, cluster *platform.Cluster,
return nil, nil, "", errors.NewInternalError(err)
}

restconfig, err := provider.GetRestConfig(ctx, platformClient, cluster, username)
restconfig, err := provider.GetRestConfig(ctx, cluster, username)
if err != nil {
return nil, nil, "", errors.NewInternalError(err)
}
Expand Down
6 changes: 5 additions & 1 deletion test/e2e/platform/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"tkestack.io/tke/pkg/platform/apiserver/cluster"
baremetalcluster "tkestack.io/tke/pkg/platform/provider/baremetal/cluster"
baremetalmachine "tkestack.io/tke/pkg/platform/provider/baremetal/machine"
clusterprovider "tkestack.io/tke/pkg/platform/provider/cluster"
importedcluster "tkestack.io/tke/pkg/platform/provider/imported/cluster"
"tkestack.io/tke/test/e2e/tke"
tke2 "tkestack.io/tke/test/tke"
Expand All @@ -52,7 +53,8 @@ var (
)

var _ = BeforeSuite(func() {
baremetalcluster.RegisterProvider()
// baremetalcluster.RegisterProvider()
bp, _ := baremetalcluster.NewProvider()
baremetalmachine.RegisterProvider()
importedcluster.RegisterProvider()
t.Create()
Expand All @@ -61,6 +63,8 @@ var _ = BeforeSuite(func() {
restConf, err := t.GetKubeConfig()
Expect(err).To(BeNil())
tkeClient := tkeclientset.NewForConfigOrDie(restConf)
bp.PlatformClient = tkeClient.PlatformV1()
clusterprovider.Register(bp.Name(), bp)
testTKE = tke2.Init(tkeClient, provider)
})

Expand Down

0 comments on commit 3d1cbba

Please sign in to comment.