Skip to content

Commit 41540b8

Browse files
committed
Sync refactor with tls enable patches
1 parent 5daf412 commit 41540b8

File tree

9 files changed

+114
-108
lines changed

9 files changed

+114
-108
lines changed

controllers/placementapi_controller.go

Lines changed: 99 additions & 23 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"
@@ -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
}

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
}

templates/placementapi/config/placement-api-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"perm": "0600"
2121
},
2222
{
23-
"source": "/var/lib/config-data/merged/ssl.conf",
23+
"source": "/var/lib/config-data/ssl.conf",
2424
"dest": "/etc/httpd/conf.d/ssl.conf",
2525
"owner": "apache",
2626
"perm": "0644"

templates/placementapi/config/placement.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ log_file = {{ .log_file }}
99
debug = true
1010

1111
[placement_database]
12-
connection = mysql+pymysql://{{ .DBUser }}:{{ .DBPassword}}@{{ .DBAddress }}/{{ .DBName }}
12+
connection = mysql+pymysql://{{ .DBUser }}:{{ .DBPassword }}@{{ .DBAddress }}/{{ .DBName }}
1313

1414
[api]
1515
auth_strategy = keystone

tests/functional/placementapi_controller_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ var _ = Describe("PlacementAPI controller", func() {
252252
Expect(cm.Data["placement.conf"]).Should(
253253
ContainSubstring("username = placement"))
254254
Expect(cm.Data["placement.conf"]).Should(
255-
ContainSubstring("connection = mysql+pymysql://placement:PlacementDatabasePassword@/placement"))
255+
ContainSubstring("password = 12345678"))
256+
Expect(cm.Data["placement.conf"]).Should(
257+
ContainSubstring("connection = mysql+pymysql://placement:12345678@/placement"))
256258
})
257259

258260
It("creates service account, role and rolebindig", func() {
@@ -757,7 +759,7 @@ var _ = Describe("PlacementAPI controller", func() {
757759
Expect(container.ReadinessProbe.HTTPGet.Scheme).To(Equal(corev1.URISchemeHTTPS))
758760
Expect(container.LivenessProbe.HTTPGet.Scheme).To(Equal(corev1.URISchemeHTTPS))
759761

760-
configDataMap := th.GetConfigMap(names.ConfigMapName)
762+
configDataMap := th.GetSecret(names.ConfigMapName)
761763
Expect(configDataMap).ShouldNot(BeNil())
762764
Expect(configDataMap.Data).Should(HaveKey("httpd.conf"))
763765
Expect(configDataMap.Data).Should(HaveKey("ssl.conf"))

tests/kuttl/common/assert_sample_deployment.yaml

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ spec:
145145
- mountPath: /usr/local/bin/container-scripts
146146
name: scripts
147147
readOnly: true
148-
- mountPath: /var/lib/config-data/merged
149-
name: config-data-merged
148+
- mountPath: /var/lib/config-data/
149+
name: config-data
150150
- mountPath: /var/lib/kolla/config_files/config.json
151-
name: config-data-merged
151+
name: config-data
152152
readOnly: true
153153
subPath: placement-api-config.json
154154
- mountPath: /var/log/placement
@@ -189,17 +189,6 @@ status:
189189
availableReplicas: 1
190190
replicas: 1
191191
---
192-
# the openshift annotations can't be checked through the deployment above
193-
apiVersion: v1
194-
kind: Pod
195-
metadata:
196-
annotations:
197-
openshift.io/scc: anyuid
198-
labels:
199-
service: placement
200-
status:
201-
phase: Running
202-
---
203192
apiVersion: v1
204193
kind: Service
205194
metadata:
@@ -229,19 +218,7 @@ spec:
229218
type: ClusterIP
230219
---
231220
apiVersion: v1
232-
kind: ConfigMap
233-
metadata:
234-
labels:
235-
placement.openstack.org/name: placement
236-
name: placement-scripts
237-
ownerReferences:
238-
- blockOwnerDeletion: true
239-
controller: true
240-
kind: PlacementAPI
241-
name: placement
242-
---
243-
apiVersion: v1
244-
kind: ConfigMap
221+
kind: Secret
245222
metadata:
246223
labels:
247224
placement.openstack.org/name: placement

tests/kuttl/common/errors_cleanup_placement.yaml

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@ kind: Deployment
1818
metadata:
1919
name: placement
2020
---
21-
# the openshift annotations can't be checked through the deployment above
22-
apiVersion: v1
23-
kind: Pod
24-
metadata:
25-
annotations:
26-
openshift.io/scc: anyuid
27-
labels:
28-
service: placement
29-
---
3021
apiVersion: v1
3122
kind: Service
3223
metadata:
@@ -70,19 +61,7 @@ spec:
7061
type: ClusterIP
7162
---
7263
apiVersion: v1
73-
kind: ConfigMap
74-
metadata:
75-
labels:
76-
placement.openstack.org/name: placement
77-
name: placement-scripts
78-
ownerReferences:
79-
- blockOwnerDeletion: true
80-
controller: true
81-
kind: PlacementAPI
82-
name: placement
83-
---
84-
apiVersion: v1
85-
kind: ConfigMap
64+
kind: Secret
8665
metadata:
8766
labels:
8867
placement.openstack.org/name: placement

0 commit comments

Comments
 (0)