metrics.go 4.0 KB

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