Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit c4f22d2

Browse files
author
Rodrigo Valin
authored
Accepts metrics.prometheus endpoint. (#888)
1 parent d94c20a commit c4f22d2

File tree

16 files changed

+315
-6
lines changed

16 files changed

+315
-6
lines changed

.action_templates/jobs/tests.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ tests:
2323
distro: ubi
2424
- test-name: feature_compatibility_version_upgrade
2525
distro: ubi
26+
- test-name: prometheus
27+
distro: ubi
2628
- test-name: replica_set_tls
2729
distro: ubi
2830
- test-name: replica_set_tls_pem_file

.github/workflows/e2e-fork.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ jobs:
105105
distro: ubi
106106
- test-name: feature_compatibility_version_upgrade
107107
distro: ubi
108+
- test-name: prometheus
109+
distro: ubi
108110
- test-name: replica_set_tls
109111
distro: ubi
110112
- test-name: replica_set_tls_pem_file

.github/workflows/e2e.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ jobs:
111111
distro: ubi
112112
- test-name: feature_compatibility_version_upgrade
113113
distro: ubi
114+
- test-name: prometheus
115+
distro: ubi
114116
- test-name: replica_set_tls
115117
distro: ubi
116118
- test-name: replica_set_tls_pem_file

api/v1/mongodbcommunity_types.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ type MongoDBCommunitySpec struct {
106106
// AutomationConfigOverride is merged on top of the operator created automation config. Processes are merged
107107
// by name. Currently Only the process.disabled field is supported.
108108
AutomationConfigOverride *AutomationConfigOverride `json:"automationConfig,omitempty"`
109+
110+
// Prometheus configurations.
111+
// +optional
112+
Prometheus *Prometheus `json:"prometheus,omitempty"`
109113
}
110114

111115
// ReplicaSetHorizonConfiguration holds the split horizon DNS settings for
@@ -128,6 +132,30 @@ type CustomRole struct {
128132
AuthenticationRestrictions []AuthenticationRestriction `json:"authenticationRestrictions,omitempty"`
129133
}
130134

135+
type Prometheus struct {
136+
// Port where metrics endpoint will bind to. Defaults to 9216.
137+
// +optional
138+
Port int `json:"port,omitempty"`
139+
140+
// HTTP Basic Auth Username for metrics endpoint.
141+
Username string `json:"username"`
142+
143+
// Name of a Secret containing a HTTP Basic Auth Password.
144+
PasswordSecretRef SecretKeyReference `json:"passwordSecretRef"`
145+
146+
// Indicates path to the metrics endpoint.
147+
// +kubebuilder:validation:Pattern=^\/[a-z0-9]+$
148+
MetricsPath string `json:"metricsPath,omitempty"`
149+
}
150+
151+
func (p Prometheus) GetPasswordKey() string {
152+
if p.PasswordSecretRef.Key != "" {
153+
return p.PasswordSecretRef.Key
154+
}
155+
156+
return "password"
157+
}
158+
131159
// ConvertToAutomationConfigCustomRole converts between a custom role defined by the crd and a custom role
132160
// that can be used in the automation config.
133161
func (c CustomRole) ConvertToAutomationConfigCustomRole() automationconfig.CustomRole {

config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,38 @@ spec:
8888
members:
8989
description: Members is the number of members in the replica set
9090
type: integer
91+
prometheus:
92+
description: Prometheus configurations.
93+
properties:
94+
metricsPath:
95+
description: Indicates path to the metrics endpoint.
96+
pattern: ^\/[a-z0-9]+$
97+
type: string
98+
passwordSecretRef:
99+
description: Name of a Secret containing a HTTP Basic Auth Password.
100+
properties:
101+
key:
102+
description: Key is the key in the secret storing this password.
103+
Defaults to "password"
104+
type: string
105+
name:
106+
description: Name is the name of the secret storing this user's
107+
password
108+
type: string
109+
required:
110+
- name
111+
type: object
112+
port:
113+
description: Port where metrics endpoint will bind to. Defaults
114+
to 9216.
115+
type: integer
116+
username:
117+
description: HTTP Basic Auth Username for metrics endpoint.
118+
type: string
119+
required:
120+
- passwordSecretRef
121+
- username
122+
type: object
91123
replicaSetHorizons:
92124
description: ReplicaSetHorizons Add this parameter and values if you
93125
need your database to be accessed outside of Kubernetes. This setting
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
apiVersion: mongodbcommunity.mongodb.com/v1
3+
kind: MongoDBCommunity
4+
metadata:
5+
name: example-prometheus
6+
spec:
7+
members: 3
8+
type: ReplicaSet
9+
version: "5.0.6"
10+
11+
# You can expose metrics for Prometheus polling using the
12+
# `prometheus` entry.
13+
prometheus:
14+
# Metrics endpoint HTTP Basic Auth username
15+
username: <username>
16+
17+
# Metrics endpoint HTTP Basic Auth password
18+
passwordSecretRef:
19+
name: metrics-endpoint-password
20+
21+
# Optional, defaults to `/metrics`
22+
# metricsPath: /metrics
23+
24+
# Optional defaults to 9216
25+
# port: 9216
26+
27+
security:
28+
authentication:
29+
modes: ["SCRAM"]
30+
31+
users:
32+
- name: my-user
33+
db: admin
34+
passwordSecretRef:
35+
name: my-user-password
36+
roles:
37+
- name: clusterAdmin
38+
db: admin
39+
- name: userAdminAnyDatabase
40+
db: admin
41+
scramCredentialsSecretName: my-scram
42+
43+
# the user credentials will be generated from this secret
44+
# once the credentials are generated, this secret is no longer required
45+
---
46+
apiVersion: v1
47+
kind: Secret
48+
metadata:
49+
name: my-user-password
50+
type: Opaque
51+
stringData:
52+
password: <your-user-password>
53+
54+
# Secret holding the prometheus metrics endpoint HTTP Password.
55+
---
56+
apiVersion: v1
57+
kind: Secret
58+
metadata:
59+
name: metrics-endpoint-password
60+
type: Opaque
61+
stringData:
62+
password: <your-metrics-endpoint-password>

controllers/replica_set_controller.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"sigs.k8s.io/controller-runtime/pkg/source"
1616

1717
"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/container"
18+
"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/secret"
19+
"github.com/mongodb/mongodb-kubernetes-operator/pkg/monitoring"
1820

1921
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/functions"
2022
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/merge"
@@ -578,12 +580,30 @@ func (r ReplicaSetReconciler) buildAutomationConfig(mdb mdbv1.MongoDBCommunity)
578580
return automationconfig.AutomationConfig{}, errors.Errorf("could not configure scram authentication: %s", err)
579581
}
580582

583+
prometheusModification := automationconfig.NOOP()
584+
if mdb.Spec.Prometheus != nil {
585+
secretNamespacedName := types.NamespacedName{Name: mdb.Spec.Prometheus.PasswordSecretRef.Name, Namespace: mdb.Namespace}
586+
r.secretWatcher.Watch(secretNamespacedName, mdb.NamespacedName())
587+
588+
password, err := secret.ReadKey(r.client, mdb.Spec.Prometheus.GetPasswordKey(), secretNamespacedName)
589+
if err != nil {
590+
if apiErrors.IsNotFound(err) {
591+
r.log.Infof("Could not read Secret %s. Prometheus will not be configured during this reconciliation, %s", mdb.Spec.Prometheus.PasswordSecretRef.Name, err)
592+
} else {
593+
return automationconfig.AutomationConfig{}, errors.Errorf("could not configure Prometheus modification: %s", err)
594+
}
595+
} else {
596+
prometheusModification = monitoring.PrometheusModification(mdb, password)
597+
}
598+
}
599+
581600
automationConfig, err := buildAutomationConfig(
582601
mdb,
583602
auth,
584603
currentAC,
585604
tlsModification,
586605
customRolesModification,
606+
prometheusModification,
587607
)
588608
if err != nil {
589609
return automationconfig.AutomationConfig{}, errors.Errorf("could not create an automation config: %s", err)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
)
2424

2525
require (
26+
cloud.google.com/go v0.54.0 // indirect
2627
github.com/beorn7/perks v1.0.1 // indirect
2728
github.com/cespare/xxhash/v2 v2.1.1 // indirect
2829
github.com/davecgh/go-spew v1.1.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg
88
cloud.google.com/go v0.50.0/go.mod h1:r9sluTvynVuxRIOHXQEHMFffphuXHOMZMycpNR5e6To=
99
cloud.google.com/go v0.52.0/go.mod h1:pXajvRH/6o3+F9jDHZWQ5PbGhn+o8w9qiu/CffaVdO4=
1010
cloud.google.com/go v0.53.0/go.mod h1:fp/UouUEsRkN6ryDKNW/Upv/JBKnv6WDthjR6+vze6M=
11+
cloud.google.com/go v0.54.0 h1:3ithwDMr7/3vpAMXiH+ZQnYbuIsh+OPhUPMFC9enmn0=
1112
cloud.google.com/go v0.54.0/go.mod h1:1rq2OEkV3YMf6n/9ZvGWI3GWw0VoqH/1x2nd8Is/bPc=
1213
cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o=
1314
cloud.google.com/go/bigquery v1.3.0/go.mod h1:PjpwJnslEMmckchkHFfq+HTD2DmtT67aNFKH1/VBDHE=
@@ -661,6 +662,7 @@ golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roY
661662
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
662663
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
663664
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
665+
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
664666
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
665667
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
666668
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

pkg/automationconfig/automation_config.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type AutomationConfig struct {
2020
Processes []Process `json:"processes"`
2121
ReplicaSets []ReplicaSet `json:"replicaSets"`
2222
Auth Auth `json:"auth"`
23+
Prometheus *Prometheus `json:"prometheus,omitempty"`
2324

2425
// TLSConfig and SSLConfig exist to allow configuration of older agents which accept the "ssl" field rather or "tls"
2526
// only one of these should be set.
@@ -185,6 +186,31 @@ type Auth struct {
185186
AutoPwd string `json:"autoPwd,omitempty"`
186187
}
187188

189+
type Prometheus struct {
190+
Enabled bool `json:"enabled"`
191+
Username string `json:"username"`
192+
Password string `json:"password"`
193+
Scheme string `json:"scheme"`
194+
TLSPemPath string `json:"tlsPemPath"`
195+
TLSPemPassword string `json:"tlsPemPassword"`
196+
Mode string `json:"mode"`
197+
ListenAddress string `json:"listenAddress"`
198+
MetricsPath string `json:"metricsPath"`
199+
ServiceDiscovery string `json:"serviceDiscovery"`
200+
}
201+
202+
func NewDefaultPrometheus(username string) Prometheus {
203+
return Prometheus{
204+
Enabled: true,
205+
Username: username,
206+
Scheme: "http",
207+
Mode: "opsManager",
208+
ListenAddress: "0.0.0.0:9216",
209+
MetricsPath: "/metrics",
210+
ServiceDiscovery: "file",
211+
}
212+
}
213+
188214
type CustomRole struct {
189215
Role string `json:"role"`
190216
DB string `json:"db"`

0 commit comments

Comments
 (0)