From 9e580eb89e80f166244602030b6e34899228b171 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Tue, 11 Feb 2020 15:01:24 -0800 Subject: [PATCH] metrics: include upi installs in cluster_installer This is a follow-on to c1477328, which added the cluster_installer series originally. Recently, the installer started injecting a new ConfigMap (openshift-install-manifests), which includes information about the manifest generation stage of the install. Because this phase always happens, this ConfigMap is included in every cluster created by this newer installer. We can now look at the presence of this new ConfigMap and the absence of the openshift-install ConfigMap to determine that the cluster was installed using UPI. We can additionally look at its contents to determine information about the installer. --- pkg/cvo/metrics.go | 38 +++++++++++++++++------------ pkg/cvo/metrics_test.go | 50 ++++++++++++++++++++++++++++++++++++++- pkg/internal/constants.go | 1 + 3 files changed, 73 insertions(+), 16 deletions(-) diff --git a/pkg/cvo/metrics.go b/pkg/cvo/metrics.go index 50eaf6165b..db03328694 100644 --- a/pkg/cvo/metrics.go +++ b/pkg/cvo/metrics.go @@ -4,6 +4,7 @@ import ( "time" "github.com/prometheus/client_golang/prometheus" + corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/util/sets" @@ -286,21 +287,11 @@ func (m *operatorMetrics) Collect(ch chan<- prometheus.Metric) { ch <- g } - installer, err := m.optr.cmConfigLister.Get(internal.InstallerConfigMap) - if err == nil { - version := "" - invoker := "" - - if v, ok := installer.Data["version"]; ok { - version = v - } - if i, ok := installer.Data["invoker"]; ok { - invoker = i - } - - g := m.clusterInstaller.WithLabelValues("openshift-install", version, invoker) - g.Set(1.0) - ch <- g + if installer, err := m.optr.cmConfigLister.Get(internal.InstallerConfigMap); err == nil { + ch <- gaugeFromInstallConfigMap(installer, m.clusterInstaller, "openshift-install") + } else if !apierrors.IsNotFound(err) { + } else if manifests, err := m.optr.cmConfigLister.Get(internal.ManifestsConfigMap); err == nil { + ch <- gaugeFromInstallConfigMap(manifests, m.clusterInstaller, "other") } else if apierrors.IsNotFound(err) { g := m.clusterInstaller.WithLabelValues("", "", "") g.Set(1.0) @@ -308,6 +299,23 @@ func (m *operatorMetrics) Collect(ch chan<- prometheus.Metric) { } } +func gaugeFromInstallConfigMap(cm *corev1.ConfigMap, gauge *prometheus.GaugeVec, installType string) prometheus.Gauge { + version := "" + invoker := "" + + if v, ok := cm.Data["version"]; ok { + version = v + } + if i, ok := cm.Data["invoker"]; ok { + invoker = i + } + + g := gauge.WithLabelValues(installType, version, invoker) + g.Set(1.0) + + return g +} + // mostRecentTimestamp finds the most recent change recorded to the status and // returns the seconds since the epoch. func mostRecentTimestamp(cv *configv1.ClusterVersion) int64 { diff --git a/pkg/cvo/metrics_test.go b/pkg/cvo/metrics_test.go index 84f7e13972..b1a82d9a0a 100644 --- a/pkg/cvo/metrics_test.go +++ b/pkg/cvo/metrics_test.go @@ -404,7 +404,7 @@ func Test_operatorMetrics_Collect(t *testing.T) { }, }, { - name: "collects openshift-install info", + name: "collects legacy openshift-install info", optr: &Operator{ releaseVersion: "0.0.2", releaseImage: "test/image:1", @@ -424,6 +424,54 @@ func Test_operatorMetrics_Collect(t *testing.T) { expectMetric(t, metrics[1], 1, map[string]string{"type": "openshift-install", "version": "v0.0.2", "invoker": "jane"}) }, }, + { + name: "collects openshift-install info", + optr: &Operator{ + releaseVersion: "0.0.2", + releaseImage: "test/image:1", + releaseCreated: time.Unix(3, 0), + cmConfigLister: &cmConfigLister{ + Items: []*corev1.ConfigMap{ + { + ObjectMeta: metav1.ObjectMeta{Name: "openshift-install"}, + Data: map[string]string{"version": "v0.0.2", "invoker": "jane"}, + }, + { + ObjectMeta: metav1.ObjectMeta{Name: "openshift-install-manifests"}, + Data: map[string]string{"version": "v0.0.1", "invoker": "bill"}, + }, + }, + }, + }, + wants: func(t *testing.T, metrics []prometheus.Metric) { + if len(metrics) != 2 { + t.Fatalf("Unexpected metrics %s", spew.Sdump(metrics)) + } + expectMetric(t, metrics[0], 3, map[string]string{"type": "current", "version": "0.0.2", "image": "test/image:1"}) + expectMetric(t, metrics[1], 1, map[string]string{"type": "openshift-install", "version": "v0.0.2", "invoker": "jane"}) + }, + }, + { + name: "collects openshift-install-manifests info", + optr: &Operator{ + releaseVersion: "0.0.2", + releaseImage: "test/image:1", + releaseCreated: time.Unix(3, 0), + cmConfigLister: &cmConfigLister{ + Items: []*corev1.ConfigMap{{ + ObjectMeta: metav1.ObjectMeta{Name: "openshift-install-manifests"}, + Data: map[string]string{"version": "v0.0.1", "invoker": "bill"}, + }}, + }, + }, + wants: func(t *testing.T, metrics []prometheus.Metric) { + if len(metrics) != 2 { + t.Fatalf("Unexpected metrics %s", spew.Sdump(metrics)) + } + expectMetric(t, metrics[0], 3, map[string]string{"type": "current", "version": "0.0.2", "image": "test/image:1"}) + expectMetric(t, metrics[1], 1, map[string]string{"type": "other", "version": "v0.0.1", "invoker": "bill"}) + }, + }, { name: "collects empty openshift-install info", optr: &Operator{ diff --git a/pkg/internal/constants.go b/pkg/internal/constants.go index efc9b1cd91..df5f417bbe 100644 --- a/pkg/internal/constants.go +++ b/pkg/internal/constants.go @@ -3,4 +3,5 @@ package internal const ( ConfigNamespace = "openshift-config" InstallerConfigMap = "openshift-install" + ManifestsConfigMap = "openshift-install-manifests" )