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)) + }) +}