Skip to content

Commit

Permalink
improving the readability and cosmetic changes
Browse files Browse the repository at this point in the history
Signed-off-by: Feny Mehta <[email protected]>
  • Loading branch information
fbm3307 committed Dec 20, 2024
1 parent 5163996 commit 53aaed1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
18 changes: 8 additions & 10 deletions pkg/cmd/adm/register_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func NewRegisterMemberCmd() *cobra.Command {
}

func registerMemberCluster(ctx *extendedCommandContext, args registerMemberArgs, restart restartFunc) error {
validated, err := validateArgs(ctx, args, restart)
validated, err := validateArgs(ctx, args)
if err != nil {
return err
}
Expand All @@ -115,7 +115,7 @@ func registerMemberCluster(ctx *extendedCommandContext, args registerMemberArgs,
return nil
}

return validated.perform(ctx)
return validated.perform(ctx, restart)
}

func (v *registerMemberValidated) getSourceAndTargetClusters(sourceClusterType configuration.ClusterType) (clusterData, clusterData) {
Expand Down Expand Up @@ -352,7 +352,6 @@ type registerMemberValidated struct {
memberClusterData clusterData
warnings []string
errors []string
restartFunc func(ctx *clicontext.CommandContext, clusterName string, cfcGetter ConfigFlagsAndClientGetterFunc) error
}

func getApiEndpointAndClient(ctx *extendedCommandContext, kubeConfigPath string) (apiEndpoint string, cl runtimeclient.Client, err error) {
Expand All @@ -376,7 +375,7 @@ func getApiEndpointAndClient(ctx *extendedCommandContext, kubeConfigPath string)
return
}

func validateArgs(ctx *extendedCommandContext, args registerMemberArgs, restart restartFunc) (*registerMemberValidated, error) {
func validateArgs(ctx *extendedCommandContext, args registerMemberArgs) (*registerMemberValidated, error) {
hostApiEndpoint, hostClusterClient, err := getApiEndpointAndClient(ctx, args.hostKubeConfig)
if err != nil {
return nil, err
Expand Down Expand Up @@ -444,9 +443,8 @@ func validateArgs(ctx *extendedCommandContext, args registerMemberArgs, restart
toolchainClusterName: memberToolchainClusterName,
kubeConfig: args.memberKubeConfig,
},
warnings: warnings,
errors: errors,
restartFunc: restart,
warnings: warnings,
errors: errors,
}, nil
}

Expand All @@ -473,7 +471,7 @@ func (v *registerMemberValidated) confirmationPrompt() ioutils.ConfirmationMessa
return ioutils.WithMessagef(sb.String(), args...)
}

func (v *registerMemberValidated) perform(ctx *extendedCommandContext) error {
func (v *registerMemberValidated) perform(ctx *extendedCommandContext, restart restartFunc) error {
// add the host entry to the member cluster first. We assume that there is just 1 toolchain cluster entry in the member
// cluster (i.e. it just points back to the host), so there's no need to determine the number of entries with the same
// API endpoint.
Expand All @@ -487,7 +485,7 @@ func (v *registerMemberValidated) perform(ctx *extendedCommandContext) error {
}

// restart Host Operator using the adm-restart command
if err := v.restartFunc(ctx.CommandContext, configuration.HostName, v.getRegMemConfigFlagsAndClient); err != nil {
if err := restart(ctx.CommandContext, configuration.HostName, v.getRegMemConfigFlagsAndClient); err != nil {
return err
}

Expand Down Expand Up @@ -517,7 +515,7 @@ until the SpaceProvisionerConfig.spec.enabled is set to true.
`, v.hostClusterData.apiEndpoint))
}

func (v *registerMemberValidated) getRegMemConfigFlagsAndClient(ctx *clicontext.CommandContext, clusterName string) (kubeConfigFlag *genericclioptions.ConfigFlags, rccl runtimeclient.Client, err error) {
func (v *registerMemberValidated) getRegMemConfigFlagsAndClient(_ *clicontext.CommandContext, _ string) (kubeConfigFlag *genericclioptions.ConfigFlags, rccl runtimeclient.Client, err error) {
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()

kubeConfigFlags.ClusterName = nil // `cluster` flag is redefined for our own purpose
Expand Down
25 changes: 25 additions & 0 deletions pkg/cmd/adm/register_member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,24 @@ func TestRegisterMember(t *testing.T) {
require.EqualError(t, err, "restart did not happen")
})

t.Run("Register-member calls restart ", func(t *testing.T) {
// given
term := NewFakeTerminalWithResponse("Y")
newClient, fakeClient := newFakeClientsFromRestConfig(t, &toolchainClusterMemberSa, &toolchainClusterHostSa)
mockCreateToolchainClusterWithReadyCondition(t, fakeClient)
ctx := newExtendedCommandContext(term, newClient)
called := 0
// when
err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error {
called++
return mockRestartReg(ctx.CommandContext, configuration.HostName, nil)
})

// then
require.NoError(t, err)
assert.Equal(t, 1, called)
})

}

func mockCreateToolchainClusterInNamespaceWithReadyCondition(t *testing.T, fakeClient *test.FakeClient, namespace string) {
Expand Down Expand Up @@ -635,3 +653,10 @@ func defaultRegisterMemberArgs() registerMemberArgs {

return args
}

func mockRestartReg(ctx *clicontext.CommandContext, clusterName string, _ ConfigFlagsAndClientGetterFunc) error {
if clusterName == "host" && ctx != nil {
return nil
}
return fmt.Errorf("cluster name is wrong")
}

0 comments on commit 53aaed1

Please sign in to comment.