diff --git a/go.mod b/go.mod index aceed0bb7..4f57ada90 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/openshift/api v0.0.0-20211209135129-c58d9f695577 github.com/openshift/build-machinery-go v0.0.0-20211213093930-7e33a7eb4ce3 github.com/openshift/client-go v0.0.0-20211209144617-7385dd6338e3 - github.com/openshift/library-go v0.0.0-20211214183058-58531ccbde67 + github.com/openshift/library-go v0.0.0-20211220195323-eca2c467c492 github.com/prometheus/client_golang v1.11.0 github.com/spf13/cobra v1.2.1 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index 6389e735c..60686c28f 100644 --- a/go.sum +++ b/go.sum @@ -72,6 +72,7 @@ github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= +github.com/RangelReale/osincli v0.0.0-20160924135400-fababb0555f2/go.mod h1:XyjUkMA8GN+tOOPXvnbi3XuRxWFvTJntqvTFnjmhzbk= github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -508,15 +509,14 @@ github.com/openshift/api v0.0.0-20211209135129-c58d9f695577 h1:NUe82M8wMYXbd5s+W github.com/openshift/api v0.0.0-20211209135129-c58d9f695577/go.mod h1:DoslCwtqUpr3d/gsbq4ZlkaMEdYqKxuypsDjorcHhME= github.com/openshift/build-machinery-go v0.0.0-20210423112049-9415d7ebd33e/go.mod h1:b1BuldmJlbA/xYtdZvKi+7j5YGB44qJUJDZ9zwiNCfE= github.com/openshift/build-machinery-go v0.0.0-20210712174854-1bb7fd1518d3/go.mod h1:b1BuldmJlbA/xYtdZvKi+7j5YGB44qJUJDZ9zwiNCfE= -github.com/openshift/build-machinery-go v0.0.0-20210806203541-4ea9b6da3a37/go.mod h1:b1BuldmJlbA/xYtdZvKi+7j5YGB44qJUJDZ9zwiNCfE= github.com/openshift/build-machinery-go v0.0.0-20211213093930-7e33a7eb4ce3 h1:65oBhJYHzYK5VL0gF1eiYY37lLzyLZ47b9y5Kib1nf8= github.com/openshift/build-machinery-go v0.0.0-20211213093930-7e33a7eb4ce3/go.mod h1:b1BuldmJlbA/xYtdZvKi+7j5YGB44qJUJDZ9zwiNCfE= github.com/openshift/client-go v0.0.0-20211209144617-7385dd6338e3 h1:SG1aqwleU6bGD0X4mhkTNupjVnByMYYuW4XbnCPavQU= github.com/openshift/client-go v0.0.0-20211209144617-7385dd6338e3/go.mod h1:cwhyki5lqBmrT0m8Im+9I7PGFaraOzcYPtEz93RcsGY= github.com/openshift/kube-storage-version-migrator v0.0.3-0.20210503105529-901a6d221d1c h1:+kJ9NXLQ24FIMt85B++O+XuDQi7mwM/JFq6xDQoJUkw= github.com/openshift/kube-storage-version-migrator v0.0.3-0.20210503105529-901a6d221d1c/go.mod h1:h1vYwmSiI2QF0w0M12CSOhCWTJY/ZifTDOqjKdUJtk0= -github.com/openshift/library-go v0.0.0-20211214183058-58531ccbde67 h1:wNd5jvgf9kXsyT+z11aBlh5spqKPNwsQKplrJRx4nsc= -github.com/openshift/library-go v0.0.0-20211214183058-58531ccbde67/go.mod h1:M/Gi/GUUrMdSS07nrYtTiK43J6/VUAyk/+IfN4ZqUY4= +github.com/openshift/library-go v0.0.0-20211220195323-eca2c467c492 h1:oj/rSQqVWVj6YJUydZwLz2frrJreiyI4oa9g/YPgMsM= +github.com/openshift/library-go v0.0.0-20211220195323-eca2c467c492/go.mod h1:4UQ9snU1vg53fyTpHQw3vLPiAxI8ub5xrc+y8KPQQFs= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= diff --git a/vendor/github.com/openshift/library-go/pkg/controller/controllercmd/cmd.go b/vendor/github.com/openshift/library-go/pkg/controller/controllercmd/cmd.go index 5196e350a..fb5bd2afb 100644 --- a/vendor/github.com/openshift/library-go/pkg/controller/controllercmd/cmd.go +++ b/vendor/github.com/openshift/library-go/pkg/controller/controllercmd/cmd.go @@ -28,8 +28,8 @@ import ( "github.com/openshift/library-go/pkg/operator/events" "github.com/openshift/library-go/pkg/serviceability" - // for metrics - _ "github.com/openshift/library-go/pkg/controller/metrics" + // load all the prometheus client-go metrics + _ "k8s.io/component-base/metrics/prometheus/clientgo" ) // ControllerCommandConfig holds values required to construct a command to run. diff --git a/vendor/github.com/openshift/library-go/pkg/controller/metrics/client_metrics.go b/vendor/github.com/openshift/library-go/pkg/controller/metrics/client_metrics.go deleted file mode 100644 index c36e2d3c8..000000000 --- a/vendor/github.com/openshift/library-go/pkg/controller/metrics/client_metrics.go +++ /dev/null @@ -1,97 +0,0 @@ -package metrics - -import ( - "net/url" - "time" - - "github.com/blang/semver" - k8smetrics "k8s.io/component-base/metrics" - "k8s.io/component-base/metrics/legacyregistry" - - "github.com/prometheus/client_golang/prometheus" -) - -var ( - // requestLatency is a Prometheus Summary metric type partitioned by - // "verb" and "url" labels. It is used for the rest client latency metrics. - requestLatency = k8smetrics.NewHistogramVec( - &k8smetrics.HistogramOpts{ - Name: "rest_client_request_latency_seconds", - Help: "Request latency in seconds. Broken down by verb and URL.", - Buckets: prometheus.ExponentialBuckets(0.001, 2, 10), - }, - []string{"verb", "url"}, - ) - - requestResult = k8smetrics.NewCounterVec( - &k8smetrics.CounterOpts{ - Name: "rest_client_requests_total", - Help: "Number of HTTP requests, partitioned by status code, method, and host.", - }, - []string{"code", "method", "host"}, - ) -) - -func init() { - legacyregistry.MustRegister(requestLatency) - legacyregistry.MustRegister(requestResult) - - legacyregistry.Register(&latencyAdapter{requestLatency}) - legacyregistry.Register(&resultAdapter{requestResult}) -} - -type latencyAdapter struct { - m *k8smetrics.HistogramVec -} - -func (l *latencyAdapter) Describe(c chan<- *prometheus.Desc) { - l.m.Describe(c) -} - -func (l *latencyAdapter) Collect(c chan<- prometheus.Metric) { - l.m.Collect(c) -} - -func (l *latencyAdapter) Create(version *semver.Version) bool { - return l.m.Create(version) -} - -func (l *latencyAdapter) Observe(verb string, u url.URL, latency time.Duration) { - l.m.WithLabelValues(verb, u.String()).Observe(latency.Seconds()) -} - -func (l *latencyAdapter) ClearState() { - l.m.Reset() -} - -func (l *latencyAdapter) FQName() string { - return l.m.FQName() -} - -type resultAdapter struct { - m *k8smetrics.CounterVec -} - -func (r *resultAdapter) Describe(c chan<- *prometheus.Desc) { - r.m.Describe(c) -} - -func (r *resultAdapter) Collect(c chan<- prometheus.Metric) { - r.m.Collect(c) -} - -func (r *resultAdapter) Create(version *semver.Version) bool { - return r.m.Create(version) -} - -func (r *resultAdapter) Increment(code, method, host string) { - r.m.WithLabelValues(code, method, host).Inc() -} - -func (r *resultAdapter) ClearState() { - r.m.Reset() -} - -func (r *resultAdapter) FQName() string { - return r.m.FQName() -} diff --git a/vendor/github.com/openshift/library-go/pkg/controller/metrics/workqueue_metrics.go b/vendor/github.com/openshift/library-go/pkg/controller/metrics/workqueue_metrics.go deleted file mode 100644 index 47002e74d..000000000 --- a/vendor/github.com/openshift/library-go/pkg/controller/metrics/workqueue_metrics.go +++ /dev/null @@ -1,210 +0,0 @@ -package metrics - -import ( - "k8s.io/client-go/util/workqueue" - k8smetrics "k8s.io/component-base/metrics" - "k8s.io/component-base/metrics/legacyregistry" - "k8s.io/klog/v2" - - "github.com/prometheus/client_golang/prometheus" -) - -// Package prometheus sets the workqueue DefaultMetricsFactory to produce -// prometheus metrics. To use this package, you just have to import it. - -func init() { - workqueue.SetProvider(prometheusMetricsProvider{}) -} - -// Package prometheus sets the workqueue DefaultMetricsFactory to produce -// prometheus metrics. To use this package, you just have to import it. - -// Metrics subsystem and keys used by the workqueue. -const ( - WorkQueueSubsystem = "workqueue" - DepthKey = "depth" - AddsKey = "adds_total" - QueueLatencyKey = "queue_duration_seconds" - WorkDurationKey = "work_duration_seconds" - UnfinishedWorkKey = "unfinished_work_seconds" - LongestRunningProcessorKey = "longest_running_processor_seconds" - RetriesKey = "retries_total" -) - -func init() { - workqueue.SetProvider(prometheusMetricsProvider{}) -} - -type prometheusMetricsProvider struct{} - -func (prometheusMetricsProvider) NewDepthMetric(name string) workqueue.GaugeMetric { - depth := k8smetrics.NewGauge(&k8smetrics.GaugeOpts{ - Subsystem: WorkQueueSubsystem, - Name: DepthKey, - Help: "Current depth of workqueue", - ConstLabels: prometheus.Labels{"name": name}, - }) - legacyregistry.Register(depth) - return depth -} - -func (prometheusMetricsProvider) NewAddsMetric(name string) workqueue.CounterMetric { - adds := k8smetrics.NewCounter(&k8smetrics.CounterOpts{ - Subsystem: WorkQueueSubsystem, - Name: AddsKey, - Help: "Total number of adds handled by workqueue", - ConstLabels: prometheus.Labels{"name": name}, - }) - legacyregistry.Register(adds) - return adds -} - -func (prometheusMetricsProvider) NewLatencyMetric(name string) workqueue.HistogramMetric { - latency := k8smetrics.NewHistogram(&k8smetrics.HistogramOpts{ - Subsystem: WorkQueueSubsystem, - Name: QueueLatencyKey, - Help: "How long in seconds an item stays in workqueue before being requested.", - ConstLabels: prometheus.Labels{"name": name}, - Buckets: prometheus.ExponentialBuckets(10e-9, 10, 10), - }) - legacyregistry.Register(latency) - return latency -} - -func (prometheusMetricsProvider) NewWorkDurationMetric(name string) workqueue.HistogramMetric { - workDuration := k8smetrics.NewHistogram(&k8smetrics.HistogramOpts{ - Subsystem: WorkQueueSubsystem, - Name: WorkDurationKey, - Help: "How long in seconds processing an item from workqueue takes.", - ConstLabels: prometheus.Labels{"name": name}, - Buckets: prometheus.ExponentialBuckets(10e-9, 10, 10), - }) - legacyregistry.Register(workDuration) - return workDuration -} - -func (prometheusMetricsProvider) NewUnfinishedWorkSecondsMetric(name string) workqueue.SettableGaugeMetric { - unfinished := k8smetrics.NewGauge(&k8smetrics.GaugeOpts{ - Subsystem: WorkQueueSubsystem, - Name: UnfinishedWorkKey, - Help: "How many seconds of work has done that " + - "is in progress and hasn't been observed by work_duration. Large " + - "values indicate stuck threads. One can deduce the number of stuck " + - "threads by observing the rate at which this increases.", - ConstLabels: prometheus.Labels{"name": name}, - }) - legacyregistry.Register(unfinished) - return unfinished -} - -func (prometheusMetricsProvider) NewLongestRunningProcessorSecondsMetric(name string) workqueue.SettableGaugeMetric { - unfinished := k8smetrics.NewGauge(&k8smetrics.GaugeOpts{ - Subsystem: WorkQueueSubsystem, - Name: LongestRunningProcessorKey, - Help: "How many seconds has the longest running " + - "processor for workqueue been running.", - ConstLabels: prometheus.Labels{"name": name}, - }) - legacyregistry.Register(unfinished) - return unfinished -} - -func (prometheusMetricsProvider) NewRetriesMetric(name string) workqueue.CounterMetric { - retries := k8smetrics.NewCounter(&k8smetrics.CounterOpts{ - Subsystem: WorkQueueSubsystem, - Name: RetriesKey, - Help: "Total number of retries handled by workqueue", - ConstLabels: prometheus.Labels{"name": name}, - }) - legacyregistry.Register(retries) - return retries -} - -// TODO(danielqsj): Remove the following metrics, they are deprecated -func (prometheusMetricsProvider) NewDeprecatedDepthMetric(name string) workqueue.GaugeMetric { - depth := k8smetrics.NewGauge(&k8smetrics.GaugeOpts{ - Subsystem: name, - Name: "depth", - Help: "(Deprecated) Current depth of workqueue: " + name, - }) - if err := legacyregistry.Register(depth); err != nil { - klog.Errorf("failed to register depth metric %v: %v", name, err) - } - return depth -} - -func (prometheusMetricsProvider) NewDeprecatedAddsMetric(name string) workqueue.CounterMetric { - adds := k8smetrics.NewCounter(&k8smetrics.CounterOpts{ - Subsystem: name, - Name: "adds", - Help: "(Deprecated) Total number of adds handled by workqueue: " + name, - }) - if err := legacyregistry.Register(adds); err != nil { - klog.Errorf("failed to register adds metric %v: %v", name, err) - } - return adds -} - -func (prometheusMetricsProvider) NewDeprecatedLatencyMetric(name string) workqueue.SummaryMetric { - latency := k8smetrics.NewSummary(&k8smetrics.SummaryOpts{ - Subsystem: name, - Name: "queue_latency", - Help: "(Deprecated) How long an item stays in workqueue" + name + " before being requested.", - }) - if err := legacyregistry.Register(latency); err != nil { - klog.Errorf("failed to register latency metric %v: %v", name, err) - } - return latency -} - -func (prometheusMetricsProvider) NewDeprecatedWorkDurationMetric(name string) workqueue.SummaryMetric { - workDuration := k8smetrics.NewSummary(&k8smetrics.SummaryOpts{ - Subsystem: name, - Name: "work_duration", - Help: "(Deprecated) How long processing an item from workqueue" + name + " takes.", - }) - if err := legacyregistry.Register(workDuration); err != nil { - klog.Errorf("failed to register work_duration metric %v: %v", name, err) - } - return workDuration -} - -func (prometheusMetricsProvider) NewDeprecatedUnfinishedWorkSecondsMetric(name string) workqueue.SettableGaugeMetric { - unfinished := k8smetrics.NewGauge(&k8smetrics.GaugeOpts{ - Subsystem: name, - Name: "unfinished_work_seconds", - Help: "(Deprecated) How many seconds of work " + name + " has done that " + - "is in progress and hasn't been observed by work_duration. Large " + - "values indicate stuck threads. One can deduce the number of stuck " + - "threads by observing the rate at which this increases.", - }) - if err := legacyregistry.Register(unfinished); err != nil { - klog.Errorf("failed to register unfinished_work_seconds metric %v: %v", name, err) - } - return unfinished -} - -func (prometheusMetricsProvider) NewDeprecatedLongestRunningProcessorMicrosecondsMetric(name string) workqueue.SettableGaugeMetric { - unfinished := k8smetrics.NewGauge(&k8smetrics.GaugeOpts{ - Subsystem: name, - Name: "longest_running_processor_microseconds", - Help: "(Deprecated) How many microseconds has the longest running " + - "processor for " + name + " been running.", - }) - if err := legacyregistry.Register(unfinished); err != nil { - klog.Errorf("failed to register longest_running_processor_microseconds metric %v: %v", name, err) - } - return unfinished -} - -func (prometheusMetricsProvider) NewDeprecatedRetriesMetric(name string) workqueue.CounterMetric { - retries := k8smetrics.NewCounter(&k8smetrics.CounterOpts{ - Subsystem: name, - Name: "retries", - Help: "(Deprecated) Total number of retries handled by workqueue: " + name, - }) - if err := legacyregistry.Register(retries); err != nil { - klog.Errorf("failed to register retries metric %v: %v", name, err) - } - return retries -} diff --git a/vendor/github.com/openshift/library-go/pkg/operator/resource/resourceapply/core.go b/vendor/github.com/openshift/library-go/pkg/operator/resource/resourceapply/core.go index 2f952f5c5..1ffee4f80 100644 --- a/vendor/github.com/openshift/library-go/pkg/operator/resource/resourceapply/core.go +++ b/vendor/github.com/openshift/library-go/pkg/operator/resource/resourceapply/core.go @@ -337,16 +337,10 @@ func ApplyConfigMapImproved(ctx context.Context, client coreclientv1.ConfigMapsG // ApplySecret merges objectmeta, requires data func ApplySecretImproved(ctx context.Context, client coreclientv1.SecretsGetter, recorder events.Recorder, requiredInput *corev1.Secret, cache ResourceCache) (*corev1.Secret, bool, error) { + // copy the stringData to data. Error on a data content conflict inside required. This is usually a bug. + existing, err := client.Secrets(requiredInput.Namespace).Get(ctx, requiredInput.Name, metav1.GetOptions{}) - if apierrors.IsNotFound(err) { - requiredCopy := requiredInput.DeepCopy() - actual, err := client.Secrets(requiredCopy.Namespace). - Create(ctx, resourcemerge.WithCleanLabelsAndAnnotations(requiredCopy).(*corev1.Secret), metav1.CreateOptions{}) - reportCreateEvent(recorder, requiredCopy, err) - cache.UpdateCachedResourceMetadata(requiredInput, actual) - return actual, true, err - } - if err != nil { + if err != nil && !apierrors.IsNotFound(err) { return nil, false, err } @@ -354,7 +348,6 @@ func ApplySecretImproved(ctx context.Context, client coreclientv1.SecretsGetter, return existing, false, nil } - // copy the stringData to data. Error on a data content conflict inside required. This is usually a bug. required := requiredInput.DeepCopy() if required.Data == nil { required.Data = map[string][]byte{} @@ -369,6 +362,18 @@ func ApplySecretImproved(ctx context.Context, client coreclientv1.SecretsGetter, } required.StringData = nil + if apierrors.IsNotFound(err) { + requiredCopy := required.DeepCopy() + actual, err := client.Secrets(requiredCopy.Namespace). + Create(ctx, resourcemerge.WithCleanLabelsAndAnnotations(requiredCopy).(*corev1.Secret), metav1.CreateOptions{}) + reportCreateEvent(recorder, requiredCopy, err) + cache.UpdateCachedResourceMetadata(requiredInput, actual) + return actual, true, err + } + if err != nil { + return nil, false, err + } + existingCopy := existing.DeepCopy() resourcemerge.EnsureObjectMeta(resourcemerge.BoolPtr(false), &existingCopy.ObjectMeta, required.ObjectMeta) @@ -397,7 +402,7 @@ func ApplySecretImproved(ctx context.Context, client coreclientv1.SecretsGetter, } if equality.Semantic.DeepEqual(existingCopy, existing) { - cache.UpdateCachedResourceMetadata(required, existingCopy) + cache.UpdateCachedResourceMetadata(requiredInput, existingCopy) return existing, false, nil } @@ -431,7 +436,7 @@ func ApplySecretImproved(ctx context.Context, client coreclientv1.SecretsGetter, existingCopy.ResourceVersion = "" actual, err = client.Secrets(required.Namespace).Create(ctx, existingCopy, metav1.CreateOptions{}) reportCreateEvent(recorder, existingCopy, err) - cache.UpdateCachedResourceMetadata(required, actual) + cache.UpdateCachedResourceMetadata(requiredInput, actual) return actual, true, err } diff --git a/vendor/github.com/openshift/library-go/pkg/operator/resource/resourceapply/resource_cache.go b/vendor/github.com/openshift/library-go/pkg/operator/resource/resourceapply/resource_cache.go index 2ff94536e..daa1a5e15 100644 --- a/vendor/github.com/openshift/library-go/pkg/operator/resource/resourceapply/resource_cache.go +++ b/vendor/github.com/openshift/library-go/pkg/operator/resource/resourceapply/resource_cache.go @@ -4,6 +4,7 @@ import ( "crypto/md5" "fmt" "io" + "reflect" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime" @@ -42,10 +43,16 @@ func NewResourceCache() *resourceCache { var noCache *resourceCache func getResourceMetadata(obj runtime.Object) (schema.GroupKind, string, string, string, error) { + if obj == nil { + return schema.GroupKind{}, "", "", "", fmt.Errorf("nil object has no metadata") + } metadata, err := meta.Accessor(obj) if err != nil { return schema.GroupKind{}, "", "", "", err } + if metadata == nil || reflect.ValueOf(metadata).IsNil() { + return schema.GroupKind{}, "", "", "", fmt.Errorf("object has no metadata") + } resourceHash := hashOfResourceStruct(obj) // retrieve kind, sometimes this can be done via the accesor, sometimes not (depends on the type) @@ -66,10 +73,16 @@ func getResourceMetadata(obj runtime.Object) (schema.GroupKind, string, string, } func getResourceVersion(obj runtime.Object) (string, error) { + if obj == nil { + return "", fmt.Errorf("nil object has no resourceVersion") + } metadata, err := meta.Accessor(obj) if err != nil { return "", err } + if metadata == nil || reflect.ValueOf(metadata).IsNil() { + return "", fmt.Errorf("object has no metadata") + } rv := metadata.GetResourceVersion() if len(rv) == 0 { return "", fmt.Errorf("missing resourceVersion") diff --git a/vendor/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection/metrics.go b/vendor/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection/metrics.go new file mode 100644 index 000000000..6592c755b --- /dev/null +++ b/vendor/k8s.io/component-base/metrics/prometheus/clientgo/leaderelection/metrics.go @@ -0,0 +1,53 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package leaderelection + +import ( + "k8s.io/client-go/tools/leaderelection" + k8smetrics "k8s.io/component-base/metrics" + "k8s.io/component-base/metrics/legacyregistry" +) + +var ( + leaderGauge = k8smetrics.NewGaugeVec(&k8smetrics.GaugeOpts{ + Name: "leader_election_master_status", + Help: "Gauge of if the reporting system is master of the relevant lease, 0 indicates backup, 1 indicates master. 'name' is the string used to identify the lease. Please make sure to group by name.", + }, []string{"name"}) +) + +func init() { + legacyregistry.MustRegister(leaderGauge) + leaderelection.SetProvider(prometheusMetricsProvider{}) +} + +type prometheusMetricsProvider struct{} + +func (prometheusMetricsProvider) NewLeaderMetric() leaderelection.SwitchMetric { + return &switchAdapter{gauge: leaderGauge} +} + +type switchAdapter struct { + gauge *k8smetrics.GaugeVec +} + +func (s *switchAdapter) On(name string) { + s.gauge.WithLabelValues(name).Set(1.0) +} + +func (s *switchAdapter) Off(name string) { + s.gauge.WithLabelValues(name).Set(0.0) +} diff --git a/vendor/k8s.io/component-base/metrics/prometheus/clientgo/metrics.go b/vendor/k8s.io/component-base/metrics/prometheus/clientgo/metrics.go new file mode 100644 index 000000000..43574ca9a --- /dev/null +++ b/vendor/k8s.io/component-base/metrics/prometheus/clientgo/metrics.go @@ -0,0 +1,23 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package clientgo + +import ( + _ "k8s.io/component-base/metrics/prometheus/clientgo/leaderelection" // load leaderelection metrics + _ "k8s.io/component-base/metrics/prometheus/restclient" // load restclient metrics + _ "k8s.io/component-base/metrics/prometheus/workqueue" // load the workqueue metrics +) diff --git a/vendor/k8s.io/component-base/metrics/prometheus/restclient/metrics.go b/vendor/k8s.io/component-base/metrics/prometheus/restclient/metrics.go new file mode 100644 index 000000000..21e4bb359 --- /dev/null +++ b/vendor/k8s.io/component-base/metrics/prometheus/restclient/metrics.go @@ -0,0 +1,176 @@ +/* +Copyright 2016 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package restclient + +import ( + "context" + "fmt" + "math" + "net/url" + "time" + + "k8s.io/client-go/tools/metrics" + k8smetrics "k8s.io/component-base/metrics" + "k8s.io/component-base/metrics/legacyregistry" +) + +var ( + // requestLatency is a Prometheus Summary metric type partitioned by + // "verb" and "url" labels. It is used for the rest client latency metrics. + requestLatency = k8smetrics.NewHistogramVec( + &k8smetrics.HistogramOpts{ + Name: "rest_client_request_duration_seconds", + Help: "Request latency in seconds. Broken down by verb and URL.", + Buckets: k8smetrics.ExponentialBuckets(0.001, 2, 10), + }, + []string{"verb", "url"}, + ) + + rateLimiterLatency = k8smetrics.NewHistogramVec( + &k8smetrics.HistogramOpts{ + Name: "rest_client_rate_limiter_duration_seconds", + Help: "Client side rate limiter latency in seconds. Broken down by verb and URL.", + Buckets: k8smetrics.ExponentialBuckets(0.001, 2, 10), + }, + []string{"verb", "url"}, + ) + + requestResult = k8smetrics.NewCounterVec( + &k8smetrics.CounterOpts{ + Name: "rest_client_requests_total", + Help: "Number of HTTP requests, partitioned by status code, method, and host.", + }, + []string{"code", "method", "host"}, + ) + + execPluginCertTTLAdapter = &expiryToTTLAdapter{} + + execPluginCertTTL = k8smetrics.NewGaugeFunc( + k8smetrics.GaugeOpts{ + Name: "rest_client_exec_plugin_ttl_seconds", + Help: "Gauge of the shortest TTL (time-to-live) of the client " + + "certificate(s) managed by the auth exec plugin. The value " + + "is in seconds until certificate expiry (negative if " + + "already expired). If auth exec plugins are unused or manage no " + + "TLS certificates, the value will be +INF.", + StabilityLevel: k8smetrics.ALPHA, + }, + func() float64 { + if execPluginCertTTLAdapter.e == nil { + return math.Inf(1) + } + return execPluginCertTTLAdapter.e.Sub(time.Now()).Seconds() + }, + ) + + execPluginCertRotation = k8smetrics.NewHistogram( + &k8smetrics.HistogramOpts{ + Name: "rest_client_exec_plugin_certificate_rotation_age", + Help: "Histogram of the number of seconds the last auth exec " + + "plugin client certificate lived before being rotated. " + + "If auth exec plugin client certificates are unused, " + + "histogram will contain no data.", + // There are three sets of ranges these buckets intend to capture: + // - 10-60 minutes: captures a rotation cadence which is + // happening too quickly. + // - 4 hours - 1 month: captures an ideal rotation cadence. + // - 3 months - 4 years: captures a rotation cadence which is + // is probably too slow or much too slow. + Buckets: []float64{ + 600, // 10 minutes + 1800, // 30 minutes + 3600, // 1 hour + 14400, // 4 hours + 86400, // 1 day + 604800, // 1 week + 2592000, // 1 month + 7776000, // 3 months + 15552000, // 6 months + 31104000, // 1 year + 124416000, // 4 years + }, + }, + ) + + execPluginCalls = k8smetrics.NewCounterVec( + &k8smetrics.CounterOpts{ + Name: "rest_client_exec_plugin_call_total", + Help: "Number of calls to an exec plugin, partitioned by the type of " + + "event encountered (no_error, plugin_execution_error, plugin_not_found_error, " + + "client_internal_error) and an optional exit code. The exit code will " + + "be set to 0 if and only if the plugin call was successful.", + }, + []string{"code", "call_status"}, + ) +) + +func init() { + + legacyregistry.MustRegister(requestLatency) + legacyregistry.MustRegister(rateLimiterLatency) + legacyregistry.MustRegister(requestResult) + legacyregistry.RawMustRegister(execPluginCertTTL) + legacyregistry.MustRegister(execPluginCertRotation) + metrics.Register(metrics.RegisterOpts{ + ClientCertExpiry: execPluginCertTTLAdapter, + ClientCertRotationAge: &rotationAdapter{m: execPluginCertRotation}, + RequestLatency: &latencyAdapter{m: requestLatency}, + RateLimiterLatency: &latencyAdapter{m: rateLimiterLatency}, + RequestResult: &resultAdapter{requestResult}, + ExecPluginCalls: &callsAdapter{m: execPluginCalls}, + }) +} + +type latencyAdapter struct { + m *k8smetrics.HistogramVec +} + +func (l *latencyAdapter) Observe(ctx context.Context, verb string, u url.URL, latency time.Duration) { + l.m.WithContext(ctx).WithLabelValues(verb, u.String()).Observe(latency.Seconds()) +} + +type resultAdapter struct { + m *k8smetrics.CounterVec +} + +func (r *resultAdapter) Increment(ctx context.Context, code, method, host string) { + r.m.WithContext(ctx).WithLabelValues(code, method, host).Inc() +} + +type expiryToTTLAdapter struct { + e *time.Time +} + +func (e *expiryToTTLAdapter) Set(expiry *time.Time) { + e.e = expiry +} + +type rotationAdapter struct { + m *k8smetrics.Histogram +} + +func (r *rotationAdapter) Observe(d time.Duration) { + r.m.Observe(d.Seconds()) +} + +type callsAdapter struct { + m *k8smetrics.CounterVec +} + +func (r *callsAdapter) Increment(code int, callStatus string) { + r.m.WithLabelValues(fmt.Sprintf("%d", code), callStatus).Inc() +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 5fe5e78aa..683f73366 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -214,7 +214,7 @@ github.com/openshift/client-go/config/informers/externalversions/config github.com/openshift/client-go/config/informers/externalversions/config/v1 github.com/openshift/client-go/config/informers/externalversions/internalinterfaces github.com/openshift/client-go/config/listers/config/v1 -# github.com/openshift/library-go v0.0.0-20211214183058-58531ccbde67 +# github.com/openshift/library-go v0.0.0-20211220195323-eca2c467c492 ## explicit; go 1.17 github.com/openshift/library-go/pkg/authorization/hardcodedauthorizer github.com/openshift/library-go/pkg/config/client @@ -226,7 +226,6 @@ github.com/openshift/library-go/pkg/config/serving github.com/openshift/library-go/pkg/controller/controllercmd github.com/openshift/library-go/pkg/controller/factory github.com/openshift/library-go/pkg/controller/fileobserver -github.com/openshift/library-go/pkg/controller/metrics github.com/openshift/library-go/pkg/crypto github.com/openshift/library-go/pkg/network github.com/openshift/library-go/pkg/operator/deploymentcontroller @@ -1029,6 +1028,9 @@ k8s.io/component-base/logs/registry k8s.io/component-base/logs/sanitization k8s.io/component-base/metrics k8s.io/component-base/metrics/legacyregistry +k8s.io/component-base/metrics/prometheus/clientgo +k8s.io/component-base/metrics/prometheus/clientgo/leaderelection +k8s.io/component-base/metrics/prometheus/restclient k8s.io/component-base/metrics/prometheus/workqueue k8s.io/component-base/metrics/testutil k8s.io/component-base/traces