From b2c503859972c46fb61e7cb6028182a59e3ba984 Mon Sep 17 00:00:00 2001 From: Luis Sanchez Date: Mon, 3 Jun 2024 13:49:13 -0400 Subject: [PATCH] ignore DeploymentConfig warning on oc status --- pkg/cli/cli.go | 19 +++++-- pkg/cli/status/status.go | 1 - pkg/helpers/apps/warning_handler.go | 22 ++++++++ pkg/helpers/apps/warning_handler_test.go | 66 ++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 pkg/helpers/apps/warning_handler.go create mode 100644 pkg/helpers/apps/warning_handler_test.go diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index b8669cc9b6..4db2386e06 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -3,13 +3,15 @@ package cli import ( "flag" "fmt" + "log/slog" "os" "runtime" + "slices" "strings" "github.com/MakeNowJust/heredoc" + "github.com/openshift/oc/pkg/helpers/apps" "github.com/spf13/cobra" - "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericiooptions" "k8s.io/client-go/rest" @@ -176,8 +178,19 @@ func NewOcCommand(o kubecmd.KubectlOptions) *cobra.Command { Long: cliLong, Run: runHelp, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - rest.SetDefaultWarningHandler(warningHandler) - + switch cmd.Name() { + case "status": + rest.SetDefaultWarningHandler(apps.NewIgnoreDeploymentConfigWarningHandler(warningHandler)) + case "get": + if slices.ContainsFunc(args, func(s string) bool { return slices.Contains(strings.Split(s, ","), "all") }) { + rest.SetDefaultWarningHandler(apps.NewIgnoreDeploymentConfigWarningHandler(warningHandler)) + } else { + rest.SetDefaultWarningHandler(warningHandler) + } + default: + rest.SetDefaultWarningHandler(warningHandler) + } + slog.Info("PersistentPreRunE", "name", cmd.Name(), "calledAs", cmd.CalledAs(), "args", args) if cmd.Name() == cobra.ShellCompRequestCmd { // This is the __complete or __completeNoDesc command which // indicates shell completion has been requested. diff --git a/pkg/cli/status/status.go b/pkg/cli/status/status.go index 6512bdef70..08c561e35b 100644 --- a/pkg/cli/status/status.go +++ b/pkg/cli/status/status.go @@ -7,7 +7,6 @@ import ( "github.com/gonum/graph/encoding/dot" "github.com/spf13/cobra" - kapierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/cli-runtime/pkg/genericiooptions" diff --git a/pkg/helpers/apps/warning_handler.go b/pkg/helpers/apps/warning_handler.go new file mode 100644 index 0000000000..4eac5698b6 --- /dev/null +++ b/pkg/helpers/apps/warning_handler.go @@ -0,0 +1,22 @@ +package apps + +import ( + "strings" + + "k8s.io/client-go/rest" +) + +type ignoreDeploymentConfigWarningHandler struct { + handler rest.WarningHandler +} + +func NewIgnoreDeploymentConfigWarningHandler(handler rest.WarningHandler) rest.WarningHandler { + return &ignoreDeploymentConfigWarningHandler{handler: handler} +} + +func (h *ignoreDeploymentConfigWarningHandler) HandleWarningHeader(code int, agent string, text string) { + if h.handler == nil || strings.Contains(text, `apps.openshift.io/v1 DeploymentConfig is deprecated`) { + return + } + h.handler.HandleWarningHeader(code, agent, text) +} diff --git a/pkg/helpers/apps/warning_handler_test.go b/pkg/helpers/apps/warning_handler_test.go new file mode 100644 index 0000000000..46f2aa92cc --- /dev/null +++ b/pkg/helpers/apps/warning_handler_test.go @@ -0,0 +1,66 @@ +package apps + +import ( + "net/http" + "testing" +) + +func TestHandleWarningHeader(t *testing.T) { + testCases := []struct { + name string + text string + wraps bool + expectOutput bool + }{ + { + name: "deprecated old", + text: `apps.openshift.io/v1 DeploymentConfig is deprecated in v4.14+, unavailable in v4.10000+`, + wraps: true, + expectOutput: false, + }, + { + name: "deprecated new", + text: `apps.openshift.io/v1 DeploymentConfig is deprecated in v1.27+`, + wraps: true, + expectOutput: false, + }, + { + name: "do not panic no match", + text: `something else`, + }, + { + name: "do not panic match", + text: `apps.openshift.io/v1 DeploymentConfig is deprecated in v1.27+`, + wraps: false, + }, + { + name: "not deprecation warning", + text: `DeploymentConfig is not deprecated`, + wraps: true, + expectOutput: true, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + var wrappedHandler mockWarningHandler + if tc.wraps { + wrappedHandler = mockWarningHandler{} + } + h := NewIgnoreDeploymentConfigWarningHandler(&wrappedHandler) + h.HandleWarningHeader(http.StatusOK, "agent", tc.text) + if tc.wraps { + if tc.expectOutput != wrappedHandler.invoked { + t.Fatalf("Wrapped hander: invoke expected: %v, invoke actual: %v", tc.expectOutput, wrappedHandler.invoked) + } + } + }) + } +} + +type mockWarningHandler struct { + invoked bool +} + +func (m *mockWarningHandler) HandleWarningHeader(warnStatusCode int, header string, text string) { + m.invoked = true +}