From 9b889ccfddbf56176d869866aaecb217b49c0a1e Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Thu, 14 Nov 2024 17:41:51 +0530 Subject: [PATCH] some changes to status func Signed-off-by: Feny Mehta --- pkg/cmd/adm/restart.go | 16 ++++++++-------- pkg/cmd/adm/restart_test.go | 38 +++++++++++++------------------------ 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/pkg/cmd/adm/restart.go b/pkg/cmd/adm/restart.go index d87dcdb..4c0cca4 100644 --- a/pkg/cmd/adm/restart.go +++ b/pkg/cmd/adm/restart.go @@ -21,7 +21,7 @@ import ( type ( NonOperatorDeploymentsRestarterFunc func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error - RolloutStatusCheckerFunc func(ctx *clicontext.CommandContext, labelSelector string) error + RolloutStatusCheckerFunc func(ctx *clicontext.CommandContext, labelSelector string, deployment appsv1.Deployment) error ) // NewRestartCmd() is a function to restart the whole operator, it relies on the target cluster and fetches the cluster config @@ -86,8 +86,8 @@ func restart(ctx *clicontext.CommandContext, clusterName string) error { } //return restartDeployment(ctx, cl, cfg.OperatorNamespace, factory, ioStreams, checkRolloutStatus, restartNonOperatorDeployments) - return restartDeployments(ctx, cl, cfg.OperatorNamespace, func(ctx *clicontext.CommandContext, labelSelector string) error { - return checkRolloutStatus(ctx, factory, ioStreams, labelSelector) + return restartDeployments(ctx, cl, cfg.OperatorNamespace, func(ctx *clicontext.CommandContext, labelSelector string, deployment appsv1.Deployment) error { + return checkRolloutStatus(ctx, factory, ioStreams, labelSelector, deployment) }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { return restartNonOlmDeployments(ctx, deployment, factory, ioStreams) }) @@ -117,7 +117,7 @@ func restartDeployments(ctx *clicontext.CommandContext, cl runtimeclient.Client, ctx.Printlnf("Checking the status of the deleted pod's deployment %v", olmOperatorDeployment.Name) //check the rollout status - if err := checker(ctx, "kubesaw-control-plane=kubesaw-controller-manager"); err != nil { + if err := checker(ctx, "kubesaw-control-plane=kubesaw-controller-manager", olmOperatorDeployment); err != nil { return err } } @@ -140,7 +140,7 @@ func restartDeployments(ctx *clicontext.CommandContext, cl runtimeclient.Client, } //check the rollout status ctx.Printlnf("Checking the status of the rolled out deployment %v", nonOlmDeployment.Name) - if err := checker(ctx, "toolchain.dev.openshift.com/provider=codeready-toolchain"); err != nil { + if err := checker(ctx, "toolchain.dev.openshift.com/provider=codeready-toolchain", nonOlmDeployment); err != nil { return err } return nil @@ -192,15 +192,15 @@ func restartNonOlmDeployments(ctx *clicontext.CommandContext, deployment appsv1. return o.RunRestart() } -func checkRolloutStatus(ctx *clicontext.CommandContext, f cmdutil.Factory, ioStreams genericclioptions.IOStreams, labelSelector string) error { +func checkRolloutStatus(ctx *clicontext.CommandContext, f cmdutil.Factory, ioStreams genericclioptions.IOStreams, labelSelector string, deployment appsv1.Deployment) error { cmd := kubectlrollout.NewRolloutStatusOptions(ioStreams) - if err := cmd.Complete(f, []string{"deployment"}); err != nil { + if err := cmd.Complete(f, []string{"deployment/" + deployment.Name}); err != nil { return err } - cmd.LabelSelector = labelSelector + //cmd.LabelSelector = labelSelector if err := cmd.Validate(); err != nil { return err } diff --git a/pkg/cmd/adm/restart_test.go b/pkg/cmd/adm/restart_test.go index 81426c9..c27bf77 100644 --- a/pkg/cmd/adm/restart_test.go +++ b/pkg/cmd/adm/restart_test.go @@ -92,8 +92,8 @@ func TestKubectlRolloutFunctionality(t *testing.T) { ctx := clicontext.NewCommandContext(term, newClient) //when - err := restartDeployments(ctx, fakeClient, HostNamespacedName.Namespace, func(ctx *clicontext.CommandContext, labelSelector string) error { - return checkRolloutStatus(ctx, tf, streams, labelSelector) + err := restartDeployments(ctx, fakeClient, HostNamespacedName.Namespace, func(ctx *clicontext.CommandContext, labelSelector string, deployment appsv1.Deployment) error { + return checkRolloutStatus(ctx, tf, streams, labelSelector, *hostDep) }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { return restartNonOlmDeployments(ctx, deployment, tf, streams) }) @@ -124,8 +124,8 @@ func TestKubectlRolloutFunctionality(t *testing.T) { ctx := clicontext.NewCommandContext(term, newClient) //when - err := restartDeployments(ctx, fakeClient, HostNamespacedName.Namespace, func(ctx *clicontext.CommandContext, labelSelector string) error { - return checkRolloutStatus(ctx, tf, streams, labelSelector) + err := restartDeployments(ctx, fakeClient, HostNamespacedName.Namespace, func(ctx *clicontext.CommandContext, labelSelector string, deployment appsv1.Deployment) error { + return checkRolloutStatus(ctx, tf, streams, labelSelector, *hostDep) }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { return restartNonOlmDeployments(ctx, deployment, tf, streams) }) @@ -141,8 +141,8 @@ func TestKubectlRolloutFunctionality(t *testing.T) { ctx := clicontext.NewCommandContext(term, newClient) //when - err := restartDeployments(ctx, fakeClient, HostNamespacedName.Namespace, func(ctx *clicontext.CommandContext, labelSelector string) error { - return checkRolloutStatus(ctx, tf, streams, labelSelector) + err := restartDeployments(ctx, fakeClient, HostNamespacedName.Namespace, func(ctx *clicontext.CommandContext, labelSelector string, deployment appsv1.Deployment) error { + return checkRolloutStatus(ctx, tf, streams, labelSelector, *hostDep) }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { return restartNonOlmDeployments(ctx, deployment, tf, streams) }) @@ -183,7 +183,7 @@ func TestRestartDeployment(t *testing.T) { //when err := restartDeployments(ctx, fakeClient, "toolchain-host-operator", - func(ctx *clicontext.CommandContext, labelSelector string) error { + func(ctx *clicontext.CommandContext, labelSelector string, deployment appsv1.Deployment) error { require.Equal(t, "toolchain.dev.openshift.com/provider=codeready-toolchain", labelSelector) return nil }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { @@ -202,7 +202,7 @@ func TestRestartDeployment(t *testing.T) { //when err := restartDeployments(ctx, fakeClient, "toolchain-host-operator", - func(ctx *clicontext.CommandContext, labelSelector string) error { + func(ctx *clicontext.CommandContext, labelSelector string, deployment appsv1.Deployment) error { return nil }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { return nil @@ -230,7 +230,7 @@ func TestRestartDeployment(t *testing.T) { //when err := restartDeployments(ctx, fakeClient, "toolchain-host-operator", - func(ctx *clicontext.CommandContext, labelSelector string) error { + func(ctx *clicontext.CommandContext, labelSelector string, deployment appsv1.Deployment) error { return nil }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { return nil @@ -248,7 +248,7 @@ 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, labelSelector string) error { + func(ctx *clicontext.CommandContext, labelSelector string, deployment appsv1.Deployment) error { return nil }, func(ctx *clicontext.CommandContext, deployment appsv1.Deployment) error { return expectedErr @@ -265,7 +265,7 @@ func TestRestartDeployment(t *testing.T) { //when err := restartDeployments(ctx, fakeClient, "toolchain-host-operator", - func(ctx *clicontext.CommandContext, labelSelector string) error { + func(ctx *clicontext.CommandContext, labelSelector string, deployment appsv1.Deployment) error { return nil }, nil) @@ -280,7 +280,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, labelSelector string) error { + func(ctx *clicontext.CommandContext, labelSelector string, deployment appsv1.Deployment) error { return expectedErr }, nil) @@ -313,7 +313,7 @@ func TestRestartAutoScalerDeployment(t *testing.T) { ctx := clicontext.NewCommandContext(term, newClient) //when err := restartDeployments(ctx, fakeClient, "toolchain-member-operator", - func(ctx *clicontext.CommandContext, labelSelector string) error { + func(ctx *clicontext.CommandContext, labelSelector string, deployment appsv1.Deployment) error { return nil }, mockRolloutRestartInterceptor()) @@ -359,18 +359,6 @@ func TestRestart(t *testing.T) { require.NotContains(t, term.Output(), "Fetching the current OLM and non-OLM deployments of the operator in") }) - t.Run("fails when no factory and stream provided", func(t *testing.T) { - term := NewFakeTerminalWithResponse("Y") - //given - newClient, _ := NewFakeClients(t, toolchainCluster, hostDeployment, hostPod) - ctx := clicontext.NewCommandContext(term, newClient) - //when - err := restart(ctx, "host") - - //then - require.Error(t, err, "the server doesn't have a resource type deployment") - - }) } func newDeployment(namespacedName types.NamespacedName, replicas int32) *appsv1.Deployment { //nolint:unparam