Skip to content

Commit 15ccf8a

Browse files
committed
fix: adjust usage of PrometheusSpec type
Problem: Embedded structs can as of yet not be directly referenced in struct literals. Solution: Explicitly use the embedded structs. Issues: golang/go#9859, prometheus-operator/prometheus-operator#4539 Signed-off-by: Jan Fajerski <[email protected]>
1 parent ee1c6ea commit 15ccf8a

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed

pkg/controllers/monitoring-stack/components.go

+44-39
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,53 @@ func newPrometheus(
123123
},
124124

125125
Spec: monv1.PrometheusSpec{
126-
Replicas: config.Replicas,
126+
CommonPrometheusFields: monv1.CommonPrometheusFields{
127+
Replicas: config.Replicas,
127128

128-
PodMetadata: &monv1.EmbeddedObjectMetadata{
129-
Labels: podLabels("prometheus", ms.Name),
130-
},
131-
132-
// Prometheus does not use an Enum for LogLevel, so need to convert to string
133-
LogLevel: string(ms.Spec.LogLevel),
134-
135-
Retention: ms.Spec.Retention,
136-
Resources: ms.Spec.Resources,
129+
PodMetadata: &monv1.EmbeddedObjectMetadata{
130+
Labels: podLabels("prometheus", ms.Name),
131+
},
137132

138-
ServiceAccountName: rbacResourceName,
133+
// Prometheus does not use an Enum for LogLevel, so need to convert to string
134+
LogLevel: string(ms.Spec.LogLevel),
135+
136+
Resources: ms.Spec.Resources,
137+
138+
ServiceAccountName: rbacResourceName,
139+
140+
ServiceMonitorSelector: prometheusSelector,
141+
ServiceMonitorNamespaceSelector: nil,
142+
PodMonitorSelector: prometheusSelector,
143+
PodMonitorNamespaceSelector: nil,
144+
Affinity: &corev1.Affinity{
145+
PodAntiAffinity: &corev1.PodAntiAffinity{
146+
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
147+
{
148+
TopologyKey: "kubernetes.io/hostname",
149+
LabelSelector: &metav1.LabelSelector{
150+
MatchLabels: podLabels("prometheus", ms.Name),
151+
},
152+
},
153+
},
154+
},
155+
},
139156

140-
ServiceMonitorSelector: prometheusSelector,
141-
PodMonitorSelector: prometheusSelector,
142-
RuleSelector: prometheusSelector,
157+
// Prometheus should be configured for self-scraping through a static job.
158+
// It avoids the need to synthesize a ServiceMonitor with labels that will match
159+
// what the user defines in the monitoring stacks's resourceSelector field.
160+
AdditionalScrapeConfigs: &corev1.SecretKeySelector{
161+
LocalObjectReference: corev1.LocalObjectReference{
162+
Name: additionalScrapeConfigsSecretName,
163+
},
164+
Key: AdditionalScrapeConfigsSelfScrapeKey,
165+
},
166+
Storage: storageForPVC(config.PersistentVolumeClaim),
167+
RemoteWrite: config.RemoteWrite,
168+
ExternalLabels: config.ExternalLabels,
169+
},
170+
Retention: ms.Spec.Retention,
171+
RuleSelector: prometheusSelector,
172+
RuleNamespaceSelector: nil,
143173

144174
Alerting: &monv1.AlertingSpec{
145175
Alertmanagers: []monv1.AlertmanagerEndpoints{
@@ -152,35 +182,10 @@ func newPrometheus(
152182
},
153183
},
154184
},
155-
Affinity: &corev1.Affinity{
156-
PodAntiAffinity: &corev1.PodAntiAffinity{
157-
RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{
158-
{
159-
TopologyKey: "kubernetes.io/hostname",
160-
LabelSelector: &metav1.LabelSelector{
161-
MatchLabels: podLabels("prometheus", ms.Name),
162-
},
163-
},
164-
},
165-
},
166-
},
167-
168-
// Prometheus should be configured for self-scraping through a static job.
169-
// It avoids the need to synthesize a ServiceMonitor with labels that will match
170-
// what the user defines in the monitoring stacks's resourceSelector field.
171-
AdditionalScrapeConfigs: &corev1.SecretKeySelector{
172-
LocalObjectReference: corev1.LocalObjectReference{
173-
Name: additionalScrapeConfigsSecretName,
174-
},
175-
Key: AdditionalScrapeConfigsSelfScrapeKey,
176-
},
177185
Thanos: &monv1.ThanosSpec{
178186
BaseImage: stringPtr("quay.io/thanos/thanos"),
179187
Version: stringPtr("v0.24.0"),
180188
},
181-
Storage: storageForPVC(config.PersistentVolumeClaim),
182-
RemoteWrite: config.RemoteWrite,
183-
ExternalLabels: config.ExternalLabels,
184189
},
185190
}
186191

test/e2e/monitoring_stack_controller_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,11 @@ func reconcileStack(t *testing.T) {
172172
f.GetResourceWithRetry(t, ms.Name, ms.Namespace, &generated)
173173

174174
expected := monv1.PrometheusSpec{
175-
LogLevel: string(ms.Spec.LogLevel),
176-
Retention: ms.Spec.Retention,
177-
ServiceMonitorSelector: ms.Spec.ResourceSelector.DeepCopy(),
175+
Retention: ms.Spec.Retention,
176+
CommonPrometheusFields: monv1.CommonPrometheusFields{
177+
LogLevel: string(ms.Spec.LogLevel),
178+
ServiceMonitorSelector: ms.Spec.ResourceSelector.DeepCopy(),
179+
},
178180
}
179181

180182
assert.DeepEqual(t, expected.ServiceMonitorSelector, generated.Spec.ServiceMonitorSelector)

0 commit comments

Comments
 (0)