Skip to content

Commit

Permalink
simplify remote namespace passing. ensure remote namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
harveyxia committed Sep 15, 2020
1 parent 05a6e64 commit c21898d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
18 changes: 10 additions & 8 deletions pkg/multicluster/register/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type RegistrationOptions struct {
ClusterRoleBindings []client.ObjectKey

// Set of labels to include on the KubernetesCluster resource.
Labels map[string]string
KubernetesClusterLabels map[string]string
}

/*
Expand Down Expand Up @@ -122,8 +122,7 @@ func (opts RegistrationOptions) RegisterProviderCluster(
rbacOpts,
registrant,
providerInfo,
opts.Labels,
opts.RemoteNamespace,
opts.KubernetesClusterLabels,
clusterRolePolicyRules,
)
}
Expand Down Expand Up @@ -183,7 +182,7 @@ func RegisterClusterFromConfig(
opts RbacOptions,
registrant ClusterRegistrant,
) error {
return RegisterProviderClusterFromConfig(ctx, masterClusterCfg, remoteCfg, opts, registrant, nil, nil, "", nil)
return RegisterProviderClusterFromConfig(ctx, masterClusterCfg, remoteCfg, opts, registrant, nil, nil, nil)
}

func RegisterProviderClusterFromConfig(
Expand All @@ -193,10 +192,14 @@ func RegisterProviderClusterFromConfig(
opts RbacOptions,
registrant ClusterRegistrant,
providerInfo *v1alpha1.KubernetesClusterSpec_ProviderInfo,
labels map[string]string,
namespace string,
kubeClusterLabels map[string]string,
policyRules []*v1alpha1.PolicyRule,
) error {
err := registrant.EnsureRemoteNamespace(ctx, remoteCfg, opts.RemoteNamespace)
if err != nil {
return err
}

sa, err := registrant.EnsureRemoteServiceAccount(ctx, remoteCfg, opts.Options)
if err != nil {
return err
Expand All @@ -217,8 +220,7 @@ func RegisterProviderClusterFromConfig(
token,
opts.Options,
providerInfo,
labels,
namespace,
kubeClusterLabels,
policyRules,
)
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/multicluster/register/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ func (o *Options) validate() error {
the registrant instance.
*/
type ClusterRegistrant interface {
/*
EnsureRemoteNamespace ensures that the specified remoteNamespace exists on the remote cluster being registered.
*/
EnsureRemoteNamespace(
ctx context.Context,
remoteClientCfg clientcmd.ClientConfig,
remoteNamespace string,
) error

/*
EnsureRemoteServiceAccount takes an instance of a remote config, and ensure a ServiceAccount exists on the
Expand Down Expand Up @@ -158,7 +166,6 @@ type ClusterRegistrant interface {
opts Options,
providerInfo *v1alpha1.KubernetesClusterSpec_ProviderInfo,
labels map[string]string,
namespace string,
policyRules []*v1alpha1.PolicyRule,
) error

Expand Down
21 changes: 16 additions & 5 deletions pkg/multicluster/register/registrant.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ type clusterRegistrant struct {
localAPIServerAddress string
}

func (c *clusterRegistrant) EnsureRemoteNamespace(
ctx context.Context,
remoteClientCfg clientcmd.ClientConfig,
remoteNamespace string,
) error {
remoteRestCfg, err := remoteClientCfg.ClientConfig()
if err != nil {
return err
}
return c.ensureRemoteNamespace(ctx, remoteNamespace, remoteRestCfg)
}

func (c *clusterRegistrant) EnsureRemoteServiceAccount(
ctx context.Context,
remoteClientCfg clientcmd.ClientConfig,
Expand Down Expand Up @@ -289,7 +301,7 @@ func (c *clusterRegistrant) RegisterClusterWithToken(
token string,
opts Options,
) error {
return c.RegisterProviderClusterWithToken(ctx, masterClusterCfg, remoteClientCfg, token, opts, nil, nil, "", nil)
return c.RegisterProviderClusterWithToken(ctx, masterClusterCfg, remoteClientCfg, token, opts, nil, nil, nil)
}

func (c *clusterRegistrant) RegisterProviderClusterWithToken(
Expand All @@ -300,7 +312,6 @@ func (c *clusterRegistrant) RegisterProviderClusterWithToken(
opts Options,
providerInfo *v1alpha1.KubernetesClusterSpec_ProviderInfo,
labels map[string]string,
namespace string,
policyRules []*v1alpha1.PolicyRule,
) error {
if err := (&opts).validate(); err != nil {
Expand Down Expand Up @@ -346,7 +357,7 @@ func (c *clusterRegistrant) RegisterProviderClusterWithToken(
return err
}

kubeCluster := buildKubeClusterResource(kcSecret, labels, opts.ClusterDomain, providerInfo, namespace, policyRules)
kubeCluster := buildKubeClusterResource(kcSecret, labels, opts.ClusterDomain, providerInfo, opts.RemoteNamespace, policyRules)

kubeClusterClient, err := c.kubeClusterFactory(masterClusterCfg)
if err != nil {
Expand Down Expand Up @@ -392,7 +403,7 @@ func buildKubeClusterResource(
labels map[string]string,
clusterDomain string,
providerInfo *v1alpha1.KubernetesClusterSpec_ProviderInfo,
namespace string,
remoteNamespace string,
policyRules []*v1alpha1.PolicyRule,
) *v1alpha1.KubernetesCluster {
if clusterDomain == "" {
Expand All @@ -406,7 +417,7 @@ func buildKubeClusterResource(
ProviderInfo: providerInfo,
},
Status: v1alpha1.KubernetesClusterStatus{
Namespace: namespace,
Namespace: remoteNamespace,
PolicyRules: policyRules,
},
}
Expand Down

0 comments on commit c21898d

Please sign in to comment.