k8s.io/client-go@v0.31.1/plugin/pkg/client/auth/exec/metrics.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package exec
    18  
    19  import (
    20  	"errors"
    21  	"io/fs"
    22  	"os/exec"
    23  	"reflect"
    24  	"sync"
    25  	"time"
    26  
    27  	"k8s.io/klog/v2"
    28  
    29  	"k8s.io/client-go/tools/metrics"
    30  )
    31  
    32  // The following constants shadow the special values used in the prometheus metrics implementation.
    33  const (
    34  	// noError indicates that the plugin process was successfully started and exited with an exit
    35  	// code of 0.
    36  	noError = "no_error"
    37  	// pluginExecutionError indicates that the plugin process was successfully started and then
    38  	// it returned a non-zero exit code.
    39  	pluginExecutionError = "plugin_execution_error"
    40  	// pluginNotFoundError indicates that we could not find the exec plugin.
    41  	pluginNotFoundError = "plugin_not_found_error"
    42  	// clientInternalError indicates that we attempted to start the plugin process, but failed
    43  	// for some reason.
    44  	clientInternalError = "client_internal_error"
    45  
    46  	// successExitCode represents an exec plugin invocation that was successful.
    47  	successExitCode = 0
    48  	// failureExitCode represents an exec plugin invocation that was not successful. This code is
    49  	// used in some failure modes (e.g., plugin not found, client internal error) so that someone
    50  	// can more easily monitor all unsuccessful invocations.
    51  	failureExitCode = 1
    52  )
    53  
    54  type certificateExpirationTracker struct {
    55  	mu        sync.RWMutex
    56  	m         map[*Authenticator]time.Time
    57  	metricSet func(*time.Time)
    58  }
    59  
    60  var expirationMetrics = &certificateExpirationTracker{
    61  	m: map[*Authenticator]time.Time{},
    62  	metricSet: func(e *time.Time) {
    63  		metrics.ClientCertExpiry.Set(e)
    64  	},
    65  }
    66  
    67  // set stores the given expiration time and updates the updates the certificate
    68  // expiry metric to the earliest expiration time.
    69  func (c *certificateExpirationTracker) set(a *Authenticator, t time.Time) {
    70  	c.mu.Lock()
    71  	defer c.mu.Unlock()
    72  	c.m[a] = t
    73  
    74  	earliest := time.Time{}
    75  	for _, t := range c.m {
    76  		if t.IsZero() {
    77  			continue
    78  		}
    79  		if earliest.IsZero() || earliest.After(t) {
    80  			earliest = t
    81  		}
    82  	}
    83  	if earliest.IsZero() {
    84  		c.metricSet(nil)
    85  	} else {
    86  		c.metricSet(&earliest)
    87  	}
    88  }
    89  
    90  // incrementCallsMetric increments a global metrics counter for the number of calls to an exec
    91  // plugin, partitioned by exit code. The provided err should be the return value from
    92  // exec.Cmd.Run().
    93  func incrementCallsMetric(err error) {
    94  	execExitError := &exec.ExitError{}
    95  	execError := &exec.Error{}
    96  	pathError := &fs.PathError{}
    97  	switch {
    98  	case err == nil: // Binary execution succeeded.
    99  		metrics.ExecPluginCalls.Increment(successExitCode, noError)
   100  
   101  	case errors.As(err, &execExitError): // Binary execution failed (see "os/exec".Cmd.Run()).
   102  		metrics.ExecPluginCalls.Increment(execExitError.ExitCode(), pluginExecutionError)
   103  
   104  	case errors.As(err, &execError), errors.As(err, &pathError): // Binary does not exist (see exec.Error, fs.PathError).
   105  		metrics.ExecPluginCalls.Increment(failureExitCode, pluginNotFoundError)
   106  
   107  	default: // We don't know about this error type.
   108  		klog.V(2).InfoS("unexpected exec plugin return error type", "type", reflect.TypeOf(err).String(), "err", err)
   109  		metrics.ExecPluginCalls.Increment(failureExitCode, clientInternalError)
   110  	}
   111  }