Skip to content

Commit

Permalink
client-go exec: fix metrics related to plugin not found
Browse files Browse the repository at this point in the history
These were missed because our tests did not pass in the correct test
data input (the command to execute).

Signed-off-by: Monis Khan <mok@vmware.com>

Kubernetes-commit: a6ac42082b4d7c0057b52900736bd7fbc2c44241
  • Loading branch information
enj authored and k8s-publishing-bot committed Jul 14, 2021
1 parent 7a90b08 commit ef1d5d2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 3 additions & 1 deletion plugin/pkg/client/auth/exec/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package exec

import (
"errors"
"io/fs"
"os/exec"
"reflect"
"sync"
Expand Down Expand Up @@ -92,14 +93,15 @@ 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)

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.
Expand Down
5 changes: 4 additions & 1 deletion plugin/pkg/client/auth/exec/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package exec

import (
"fmt"
"io"
"testing"
"time"

Expand Down Expand Up @@ -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.
Expand All @@ -172,14 +174,15 @@ 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,
}
a, err := newAuthenticator(newCache(), func(_ int) bool { return false }, &c, nil)
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")
}
Expand Down

0 comments on commit ef1d5d2

Please sign in to comment.