Merge pull request #103689 from enj/enj/t/exec_metrics

client-go exec: fix metrics related to plugin not found
This commit is contained in:
Kubernetes Prow Robot 2021-07-14 13:54:41 -07:00 committed by GitHub
commit 6609899398
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 3 deletions

View File

@ -18,6 +18,7 @@ package exec
import ( import (
"errors" "errors"
"io/fs"
"os/exec" "os/exec"
"reflect" "reflect"
"sync" "sync"
@ -92,6 +93,7 @@ func (c *certificateExpirationTracker) set(a *Authenticator, t time.Time) {
func incrementCallsMetric(err error) { func incrementCallsMetric(err error) {
execExitError := &exec.ExitError{} execExitError := &exec.ExitError{}
execError := &exec.Error{} execError := &exec.Error{}
pathError := &fs.PathError{}
switch { switch {
case err == nil: // Binary execution succeeded. case err == nil: // Binary execution succeeded.
metrics.ExecPluginCalls.Increment(successExitCode, noError) 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()). case errors.As(err, &execExitError): // Binary execution failed (see "os/exec".Cmd.Run()).
metrics.ExecPluginCalls.Increment(execExitError.ExitCode(), pluginExecutionError) 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) metrics.ExecPluginCalls.Increment(failureExitCode, pluginNotFoundError)
default: // We don't know about this error type. default: // We don't know about this error type.

View File

@ -18,6 +18,7 @@ package exec
import ( import (
"fmt" "fmt"
"io"
"testing" "testing"
"time" "time"
@ -147,6 +148,7 @@ func TestCallsMetric(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
a.stderr = io.Discard
// Run refresh creds twice so that our test validates that the metrics are set correctly twice // Run refresh creds twice so that our test validates that the metrics are set correctly twice
// in a row with the same authenticator. // in a row with the same authenticator.
@ -172,7 +174,7 @@ func TestCallsMetric(t *testing.T) {
// metric values. // metric values.
refreshCreds := func(command string) { refreshCreds := func(command string) {
c := api.ExecConfig{ c := api.ExecConfig{
Command: "does not exist", Command: command,
APIVersion: "client.authentication.k8s.io/v1beta1", APIVersion: "client.authentication.k8s.io/v1beta1",
InteractiveMode: api.IfAvailableExecInteractiveMode, InteractiveMode: api.IfAvailableExecInteractiveMode,
} }
@ -180,6 +182,7 @@ func TestCallsMetric(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
a.stderr = io.Discard
if err := a.refreshCredsLocked(&clientauthentication.Response{}); err == nil { if err := a.refreshCredsLocked(&clientauthentication.Response{}); err == nil {
t.Fatal("expected the authenticator to fail because the plugin does not exist") t.Fatal("expected the authenticator to fail because the plugin does not exist")
} }

View File

@ -391,7 +391,7 @@ func execPluginClientTests(t *testing.T, unauthorizedCert, unauthorizedKey []byt
}, },
wantGetCertificateErrorPrefix: "exec: fork/exec ./testdata/exec-plugin-not-executable.sh: permission denied", wantGetCertificateErrorPrefix: "exec: fork/exec ./testdata/exec-plugin-not-executable.sh: permission denied",
wantClientErrorPrefix: `Get "https`, wantClientErrorPrefix: `Get "https`,
wantMetrics: &execPluginMetrics{calls: []execPluginCall{{exitCode: 1, callStatus: "client_internal_error"}}}, wantMetrics: &execPluginMetrics{calls: []execPluginCall{{exitCode: 1, callStatus: "plugin_not_found_error"}}},
}, },
{ {
name: "binary fails", name: "binary fails",