From ec701fa14adfc41717845b13190b738895f4c23b Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Mon, 23 Dec 2024 17:26:32 +0530 Subject: [PATCH] KUBESAW-172: Restart host-operator at the end of the register-member command (#90) * KUBESAW-172: Restart host-operator at the end of the register-member command Signed-off-by: Feny Mehta * Register member test cases Signed-off-by: Feny Mehta * clean up Signed-off-by: Feny Mehta * e2e error fix Signed-off-by: Feny Mehta * rename to restartFunc in reg-mem Signed-off-by: Feny Mehta * some changes Signed-off-by: Feny Mehta * clean up Signed-off-by: Feny Mehta * cosmetic changes and cleanup Signed-off-by: Feny Mehta * improving the readability and cosmetic changes Signed-off-by: Feny Mehta --------- Signed-off-by: Feny Mehta Co-authored-by: Francisc Munteanu Co-authored-by: Matous Jobanek --- pkg/cmd/adm/register_member.go | 28 ++++++- pkg/cmd/adm/register_member_test.go | 107 ++++++++++++++++++++++---- pkg/cmd/adm/restart.go | 51 +++++++----- pkg/cmd/adm/restart_test.go | 30 ++++---- pkg/cmd/adm/unregister_member.go | 4 +- pkg/cmd/adm/unregister_member_test.go | 22 +++--- 6 files changed, 175 insertions(+), 67 deletions(-) diff --git a/pkg/cmd/adm/register_member.go b/pkg/cmd/adm/register_member.go index e759eb0..7bd6d5a 100644 --- a/pkg/cmd/adm/register_member.go +++ b/pkg/cmd/adm/register_member.go @@ -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" @@ -72,7 +73,7 @@ func NewRegisterMemberCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { term := ioutils.NewTerminal(cmd.InOrStdin, cmd.OutOrStdout) ctx := newExtendedCommandContext(term, client.DefaultNewClientFromRestConfig) - return registerMemberCluster(ctx, commandArgs) + return registerMemberCluster(ctx, commandArgs, restart) }, } @@ -94,7 +95,7 @@ func NewRegisterMemberCmd() *cobra.Command { return cmd } -func registerMemberCluster(ctx *extendedCommandContext, args registerMemberArgs) error { +func registerMemberCluster(ctx *extendedCommandContext, args registerMemberArgs, restart restartFunc) error { validated, err := validateArgs(ctx, args) if err != nil { return err @@ -114,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) { @@ -470,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. @@ -483,6 +484,11 @@ func (v *registerMemberValidated) perform(ctx *extendedCommandContext) error { return err } + // restart Host Operator using the adm-restart command + if err := restart(ctx.CommandContext, configuration.HostName, v.getRegMemConfigFlagsAndClient); err != nil { + return err + } + exampleSPC := &toolchainv1alpha1.SpaceProvisionerConfig{ TypeMeta: metav1.TypeMeta{ Kind: "SpaceProvisionerConfig", @@ -509,6 +515,20 @@ until the SpaceProvisionerConfig.spec.enabled is set to true. `, v.hostClusterData.apiEndpoint)) } +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 + kubeConfigFlags.AuthInfoName = nil // unused here, so we can hide it + kubeConfigFlags.Context = nil // unused here, so we can hide it + + kubeConfigFlags.Namespace = &v.hostClusterData.namespace + kubeConfigFlags.APIServer = &v.hostClusterData.apiEndpoint + kubeConfigFlags.KubeConfig = &v.hostClusterData.kubeConfig + + return kubeConfigFlags, v.hostClusterData.client, nil +} + func findToolchainClusterForMember(allToolchainClusters []toolchainv1alpha1.ToolchainCluster, memberAPIEndpoint, memberOperatorNamespace string) *toolchainv1alpha1.ToolchainCluster { for _, tc := range allToolchainClusters { if tc.Status.APIEndpoint == memberAPIEndpoint && tc.Status.OperatorNamespace == memberOperatorNamespace { diff --git a/pkg/cmd/adm/register_member_test.go b/pkg/cmd/adm/register_member_test.go index 3d54ecf..3ba43f1 100644 --- a/pkg/cmd/adm/register_member_test.go +++ b/pkg/cmd/adm/register_member_test.go @@ -13,6 +13,7 @@ import ( "github.com/codeready-toolchain/toolchain-common/pkg/test" "github.com/ghodss/yaml" "github.com/kubesaw/ksctl/pkg/configuration" + clicontext "github.com/kubesaw/ksctl/pkg/context" . "github.com/kubesaw/ksctl/pkg/test" "github.com/kubesaw/ksctl/pkg/utils" "github.com/stretchr/testify/assert" @@ -82,7 +83,9 @@ func TestRegisterMember(t *testing.T) { } // when - err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false)) + err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) // then require.NoError(t, err) @@ -115,7 +118,9 @@ func TestRegisterMember(t *testing.T) { ctx := newExtendedCommandContext(term, newClient) // when - err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false)) + err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) // then require.Error(t, err) @@ -135,7 +140,9 @@ func TestRegisterMember(t *testing.T) { ctx := newExtendedCommandContext(term, newClient) // when - err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false)) + err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) // then require.Error(t, err) @@ -155,7 +162,9 @@ func TestRegisterMember(t *testing.T) { ctx := newExtendedCommandContext(term, newClient) // when - err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false)) + err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) // then require.NoError(t, err) @@ -171,7 +180,9 @@ func TestRegisterMember(t *testing.T) { ctx := newExtendedCommandContext(term, newClient) // when - err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, true)) + err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, true), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) // then require.NoError(t, err) @@ -206,7 +217,9 @@ func TestRegisterMember(t *testing.T) { mockCreateToolchainClusterWithReadyCondition(t, fakeClient) // when - err := registerMemberCluster(ctx, newRegisterMemberArgsWithSuffix(hostKubeconfig, memberKubeconfig, false, "2")) + err := registerMemberCluster(ctx, newRegisterMemberArgsWithSuffix(hostKubeconfig, memberKubeconfig, false, "2"), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) // then require.NoError(t, err) @@ -227,8 +240,12 @@ func TestRegisterMember(t *testing.T) { ctx2 := newExtendedCommandContext(term2, newClient) // when - err1 := registerMemberCluster(ctx1, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false)) - err2 := registerMemberCluster(ctx2, newRegisterMemberArgsWithSuffix(hostKubeconfig, memberKubeconfig, false, "1")) + err1 := registerMemberCluster(ctx1, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) + err2 := registerMemberCluster(ctx2, newRegisterMemberArgsWithSuffix(hostKubeconfig, memberKubeconfig, false, "1"), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) // then require.NoError(t, err1) @@ -250,8 +267,12 @@ func TestRegisterMember(t *testing.T) { ctx2 := newExtendedCommandContext(term2, newClient) // when - err1 := registerMemberCluster(ctx1, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false)) - err2 := registerMemberCluster(ctx2, newRegisterMemberArgsWithSuffix(hostKubeconfig, memberKubeconfig, false, "")) + err1 := registerMemberCluster(ctx1, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) + err2 := registerMemberCluster(ctx2, newRegisterMemberArgsWithSuffix(hostKubeconfig, memberKubeconfig, false, ""), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) // then require.NoError(t, err1) @@ -305,7 +326,9 @@ func TestRegisterMember(t *testing.T) { require.NoError(t, fakeClient.Create(context.TODO(), preexistingToolchainCluster2.DeepCopy())) // when - err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false)) + err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) // then require.Error(t, err) @@ -337,7 +360,9 @@ func TestRegisterMember(t *testing.T) { require.NoError(t, fakeClient.Create(context.TODO(), preexistingToolchainCluster.DeepCopy())) // when - err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false)) + err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) // then require.Error(t, err) @@ -369,7 +394,9 @@ func TestRegisterMember(t *testing.T) { require.NoError(t, fakeClient.Create(context.TODO(), preexistingToolchainCluster.DeepCopy())) // when - err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false)) + err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) // then require.Error(t, err) @@ -402,7 +429,9 @@ func TestRegisterMember(t *testing.T) { require.NoError(t, fakeClient.Create(context.TODO(), preexistingToolchainCluster.DeepCopy())) // when - err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false)) + err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) // then require.Error(t, err) @@ -419,7 +448,9 @@ func TestRegisterMember(t *testing.T) { ctx := newExtendedCommandContext(term, newClient) // when - err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false)) + err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) // then require.Error(t, err) @@ -438,7 +469,9 @@ func TestRegisterMember(t *testing.T) { ctx := newExtendedCommandContext(term, newClient) // when - err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false)) + err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return nil + }) // then require.Error(t, err) @@ -449,6 +482,41 @@ func TestRegisterMember(t *testing.T) { require.NoError(t, fakeClient.List(context.TODO(), tcs, runtimeclient.InNamespace(test.MemberOperatorNs))) assert.Empty(t, tcs.Items) }) + + t.Run("reports error when host-operator is not restarted", func(t *testing.T) { + // given + term := NewFakeTerminalWithResponse("Y") + newClient, fakeClient := newFakeClientsFromRestConfig(t, &toolchainClusterMemberSa, &toolchainClusterHostSa) + mockCreateToolchainClusterWithReadyCondition(t, fakeClient) + ctx := newExtendedCommandContext(term, newClient) + + // when + err := registerMemberCluster(ctx, newRegisterMemberArgsWith(hostKubeconfig, memberKubeconfig, false), func(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { + return fmt.Errorf("restart did not happen") + }) + + // then + 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) { @@ -585,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") +} diff --git a/pkg/cmd/adm/restart.go b/pkg/cmd/adm/restart.go index 2546061..e9ea06a 100644 --- a/pkg/cmd/adm/restart.go +++ b/pkg/cmd/adm/restart.go @@ -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) (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 @@ -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) }, } 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, } + + kubeConfigFlags, cl, err := configFlagsClientGetter(ctx, clusterName) + if err != nil { + return err + } + factory := cmdutil.NewFactory(cmdutil.NewMatchVersionFlags(kubeConfigFlags)) + + if !ctx.AskForConfirmation( + ioutils.WithMessagef("restart all the deployments in the cluster '%s' and namespace '%s' \n", clusterName, *kubeConfigFlags.Namespace)) { + return nil + } + + return restartDeployments(ctx, cl, *kubeConfigFlags.Namespace, 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) + }) +} + +func getConfigFlagsAndClient(ctx *clicontext.CommandContext, clusterName 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 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 nil, nil, err } kubeConfigFlags.Namespace = &cfg.OperatorNamespace kubeConfigFlags.APIServer = &cfg.ServerAPI kubeConfigFlags.BearerToken = &cfg.Token kubeconfig, err := client.EnsureKsctlConfigFile() if err != nil { - return err + return nil, nil, err } 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 nil, nil, err } - - 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 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 diff --git a/pkg/cmd/adm/restart_test.go b/pkg/cmd/adm/restart_test.go index 6292f44..d100542 100644 --- a/pkg/cmd/adm/restart_test.go +++ b/pkg/cmd/adm/restart_test.go @@ -91,7 +91,7 @@ func TestKubectlRolloutFunctionality(t *testing.T) { ctx := clicontext.NewCommandContext(term, newClient) //when - err := restartDeployments(ctx, fakeClient, HostNamespacedName.Namespace, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { + err := restartDeployments(ctx, fakeClient, HostNamespacedName.Namespace, func(ctx *clicontext.CommandContext, _ appsv1.Deployment) error { return checkRolloutStatus(ctx, tf, streams, *hostDep) }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { return restartNonOlmDeployments(ctx, deployment, tf, streams) @@ -123,7 +123,7 @@ func TestKubectlRolloutFunctionality(t *testing.T) { ctx := clicontext.NewCommandContext(term, newClient) //when - err := restartDeployments(ctx, fakeClient, HostNamespacedName.Namespace, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { + err := restartDeployments(ctx, fakeClient, HostNamespacedName.Namespace, func(ctx *clicontext.CommandContext, _ appsv1.Deployment) error { return checkRolloutStatus(ctx, tf, streams, *hostDep) }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { return restartNonOlmDeployments(ctx, deployment, tf, streams) @@ -140,7 +140,7 @@ func TestKubectlRolloutFunctionality(t *testing.T) { ctx := clicontext.NewCommandContext(term, newClient) //when - err := restartDeployments(ctx, fakeClient, HostNamespacedName.Namespace, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { + err := restartDeployments(ctx, fakeClient, HostNamespacedName.Namespace, func(ctx *clicontext.CommandContext, _ appsv1.Deployment) error { return checkRolloutStatus(ctx, tf, streams, *hostDep) }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { return restartNonOlmDeployments(ctx, deployment, tf, streams) @@ -181,10 +181,10 @@ func TestRestartDeployment(t *testing.T) { //when err := restartDeployments(ctx, fakeClient, "toolchain-host-operator", - func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { + func(_ *clicontext.CommandContext, deployment appsv1.Deployment) error { require.Equal(t, "host-operator-controller-manager", deployment.Name) return nil - }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { + }, func(_ *clicontext.CommandContext, deployment appsv1.Deployment) error { require.Equal(t, regServDeployment, deployment) return nil }) @@ -200,9 +200,9 @@ func TestRestartDeployment(t *testing.T) { //when err := restartDeployments(ctx, fakeClient, "toolchain-host-operator", - func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { + func(_ *clicontext.CommandContext, _ appsv1.Deployment) error { return nil - }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { + }, func(_ *clicontext.CommandContext, _ appsv1.Deployment) error { return nil }) @@ -228,9 +228,9 @@ func TestRestartDeployment(t *testing.T) { //when err := restartDeployments(ctx, fakeClient, "toolchain-host-operator", - func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { + func(_ *clicontext.CommandContext, _ appsv1.Deployment) error { return nil - }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { + }, func(_ *clicontext.CommandContext, _ appsv1.Deployment) error { return nil }) @@ -246,9 +246,9 @@ func TestRestartDeployment(t *testing.T) { expectedErr := fmt.Errorf("Could not do rollout restart of the deployment") //when err := restartDeployments(ctx, fakeClient, "toolchain-host-operator", - func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { + func(_ *clicontext.CommandContext, _ appsv1.Deployment) error { return nil - }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { + }, func(_ *clicontext.CommandContext, _ appsv1.Deployment) error { return expectedErr }) @@ -263,7 +263,7 @@ func TestRestartDeployment(t *testing.T) { //when err := restartDeployments(ctx, fakeClient, "toolchain-host-operator", - func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { + func(_ *clicontext.CommandContext, _ appsv1.Deployment) error { return nil }, nil) @@ -278,7 +278,7 @@ func TestRestartDeployment(t *testing.T) { expectedErr := fmt.Errorf("Could not check the status of the deployment") //when err := restartDeployments(ctx, fakeClient, "toolchain-host-operator", - func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { + func(_ *clicontext.CommandContext, _ appsv1.Deployment) error { return expectedErr }, nil) @@ -310,7 +310,7 @@ func TestRestartAutoScalerDeployment(t *testing.T) { ctx := clicontext.NewCommandContext(term, newClient) //when err := restartDeployments(ctx, fakeClient, "toolchain-member-operator", - func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { + func(_ *clicontext.CommandContext, _ appsv1.Deployment) error { return nil }, mockRolloutRestartInterceptor()) @@ -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) diff --git a/pkg/cmd/adm/unregister_member.go b/pkg/cmd/adm/unregister_member.go index c082321..46ff9b9 100644 --- a/pkg/cmd/adm/unregister_member.go +++ b/pkg/cmd/adm/unregister_member.go @@ -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{ @@ -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) } diff --git a/pkg/cmd/adm/unregister_member_test.go b/pkg/cmd/adm/unregister_member_test.go index fb7575b..0e42cdd 100644 --- a/pkg/cmd/adm/unregister_member_test.go +++ b/pkg/cmd/adm/unregister_member_test.go @@ -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(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { return nil }) @@ -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(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { return fmt.Errorf("restart did not happen") }) @@ -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, _ ConfigFlagsAndClientGetterFunc) error { called++ - return mockRestart(ctx, restartClusterName) + return mockRestart(ctx, restartClusterName, getConfigFlagsAndClient) }) // then @@ -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(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { return nil }) @@ -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(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { return nil }) @@ -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(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { return nil }) @@ -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(_ *clicontext.CommandContext, _ string, _ ConfigFlagsAndClientGetterFunc) error { return nil }) @@ -164,9 +164,11 @@ func TestUnregisterMemberLacksPermissions(t *testing.T) { AssertToolchainClusterSpec(t, fakeClient, toolchainCluster) } -func mockRestart(ctx *clicontext.CommandContext, clusterName string) error { - if clusterName == "host" && ctx != nil { +func mockRestart(ctx *clicontext.CommandContext, clusterName string, cfc ConfigFlagsAndClientGetterFunc) error { + _, _, cfcerr := cfc(ctx, clusterName) + if clusterName == "host" && ctx != nil && cfcerr == nil { return nil } + return fmt.Errorf("cluster name is wrong") }