@@ -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 //
@@ -398,16 +446,8 @@ func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request
398446 }
399447
400448 result , err = r .ensureDbSync (ctx , instance , h , serviceAnnotations )
401- if err != nil {
402- return result , err
403- } else if (result != ctrl.Result {}) {
404- return result , nil
405- }
406-
407449 if (err != nil || result != ctrl.Result {}) {
408- // We can ignore RequeueAfter as we are watching the Service resource
409- // but we have to return while waiting for the service to be exposed
410- return ctrl.Result {}, err
450+ return result , err
411451 }
412452
413453 result , err = r .ensureDeployment (ctx , h , instance , inputHash , serviceAnnotations )
@@ -424,9 +464,10 @@ func (r *PlacementAPIReconciler) Reconcile(ctx context.Context, req ctrl.Request
424464 return ctrl.Result {}, nil
425465}
426466
427- func getServiceLabels () map [string ]string {
467+ func getServiceLabels (instance * placementv1. PlacementAPI ) map [string ]string {
428468 return map [string ]string {
429- common .AppSelector : placement .ServiceName ,
469+ common .AppSelector : placement .ServiceName ,
470+ common .OwnerSelector : instance .Name ,
430471 }
431472}
432473
@@ -451,7 +492,7 @@ func (r *PlacementAPIReconciler) ensureServiceExposed(
451492 }
452493
453494 exportLabels := util .MergeStringMaps (
454- getServiceLabels (),
495+ getServiceLabels (instance ),
455496 map [string ]string {
456497 service .AnnotationEndpointKey : endpointTypeStr ,
457498 },
@@ -463,7 +504,7 @@ func (r *PlacementAPIReconciler) ensureServiceExposed(
463504 Name : endpointName ,
464505 Namespace : instance .Namespace ,
465506 Labels : exportLabels ,
466- Selector : getServiceLabels (),
507+ Selector : getServiceLabels (instance ),
467508 Port : service.GenericServicePort {
468509 Name : endpointName ,
469510 Port : data .Port ,
@@ -524,7 +565,12 @@ func (r *PlacementAPIReconciler) ensureServiceExposed(
524565 }
525566 // create service - end
526567
527- // TODO: TLS, pass in https as protocol, create TLS cert
568+ // if TLS is enabled
569+ if instance .Spec .TLS .API .Enabled (endpointType ) {
570+ // set endpoint protocol to https
571+ data .Protocol = ptr .To (service .ProtocolHTTPS )
572+ }
573+
528574 apiEndpoints [string (endpointType )], err = svc .GetAPIEndpoint (
529575 svcOverride .EndpointURL , data .Protocol , data .Path )
530576 if err != nil {
@@ -593,7 +639,7 @@ func (r *PlacementAPIReconciler) ensureKeystoneServiceUser(
593639 Secret : instance .Spec .Secret ,
594640 PasswordSelector : instance .Spec .PasswordSelectors .Service ,
595641 }
596- serviceLabels := getServiceLabels ()
642+ serviceLabels := getServiceLabels (instance )
597643 ksSvc := keystonev1 .NewKeystoneService (ksSvcSpec , instance .Namespace , serviceLabels , time .Duration (10 )* time .Second )
598644 _ , err := ksSvc .CreateOrPatch (ctx , h )
599645 if err != nil {
@@ -625,7 +671,7 @@ func (r *PlacementAPIReconciler) ensureKeystoneEndpoint(
625671 placement .ServiceName ,
626672 instance .Namespace ,
627673 ksEndptSpec ,
628- getServiceLabels (),
674+ getServiceLabels (instance ),
629675 time .Duration (10 )* time .Second ,
630676 )
631677 ctrlResult , err := ksEndpt .CreateOrPatch (ctx , h )
@@ -733,6 +779,10 @@ func (r *PlacementAPIReconciler) initConditions(
733779 condition .RoleBindingReadyCondition ,
734780 condition .InitReason ,
735781 condition .RoleBindingReadyInitMessage ),
782+ condition .UnknownCondition (
783+ condition .TLSInputReadyCondition ,
784+ condition .InitReason ,
785+ condition .InputReadyInitMessage ),
736786 )
737787
738788 instance .Status .Conditions .Init (& cl )
@@ -988,7 +1038,7 @@ func (r *PlacementAPIReconciler) ensureDbSync(
9881038 serviceAnnotations map [string ]string ,
9891039) (ctrl.Result , error ) {
9901040 Log := r .GetLogger (ctx )
991- serviceLabels := getServiceLabels ()
1041+ serviceLabels := getServiceLabels (instance )
9921042 dbSyncHash := instance .Status .Hash [placementv1 .DbSyncHash ]
9931043 jobDef := placement .DbSyncJob (instance , serviceLabels , serviceAnnotations )
9941044 dbSyncjob := job .NewJob (
@@ -1037,10 +1087,20 @@ func (r *PlacementAPIReconciler) ensureDeployment(
10371087 Log := r .GetLogger (ctx )
10381088 Log .Info ("Reconciling Service" )
10391089
1040- serviceLabels := getServiceLabels ()
1090+ serviceLabels := getServiceLabels (instance )
10411091
10421092 // Define a new Deployment object
1043- deplDef := placement .Deployment (instance , inputHash , serviceLabels , serviceAnnotations )
1093+ deplDef , err := placement .Deployment (ctx , h , instance , inputHash , serviceLabels , serviceAnnotations )
1094+
1095+ if err != nil {
1096+ instance .Status .Conditions .Set (condition .FalseCondition (
1097+ condition .DeploymentReadyCondition ,
1098+ condition .ErrorReason ,
1099+ condition .SeverityWarning ,
1100+ condition .DeploymentReadyErrorMessage ,
1101+ err .Error ()))
1102+ }
1103+
10441104 depl := deployment .NewDeployment (
10451105 deplDef ,
10461106 time .Duration (5 )* time .Second ,
@@ -1111,6 +1171,7 @@ func (r *PlacementAPIReconciler) generateServiceConfigMaps(
11111171 ctx context.Context ,
11121172 h * helper.Helper ,
11131173 instance * placementv1.PlacementAPI ,
1174+ ospSecret corev1.Secret ,
11141175 envVars * map [string ]env.Setter ,
11151176) error {
11161177 //
@@ -1147,14 +1208,29 @@ func (r *PlacementAPIReconciler) generateServiceConfigMaps(
11471208 "ServiceUser" : instance .Spec .ServiceUser ,
11481209 "KeystoneInternalURL" : keystoneInternalURL ,
11491210 "KeystonePublicURL" : keystonePublicURL ,
1150- "PlacementPassword" : instance .Spec .PasswordSelectors .Service ,
1211+ "PlacementPassword" : string ( ospSecret . Data [ instance .Spec .PasswordSelectors .Service ]) ,
11511212 "DBUser" : instance .Spec .DatabaseUser ,
1152- "DBPassword" : instance .Spec .PasswordSelectors .Database ,
1213+ "DBPassword" : string ( ospSecret . Data [ instance .Spec .PasswordSelectors .Database ]) ,
11531214 "DBAddress" : instance .Status .DatabaseHostname ,
11541215 "DBName" : placement .DatabaseName ,
11551216 "log_file" : "/var/log/placement/placement-api.log" ,
11561217 }
11571218
1219+ // create httpd vhost template parameters
1220+ httpdVhostConfig := map [string ]interface {}{}
1221+ for _ , endpt := range []service.Endpoint {service .EndpointInternal , service .EndpointPublic } {
1222+ endptConfig := map [string ]interface {}{}
1223+ endptConfig ["ServerName" ] = fmt .Sprintf ("placement-%s.%s.svc" , endpt .String (), instance .Namespace )
1224+ endptConfig ["TLS" ] = false // default TLS to false, and set it bellow to true if enabled
1225+ if instance .Spec .TLS .API .Enabled (endpt ) {
1226+ endptConfig ["TLS" ] = true
1227+ endptConfig ["SSLCertificateFile" ] = fmt .Sprintf ("/etc/pki/tls/certs/%s.crt" , endpt .String ())
1228+ endptConfig ["SSLCertificateKeyFile" ] = fmt .Sprintf ("/etc/pki/tls/private/%s.key" , endpt .String ())
1229+ }
1230+ httpdVhostConfig [endpt .String ()] = endptConfig
1231+ }
1232+ templateParameters ["VHosts" ] = httpdVhostConfig
1233+
11581234 extraTemplates := map [string ]string {
11591235 "placement.conf" : "placementapi/config/placement.conf" ,
11601236 }
0 commit comments