Skip to content

Commit

Permalink
Cluster registration clientconfig (#135)
Browse files Browse the repository at this point in the history
* use ListClustersPages for better paging expression
* changelog
* Add option for providing in-memory kubeconfigs.
* implement as oneof
* add *rest.Config as another option for master kubeconfig
* tweaks
* rework KubeCfg
* fix remote context bug
* Merge branch 'master' into cluster-registration-clientconfig
* changelog
* Use clientcmd.ClientConfig for everything
* Restore RemoteCtx because of open issue.
* gen code
* restore remote kube context in tests
* update changelog
  • Loading branch information
harveyxia authored Sep 2, 2020
1 parent 571840a commit 99c35d6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 53 deletions.
4 changes: 4 additions & 0 deletions changelog/v0.11.0/multicluster-registration-in-memory.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changelog:
- type: BREAKING_CHANGE
description: For cluster registration, represent all kubeconfigs as clientcmd.ClientConfig to allow passing in memory kubeconfigs.
issueLink: https://github.com/solo-io/skv2/issues/134
58 changes: 10 additions & 48 deletions pkg/multicluster/register/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
k8s_rbac_types "k8s.io/api/rbac/v1"
"k8s.io/client-go/rest"

clientcmdapi "k8s.io/client-go/tools/clientcmd/api"

k8s_core_v1 "github.com/solo-io/skv2/pkg/multicluster/internal/k8s/core/v1"
k8s_core_v1_providers "github.com/solo-io/skv2/pkg/multicluster/internal/k8s/core/v1/providers"
rbac_v1_providers "github.com/solo-io/skv2/pkg/multicluster/internal/k8s/rbac.authorization.k8s.io/v1/providers"
Expand All @@ -23,22 +21,15 @@ import (

// Options for registering a cluster
type RegistrationOptions struct {
// override the url of the k8s server
MasterURL string

// override the path of the local kubeconfig
KubeCfgPath string

// override the context to use from the local kubeconfig.
// if unset, use current context
KubeContext string
// Management kubeconfig
KubeCfg clientcmd.ClientConfig

// override the path of the remote kubeconfig
RemoteKubeCfgPath string
// Remote kubeconfig
RemoteKubeCfg clientcmd.ClientConfig

// override the context to use from the remote kubeconfig
// if unset, use current context
RemoteKubeContext string
// Remote context name
// We need to explicitly pass this because of this open issue: https://github.com/kubernetes/client-go/issues/735
RemoteCtx string

// localAPIServerAddress is optional. When passed in, it will overwrite the Api Server endpoint in
// the kubeconfig before it is written. This is primarily useful when running multi cluster KinD environments
Expand Down Expand Up @@ -116,31 +107,23 @@ func (opts RegistrationOptions) DeregisterCluster(

// Initialize registration dependencies
func (opts RegistrationOptions) initialize() (masterRestCfg *rest.Config, remoteCfg clientcmd.ClientConfig, rbacOpts RbacOptions, registrant ClusterRegistrant, err error) {
masterCfg, err := getClientConfigWithContext(opts.MasterURL, opts.KubeCfgPath, opts.KubeContext)
masterRestCfg, err = opts.KubeCfg.ClientConfig()
if err != nil {
return masterRestCfg, remoteCfg, rbacOpts, registrant, err
}

masterRestCfg, err = masterCfg.ClientConfig()
if err != nil {
return masterRestCfg, remoteCfg, rbacOpts, registrant, err
}
remoteCfg = opts.RemoteKubeCfg

registrant, err = defaultRegistrant(masterRestCfg, opts.APIServerAddress)
if err != nil {
return masterRestCfg, remoteCfg, rbacOpts, registrant, err
}

remoteCfg, err = getClientConfigWithContext(opts.MasterURL, opts.RemoteKubeCfgPath, opts.RemoteKubeContext)
if err != nil {
return masterRestCfg, remoteCfg, rbacOpts, registrant, err
}

rbacOpts = RbacOptions{
Options: Options{
ClusterName: opts.ClusterName,
RemoteCtx: opts.RemoteKubeContext,
Namespace: opts.Namespace,
RemoteCtx: opts.RemoteCtx,
RemoteNamespace: opts.RemoteNamespace,
ClusterDomain: opts.ClusterDomain,
},
Expand Down Expand Up @@ -248,27 +231,6 @@ func defaultRegistrant(cfg *rest.Config, apiServerAddress string) (ClusterRegist
return registrant, nil
}

// Attempts to load a Client KubeConfig from a default list of sources.
func getClientConfigWithContext(masterURL, kubeCfgPath, context string) (clientcmd.ClientConfig, error) {
verifiedKubeConfigPath := clientcmd.RecommendedHomeFile
if kubeCfgPath != "" {
verifiedKubeConfigPath = kubeCfgPath
}

if err := assertKubeConfigExists(verifiedKubeConfigPath); err != nil {
return nil, err
}

loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
loadingRules.ExplicitPath = verifiedKubeConfigPath
configOverrides := &clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: masterURL}}

if context != "" {
configOverrides.CurrentContext = context
}
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides), nil
}

// expects `path` to be nonempty
func assertKubeConfigExists(path string) error {
if _, err := os.Stat(path); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions pkg/multicluster/register/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ type Options struct {
// If left empty will return error
ClusterName string

// Name of the remote cluster Kubeconfig Context
RemoteCtx string

// Namespace to write namespaced resources to in the "master" and "remote" clusters
// If left empty will return error
Namespace string

// Name of the remote cluster Kubeconfig Context.
// We need to explicitly pass this because of this open issue: https://github.com/kubernetes/client-go/issues/735
RemoteCtx string

// Namespace to write namespaced resources to in the "master" and "remote" clusters
// If left empty will return error
RemoteNamespace string
Expand Down
4 changes: 2 additions & 2 deletions pkg/multicluster/register/registrant.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@ func (c *clusterRegistrant) RegisterClusterWithToken(
}

remoteContextName := opts.RemoteCtx
if opts.RemoteCtx == "" {
if remoteContextName == "" {
remoteContextName = rawRemoteCfg.CurrentContext
}
remoteContext := rawRemoteCfg.Contexts[remoteContextName]
remoteCluster := rawRemoteCfg.Clusters[remoteContext.Cluster]

// hacky step for running locally in KIND
if err = c.hackClusterConfigForLocalTestingInKIND(remoteCluster, opts.RemoteCtx); err != nil {
if err = c.hackClusterConfigForLocalTestingInKIND(remoteCluster, remoteContextName); err != nil {
return err
}

Expand Down

0 comments on commit 99c35d6

Please sign in to comment.