Skip to content

Commit 8076033

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

File tree

9 files changed

+113
-99
lines changed

9 files changed

+113
-99
lines changed

controllers/placementapi_controller.go

Lines changed: 98 additions & 14 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
//
@@ -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
}

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)