From aca4cdbccf4bcf010b77eec002102284a5edf374 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Tue, 6 Oct 2026 11:26:36 -0400 Subject: [PATCH] Configure MCP server Prometheus/metric-storage connection On RHOSO versions where metric-storage is not registered in the Keystone catalog, the observabilityclient running inside the rhos-mcps sidecar cannot auto-discover Aetos/Prometheus and metric commands fail. Detect a telemetry MetricStorage CR in the OpenStackClient's namespace during MCP reconciliation and, when found, render its connection details (host/port, and CA cert path when Prometheus TLS is enabled) into the openstack.prometheus section of the rendered MCP config.yaml. When no MetricStorage is found, the section is omitted so the MCP server can report a clear "not configured" error instead of pointing at a nonexistent host. This mirrors the approach taken in lightspeed-operator commit 57acabc40fdf3695b42abcd85110c58e908454b4, reusing the existing telemetry-operator MetricStorage watch, RBAC, and lookup already used by the OpenStackClient controller for the CLI pod's Prometheus env vars. Co-Authored-By: Claude Sonnet 5 --- .../client/openstackclient_controller.go | 20 +++++++++- internal/openstackclient/funcs.go | 28 +++++++++++-- internal/openstackclient/funcs_test.go | 40 ++++++++++++++++++- 3 files changed, 82 insertions(+), 6 deletions(-) diff --git a/internal/controller/client/openstackclient_controller.go b/internal/controller/client/openstackclient_controller.go index fe15df0e3b..577aa5de0c 100644 --- a/internal/controller/client/openstackclient_controller.go +++ b/internal/controller/client/openstackclient_controller.go @@ -324,6 +324,18 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ }, metricStorage) if err == nil { configVars["PrometheusTls"] = env.SetValue(fmt.Sprint(metricStorage.Spec.PrometheusTLS.Enabled())) + } else if k8s_errors.IsNotFound(err) { + // MetricStorage is optional; clear the object so MCP config does not + // point at a Prometheus service that is not present. + metricStorage = nil + } else { + instance.Status.Conditions.Set(condition.FalseCondition( + clientv1.OpenStackClientReadyCondition, + condition.ErrorReason, + condition.SeverityWarning, + clientv1.OpenStackClientReadyErrorMessage, + err.Error())) + return ctrl.Result{}, err } // all cert input checks out so report InputReady @@ -370,7 +382,13 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ configVars["mcp-tls"] = env.SetValue(certHash) } - mcpConfigYAML := openstackclient.MCPConfigYAML(instance.Name, instance.Namespace, instance.Spec.CaBundleSecretName, mcpTLSSvc != nil) + mcpConfigYAML := openstackclient.MCPConfigYAML( + instance.Name, + instance.Namespace, + instance.Spec.CaBundleSecretName, + mcpTLSSvc != nil, + metricStorage, + ) mcpCloudsYAML := openstackclient.MCPCloudsYAML( internalAuthURL, diff --git a/internal/openstackclient/funcs.go b/internal/openstackclient/funcs.go index 15744de5f2..b0fac9c991 100644 --- a/internal/openstackclient/funcs.go +++ b/internal/openstackclient/funcs.go @@ -189,11 +189,33 @@ func ClientPodSpec( // name and namespace identify the owning OpenStackClient and are used to scope // mcp_transport_security to the MCP Service's own hostname (-mcp..svc:8080), // matching the URL OpenStackAssistant uses to reach it, instead of allowing any host/origin. -func MCPConfigYAML(name, namespace, caBundleSecretName string, mcpTLSEnabled bool) string { +// When MetricStorage is present, its Prometheus endpoint is configured explicitly +// for RHOSO versions where it is not registered in the Keystone catalog. +func MCPConfigYAML( + name, namespace, caBundleSecretName string, + mcpTLSEnabled bool, + metricStorage *telemetryv1.MetricStorage, +) string { caCert := "" if caBundleSecretName != "" { caCert = fmt.Sprintf("\n ca_cert: %s", tls.DownstreamTLSCABundlePath) } + prometheusConfig := "" + if metricStorage != nil { + prometheusCACert := "" + if metricStorage.Spec.PrometheusTLS.Enabled() { + prometheusCACert = fmt.Sprintf("\n ca_cert: %s", tls.DownstreamTLSCABundlePath) + } + prometheusConfig = fmt.Sprintf(` + prometheus: + host: %s-prometheus.%s.svc + port: %d%s`, + telemetryv1.DefaultServiceName, + namespace, + telemetryv1.DefaultPrometheusPort, + prometheusCACert, + ) + } mcpTLS := "" if mcpTLSEnabled { mcpTLS = ` @@ -211,7 +233,7 @@ tls: port: 8080 openstack: enabled: true - allow_write: false%s + allow_write: false%s%s openshift: enabled: false mcp_transport_security: @@ -220,7 +242,7 @@ mcp_transport_security: - "%s" allowed_origins: %s%s -`, caCert, mcpHost, allowedOrigins, mcpTLS) +`, caCert, prometheusConfig, mcpHost, allowedOrigins, mcpTLS) } // MCPCloudsYAML returns a clouds.yaml using the given auth URL for the MCP sidecar. diff --git a/internal/openstackclient/funcs_test.go b/internal/openstackclient/funcs_test.go index 0480f7b9a8..372754c866 100644 --- a/internal/openstackclient/funcs_test.go +++ b/internal/openstackclient/funcs_test.go @@ -19,12 +19,14 @@ import ( "github.com/onsi/gomega" "github.com/openstack-k8s-operators/lib-common/modules/common/tls" + telemetryv1 "github.com/openstack-k8s-operators/telemetry-operator/api/v1beta1" + "k8s.io/utils/ptr" ) func TestMCPConfigYAMLTLSIsIndependentFromCABundle(t *testing.T) { t.Run("CA bundle configures only downstream trust", func(t *testing.T) { g := gomega.NewWithT(t) - config := MCPConfigYAML("openstackclient", "openstack", "combined-ca-bundle", false) + config := MCPConfigYAML("openstackclient", "openstack", "combined-ca-bundle", false, nil) g.Expect(config).To(gomega.ContainSubstring("ca_cert: " + tls.DownstreamTLSCABundlePath)) g.Expect(config).NotTo(gomega.ContainSubstring("\ntls:\n")) @@ -33,7 +35,7 @@ func TestMCPConfigYAMLTLSIsIndependentFromCABundle(t *testing.T) { t.Run("MCP TLS does not require a downstream CA bundle", func(t *testing.T) { g := gomega.NewWithT(t) - config := MCPConfigYAML("openstackclient", "openstack", "", true) + config := MCPConfigYAML("openstackclient", "openstack", "", true, nil) g.Expect(config).NotTo(gomega.ContainSubstring("ca_cert:")) g.Expect(config).To(gomega.ContainSubstring("\ntls:\n")) @@ -41,3 +43,37 @@ func TestMCPConfigYAMLTLSIsIndependentFromCABundle(t *testing.T) { g.Expect(config).To(gomega.ContainSubstring("https://openstackclient-mcp.openstack.svc:8080")) }) } + +func TestMCPConfigYAMLPrometheus(t *testing.T) { + t.Run("no MetricStorage omits the prometheus section", func(t *testing.T) { + g := gomega.NewWithT(t) + config := MCPConfigYAML("openstackclient", "openstack", "", false, nil) + + g.Expect(config).NotTo(gomega.ContainSubstring("prometheus:")) + }) + + t.Run("MetricStorage without TLS configures host/port but no ca_cert", func(t *testing.T) { + g := gomega.NewWithT(t) + metricStorage := &telemetryv1.MetricStorage{} + config := MCPConfigYAML("openstackclient", "openstack", "", false, metricStorage) + + g.Expect(config).To(gomega.ContainSubstring(" prometheus:\n host: metric-storage-prometheus.openstack.svc\n port: 9090")) + g.Expect(config).NotTo(gomega.ContainSubstring("prometheus:\n host: metric-storage-prometheus.openstack.svc\n port: 9090\n ca_cert:")) + }) + + t.Run("MetricStorage with TLS enabled adds ca_cert under prometheus", func(t *testing.T) { + g := gomega.NewWithT(t) + metricStorage := &telemetryv1.MetricStorage{ + Spec: telemetryv1.MetricStorageSpec{ + PrometheusTLS: tls.SimpleService{ + GenericService: tls.GenericService{ + SecretName: ptr.To("prometheus-tls-secret"), + }, + }, + }, + } + config := MCPConfigYAML("openstackclient", "openstack", "", false, metricStorage) + + g.Expect(config).To(gomega.ContainSubstring(" prometheus:\n host: metric-storage-prometheus.openstack.svc\n port: 9090\n ca_cert: " + tls.DownstreamTLSCABundlePath)) + }) +}