@@ -41,7 +41,9 @@ import (
4141 "sigs.k8s.io/controller-runtime/pkg/reconcile"
4242
4343 keystonev1 "github.com/openstack-k8s-operators/keystone-operator/api/v1beta1"
44+ "github.com/openstack-k8s-operators/lib-common/modules/certmanager"
4445 "github.com/openstack-k8s-operators/lib-common/modules/common"
46+ "github.com/openstack-k8s-operators/lib-common/modules/common/clusterdns"
4547 condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition"
4648 "github.com/openstack-k8s-operators/lib-common/modules/common/configmap"
4749 "github.com/openstack-k8s-operators/lib-common/modules/common/env"
@@ -83,6 +85,8 @@ func (r *OpenStackClientReconciler) GetLogger(ctx context.Context) logr.Logger {
8385// +kubebuilder:rbac:groups="security.openshift.io",resourceNames=anyuid,resources=securitycontextconstraints,verbs=use
8486// +kubebuilder:rbac:groups="",resources=pods,verbs=create;delete;get;list;patch;update;watch;patch
8587// +kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch;create;update;patch
88+ // +kubebuilder:rbac:groups=cert-manager.io,resources=issuers,verbs=get;list;watch
89+ // +kubebuilder:rbac:groups=cert-manager.io,resources=certificates,verbs=get;list;watch;create;update;patch;delete
8690
8791// Reconcile -
8892func (r * OpenStackClientReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (result ctrl.Result , _err error ) {
@@ -308,7 +312,53 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ
308312 instance .Status .Conditions .MarkTrue (condition .TLSInputReadyCondition , condition .InputReadyMessage )
309313
310314 // Reconcile MCP sidecar resources when enabled
315+ mcpTLSSecretName := ""
311316 if instance .Spec .MCP != nil && instance .Spec .MCP .Enabled {
317+ mcpTLSEnabled := instance .Spec .CaBundleSecretName != ""
318+
319+ if mcpTLSEnabled {
320+ issuer , err := certmanager .GetIssuerByLabels (
321+ ctx , helper ,
322+ instance .Namespace ,
323+ map [string ]string {certmanager .RootCAIssuerInternalLabel : "" },
324+ )
325+ if err != nil {
326+ instance .Status .Conditions .Set (condition .FalseCondition (
327+ clientv1 .OpenStackClientReadyCondition ,
328+ condition .ErrorReason ,
329+ condition .SeverityWarning ,
330+ clientv1 .OpenStackClientReadyErrorMessage ,
331+ err .Error ()))
332+ return ctrl.Result {}, err
333+ }
334+
335+ clusterDomain := clusterdns .GetDNSClusterDomain ()
336+ mcpSvcName := instance .Name + "-mcp"
337+ certRequest := certmanager.CertificateRequest {
338+ IssuerName : issuer .Name ,
339+ CertName : mcpSvcName + "-tls" ,
340+ Hostnames : []string {
341+ fmt .Sprintf ("%s.%s.svc" , mcpSvcName , instance .Namespace ),
342+ fmt .Sprintf ("%s.%s.svc.%s" , mcpSvcName , instance .Namespace , clusterDomain ),
343+ },
344+ Labels : map [string ]string {},
345+ }
346+ certSecret , ctrlResult , err := certmanager .EnsureCert (ctx , helper , certRequest , instance )
347+ if err != nil {
348+ instance .Status .Conditions .Set (condition .FalseCondition (
349+ clientv1 .OpenStackClientReadyCondition ,
350+ condition .ErrorReason ,
351+ condition .SeverityWarning ,
352+ clientv1 .OpenStackClientReadyErrorMessage ,
353+ err .Error ()))
354+ return ctrlResult , err
355+ } else if (ctrlResult != ctrl.Result {}) {
356+ return ctrlResult , nil
357+ }
358+ mcpTLSSecretName = certSecret .Name
359+ configVars [mcpTLSSecretName ] = env .SetValue (certSecret .ResourceVersion )
360+ }
361+
312362 mcpConfigCM := & corev1.ConfigMap {
313363 ObjectMeta : metav1.ObjectMeta {
314364 Name : instance .Name + "-mcp-config" ,
@@ -317,14 +367,14 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ
317367 }
318368 _ , err = controllerutil .CreateOrPatch (ctx , r .Client , mcpConfigCM , func () error {
319369 mcpConfigCM .Data = map [string ]string {
320- "config.yaml" : openstackclient .MCPConfigYAML (instance .Spec .CaBundleSecretName ),
370+ "config.yaml" : openstackclient .MCPConfigYAML (instance .Spec .CaBundleSecretName , mcpTLSEnabled ),
321371 }
322372 return controllerutil .SetControllerReference (instance , mcpConfigCM , r .Scheme )
323373 })
324374 if err != nil {
325375 return ctrl.Result {}, fmt .Errorf ("error creating MCP config ConfigMap: %w" , err )
326376 }
327- configVars [instance .Name + "-mcp-config" ] = env .SetValue (openstackclient .MCPConfigYAML (instance .Spec .CaBundleSecretName ))
377+ configVars [instance .Name + "-mcp-config" ] = env .SetValue (openstackclient .MCPConfigYAML (instance .Spec .CaBundleSecretName , mcpTLSEnabled ))
328378
329379 }
330380
@@ -335,6 +385,12 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ
335385
336386 // Reconcile MCP Service after configVarsHash so the hash annotation captures all config changes
337387 if instance .Spec .MCP != nil && instance .Spec .MCP .Enabled {
388+ mcpTLSEnabled := instance .Spec .CaBundleSecretName != ""
389+ mcpPort := int32 (8080 )
390+ if mcpTLSEnabled {
391+ mcpPort = 8443
392+ }
393+
338394 mcpService := & corev1.Service {
339395 ObjectMeta : metav1.ObjectMeta {
340396 Name : instance .Name + "-mcp" ,
@@ -344,7 +400,7 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ
344400 mcpServiceHash , err := util .ObjectHash (map [string ]interface {}{
345401 "containerImage" : instance .Spec .ContainerImage ,
346402 "mcpContainerImage" : instance .Spec .MCP .ContainerImage ,
347- "mcpConfig" : openstackclient .MCPConfigYAML (instance .Spec .CaBundleSecretName ),
403+ "mcpConfig" : openstackclient .MCPConfigYAML (instance .Spec .CaBundleSecretName , mcpTLSEnabled ),
348404 "configVarsHash" : configVarsHash ,
349405 })
350406 if err != nil {
@@ -359,7 +415,7 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ
359415 mcpService .Spec .Ports = []corev1.ServicePort {
360416 {
361417 Name : "mcp" ,
362- Port : 8080 ,
418+ Port : mcpPort ,
363419 Protocol : corev1 .ProtocolTCP ,
364420 },
365421 }
@@ -368,6 +424,7 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ
368424 if err != nil {
369425 return ctrl.Result {}, fmt .Errorf ("error creating MCP Service: %w" , err )
370426 }
427+
371428 }
372429
373430 osclient := & corev1.Pod {
@@ -377,7 +434,7 @@ func (r *OpenStackClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ
377434 },
378435 }
379436
380- spec := openstackclient .ClientPodSpec (ctx , instance , helper , configVarsHash )
437+ spec := openstackclient .ClientPodSpec (ctx , instance , helper , configVarsHash , mcpTLSSecretName )
381438
382439 podSpecHash , err := util .ObjectHash (spec )
383440 if err != nil {
0 commit comments