Skip to content

Commit 916746b

Browse files
Merge pull request #2192 from openshift-cherrypick-robot/cherry-pick-2165-to-19.0-beta
[19.0-beta] Configure MCP server Prometheus/metric-storage connection
2 parents 8957dcb + 3869ea4 commit 916746b

3 files changed

Lines changed: 82 additions & 6 deletions

File tree

‎internal/controller/client/openstackclient_controller.go‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,18 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ
324324
}, metricStorage)
325325
if err == nil {
326326
configVars["PrometheusTls"] = env.SetValue(fmt.Sprint(metricStorage.Spec.PrometheusTLS.Enabled()))
327+
} else if k8s_errors.IsNotFound(err) {
328+
// MetricStorage is optional; clear the object so MCP config does not
329+
// point at a Prometheus service that is not present.
330+
metricStorage = nil
331+
} else {
332+
instance.Status.Conditions.Set(condition.FalseCondition(
333+
clientv1.OpenStackClientReadyCondition,
334+
condition.ErrorReason,
335+
condition.SeverityWarning,
336+
clientv1.OpenStackClientReadyErrorMessage,
337+
err.Error()))
338+
return ctrl.Result{}, err
327339
}
328340

329341
// all cert input checks out so report InputReady
@@ -370,7 +382,13 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ
370382
configVars["mcp-tls"] = env.SetValue(certHash)
371383
}
372384

373-
mcpConfigYAML := openstackclient.MCPConfigYAML(instance.Name, instance.Namespace, instance.Spec.CaBundleSecretName, mcpTLSSvc != nil)
385+
mcpConfigYAML := openstackclient.MCPConfigYAML(
386+
instance.Name,
387+
instance.Namespace,
388+
instance.Spec.CaBundleSecretName,
389+
mcpTLSSvc != nil,
390+
metricStorage,
391+
)
374392

375393
mcpCloudsYAML := openstackclient.MCPCloudsYAML(
376394
internalAuthURL,

‎internal/openstackclient/funcs.go‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,33 @@ func ClientPodSpec(
189189
// name and namespace identify the owning OpenStackClient and are used to scope
190190
// mcp_transport_security to the MCP Service's own hostname (<name>-mcp.<namespace>.svc:8080),
191191
// matching the URL OpenStackAssistant uses to reach it, instead of allowing any host/origin.
192-
func MCPConfigYAML(name, namespace, caBundleSecretName string, mcpTLSEnabled bool) string {
192+
// When MetricStorage is present, its Prometheus endpoint is configured explicitly
193+
// for RHOSO versions where it is not registered in the Keystone catalog.
194+
func MCPConfigYAML(
195+
name, namespace, caBundleSecretName string,
196+
mcpTLSEnabled bool,
197+
metricStorage *telemetryv1.MetricStorage,
198+
) string {
193199
caCert := ""
194200
if caBundleSecretName != "" {
195201
caCert = fmt.Sprintf("\n ca_cert: %s", tls.DownstreamTLSCABundlePath)
196202
}
203+
prometheusConfig := ""
204+
if metricStorage != nil {
205+
prometheusCACert := ""
206+
if metricStorage.Spec.PrometheusTLS.Enabled() {
207+
prometheusCACert = fmt.Sprintf("\n ca_cert: %s", tls.DownstreamTLSCABundlePath)
208+
}
209+
prometheusConfig = fmt.Sprintf(`
210+
prometheus:
211+
host: %s-prometheus.%s.svc
212+
port: %d%s`,
213+
telemetryv1.DefaultServiceName,
214+
namespace,
215+
telemetryv1.DefaultPrometheusPort,
216+
prometheusCACert,
217+
)
218+
}
197219
mcpTLS := ""
198220
if mcpTLSEnabled {
199221
mcpTLS = `
@@ -211,7 +233,7 @@ tls:
211233
port: 8080
212234
openstack:
213235
enabled: true
214-
allow_write: false%s
236+
allow_write: false%s%s
215237
openshift:
216238
enabled: false
217239
mcp_transport_security:
@@ -220,7 +242,7 @@ mcp_transport_security:
220242
- "%s"
221243
allowed_origins:
222244
%s%s
223-
`, caCert, mcpHost, allowedOrigins, mcpTLS)
245+
`, caCert, prometheusConfig, mcpHost, allowedOrigins, mcpTLS)
224246
}
225247

226248
// MCPCloudsYAML returns a clouds.yaml using the given auth URL for the MCP sidecar.

‎internal/openstackclient/funcs_test.go‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ import (
1919

2020
"github.com/onsi/gomega"
2121
"github.com/openstack-k8s-operators/lib-common/modules/common/tls"
22+
telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1"
23+
"k8s.io/utils/ptr"
2224
)
2325

2426
func TestMCPConfigYAMLTLSIsIndependentFromCABundle(t *testing.T) {
2527
t.Run("CA bundle configures only downstream trust", func(t *testing.T) {
2628
g := gomega.NewWithT(t)
27-
config := MCPConfigYAML("openstackclient", "openstack", "combined-ca-bundle", false)
29+
config := MCPConfigYAML("openstackclient", "openstack", "combined-ca-bundle", false, nil)
2830

2931
g.Expect(config).To(gomega.ContainSubstring("ca_cert: " + tls.DownstreamTLSCABundlePath))
3032
g.Expect(config).NotTo(gomega.ContainSubstring("\ntls:\n"))
@@ -33,11 +35,45 @@ func TestMCPConfigYAMLTLSIsIndependentFromCABundle(t *testing.T) {
3335

3436
t.Run("MCP TLS does not require a downstream CA bundle", func(t *testing.T) {
3537
g := gomega.NewWithT(t)
36-
config := MCPConfigYAML("openstackclient", "openstack", "", true)
38+
config := MCPConfigYAML("openstackclient", "openstack", "", true, nil)
3739

3840
g.Expect(config).NotTo(gomega.ContainSubstring("ca_cert:"))
3941
g.Expect(config).To(gomega.ContainSubstring("\ntls:\n"))
4042
g.Expect(config).To(gomega.ContainSubstring("ssl_certfile: /etc/pki/tls/mcp/tls.crt"))
4143
g.Expect(config).To(gomega.ContainSubstring("https://openstackclient-mcp.openstack.svc:8080"))
4244
})
4345
}
46+
47+
func TestMCPConfigYAMLPrometheus(t *testing.T) {
48+
t.Run("no MetricStorage omits the prometheus section", func(t *testing.T) {
49+
g := gomega.NewWithT(t)
50+
config := MCPConfigYAML("openstackclient", "openstack", "", false, nil)
51+
52+
g.Expect(config).NotTo(gomega.ContainSubstring("prometheus:"))
53+
})
54+
55+
t.Run("MetricStorage without TLS configures host/port but no ca_cert", func(t *testing.T) {
56+
g := gomega.NewWithT(t)
57+
metricStorage := &telemetryv1.MetricStorage{}
58+
config := MCPConfigYAML("openstackclient", "openstack", "", false, metricStorage)
59+
60+
g.Expect(config).To(gomega.ContainSubstring(" prometheus:\n host: metric-storage-prometheus.openstack.svc\n port: 9090"))
61+
g.Expect(config).NotTo(gomega.ContainSubstring("prometheus:\n host: metric-storage-prometheus.openstack.svc\n port: 9090\n ca_cert:"))
62+
})
63+
64+
t.Run("MetricStorage with TLS enabled adds ca_cert under prometheus", func(t *testing.T) {
65+
g := gomega.NewWithT(t)
66+
metricStorage := &telemetryv1.MetricStorage{
67+
Spec: telemetryv1.MetricStorageSpec{
68+
PrometheusTLS: tls.SimpleService{
69+
GenericService: tls.GenericService{
70+
SecretName: ptr.To("prometheus-tls-secret"),
71+
},
72+
},
73+
},
74+
}
75+
config := MCPConfigYAML("openstackclient", "openstack", "", false, metricStorage)
76+
77+
g.Expect(config).To(gomega.ContainSubstring(" prometheus:\n host: metric-storage-prometheus.openstack.svc\n port: 9090\n ca_cert: " + tls.DownstreamTLSCABundlePath))
78+
})
79+
}

0 commit comments

Comments
 (0)