From ef1d5d2691a6e54a5524eda7db07a918f806fbb2 Mon Sep 17 00:00:00 2001 From: Monis Khan Date: Wed, 14 Jul 2021 12:34:56 -0400 Subject: [PATCH] client-go exec: fix metrics related to plugin not found These were missed because our tests did not pass in the correct test data input (the command to execute). Signed-off-by: Monis Khan Kubernetes-commit: a6ac42082b4d7c0057b52900736bd7fbc2c44241 --- plugin/pkg/client/auth/exec/metrics.go | 4 +++- plugin/pkg/client/auth/exec/metrics_test.go | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/plugin/pkg/client/auth/exec/metrics.go b/plugin/pkg/client/auth/exec/metrics.go index 3a2cc251a1..51210975a6 100644 --- a/plugin/pkg/client/auth/exec/metrics.go +++ b/plugin/pkg/client/auth/exec/metrics.go @@ -18,6 +18,7 @@ package exec import ( "errors" + "io/fs" "os/exec" "reflect" "sync" @@ -92,6 +93,7 @@ func (c *certificateExpirationTracker) set(a *Authenticator, t time.Time) { func incrementCallsMetric(err error) { execExitError := &exec.ExitError{} execError := &exec.Error{} + pathError := &fs.PathError{} switch { case err == nil: // Binary execution succeeded. metrics.ExecPluginCalls.Increment(successExitCode, noError) @@ -99,7 +101,7 @@ func incrementCallsMetric(err error) { case errors.As(err, &execExitError): // Binary execution failed (see "os/exec".Cmd.Run()). metrics.ExecPluginCalls.Increment(execExitError.ExitCode(), pluginExecutionError) - case errors.As(err, &execError): // Binary does not exist (see exec.Error). + case errors.As(err, &execError), errors.As(err, &pathError): // Binary does not exist (see exec.Error, fs.PathError). metrics.ExecPluginCalls.Increment(failureExitCode, pluginNotFoundError) default: // We don't know about this error type. diff --git a/plugin/pkg/client/auth/exec/metrics_test.go b/plugin/pkg/client/auth/exec/metrics_test.go index 80d84480bd..4488df5997 100644 --- a/plugin/pkg/client/auth/exec/metrics_test.go +++ b/plugin/pkg/client/auth/exec/metrics_test.go @@ -18,6 +18,7 @@ package exec import ( "fmt" + "io" "testing" "time" @@ -147,6 +148,7 @@ func TestCallsMetric(t *testing.T) { if err != nil { t.Fatal(err) } + a.stderr = io.Discard // Run refresh creds twice so that our test validates that the metrics are set correctly twice // in a row with the same authenticator. @@ -172,7 +174,7 @@ func TestCallsMetric(t *testing.T) { // metric values. refreshCreds := func(command string) { c := api.ExecConfig{ - Command: "does not exist", + Command: command, APIVersion: "client.authentication.k8s.io/v1beta1", InteractiveMode: api.IfAvailableExecInteractiveMode, } @@ -180,6 +182,7 @@ func TestCallsMetric(t *testing.T) { if err != nil { t.Fatal(err) } + a.stderr = io.Discard if err := a.refreshCredsLocked(&clientauthentication.Response{}); err == nil { t.Fatal("expected the authenticator to fail because the plugin does not exist") }