Skip to content

Commit

Permalink
introduce factoryexpansion interface for karmadactl
Browse files Browse the repository at this point in the history
Signed-off-by: carlory <baofa.fan@daocloud.io>
  • Loading branch information
carlory committed Aug 4, 2022
1 parent ed9b838 commit 7a8118c
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
1 change: 0 additions & 1 deletion pkg/karmadactl/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import (

const (
printColumnClusterNum = 1
proxyURL = "/apis/cluster.karmada.io/v1alpha1/clusters/%s/proxy/"
)

var (
Expand Down
4 changes: 4 additions & 0 deletions pkg/karmadactl/get_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"k8s.io/client-go/util/homedir"
)

// TODO(@carlory): remove this file in favor of util.Factory

var (
defaultKubeConfig = filepath.Join(homedir.HomeDir(), ".kube", "config")
defaultCacheDir = filepath.Join(homedir.HomeDir(), ".kube", "cache")
Expand All @@ -37,6 +39,8 @@ var (
To view or setup config directly use the 'config' command.`)
)

const proxyURL = "/apis/cluster.karmada.io/v1alpha1/clusters/%s/proxy/"

// RESTClientGetter is an interface that the ConfigFlags describe to provide an easier way to mock for commands
// and eliminate the direct coupling to a struct type. Users may wish to duplicate this type in their own packages
// as per the golang type overlapping.
Expand Down
104 changes: 104 additions & 0 deletions pkg/karmadactl/util/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package util

import (
"context"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdutil "k8s.io/kubectl/pkg/cmd/util"

karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
)

const proxyURL = "/apis/cluster.karmada.io/v1alpha1/clusters/%s/proxy/"

// The Factory interface provides 2 major features compared to the cmdutil.Factory:
// 1. provides a method to get a karmada clientset
// 2. provides a method to get a cmdutil.Factory for the member cluster
type Factory interface {
cmdutil.Factory

// KarmadaClientSet returns a karmada clientset
KarmadaClientSet() (karmadaclientset.Interface, error)

// FacotryForMemberCluster returns a cmdutil.Factory for the member cluster
FactoryForMemberCluster(clusterName string) (cmdutil.Factory, error)
}

var _ Factory = &factoryImpl{}

// factoryImpl is the implementation of Factory
type factoryImpl struct {
cmdutil.Factory

// kubeConfigFlags holds all the flags specificed by user.
// These flags will be inherited by the member cluster's client.
kubeConfigFlags *genericclioptions.ConfigFlags
}

// NewFactory returns a new factory
func NewFactory(kubeConfigFlags *genericclioptions.ConfigFlags) Factory {
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
f := &factoryImpl{
kubeConfigFlags: kubeConfigFlags,
Factory: cmdutil.NewFactory(matchVersionKubeConfigFlags),
}
return f
}

// KarmadaClientSet returns a karmada clientset
func (f *factoryImpl) KarmadaClientSet() (karmadaclientset.Interface, error) {
clientConfig, err := f.ToRESTConfig()
if err != nil {
return nil, err
}
return karmadaclientset.NewForConfig(clientConfig)
}

// FacotryForMemberCluster returns a cmdutil.Factory for the member cluster
func (f *factoryImpl) FactoryForMemberCluster(clusterName string) (cmdutil.Factory, error) {
// Get client config of the karmada, and use it to create a cmdutil.Factory for the member cluster later.
clientConfig, err := f.ToRESTConfig()
if err != nil {
return nil, err
}
karmadaAPIServer := clientConfig.Host

// Check if the given cluster is joined to karmada
client, err := karmadaclientset.NewForConfig(clientConfig)
if err != nil {
return nil, err
}
_, err = client.ClusterV1alpha1().Clusters().Get(context.TODO(), clusterName, metav1.GetOptions{})
if err != nil {
return nil, err
}

// Inherit all properties from the original flags specified by user except for the kube-apiserver address.
kubeConfigFlags := &genericclioptions.ConfigFlags{
CacheDir: f.kubeConfigFlags.CacheDir,
KubeConfig: f.kubeConfigFlags.KubeConfig,
ClusterName: f.kubeConfigFlags.ClusterName,
AuthInfoName: f.kubeConfigFlags.AuthInfoName,
Context: f.kubeConfigFlags.Context,
Namespace: f.kubeConfigFlags.Namespace,
Insecure: f.kubeConfigFlags.Insecure,
CertFile: f.kubeConfigFlags.CertFile,
KeyFile: f.kubeConfigFlags.KeyFile,
CAFile: f.kubeConfigFlags.CAFile,
BearerToken: f.kubeConfigFlags.BearerToken,
Impersonate: f.kubeConfigFlags.Impersonate,
ImpersonateUID: f.kubeConfigFlags.ImpersonateUID,
ImpersonateGroup: f.kubeConfigFlags.ImpersonateGroup,
Username: f.kubeConfigFlags.Username,
Password: f.kubeConfigFlags.Password,
Timeout: f.kubeConfigFlags.Timeout,
WrapConfigFn: f.kubeConfigFlags.WrapConfigFn,
}
// Override the kube-apiserver address.
memberAPIserver := karmadaAPIServer + fmt.Sprintf(proxyURL, clusterName)
kubeConfigFlags.APIServer = &memberAPIserver
matchVersionKubeConfigFlags := cmdutil.NewMatchVersionFlags(kubeConfigFlags)
return cmdutil.NewFactory(matchVersionKubeConfigFlags), nil
}

0 comments on commit 7a8118c

Please sign in to comment.