Skip to content

Commit ad1ec94

Browse files
committed
Make changes for K8s version
k8s 1.28 cannot handle the .? validation we wanted to use in CEL, so this PR uses a less complex version.
1 parent f8cc2a7 commit ad1ec94

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

config/crd/bases/postgres-operator.crunchydata.com_postgresclusters.yaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16508,11 +16508,12 @@ spec:
1650816508
type: object
1650916509
type: object
1651016510
x-kubernetes-validations:
16511-
- message: config.global.logfile destination is restricted to
16512-
'/tmp/logs/pgbouncer/' or an existing additional volume
16513-
rule: self.?config.global.logfile.optMap(f, f.startsWith("/tmp/logs/pgbouncer/")
16514-
|| (self.?volumes.additional.hasValue() && self.volumes.additional.exists(v,
16515-
f.startsWith("/volumes/" + v.name)))).orValue(true)
16511+
- message: logfile destination is restricted to '/tmp/logs/pgbouncer/'
16512+
or an existing additional volume
16513+
rule: '!has(self.config) || !has(self.config.global) || !has(self.config.global.logfile)
16514+
|| self.config.global.logfile.startsWith(''/tmp/logs/pgbouncer/'')
16515+
|| (has(self.volumes) && has(self.volumes.additional) && self.volumes.additional.exists(x,
16516+
self.config.global.logfile.startsWith("/volumes/"+x.name)))'
1651616517
required:
1651716518
- pgBouncer
1651816519
type: object

pkg/apis/postgres-operator.crunchydata.com/v1/pgbouncer_types.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
)
1010

1111
// PGBouncerPodSpec defines the desired state of a PgBouncer connection pooler.
12-
// +kubebuilder:validation:XValidation:rule=`self.?config.global.logfile.optMap(f, f.startsWith("/tmp/logs/pgbouncer/") || (self.?volumes.additional.hasValue() && self.volumes.additional.exists(v, f.startsWith("/volumes/" + v.name)))).orValue(true)`,message=`config.global.logfile destination is restricted to '/tmp/logs/pgbouncer/' or an existing additional volume`
12+
// +kubebuilder:validation:XValidation:rule=`!has(self.config) || !has(self.config.global) || !has(self.config.global.logfile) || self.config.global.logfile.startsWith('/tmp/logs/pgbouncer/') || (has(self.volumes) && has(self.volumes.additional) && self.volumes.additional.exists(x, self.config.global.logfile.startsWith("/volumes/"+x.name)))`,message=`logfile destination is restricted to '/tmp/logs/pgbouncer/' or an existing additional volume`
13+
// ---
14+
// TODO: the `.?` CEL syntax is unsupported in k8s 1.28, so we cannot use the optional field syntax
15+
// of `self.?config.global.logfile` and `self.?volumes.additional`
1316
type PGBouncerPodSpec struct {
1417
v1beta1.PGBouncerPodSpec `json:",inline"`
1518
}

pkg/apis/postgres-operator.crunchydata.com/validation.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,17 @@ The `additionalProperties` property indicates that the keys are unknown; these f
9595
> When possible, use [OpenAPI properties](#FIXME) rather than CEL rules.
9696
> The former do not affect the CRD [validation budget](#FIXME). <!-- https://imgur.com/CzpJn3j -->
9797
98+
## Optional field syntax
99+
100+
CEL offers a safe traversal/retrieval through the use of `?` as an [optional field marker].
101+
102+
As an example, when attempting to retrieve `self.config.global.logfile`, in older (but still supported)
103+
versions of k8s, we may need to incrementally check the path: `has(self.config) && has(self.config.global)...`
104+
105+
With the optional field marker, we can use that field safely without checking the entire path:
106+
`self.?config.global.logfile` will return a value or no value; and anything using that value will
107+
likewise be considered optional.
108+
109+
The optional field syntax is only available in K8s 1.29+.
110+
111+
[optional field marker]: https://pkg.go.dev/github.com/google/cel-go/cel#hdr-Syntax_Changes-OptionalTypes.

0 commit comments

Comments
 (0)