From e98aba11d343a83dd3af687cf3c32476e018bb97 Mon Sep 17 00:00:00 2001 From: "jesus m. rodriguez" Date: Fri, 24 Jul 2020 11:32:40 -0400 Subject: [PATCH] replace instrumented handler with the one from operator-lib --- .../fragments/use-operator-lib-metrics.yaml | 19 +++++ go.mod | 2 +- go.sum | 4 +- pkg/ansible/controller/controller.go | 2 +- pkg/handler/instrumented_enqueue_object.go | 81 ------------------- pkg/handler/internal/metrics/metrics.go | 33 -------- pkg/helm/controller/controller.go | 2 +- 7 files changed, 24 insertions(+), 119 deletions(-) create mode 100644 changelog/fragments/use-operator-lib-metrics.yaml delete mode 100644 pkg/handler/instrumented_enqueue_object.go delete mode 100644 pkg/handler/internal/metrics/metrics.go diff --git a/changelog/fragments/use-operator-lib-metrics.yaml b/changelog/fragments/use-operator-lib-metrics.yaml new file mode 100644 index 0000000000..f8a96987a8 --- /dev/null +++ b/changelog/fragments/use-operator-lib-metrics.yaml @@ -0,0 +1,19 @@ +# entries is a list of entries to include in +# release notes and/or the migration guide +entries: + - description: > + Remove the implementation for `InstrumentedEnqueueRequestForObject` + handler from SDK repository and reference it from operator-lib instead. + + kind: "change" + + # Is this a breaking change? + breaking: true + + # Migration can be defined to automatically add a section to + # the migration guide. This is required for breaking changes. + migration: + header: Move the implementation of `InstrumentedEnqueueRequestForObject` handler + body: > + The implementation of `InstrumentedEnqueueRequestForObject` handler has been + removed from Operator SDK repository and is now imported from operator-lib. diff --git a/go.mod b/go.mod index 64924c78f4..ae7b31c329 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/onsi/ginkgo v1.12.1 github.com/onsi/gomega v1.10.1 github.com/operator-framework/api v0.3.8 - github.com/operator-framework/operator-lib v0.0.0-20200723212032-525cb9a9ed70 + github.com/operator-framework/operator-lib v0.0.0-20200724152139-f4e8074e89d3 github.com/operator-framework/operator-registry v1.12.6-0.20200611222234-275301b779f8 github.com/prometheus/client_golang v1.5.1 github.com/rogpeppe/go-internal v1.5.0 diff --git a/go.sum b/go.sum index 7421e96804..8aec80890b 100644 --- a/go.sum +++ b/go.sum @@ -586,8 +586,8 @@ github.com/operator-framework/api v0.3.7-0.20200602203552-431198de9fc2 h1:2KtDe3 github.com/operator-framework/api v0.3.7-0.20200602203552-431198de9fc2/go.mod h1:Xbje9x0SHmh0nihE21kpesB38vk3cyxnE6JdDS8Jo1Q= github.com/operator-framework/api v0.3.8 h1:tJykTCmwGKZBsPVTCfxbwz6nTF6dzmKydWJtC40erc8= github.com/operator-framework/api v0.3.8/go.mod h1:Xbje9x0SHmh0nihE21kpesB38vk3cyxnE6JdDS8Jo1Q= -github.com/operator-framework/operator-lib v0.0.0-20200723212032-525cb9a9ed70 h1:FU2KAQyEmH/Gx8Nh8bfs6qFpxg+t4Un777hMdR5/5sg= -github.com/operator-framework/operator-lib v0.0.0-20200723212032-525cb9a9ed70/go.mod h1:rgkZhbXdfdeZhjXWbl1lUzOfh3m7Fwj5DUYRihyf/w4= +github.com/operator-framework/operator-lib v0.0.0-20200724152139-f4e8074e89d3 h1:of4TVUV5Eg/xTDkoDKsERDKYrzY5nCy/e9QknvGyiF8= +github.com/operator-framework/operator-lib v0.0.0-20200724152139-f4e8074e89d3/go.mod h1:3xYWtjZXmIi9LikMmxqwLWjROJVVoil47sDJ1HD0pAI= github.com/operator-framework/operator-registry v1.12.6-0.20200611222234-275301b779f8 h1:F3zzxoBJJANdKMxmSOi5z/HWiVT+gwOdhROkEwDWD2M= github.com/operator-framework/operator-registry v1.12.6-0.20200611222234-275301b779f8/go.mod h1:loVINznYhgBIkmv83kU4yee88RS0BBk+hqOw9r4bhJk= github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k= diff --git a/pkg/ansible/controller/controller.go b/pkg/ansible/controller/controller.go index d414bc001d..a1210ca737 100644 --- a/pkg/ansible/controller/controller.go +++ b/pkg/ansible/controller/controller.go @@ -20,6 +20,7 @@ import ( "strings" "time" + "github.com/operator-framework/operator-lib/handler" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" @@ -31,7 +32,6 @@ import ( "github.com/operator-framework/operator-sdk/pkg/ansible/events" "github.com/operator-framework/operator-sdk/pkg/ansible/runner" - handler "github.com/operator-framework/operator-sdk/pkg/handler" "github.com/operator-framework/operator-sdk/pkg/predicate" ) diff --git a/pkg/handler/instrumented_enqueue_object.go b/pkg/handler/instrumented_enqueue_object.go deleted file mode 100644 index ae499d66c2..0000000000 --- a/pkg/handler/instrumented_enqueue_object.go +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2020 The Operator-SDK 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 handler - -import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/client-go/util/workqueue" - "sigs.k8s.io/controller-runtime/pkg/event" - "sigs.k8s.io/controller-runtime/pkg/handler" - - "github.com/operator-framework/operator-sdk/pkg/handler/internal/metrics" -) - -// InstrumentedEnqueueRequestForObject wraps controller-runtime handler for "EnqueueRequestForObject", and -// sets up primary resource metrics on event handlers. The main objective of this handler is to set -// prometheues metrics when create/update/delete events occur. These metrics contain below information on resource. -// -// resource_created_at_seconds{"name", "namespace", "group", "version", "kind"} -// -// '&handler.InstrumentedEnqueueRequestForObject{}' is used to call the handler. -type InstrumentedEnqueueRequestForObject struct { - handler.EnqueueRequestForObject -} - -// Create implements EventHandler, and creates the metrics. -func (h InstrumentedEnqueueRequestForObject) Create(e event.CreateEvent, q workqueue.RateLimitingInterface) { - setResourceMetric(e.Meta, e.Object) - h.EnqueueRequestForObject.Create(e, q) -} - -// Update implements EventHandler, and updates the metrics. -func (h InstrumentedEnqueueRequestForObject) Update(e event.UpdateEvent, q workqueue.RateLimitingInterface) { - setResourceMetric(e.MetaOld, e.ObjectOld) - setResourceMetric(e.MetaNew, e.ObjectNew) - - h.EnqueueRequestForObject.Update(e, q) -} - -// Delete implements EventHandler, and deletes metrics. -func (h InstrumentedEnqueueRequestForObject) Delete(e event.DeleteEvent, q workqueue.RateLimitingInterface) { - deleteResourceMetric(e.Meta, e.Object) - h.EnqueueRequestForObject.Delete(e, q) -} - -func setResourceMetric(metadata metav1.Object, obj runtime.Object) { - if metadata != nil && obj != nil { - labels := getResourceLabels(metadata, obj) - m, _ := metrics.ResourceCreatedAt.GetMetricWith(labels) - m.Set(float64(metadata.GetCreationTimestamp().UTC().Unix())) - } -} - -func deleteResourceMetric(metadata metav1.Object, obj runtime.Object) { - if metadata != nil && obj != nil { - labels := getResourceLabels(metadata, obj) - _ = metrics.ResourceCreatedAt.Delete(labels) - } -} - -func getResourceLabels(metadata metav1.Object, obj runtime.Object) map[string]string { - return map[string]string{ - "name": metadata.GetName(), - "namespace": metadata.GetNamespace(), - "group": obj.GetObjectKind().GroupVersionKind().Group, - "version": obj.GetObjectKind().GroupVersionKind().Version, - "kind": obj.GetObjectKind().GroupVersionKind().Kind, - } -} diff --git a/pkg/handler/internal/metrics/metrics.go b/pkg/handler/internal/metrics/metrics.go deleted file mode 100644 index 07d19ea8aa..0000000000 --- a/pkg/handler/internal/metrics/metrics.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2020 The Operator-SDK 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 metrics - -import ( - "github.com/prometheus/client_golang/prometheus" - "sigs.k8s.io/controller-runtime/pkg/metrics" -) - -// ResourceCreatedAt creates new prometheus metrics for primary resource, -// with information {"name", "namespace", "group", "version", "kind"} -var ResourceCreatedAt = prometheus.NewGaugeVec(prometheus.GaugeOpts{ - Name: "resource_created_at_seconds", - Help: "Timestamp at which a resource was created", -}, []string{"name", "namespace", "group", "version", "kind"}) - -func init() { - metrics.Registry.MustRegister( - ResourceCreatedAt, - ) -} diff --git a/pkg/helm/controller/controller.go b/pkg/helm/controller/controller.go index 8784e9a0c5..f28ae9a770 100644 --- a/pkg/helm/controller/controller.go +++ b/pkg/helm/controller/controller.go @@ -20,6 +20,7 @@ import ( "sync" "time" + "github.com/operator-framework/operator-lib/handler" rpb "helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/releaseutil" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -35,7 +36,6 @@ import ( libhandler "github.com/operator-framework/operator-lib/handler" "github.com/operator-framework/operator-lib/predicate" "github.com/operator-framework/operator-sdk/internal/util/k8sutil" - "github.com/operator-framework/operator-sdk/pkg/handler" "github.com/operator-framework/operator-sdk/pkg/helm/release" )