Skip to content

Commit

Permalink
KUBESAW-172: Restart host-operator at the end of the register-member …
Browse files Browse the repository at this point in the history
…command

Signed-off-by: Feny Mehta <[email protected]>
  • Loading branch information
fbm3307 committed Dec 9, 2024
1 parent a89cb52 commit 85eae83
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 32 deletions.
34 changes: 34 additions & 0 deletions pkg/cmd/adm/register_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
Expand Down Expand Up @@ -351,6 +352,7 @@ type registerMemberValidated struct {
memberClusterData clusterData
warnings []string
errors []string
//restart func(ctx *clicontext.CommandContext, clusterName string, cfcGetter ConfigFlagsAndClientGetterFunc) error
}

func getApiEndpointAndClient(ctx *extendedCommandContext, kubeConfigPath string) (apiEndpoint string, cl runtimeclient.Client, err error) {
Expand Down Expand Up @@ -483,6 +485,11 @@ func (v *registerMemberValidated) perform(ctx *extendedCommandContext) error {
return err
}

// // restart Host Operator using the restart command
// if err := v.restart(ctx.CommandContext, "host", getRegMemConfigFlagsAndClient); err != nil {
// return err
// }

exampleSPC := &toolchainv1alpha1.SpaceProvisionerConfig{
TypeMeta: metav1.TypeMeta{
Kind: "SpaceProvisionerConfig",
Expand All @@ -509,6 +516,33 @@ until the SpaceProvisionerConfig.spec.enabled is set to true.
`, v.hostClusterData.apiEndpoint))
}

func getRegMemConfigFlagsAndClient(ctx *clicontext.CommandContext, clusterName string) (confg configuration.ClusterConfig, kubeConfigFlag *genericclioptions.ConfigFlags, rccl runtimeclient.Client, err error) {

Check failure on line 519 in pkg/cmd/adm/register_member.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

func `getRegMemConfigFlagsAndClient` is unused (unused)
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()

kubeConfigFlags.ClusterName = nil // `cluster` flag is redefined for our own purpose
kubeConfigFlags.AuthInfoName = nil // unused here, so we can hide it
kubeConfigFlags.Context = nil // unused here, so we can hide it

cfg, err := configuration.LoadClusterConfig(ctx, clusterName)
if err != nil {
return cfg, nil, nil, err
}
kubeConfigFlags.Namespace = &cfg.OperatorNamespace
kubeConfigFlags.APIServer = &cfg.ServerAPI
kubeConfigFlags.BearerToken = &cfg.Token
kubeconfig, err := client.EnsureKsctlConfigFile()
if err != nil {
return cfg, nil, nil, err
}
kubeConfigFlags.KubeConfig = &kubeconfig

cl, err := ctx.NewClient(cfg.Token, cfg.ServerAPI)
if err != nil {
return cfg, nil, nil, err
}
return cfg, kubeConfigFlags, cl, nil

Check warning on line 543 in pkg/cmd/adm/register_member.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/adm/register_member.go#L519-L543

Added lines #L519 - L543 were not covered by tests
}

func findToolchainClusterForMember(allToolchainClusters []toolchainv1alpha1.ToolchainCluster, memberAPIEndpoint, memberOperatorNamespace string) *toolchainv1alpha1.ToolchainCluster {
for _, tc := range allToolchainClusters {
if tc.Status.APIEndpoint == memberAPIEndpoint && tc.Status.OperatorNamespace == memberOperatorNamespace {
Expand Down
51 changes: 31 additions & 20 deletions pkg/cmd/adm/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (
)

type (
RolloutRestartFunc func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error
RolloutStatusCheckerFunc func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error
RolloutRestartFunc func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error
RolloutStatusCheckerFunc func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error
ConfigFlagsAndClientGetterFunc func(ctx *clicontext.CommandContext, clusterName string) (cfg configuration.ClusterConfig, kubeConfigFlag *genericclioptions.ConfigFlags, rccl runtimeclient.Client, err error)
)

// NewRestartCmd() is a function to restart the whole operator, it relies on the target cluster and fetches the cluster config
Expand All @@ -45,52 +46,62 @@ func NewRestartCmd() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout)
ctx := clicontext.NewCommandContext(term, client.DefaultNewClient)
return restart(ctx, args[0])
return restart(ctx, args[0], getConfigFlagsAndClient)

Check warning on line 49 in pkg/cmd/adm/restart.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/adm/restart.go#L49

Added line #L49 was not covered by tests
},
}
return command
}

func restart(ctx *clicontext.CommandContext, clusterName string) error {
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()
func restart(ctx *clicontext.CommandContext, clusterName string, configFlagsClientGetter ConfigFlagsAndClientGetterFunc) error {
ioStreams := genericiooptions.IOStreams{
In: os.Stdin,
Out: os.Stdout,
ErrOut: os.Stderr,
}

cfg, kubeConfigFlags, cl, err := configFlagsClientGetter(ctx, clusterName)
if err != nil {
return err
}

Check warning on line 65 in pkg/cmd/adm/restart.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/adm/restart.go#L64-L65

Added lines #L64 - L65 were not covered by tests
factory := cmdutil.NewFactory(cmdutil.NewMatchVersionFlags(kubeConfigFlags))

if !ctx.AskForConfirmation(
ioutils.WithMessagef("restart all the deployments in the cluster '%s' and namespace '%s' \n", clusterName, cfg.OperatorNamespace)) {
return nil
}

return restartDeployments(ctx, cl, cfg.OperatorNamespace, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error {
return checkRolloutStatus(ctx, factory, ioStreams, deployment)
}, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error {
return restartNonOlmDeployments(ctx, deployment, factory, ioStreams)
})

Check warning on line 77 in pkg/cmd/adm/restart.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/adm/restart.go#L73-L77

Added lines #L73 - L77 were not covered by tests
}

func getConfigFlagsAndClient(ctx *clicontext.CommandContext, clusterName string) (confg configuration.ClusterConfig, kubeConfigFlag *genericclioptions.ConfigFlags, rccl runtimeclient.Client, err error) {
kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag()

kubeConfigFlags.ClusterName = nil // `cluster` flag is redefined for our own purpose
kubeConfigFlags.AuthInfoName = nil // unused here, so we can hide it
kubeConfigFlags.Context = nil // unused here, so we can hide it

cfg, err := configuration.LoadClusterConfig(ctx, clusterName)
if err != nil {
return err
return cfg, nil, nil, err

Check warning on line 89 in pkg/cmd/adm/restart.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/adm/restart.go#L89

Added line #L89 was not covered by tests
}
kubeConfigFlags.Namespace = &cfg.OperatorNamespace
kubeConfigFlags.APIServer = &cfg.ServerAPI
kubeConfigFlags.BearerToken = &cfg.Token
kubeconfig, err := client.EnsureKsctlConfigFile()
if err != nil {
return err
return cfg, nil, nil, err

Check warning on line 96 in pkg/cmd/adm/restart.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/adm/restart.go#L96

Added line #L96 was not covered by tests
}
kubeConfigFlags.KubeConfig = &kubeconfig
factory := cmdutil.NewFactory(cmdutil.NewMatchVersionFlags(kubeConfigFlags))

if !ctx.AskForConfirmation(
ioutils.WithMessagef("restart all the deployments in the cluster '%s' and namespace '%s' \n", clusterName, cfg.OperatorNamespace)) {
return nil
}

cl, err := ctx.NewClient(cfg.Token, cfg.ServerAPI)
if err != nil {
return err
return cfg, nil, nil, err

Check warning on line 102 in pkg/cmd/adm/restart.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/adm/restart.go#L102

Added line #L102 was not covered by tests
}

return restartDeployments(ctx, cl, cfg.OperatorNamespace, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error {
return checkRolloutStatus(ctx, factory, ioStreams, deployment)
}, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error {
return restartNonOlmDeployments(ctx, deployment, factory, ioStreams)
})
return cfg, kubeConfigFlags, cl, nil
}

// This function has the whole logic of getting the list of olm and non-olm based deployment, then proceed on restarting/deleting accordingly
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/adm/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func TestRestart(t *testing.T) {
newClient, _ := NewFakeClients(t)
ctx := clicontext.NewCommandContext(term, newClient)
//when
err := restart(ctx, "host")
err := restart(ctx, "host", getConfigFlagsAndClient)

//then
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/adm/unregister_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"k8s.io/apimachinery/pkg/types"
)

type restartFunc func(ctx *clicontext.CommandContext, clusterName string) error
type restartFunc func(ctx *clicontext.CommandContext, clusterName string, cfcGetter ConfigFlagsAndClientGetterFunc) error

func NewUnregisterMemberCmd() *cobra.Command {
return &cobra.Command{
Expand Down Expand Up @@ -64,5 +64,5 @@ func UnregisterMemberCluster(ctx *clicontext.CommandContext, clusterName string,
}
ctx.Printlnf("\nThe deletion of the Toolchain member cluster from the Host cluster has been triggered")

return restart(ctx, "host")
return restart(ctx, "host", getConfigFlagsAndClient)
}
18 changes: 9 additions & 9 deletions pkg/cmd/adm/unregister_member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestUnregisterMemberWhenAnswerIsY(t *testing.T) {
ctx := clicontext.NewCommandContext(term, newClient)

// when
err := UnregisterMemberCluster(ctx, "member1", func(ctx *clicontext.CommandContext, clusterName string) error {
err := UnregisterMemberCluster(ctx, "member1", func(ctx *clicontext.CommandContext, clusterName string, cfcGetter ConfigFlagsAndClientGetterFunc) error {
return nil
})

Expand All @@ -46,7 +46,7 @@ func TestUnregisterMemberWhenRestartError(t *testing.T) {
ctx := clicontext.NewCommandContext(term, newClient)

// when
err := UnregisterMemberCluster(ctx, "member1", func(ctx *clicontext.CommandContext, clusterName string) error {
err := UnregisterMemberCluster(ctx, "member1", func(ctx *clicontext.CommandContext, clusterName string, cfcGetter ConfigFlagsAndClientGetterFunc) error {
return fmt.Errorf("restart did not happen")
})

Expand All @@ -65,9 +65,9 @@ func TestUnregisterMemberCallsRestart(t *testing.T) {
ctxAct := clicontext.NewCommandContext(term, newClient)
called := 0
// when
err := UnregisterMemberCluster(ctxAct, "member1", func(ctx *clicontext.CommandContext, restartClusterName string) error {
err := UnregisterMemberCluster(ctxAct, "member1", func(ctx *clicontext.CommandContext, restartClusterName string, cfcGetter ConfigFlagsAndClientGetterFunc) error {
called++
return mockRestart(ctx, restartClusterName)
return mockRestart(ctx, restartClusterName, getConfigFlagsAndClient)
})

// then
Expand All @@ -84,7 +84,7 @@ func TestUnregisterMemberWhenAnswerIsN(t *testing.T) {
ctx := clicontext.NewCommandContext(term, newClient)

// when
err := UnregisterMemberCluster(ctx, "member1", func(ctx *clicontext.CommandContext, clusterName string) error {
err := UnregisterMemberCluster(ctx, "member1", func(ctx *clicontext.CommandContext, clusterName string, cfcGetter ConfigFlagsAndClientGetterFunc) error {
return nil
})

Expand All @@ -107,7 +107,7 @@ func TestUnregisterMemberWhenNotFound(t *testing.T) {
ctx := clicontext.NewCommandContext(term, newClient)

// when
err := UnregisterMemberCluster(ctx, "member1", func(ctx *clicontext.CommandContext, clusterName string) error {
err := UnregisterMemberCluster(ctx, "member1", func(ctx *clicontext.CommandContext, clusterName string, cfcGetter ConfigFlagsAndClientGetterFunc) error {
return nil
})

Expand All @@ -130,7 +130,7 @@ func TestUnregisterMemberWhenUnknownClusterName(t *testing.T) {
ctx := clicontext.NewCommandContext(term, newClient)

// when
err := UnregisterMemberCluster(ctx, "some", func(ctx *clicontext.CommandContext, clusterName string) error {
err := UnregisterMemberCluster(ctx, "some", func(ctx *clicontext.CommandContext, clusterName string, cfcGetter ConfigFlagsAndClientGetterFunc) error {
return nil
})

Expand All @@ -155,7 +155,7 @@ func TestUnregisterMemberLacksPermissions(t *testing.T) {
ctx := clicontext.NewCommandContext(term, newClient)

// when
err := UnregisterMemberCluster(ctx, "member1", func(ctx *clicontext.CommandContext, clusterName string) error {
err := UnregisterMemberCluster(ctx, "member1", func(ctx *clicontext.CommandContext, clusterName string, cfcGetter ConfigFlagsAndClientGetterFunc) error {
return nil
})

Expand All @@ -164,7 +164,7 @@ func TestUnregisterMemberLacksPermissions(t *testing.T) {
AssertToolchainClusterSpec(t, fakeClient, toolchainCluster)
}

func mockRestart(ctx *clicontext.CommandContext, clusterName string) error {
func mockRestart(ctx *clicontext.CommandContext, clusterName string, cfcGetter ConfigFlagsAndClientGetterFunc) error {

Check failure on line 167 in pkg/cmd/adm/unregister_member_test.go

View workflow job for this annotation

GitHub Actions / GolangCI Lint

`mockRestart` - `cfcGetter` is unused (unparam)
if clusterName == "host" && ctx != nil {
return nil
}
Expand Down

0 comments on commit 85eae83

Please sign in to comment.