@@ -26,6 +26,7 @@ import (
2626 "k8s.io/apimachinery/pkg/runtime"
2727 "k8s.io/apimachinery/pkg/types"
2828 "k8s.io/client-go/kubernetes"
29+ "k8s.io/utils/ptr"
2930 ctrl "sigs.k8s.io/controller-runtime"
3031 "sigs.k8s.io/controller-runtime/pkg/builder"
3132 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -51,6 +52,7 @@ import (
5152 common_rbac "github.com/openstack-k8s-operators/lib-common/modules/common/rbac"
5253 "github.com/openstack-k8s-operators/lib-common/modules/common/secret"
5354 "github.com/openstack-k8s-operators/lib-common/modules/common/service"
55+ "github.com/openstack-k8s-operators/lib-common/modules/common/tls"
5456 util "github.com/openstack-k8s-operators/lib-common/modules/common/util"
5557
5658 mariadbv1 "github.com/openstack-k8s-operators/mariadb-operator/api/v1beta1"
@@ -315,7 +317,7 @@ func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request
315317 //
316318 // check for required OpenStack secret holding passwords for service/admin user and add hash to the vars map
317319 //
318- hash , result , _ , err := ensureSecret (
320+ hash , result , secret , err := ensureSecret (
319321 ctx ,
320322 types.NamespacedName {Namespace : instance .Namespace , Name : instance .Spec .Secret },
321323 []string {
@@ -346,7 +348,7 @@ func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request
346348 // all our input checks out so report InputReady
347349 instance .Status .Conditions .MarkTrue (condition .InputReadyCondition , condition .InputReadyMessage )
348350
349- err = r .generateServiceConfigMaps (ctx , h , instance , & configMapVars )
351+ err = r .generateServiceConfigMaps (ctx , h , instance , secret , & configMapVars )
350352 if err != nil {
351353 instance .Status .Conditions .Set (condition .FalseCondition (
352354 condition .ServiceConfigReadyCondition ,
@@ -357,6 +359,52 @@ func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request
357359 return ctrl.Result {}, err
358360 }
359361
362+ // TLS input validation
363+ //
364+ // Validate the CA cert secret if provided
365+ if instance .Spec .TLS .CaBundleSecretName != "" {
366+ hash , ctrlResult , err := tls .ValidateCACertSecret (
367+ ctx ,
368+ h .GetClient (),
369+ types.NamespacedName {
370+ Name : instance .Spec .TLS .CaBundleSecretName ,
371+ Namespace : instance .Namespace ,
372+ },
373+ )
374+ if err != nil {
375+ instance .Status .Conditions .Set (condition .FalseCondition (
376+ condition .TLSInputReadyCondition ,
377+ condition .ErrorReason ,
378+ condition .SeverityWarning ,
379+ condition .TLSInputErrorMessage ,
380+ err .Error ()))
381+ return ctrlResult , err
382+ } else if (ctrlResult != ctrl.Result {}) {
383+ return ctrlResult , nil
384+ }
385+
386+ if hash != "" {
387+ configMapVars [tls .CABundleKey ] = env .SetValue (hash )
388+ }
389+ }
390+
391+ // Validate API service certs secrets
392+ certsHash , ctrlResult , err := instance .Spec .TLS .API .ValidateCertSecrets (ctx , h , instance .Namespace )
393+ if err != nil {
394+ instance .Status .Conditions .Set (condition .FalseCondition (
395+ condition .TLSInputReadyCondition ,
396+ condition .ErrorReason ,
397+ condition .SeverityWarning ,
398+ condition .TLSInputErrorMessage ,
399+ err .Error ()))
400+ return ctrlResult , err
401+ } else if (ctrlResult != ctrl.Result {}) {
402+ return ctrlResult , nil
403+ }
404+ configMapVars [tls .TLSHashName ] = env .SetValue (certsHash )
405+
406+ instance .Status .Conditions .MarkTrue (condition .TLSInputReadyCondition , condition .InputReadyMessage )
407+
360408 // create hash over all the different input resources to identify if any those changed
361409 // and a restart/recreate is required.
362410 //
@@ -424,9 +472,10 @@ func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request
424472 return ctrl.Result {}, nil
425473}
426474
427- func getServiceLabels () map [string ]string {
475+ func getServiceLabels (instance * placementv1. PlacementAPI ) map [string ]string {
428476 return map [string ]string {
429- common .AppSelector : placement .ServiceName ,
477+ common .AppSelector : placement .ServiceName ,
478+ common .OwnerSelector : instance .Name ,
430479 }
431480}
432481
@@ -451,7 +500,7 @@ func (r *PlacementAPIReconciler) ensureServiceExposed(
451500 }
452501
453502 exportLabels := util .MergeStringMaps (
454- getServiceLabels (),
503+ getServiceLabels (instance ),
455504 map [string ]string {
456505 service .AnnotationEndpointKey : endpointTypeStr ,
457506 },
@@ -463,7 +512,7 @@ func (r *PlacementAPIReconciler) ensureServiceExposed(
463512 Name : endpointName ,
464513 Namespace : instance .Namespace ,
465514 Labels : exportLabels ,
466- Selector : getServiceLabels (),
515+ Selector : getServiceLabels (instance ),
467516 Port : service.GenericServicePort {
468517 Name : endpointName ,
469518 Port : data .Port ,
@@ -524,7 +573,12 @@ func (r *PlacementAPIReconciler) ensureServiceExposed(
524573 }
525574 // create service - end
526575
527- // TODO: TLS, pass in https as protocol, create TLS cert
576+ // if TLS is enabled
577+ if instance .Spec .TLS .API .Enabled (endpointType ) {
578+ // set endpoint protocol to https
579+ data .Protocol = ptr .To (service .ProtocolHTTPS )
580+ }
581+
528582 apiEndpoints [string (endpointType )], err = svc .GetAPIEndpoint (
529583 svcOverride .EndpointURL , data .Protocol , data .Path )
530584 if err != nil {
@@ -593,7 +647,7 @@ func (r *PlacementAPIReconciler) ensureKeystoneServiceUser(
593647 Secret : instance .Spec .Secret ,
594648 PasswordSelector : instance .Spec .PasswordSelectors .Service ,
595649 }
596- serviceLabels := getServiceLabels ()
650+ serviceLabels := getServiceLabels (instance )
597651 ksSvc := keystonev1 .NewKeystoneService (ksSvcSpec , instance .Namespace , serviceLabels , time .Duration (10 )* time .Second )
598652 _ , err := ksSvc .CreateOrPatch (ctx , h )
599653 if err != nil {
@@ -625,7 +679,7 @@ func (r *PlacementAPIReconciler) ensureKeystoneEndpoint(
625679 placement .ServiceName ,
626680 instance .Namespace ,
627681 ksEndptSpec ,
628- getServiceLabels (),
682+ getServiceLabels (instance ),
629683 time .Duration (10 )* time .Second ,
630684 )
631685 ctrlResult , err := ksEndpt .CreateOrPatch (ctx , h )
@@ -733,6 +787,10 @@ func (r *PlacementAPIReconciler) initConditions(
733787 condition .RoleBindingReadyCondition ,
734788 condition .InitReason ,
735789 condition .RoleBindingReadyInitMessage ),
790+ condition .UnknownCondition (
791+ condition .TLSInputReadyCondition ,
792+ condition .InitReason ,
793+ condition .InputReadyInitMessage ),
736794 )
737795
738796 instance .Status .Conditions .Init (& cl )
@@ -988,7 +1046,7 @@ func (r *PlacementAPIReconciler) ensureDbSync(
9881046 serviceAnnotations map [string ]string ,
9891047) (ctrl.Result , error ) {
9901048 Log := r .GetLogger (ctx )
991- serviceLabels := getServiceLabels ()
1049+ serviceLabels := getServiceLabels (instance )
9921050 dbSyncHash := instance .Status .Hash [placementv1 .DbSyncHash ]
9931051 jobDef := placement .DbSyncJob (instance , serviceLabels , serviceAnnotations )
9941052 dbSyncjob := job .NewJob (
@@ -1037,10 +1095,20 @@ func (r *PlacementAPIReconciler) ensureDeployment(
10371095 Log := r .GetLogger (ctx )
10381096 Log .Info ("Reconciling Service" )
10391097
1040- serviceLabels := getServiceLabels ()
1098+ serviceLabels := getServiceLabels (instance )
10411099
10421100 // Define a new Deployment object
1043- deplDef := placement .Deployment (instance , inputHash , serviceLabels , serviceAnnotations )
1101+ deplDef , err := placement .Deployment (ctx , h , instance , inputHash , serviceLabels , serviceAnnotations )
1102+
1103+ if err != nil {
1104+ instance .Status .Conditions .Set (condition .FalseCondition (
1105+ condition .DeploymentReadyCondition ,
1106+ condition .ErrorReason ,
1107+ condition .SeverityWarning ,
1108+ condition .DeploymentReadyErrorMessage ,
1109+ err .Error ()))
1110+ }
1111+
10441112 depl := deployment .NewDeployment (
10451113 deplDef ,
10461114 time .Duration (5 )* time .Second ,
@@ -1111,6 +1179,7 @@ func (r *PlacementAPIReconciler) generateServiceConfigMaps(
11111179 ctx context.Context ,
11121180 h * helper.Helper ,
11131181 instance * placementv1.PlacementAPI ,
1182+ ospSecret corev1.Secret ,
11141183 envVars * map [string ]env.Setter ,
11151184) error {
11161185 //
@@ -1147,14 +1216,29 @@ func (r *PlacementAPIReconciler) generateServiceConfigMaps(
11471216 "ServiceUser" : instance .Spec .ServiceUser ,
11481217 "KeystoneInternalURL" : keystoneInternalURL ,
11491218 "KeystonePublicURL" : keystonePublicURL ,
1150- "PlacementPassword" : instance .Spec .PasswordSelectors .Service ,
1219+ "PlacementPassword" : string ( ospSecret . Data [ instance .Spec .PasswordSelectors .Service ]) ,
11511220 "DBUser" : instance .Spec .DatabaseUser ,
1152- "DBPassword" : instance .Spec .PasswordSelectors .Database ,
1221+ "DBPassword" : string ( ospSecret . Data [ instance .Spec .PasswordSelectors .Database ]) ,
11531222 "DBAddress" : instance .Status .DatabaseHostname ,
11541223 "DBName" : placement .DatabaseName ,
11551224 "log_file" : "/var/log/placement/placement-api.log" ,
11561225 }
11571226
1227+ // create httpd vhost template parameters
1228+ httpdVhostConfig := map [string ]interface {}{}
1229+ for _ , endpt := range []service.Endpoint {service .EndpointInternal , service .EndpointPublic } {
1230+ endptConfig := map [string ]interface {}{}
1231+ endptConfig ["ServerName" ] = fmt .Sprintf ("placement-%s.%s.svc" , endpt .String (), instance .Namespace )
1232+ endptConfig ["TLS" ] = false // default TLS to false, and set it bellow to true if enabled
1233+ if instance .Spec .TLS .API .Enabled (endpt ) {
1234+ endptConfig ["TLS" ] = true
1235+ endptConfig ["SSLCertificateFile" ] = fmt .Sprintf ("/etc/pki/tls/certs/%s.crt" , endpt .String ())
1236+ endptConfig ["SSLCertificateKeyFile" ] = fmt .Sprintf ("/etc/pki/tls/private/%s.key" , endpt .String ())
1237+ }
1238+ httpdVhostConfig [endpt .String ()] = endptConfig
1239+ }
1240+ templateParameters ["VHosts" ] = httpdVhostConfig
1241+
11581242 extraTemplates := map [string ]string {
11591243 "placement.conf" : "placementapi/config/placement.conf" ,
11601244 }
0 commit comments