Skip to content

Commit 7e8f668

Browse files
make tls pr798 use additionalVolumes capability from pr736 (zalando#920)
* make tls pr798 use additionalVolumes capability from pr736 * move the volume* sections lower * update helm chart crds and docs * fix user.md typos
1 parent ea3eef4 commit 7e8f668

File tree

11 files changed

+150
-45
lines changed

11 files changed

+150
-45
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
_obj
88
_test
99
_manifests
10+
_tmp
11+
github.com
1012

1113
# Architecture specific extensions/prefixes
1214
*.[568vq]

charts/postgres-operator/crds/postgresqls.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,21 @@ spec:
364364
type: string
365365
teamId:
366366
type: string
367+
tls:
368+
type: object
369+
required:
370+
- secretName
371+
properties:
372+
secretName:
373+
type: string
374+
certificateFile:
375+
type: string
376+
privateKeyFile:
377+
type: string
378+
caFile:
379+
type: string
380+
caSecretName:
381+
type: string
367382
tolerations:
368383
type: array
369384
items:

docs/reference/cluster_manifest.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,5 +435,12 @@ Those parameters are grouped under the `tls` top-level key.
435435
client connects with `sslmode=verify-ca` or `sslmode=verify-full`.
436436
Default is empty.
437437

438+
* **caSecretName**
439+
By setting the `caSecretName` value, the ca certificate file defined by the
440+
`caFile` will be fetched from this secret instead of `secretName` above.
441+
This secret has to hold a file with that name in its root.
442+
438443
Optionally one can provide full path for any of them. By default it is
439444
relative to the "/tls/", which is mount path of the tls secret.
445+
If `caSecretName` is defined, the ca.crt path is relative to "/tlsca/",
446+
otherwise to the same "/tls/".

docs/user.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ OpenShift allocates the users and groups dynamically (based on scc), and their
585585
range is different in every namespace. Due to this dynamic behaviour, it's not
586586
trivial to know at deploy time the uid/gid of the user in the cluster.
587587
Therefore, instead of using a global `spilo_fsgroup` setting, use the `spiloFSGroup` field
588-
per Postgres cluster.```
588+
per Postgres cluster.
589589

590590
Upload the cert as a kubernetes secret:
591591
```sh
@@ -594,17 +594,14 @@ kubectl create secret tls pg-tls \
594594
--cert pg-tls.crt
595595
```
596596

597-
Or with a CA:
597+
When doing client auth, CA can come optionally from the same secret:
598598
```sh
599599
kubectl create secret generic pg-tls \
600600
--from-file=tls.crt=server.crt \
601601
--from-file=tls.key=server.key \
602602
--from-file=ca.crt=ca.crt
603603
```
604604

605-
Alternatively it is also possible to use
606-
[cert-manager](https://cert-manager.io/docs/) to generate these secrets.
607-
608605
Then configure the postgres resource with the TLS secret:
609606

610607
```yaml
@@ -619,5 +616,29 @@ spec:
619616
caFile: "ca.crt" # add this if the secret is configured with a CA
620617
```
621618

619+
Optionally, the CA can be provided by a different secret:
620+
```sh
621+
kubectl create secret generic pg-tls-ca \
622+
--from-file=ca.crt=ca.crt
623+
```
624+
625+
Then configure the postgres resource with the TLS secret:
626+
627+
```yaml
628+
apiVersion: "acid.zalan.do/v1"
629+
kind: postgresql
630+
631+
metadata:
632+
name: acid-test-cluster
633+
spec:
634+
tls:
635+
secretName: "pg-tls" # this should hold tls.key and tls.crt
636+
caSecretName: "pg-tls-ca" # this should hold ca.crt
637+
caFile: "ca.crt" # add this if the secret is configured with a CA
638+
```
639+
640+
Alternatively, it is also possible to use
641+
[cert-manager](https://cert-manager.io/docs/) to generate these secrets.
642+
622643
Certificate rotation is handled in the spilo image which checks every 5
623644
minutes if the certificates have changed and reloads postgres accordingly.

manifests/complete-postgres-manifest.yaml

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ metadata:
99
spec:
1010
dockerImage: registry.opensource.zalan.do/acid/spilo-12:1.6-p2
1111
teamId: "acid"
12+
numberOfInstances: 2
13+
users: # Application/Robot users
14+
zalando:
15+
- superuser
16+
- createdb
17+
enableMasterLoadBalancer: false
18+
enableReplicaLoadBalancer: false
19+
# enableConnectionPooler: true # not needed when connectionPooler section is present (see below)
20+
allowedSourceRanges: # load balancers' source ranges for both master and replica services
21+
- 127.0.0.1/32
22+
databases:
23+
foo: zalando
24+
postgresql:
25+
version: "12"
26+
parameters: # Expert section
27+
shared_buffers: "32MB"
28+
max_connections: "10"
29+
log_statement: "all"
1230
volume:
1331
size: 1Gi
1432
# storageClass: my-sc
@@ -35,24 +53,6 @@ spec:
3553
- all
3654
volumeSource:
3755
emptyDir: {}
38-
numberOfInstances: 2
39-
users: # Application/Robot users
40-
zalando:
41-
- superuser
42-
- createdb
43-
enableMasterLoadBalancer: false
44-
enableReplicaLoadBalancer: false
45-
# enableConnectionPooler: true # not needed when connectionPooler section is present (see below)
46-
allowedSourceRanges: # load balancers' source ranges for both master and replica services
47-
- 127.0.0.1/32
48-
databases:
49-
foo: zalando
50-
postgresql:
51-
version: "12"
52-
parameters: # Expert section
53-
shared_buffers: "32MB"
54-
max_connections: "10"
55-
log_statement: "all"
5656

5757
enableShmVolume: true
5858
# spiloFSGroup: 103
@@ -148,8 +148,10 @@ spec:
148148
certificateFile: "tls.crt"
149149
privateKeyFile: "tls.key"
150150
caFile: "" # optionally configure Postgres with a CA certificate
151+
caSecretName: "" # optionally the ca.crt can come from this secret instead.
151152
# file names can be also defined with absolute path, and will no longer be relative
152-
# to the "/tls/" path where the secret is being mounted by default.
153+
# to the "/tls/" path where the secret is being mounted by default, and "/tlsca/"
154+
# where the caSecret is mounted by default.
153155
# When TLS is enabled, also set spiloFSGroup parameter above to the relevant value.
154156
# if unknown, set it to 103 which is the usual value in the default spilo images.
155157
# In Openshift, there is no need to set spiloFSGroup/spilo_fsgroup.

manifests/postgresql.crd.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ spec:
341341
type: string
342342
caFile:
343343
type: string
344+
caSecretName:
345+
type: string
344346
tolerations:
345347
type: array
346348
items:

pkg/apis/acid.zalan.do/v1/crds.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,9 @@ var PostgresCRDResourceValidation = apiextv1beta1.CustomResourceValidation{
513513
"caFile": {
514514
Type: "string",
515515
},
516+
"caSecretName": {
517+
Type: "string",
518+
},
516519
},
517520
},
518521
"tolerations": {

pkg/apis/acid.zalan.do/v1/postgresql_type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ type TLSDescription struct {
148148
CertificateFile string `json:"certificateFile,omitempty"`
149149
PrivateKeyFile string `json:"privateKeyFile,omitempty"`
150150
CAFile string `json:"caFile,omitempty"`
151+
CASecretName string `json:"caSecretName,omitempty"`
151152
}
152153

153154
// CloneDescription describes which cluster the new should clone and up to which point in time

pkg/apis/acid.zalan.do/v1/zz_generated.deepcopy.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cluster/k8sres.go

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,6 @@ func (c *Cluster) generatePodTemplate(
519519
podAntiAffinityTopologyKey string,
520520
additionalSecretMount string,
521521
additionalSecretMountPath string,
522-
volumes []v1.Volume,
523522
additionalVolumes []acidv1.AdditionalVolume,
524523
) (*v1.PodTemplateSpec, error) {
525524

@@ -539,7 +538,6 @@ func (c *Cluster) generatePodTemplate(
539538
InitContainers: initContainers,
540539
Tolerations: *tolerationsSpec,
541540
SecurityContext: &securityContext,
542-
Volumes: volumes,
543541
}
544542

545543
if shmVolume != nil && *shmVolume {
@@ -854,7 +852,7 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
854852
sidecarContainers []v1.Container
855853
podTemplate *v1.PodTemplateSpec
856854
volumeClaimTemplate *v1.PersistentVolumeClaim
857-
volumes []v1.Volume
855+
additionalVolumes = spec.AdditionalVolumes
858856
)
859857

860858
// Improve me. Please.
@@ -1007,8 +1005,10 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
10071005
// this is combined with the FSGroup in the section above
10081006
// to give read access to the postgres user
10091007
defaultMode := int32(0640)
1010-
volumes = append(volumes, v1.Volume{
1011-
Name: "tls-secret",
1008+
mountPath := "/tls"
1009+
additionalVolumes = append(additionalVolumes, acidv1.AdditionalVolume{
1010+
Name: spec.TLS.SecretName,
1011+
MountPath: mountPath,
10121012
VolumeSource: v1.VolumeSource{
10131013
Secret: &v1.SecretVolumeSource{
10141014
SecretName: spec.TLS.SecretName,
@@ -1017,13 +1017,6 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
10171017
},
10181018
})
10191019

1020-
mountPath := "/tls"
1021-
volumeMounts = append(volumeMounts, v1.VolumeMount{
1022-
MountPath: mountPath,
1023-
Name: "tls-secret",
1024-
ReadOnly: true,
1025-
})
1026-
10271020
// use the same filenames as Secret resources by default
10281021
certFile := ensurePath(spec.TLS.CertificateFile, mountPath, "tls.crt")
10291022
privateKeyFile := ensurePath(spec.TLS.PrivateKeyFile, mountPath, "tls.key")
@@ -1034,11 +1027,31 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
10341027
)
10351028

10361029
if spec.TLS.CAFile != "" {
1037-
caFile := ensurePath(spec.TLS.CAFile, mountPath, "")
1030+
// support scenario when the ca.crt resides in a different secret, diff path
1031+
mountPathCA := mountPath
1032+
if spec.TLS.CASecretName != "" {
1033+
mountPathCA = mountPath + "ca"
1034+
}
1035+
1036+
caFile := ensurePath(spec.TLS.CAFile, mountPathCA, "")
10381037
spiloEnvVars = append(
10391038
spiloEnvVars,
10401039
v1.EnvVar{Name: "SSL_CA_FILE", Value: caFile},
10411040
)
1041+
1042+
// the ca file from CASecretName secret takes priority
1043+
if spec.TLS.CASecretName != "" {
1044+
additionalVolumes = append(additionalVolumes, acidv1.AdditionalVolume{
1045+
Name: spec.TLS.CASecretName,
1046+
MountPath: mountPathCA,
1047+
VolumeSource: v1.VolumeSource{
1048+
Secret: &v1.SecretVolumeSource{
1049+
SecretName: spec.TLS.CASecretName,
1050+
DefaultMode: &defaultMode,
1051+
},
1052+
},
1053+
})
1054+
}
10421055
}
10431056
}
10441057

@@ -1108,8 +1121,7 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
11081121
c.OpConfig.PodAntiAffinityTopologyKey,
11091122
c.OpConfig.AdditionalSecretMount,
11101123
c.OpConfig.AdditionalSecretMountPath,
1111-
volumes,
1112-
spec.AdditionalVolumes)
1124+
additionalVolumes)
11131125

11141126
if err != nil {
11151127
return nil, fmt.Errorf("could not generate pod template: %v", err)
@@ -1614,11 +1626,11 @@ func (c *Cluster) generateCloneEnvironment(description *acidv1.CloneDescription)
16141626
c.logger.Info(msg, description.S3WalPath)
16151627

16161628
envs := []v1.EnvVar{
1617-
v1.EnvVar{
1629+
{
16181630
Name: "CLONE_WAL_S3_BUCKET",
16191631
Value: c.OpConfig.WALES3Bucket,
16201632
},
1621-
v1.EnvVar{
1633+
{
16221634
Name: "CLONE_WAL_BUCKET_SCOPE_SUFFIX",
16231635
Value: getBucketScopeSuffix(description.UID),
16241636
},
@@ -1790,7 +1802,6 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1beta1.CronJob, error) {
17901802
"",
17911803
c.OpConfig.AdditionalSecretMount,
17921804
c.OpConfig.AdditionalSecretMountPath,
1793-
nil,
17941805
[]acidv1.AdditionalVolume{}); err != nil {
17951806
return nil, fmt.Errorf("could not generate pod template for logical backup pod: %v", err)
17961807
}

0 commit comments

Comments
 (0)