Skip to content

Commit 5166e15

Browse files
committed
Sync refactor with tls enable patches
1 parent 5daf412 commit 5166e15

File tree

4 files changed

+95
-14
lines changed

4 files changed

+95
-14
lines changed

controllers/placementapi_controller.go

Lines changed: 93 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
@@ -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,
@@ -1155,6 +1223,21 @@ func (r *PlacementAPIReconciler) generateServiceConfigMaps(
11551223
"log_file": "/var/log/placement/placement-api.log",
11561224
}
11571225

1226+
// create httpd vhost template parameters
1227+
httpdVhostConfig := map[string]interface{}{}
1228+
for _, endpt := range []service.Endpoint{service.EndpointInternal, service.EndpointPublic} {
1229+
endptConfig := map[string]interface{}{}
1230+
endptConfig["ServerName"] = fmt.Sprintf("placement-%s.%s.svc", endpt.String(), instance.Namespace)
1231+
endptConfig["TLS"] = false // default TLS to false, and set it bellow to true if enabled
1232+
if instance.Spec.TLS.API.Enabled(endpt) {
1233+
endptConfig["TLS"] = true
1234+
endptConfig["SSLCertificateFile"] = fmt.Sprintf("/etc/pki/tls/certs/%s.crt", endpt.String())
1235+
endptConfig["SSLCertificateKeyFile"] = fmt.Sprintf("/etc/pki/tls/private/%s.key", endpt.String())
1236+
}
1237+
httpdVhostConfig[endpt.String()] = endptConfig
1238+
}
1239+
templateParameters["VHosts"] = httpdVhostConfig
1240+
11581241
extraTemplates := map[string]string{
11591242
"placement.conf": "placementapi/config/placement.conf",
11601243
}

pkg/placement/dbsync.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,5 @@ func DbSyncJob(
8989
},
9090
}
9191

92-
job.Spec.Template.Spec.Volumes = getVolumes(instance.Name)
93-
9492
return job
9593
}

pkg/placement/deployment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,5 @@ func Deployment(
199199
deployment.Spec.Template.Spec.NodeSelector = instance.Spec.NodeSelector
200200
}
201201

202-
return deployment
202+
return deployment, nil
203203
}

tests/functional/placementapi_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ var _ = Describe("PlacementAPI controller", func() {
757757
Expect(container.ReadinessProbe.HTTPGet.Scheme).To(Equal(corev1.URISchemeHTTPS))
758758
Expect(container.LivenessProbe.HTTPGet.Scheme).To(Equal(corev1.URISchemeHTTPS))
759759

760-
configDataMap := th.GetConfigMap(names.ConfigMapName)
760+
configDataMap := th.GetSecret(names.ConfigMapName)
761761
Expect(configDataMap).ShouldNot(BeNil())
762762
Expect(configDataMap.Data).Should(HaveKey("httpd.conf"))
763763
Expect(configDataMap.Data).Should(HaveKey("ssl.conf"))

0 commit comments

Comments
 (0)