Skip to content

Commit

Permalink
consolidate registration opts into Options struct
Browse files Browse the repository at this point in the history
  • Loading branch information
harveyxia committed Sep 16, 2020
1 parent f32afe6 commit 3f033a2
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 249 deletions.
26 changes: 13 additions & 13 deletions codegen/render/kube_multicluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,24 @@ var _ = WithRemoteClusterContextDescribe("Multicluster", func() {
remoteCfg := test.ClientConfigWithContext(remoteContext)
registrant, err := register.DefaultRegistrant("", "")
Expect(err).NotTo(HaveOccurred())
err = register.RegisterClusterFromConfig(ctx, masterConfig, remoteCfg, register.RbacOptions{
Options: register.Options{
ClusterName: cluster2,
Namespace: ns,
RemoteNamespace: ns,
RemoteCtx: remoteContext,
err = register.RegisterClusterFromConfig(ctx, masterConfig, remoteCfg, register.Options{
ClusterName: cluster2,
Namespace: ns,
RemoteNamespace: ns,
RemoteCtx: remoteContext,
RbacOptions: register.RbacOptions{
ClusterRoleBindings: test.ServiceAccountClusterAdminRoles,
},
ClusterRoleBindings: test.ServiceAccountClusterAdminRoles,
}, registrant)
Expect(err).NotTo(HaveOccurred())
cfg := test.ClientConfigWithContext("")
err = register.RegisterClusterFromConfig(ctx, masterConfig, cfg, register.RbacOptions{
Options: register.Options{
ClusterName: cluster1,
Namespace: ns,
RemoteNamespace: ns,
err = register.RegisterClusterFromConfig(ctx, masterConfig, cfg, register.Options{
ClusterName: cluster1,
Namespace: ns,
RemoteNamespace: ns,
RbacOptions: register.RbacOptions{
ClusterRoleBindings: test.ServiceAccountClusterAdminRoles,
},
ClusterRoleBindings: test.ServiceAccountClusterAdminRoles,
}, registrant)
Expect(err).NotTo(HaveOccurred())
})
Expand Down
119 changes: 57 additions & 62 deletions pkg/multicluster/register/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,35 +97,17 @@ func (opts RegistrationOptions) RegisterProviderCluster(
ctx context.Context,
providerInfo *v1alpha1.KubernetesClusterSpec_ProviderInfo,
) error {
masterRestCfg, remoteCfg, rbacOpts, registrant, err := opts.initialize()
masterRestCfg, remoteCfg, registrationOpts, registrant, err := opts.initialize(providerInfo)
if err != nil {
return err
}
// Parse ClusterRole policy rules and pass into RegisterProviderClusterFromConfig in order to persist them to the resulting
// KubernetesCluster status.
var clusterRolePolicyRules []*v1alpha1.PolicyRule
for _, clusterRole := range opts.ClusterRoles {
for _, policyRules := range clusterRole.Rules {
clusterRolePolicyRules = append(clusterRolePolicyRules, &v1alpha1.PolicyRule{
Verbs: policyRules.Verbs,
ApiGroups: policyRules.APIGroups,
Resources: policyRules.Resources,
ResourceNames: policyRules.ResourceNames,
NonResourceUrls: policyRules.NonResourceURLs,
})
}
}
return RegisterProviderClusterFromConfig(

return RegisterClusterFromConfig(
ctx,
masterRestCfg,
remoteCfg,
rbacOpts,
registrationOpts,
registrant,
RegistrationMetadata{
ProviderInfo: providerInfo,
ResourceLabels: opts.ResourceLabels,
ClusterRolePolicyRules: clusterRolePolicyRules,
},
)
}

Expand All @@ -139,104 +121,117 @@ func (opts RegistrationOptions) RegisterProviderCluster(
func (opts RegistrationOptions) DeregisterCluster(
ctx context.Context,
) error {
masterRestCfg, remoteCfg, rbacOpts, registrant, err := opts.initialize()
masterRestCfg, remoteCfg, registrationOpts, registrant, err := opts.initialize(nil)
if err != nil {
return err
}
return DeregisterClusterFromConfig(ctx, masterRestCfg, remoteCfg, rbacOpts, registrant)
return DeregisterClusterFromConfig(ctx, masterRestCfg, remoteCfg, registrationOpts, registrant)
}

// Initialize registration dependencies
func (opts RegistrationOptions) initialize() (masterRestCfg *rest.Config, remoteCfg clientcmd.ClientConfig, rbacOpts RbacOptions, registrant ClusterRegistrant, err error) {
func (opts RegistrationOptions) initialize(
providerInfo *v1alpha1.KubernetesClusterSpec_ProviderInfo,
) (masterRestCfg *rest.Config, remoteCfg clientcmd.ClientConfig, registrationOpts Options, registrant ClusterRegistrant, err error) {
masterRestCfg, err = opts.KubeCfg.ClientConfig()
if err != nil {
return masterRestCfg, remoteCfg, rbacOpts, registrant, err
return masterRestCfg, remoteCfg, registrationOpts, registrant, err
}

remoteCfg = opts.RemoteKubeCfg

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

rbacOpts = RbacOptions{
Options: Options{
ClusterName: opts.ClusterName,
Namespace: opts.Namespace,
RemoteCtx: opts.RemoteCtx,
RemoteNamespace: opts.RemoteNamespace,
ClusterDomain: opts.ClusterDomain,
// Parse ClusterRole policy rules by iterating all cluster roles
var clusterRolePolicyRules []*v1alpha1.PolicyRule
for _, clusterRole := range opts.ClusterRoles {
for _, policyRules := range clusterRole.Rules {
clusterRolePolicyRules = append(clusterRolePolicyRules, &v1alpha1.PolicyRule{
Verbs: policyRules.Verbs,
ApiGroups: policyRules.APIGroups,
Resources: policyRules.Resources,
ResourceNames: policyRules.ResourceNames,
NonResourceUrls: policyRules.NonResourceURLs,
})
}
}

registrationOpts = Options{
ClusterName: opts.ClusterName,
Namespace: opts.Namespace,
RemoteCtx: opts.RemoteCtx,
RemoteNamespace: opts.RemoteNamespace,
ClusterDomain: opts.ClusterDomain,
RbacOptions: RbacOptions{
Roles: opts.Roles,
ClusterRoles: opts.ClusterRoles,
RoleBindings: opts.RoleBindings,
ClusterRoleBindings: opts.ClusterRoleBindings,
},
RegistrationMetadata: RegistrationMetadata{
ProviderInfo: providerInfo,
ResourceLabels: opts.ResourceLabels,
ClusterRolePolicyRules: clusterRolePolicyRules,
},
Roles: opts.Roles,
ClusterRoles: opts.ClusterRoles,
RoleBindings: opts.RoleBindings,
ClusterRoleBindings: opts.ClusterRoleBindings,
}

return masterRestCfg, remoteCfg, rbacOpts, registrant, nil
return masterRestCfg, remoteCfg, registrationOpts, registrant, nil
}

func RegisterClusterFromConfig(
ctx context.Context,
masterClusterCfg *rest.Config,
remoteCfg clientcmd.ClientConfig,
opts RbacOptions,
opts Options,
registrant ClusterRegistrant,
) error {
return RegisterProviderClusterFromConfig(ctx, masterClusterCfg, remoteCfg, opts, registrant, RegistrationMetadata{})
}

func RegisterProviderClusterFromConfig(
ctx context.Context,
masterClusterCfg *rest.Config,
remoteCfg clientcmd.ClientConfig,
opts RbacOptions,
registrant ClusterRegistrant,
metadata RegistrationMetadata,
) error {
err := registrant.EnsureRemoteNamespace(ctx, remoteCfg, opts.RemoteNamespace)
if err != nil {
return err
}

sa, err := registrant.EnsureRemoteServiceAccount(ctx, remoteCfg, opts.Options)
sa, err := registrant.EnsureRemoteServiceAccount(ctx, remoteCfg, opts)
if err != nil {
return err
}

token, err := registrant.CreateRemoteAccessToken(ctx, remoteCfg, client.ObjectKey{
Namespace: sa.GetNamespace(),
Name: sa.GetName(),
}, opts)
token, err := registrant.CreateRemoteAccessToken(
ctx,
remoteCfg,
client.ObjectKey{
Namespace: sa.GetNamespace(),
Name: sa.GetName(),
},
opts)
if err != nil {
return err
}

return registrant.RegisterProviderClusterWithToken(
return registrant.RegisterClusterWithToken(
ctx,
masterClusterCfg,
remoteCfg,
token,
opts.Options,
metadata,
opts,
)
}

func DeregisterClusterFromConfig(
ctx context.Context,
masterClusterCfg *rest.Config,
remoteCfg clientcmd.ClientConfig,
opts RbacOptions,
opts Options,
registrant ClusterRegistrant,
) error {
var multierr *multierror.Error

if err := registrant.DeregisterCluster(ctx, masterClusterCfg, opts.Options); err != nil {
if err := registrant.DeregisterCluster(ctx, masterClusterCfg, opts); err != nil {
multierr = multierror.Append(multierr, err)
}

if err := registrant.DeleteRemoteServiceAccount(ctx, remoteCfg, opts.Options); err != nil {
if err := registrant.DeleteRemoteServiceAccount(ctx, remoteCfg, opts); err != nil {
multierr = multierror.Append(multierr, err)
}

Expand Down
35 changes: 19 additions & 16 deletions pkg/multicluster/register/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package register
import (
"context"

"github.com/solo-io/skv2/pkg/api/multicluster.solo.io/v1alpha1"
"k8s.io/client-go/rest"

"github.com/rotisserie/eris"
Expand Down Expand Up @@ -45,11 +46,25 @@ type Options struct {
// Defaults to 'cluster.local'
// Read more: https://kubernetes.io/docs/tasks/administer-cluster/dns-custom-nameservers/
ClusterDomain string

RegistrationMetadata RegistrationMetadata

RbacOptions RbacOptions
}

type RbacOptions struct {
Options
// Optional additional metadata to persist to registration output resources.
type RegistrationMetadata struct {
// Metadata about the provider for cloud hosted k8s clusters.
ProviderInfo *v1alpha1.KubernetesClusterSpec_ProviderInfo

// Labels to add to registration output resources (KubernetesCluster and Secret).
ResourceLabels map[string]string

// The set of PolicyRules for the cluster roles created on the remote cluster upon registration.
ClusterRolePolicyRules []*v1alpha1.PolicyRule
}

type RbacOptions struct {
// A list of roles to bind the New kubeconfig token to
// Any Roles in this list will be Upserted by the registrant, prior to binding
Roles []*k8s_rbac_types.Role
Expand Down Expand Up @@ -129,7 +144,7 @@ type ClusterRegistrant interface {
ctx context.Context,
remoteClientCfg clientcmd.ClientConfig,
sa client.ObjectKey,
opts RbacOptions,
opts Options,
) (token string, err error)

/*
Expand All @@ -139,7 +154,7 @@ type ClusterRegistrant interface {
DeleteRemoteAccessResources(
ctx context.Context,
remoteClientCfg clientcmd.ClientConfig,
opts RbacOptions,
opts Options,
) error

/*
Expand All @@ -154,18 +169,6 @@ type ClusterRegistrant interface {
opts Options,
) error

/*
Same functionality as RegisterClusterWithToken but supply extra ProviderInfo metadata and registration settings (namespace and policyRules).
*/
RegisterProviderClusterWithToken(
ctx context.Context,
masterClusterCfg *rest.Config,
remoteClientCfg clientcmd.ClientConfig,
token string,
opts Options,
metadata RegistrationMetadata,
) error

/*
DeregisterClusterWithToken deletes all resources created by RegisterClusterWithToken.
*/
Expand Down
18 changes: 2 additions & 16 deletions pkg/multicluster/register/mocks/mock_interfaces.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3f033a2

Please sign in to comment.