Skip to content

Commit d6907e0

Browse files
mprahlopenshift-merge-robot
authored andcommitted
Use the controller's namespace for ServiceMonitor definitions
Instead of defaulting to the `openshift-monitoring` namespace for OpenShift deployments, the ServiceMonitor is now defined in the controller namespace by default. When the namespace is created by the addon, the `openshift.io/cluster-monitoring: "true"` label is set on it to enable automatic scraping from the OpenShift platform Prometheus. Relates: https://issues.redhat.com/browse/ACM-7633 Signed-off-by: mprahl <[email protected]>
1 parent 91f6c2a commit d6907e0

File tree

10 files changed

+41
-38
lines changed

10 files changed

+41
-38
lines changed

pkg/addon/configpolicy/agent_addon.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@ func getValues(cluster *clusterv1.ManagedCluster,
103103

104104
// Enable Prometheus metrics by default on OpenShift
105105
userValues.Prometheus["enabled"] = userValues.KubernetesDistribution == "OpenShift"
106-
if userValues.KubernetesDistribution == "OpenShift" {
107-
userValues.Prometheus["serviceMonitor"] = map[string]interface{}{"namespace": "openshift-monitoring"}
108-
}
109106

110107
annotations := addon.GetAnnotations()
111108

pkg/addon/configpolicy/manifests/managedclusterchart/templates/install-namespace.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ metadata:
88
labels:
99
addon.open-cluster-management.io/namespace: "true"
1010
addon.open-cluster-management.io/hosted-manifest-location: hosting
11+
{{- if and .Values.prometheus.enabled (eq .Values.kubernetesDistribution "OpenShift") }}
12+
openshift.io/cluster-monitoring: "true"
13+
{{- end }}
1114
{{- if eq (.Release.Namespace | trimPrefix "klusterlet-") .Values.clusterName }}
1215
annotations:
1316
"addon.open-cluster-management.io/deletion-orphan": ""

pkg/addon/configpolicy/manifests/managedclusterchart/templates/service_monitor.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
apiVersion: monitoring.coreos.com/v1
55
kind: ServiceMonitor
66
metadata:
7-
name: ocm-{{ include "controller.fullname" . }}-{{ .Release.Namespace }}-metrics
7+
name: ocm-{{ include "controller.fullname" . }}-metrics
88
namespace: {{ .Values.prometheus.serviceMonitor.namespace | default .Release.Namespace }}
99
labels:
1010
app: {{ include "controller.fullname" . }}

pkg/addon/configpolicy/manifests/managedclusterchart/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ prometheus:
4444
# This will be automatically enabled if it's an OpenShift cluster.
4545
enabled: false
4646
serviceMonitor:
47-
# This will be automatically set to openshift-monitoring if it's an OpenShift cluster.
47+
# This will be automatically set to the controller's namespace.
4848
namespace: null
4949

5050
global:

pkg/addon/policyframework/agent_addon.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ func getValues(cluster *clusterv1.ManagedCluster,
133133

134134
// Enable Prometheus metrics by default on OpenShift
135135
userValues.Prometheus["enabled"] = userValues.KubernetesDistribution == "OpenShift"
136-
if userValues.KubernetesDistribution == "OpenShift" {
137-
userValues.Prometheus["serviceMonitor"] = map[string]interface{}{"namespace": "openshift-monitoring"}
138-
}
139136

140137
if val, ok := annotations[prometheusEnabledAnnotation]; ok {
141138
valBool, err := strconv.ParseBool(val)

pkg/addon/policyframework/manifests/managedclusterchart/templates/install-namespace.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ metadata:
88
labels:
99
addon.open-cluster-management.io/namespace: "true"
1010
addon.open-cluster-management.io/hosted-manifest-location: hosting
11+
{{- if and .Values.prometheus.enabled (eq .Values.kubernetesDistribution "OpenShift") }}
12+
openshift.io/cluster-monitoring: "true"
13+
{{- end }}
1114
{{- if eq (.Release.Namespace | trimPrefix "klusterlet-") .Values.clusterName }}
1215
annotations:
1316
"addon.open-cluster-management.io/deletion-orphan": ""

pkg/addon/policyframework/manifests/managedclusterchart/templates/service_monitor.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
apiVersion: monitoring.coreos.com/v1
55
kind: ServiceMonitor
66
metadata:
7-
name: ocm-{{ include "controller.fullname" . }}-{{ .Release.Namespace }}-metrics
7+
name: ocm-{{ include "controller.fullname" . }}-metrics
88
namespace: {{ .Values.prometheus.serviceMonitor.namespace | default .Release.Namespace }}
99
labels:
1010
app: {{ include "controller.fullname" . }}

pkg/addon/policyframework/manifests/managedclusterchart/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ prometheus:
4444
# This will be automatically enabled if it's an OpenShift cluster.
4545
enabled: false
4646
serviceMonitor:
47-
# This will be automatically set to openshift-monitoring if it's an OpenShift cluster.
47+
# This will be automatically set to the controller's namespace.
4848
namespace: null
4949

5050
global:

test/e2e/case2_config_deployment_test.go

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ import (
1616
)
1717

1818
const (
19-
case2ManagedClusterAddOnName string = "config-policy-controller"
20-
case2ManagedClusterAddOnCR string = "../resources/config_policy_addon_cr.yaml"
21-
case2ClusterManagementAddOnCR string = "../resources/config_policy_clustermanagementaddon.yaml"
22-
case2DeploymentName string = "config-policy-controller"
23-
case2PodSelector string = "app=config-policy-controller"
24-
case2OpenShiftClusterClaim string = "../resources/openshift_cluster_claim.yaml"
25-
policyCrdName string = "policies.policy.open-cluster-management.io"
26-
deletionOrphanAnnotationKey string = "addon.open-cluster-management.io/deletion-orphan"
19+
case2ManagedClusterAddOnName string = "config-policy-controller"
20+
case2ManagedClusterAddOnCR string = "../resources/config_policy_addon_cr.yaml"
21+
case2ManagedClusterCustomNsAddOnCR string = "../resources/config_policy_addon_custom_ns.yaml"
22+
case2ClusterManagementAddOnCR string = "../resources/config_policy_clustermanagementaddon.yaml"
23+
case2DeploymentName string = "config-policy-controller"
24+
case2PodSelector string = "app=config-policy-controller"
25+
case2OpenShiftClusterClaim string = "../resources/openshift_cluster_claim.yaml"
26+
policyCrdName string = "policies.policy.open-cluster-management.io"
27+
deletionOrphanAnnotationKey string = "addon.open-cluster-management.io/deletion-orphan"
2728
)
2829

2930
func verifyConfigPolicyDeployment(
@@ -369,7 +370,7 @@ var _ = Describe("Test config-policy-controller deployment", func() {
369370
By(logPrefix + "verifying that the metrics ServiceMonitor exists")
370371
Eventually(func(g Gomega) {
371372
sm, err := cluster.clusterClient.Resource(gvrServiceMonitor).Namespace(addonNamespace).Get(
372-
context.TODO(), "ocm-config-policy-controller-"+addonNamespace+"-metrics", metav1.GetOptions{},
373+
context.TODO(), "ocm-config-policy-controller-metrics", metav1.GetOptions{},
373374
)
374375
g.Expect(err).ToNot(HaveOccurred())
375376

@@ -405,18 +406,12 @@ var _ = Describe("Test config-policy-controller deployment", func() {
405406
It("should create a config-policy-controller deployment with metrics monitoring on OpenShift clusters", func() {
406407
Expect(managedClusterList).ToNot(BeEmpty())
407408
hubClient := managedClusterList[0].clusterClient
409+
// A custom namespace is used to see if the openshift.io/cluster-monitoring label is set.
410+
addonNamespace := "e2e-test-config-policy-controller"
408411

409412
for i, cluster := range managedClusterList {
410413
logPrefix := cluster.clusterType + " " + cluster.clusterName + ": "
411414

412-
By(logPrefix + "creating the openshift-monitoring namespace")
413-
Kubectl(
414-
"create",
415-
"namespace",
416-
"openshift-monitoring",
417-
fmt.Sprintf("--kubeconfig=%s%d.kubeconfig", kubeconfigFilename, i+1),
418-
)
419-
420415
By(logPrefix + "setting the product.open-cluster-management.io ClusterClaim to OpenShift")
421416
Kubectl(
422417
"apply",
@@ -451,7 +446,7 @@ var _ = Describe("Test config-policy-controller deployment", func() {
451446
// The status doesn't need to be checked on the deployment because the deployment requires a cert that
452447
// is auto-generated by OpenShift, which won't be present.
453448
By(logPrefix + "deploying the default config-policy-controller managedclusteraddon")
454-
Kubectl("apply", "-n", cluster.clusterName, "-f", case2ManagedClusterAddOnCR)
449+
Kubectl("apply", "-n", cluster.clusterName, "-f", case2ManagedClusterCustomNsAddOnCR)
455450
deploy := GetWithTimeout(
456451
cluster.clusterClient, gvrDeployment, case2DeploymentName, addonNamespace, true, 30,
457452
)
@@ -464,8 +459,8 @@ var _ = Describe("Test config-policy-controller deployment", func() {
464459

465460
By(logPrefix + "verifying that the metrics ServiceMonitor exists")
466461
Eventually(func(g Gomega) {
467-
sm, err := cluster.clusterClient.Resource(gvrServiceMonitor).Namespace("openshift-monitoring").Get(
468-
context.TODO(), "ocm-config-policy-controller-"+addonNamespace+"-metrics", metav1.GetOptions{},
462+
sm, err := cluster.clusterClient.Resource(gvrServiceMonitor).Namespace(addonNamespace).Get(
463+
context.TODO(), "ocm-config-policy-controller-metrics", metav1.GetOptions{},
469464
)
470465
g.Expect(err).ToNot(HaveOccurred())
471466

@@ -488,6 +483,16 @@ var _ = Describe("Test config-policy-controller deployment", func() {
488483
g.Expect(port["targetPort"].(int64)).To(Equal(int64(8443)))
489484
}, 120, 3).Should(Succeed())
490485

486+
By(logPrefix + "verifying that the addon namespace has the openshift.io/cluster-monitoring label set")
487+
Eventually(func(g Gomega) {
488+
ns, err := cluster.clusterClient.Resource(gvrNamespace).Get(
489+
context.TODO(), addonNamespace, metav1.GetOptions{},
490+
)
491+
g.Expect(err).ToNot(HaveOccurred())
492+
493+
g.Expect(ns.GetLabels()["openshift.io/cluster-monitoring"]).To(Equal("true"))
494+
}, 30, 3).Should(Succeed())
495+
491496
By(logPrefix + "cleaning up")
492497
Kubectl(
493498
"delete",
@@ -503,14 +508,6 @@ var _ = Describe("Test config-policy-controller deployment", func() {
503508
)
504509
Expect(deploy).To(BeNil())
505510

506-
Kubectl(
507-
"delete",
508-
"namespace",
509-
"openshift-monitoring",
510-
fmt.Sprintf("--kubeconfig=%s%d.kubeconfig", kubeconfigFilename, i+1),
511-
"--timeout=15s",
512-
)
513-
514511
By(logPrefix + "waiting for the ClusterClaim to not be in the ManagedCluster status")
515512
Eventually(func(g Gomega) {
516513
managedCluster, err := hubClient.Resource(gvrManagedCluster).Get(
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: addon.open-cluster-management.io/v1alpha1
2+
kind: ManagedClusterAddOn
3+
metadata:
4+
name: config-policy-controller
5+
spec:
6+
installNamespace: e2e-test-config-policy-controller

0 commit comments

Comments
 (0)