Skip to content

Commit cb3711b

Browse files
committed
discussion with Chris
1 parent d04694f commit cb3711b

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

internal/controller/postgrescluster/pgbouncer.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,14 @@ func (r *Reconciler) reconcilePGBouncer(
6868
func setPGBouncerLogfile(cluster *v1beta1.PostgresCluster) string {
6969
logfile := naming.PGBouncerFullLogPath
7070

71-
if dest, ok := cluster.Spec.Proxy.PGBouncer.Config.Global["logfile"]; ok {
72-
logfile = dest
71+
if cluster.Spec.Proxy == nil || cluster.Spec.Proxy.PGBouncer == nil {
72+
return ""
73+
}
74+
75+
if cluster.Spec.Proxy.PGBouncer.Config.Global != nil {
76+
if dest, ok := cluster.Spec.Proxy.PGBouncer.Config.Global["logfile"]; ok {
77+
logfile = dest
78+
}
7379
}
7480

7581
return logfile
@@ -478,7 +484,9 @@ func (r *Reconciler) generatePGBouncerDeployment(
478484
// Do not add environment variables describing services in this namespace.
479485
deploy.Spec.Template.Spec.EnableServiceLinks = initialize.Bool(false)
480486

481-
deploy.Spec.Template.Spec.SecurityContext = util.PodSecurityContext(ctx, 2, cluster.Spec.SupplementalGroups)
487+
deploy.Spec.Template.Spec.SecurityContext = util.PodSecurityContext(2,
488+
cluster.Spec.SupplementalGroups, initialize.FromPointer(cluster.Spec.OpenShift),
489+
)
482490

483491
// set the image pull secrets, if any exist
484492
deploy.Spec.Template.Spec.ImagePullSecrets = cluster.Spec.ImagePullSecrets

internal/util/pod_security.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55
package util
66

77
import (
8-
"context"
9-
108
corev1 "k8s.io/api/core/v1"
119

1210
"github.com/crunchydata/postgres-operator/internal/initialize"
13-
"github.com/crunchydata/postgres-operator/internal/kubernetes"
1411
)
1512

1613
// PodSecurityContext returns a v1.PodSecurityContext for cluster that can write
1714
// to PersistentVolumes.
18-
func PodSecurityContext(ctx context.Context, fsgroup int64, supplementalGroups []int64) *corev1.PodSecurityContext {
15+
func PodSecurityContext(fsgroup int64, supplementalGroups []int64, openshift bool) *corev1.PodSecurityContext {
1916
psc := initialize.PodSecurityContext()
2017

2118
// Use the specified supplementary groups except for root. The CRD has
@@ -33,9 +30,7 @@ func PodSecurityContext(ctx context.Context, fsgroup int64, supplementalGroups [
3330
// - https://cloud.redhat.com/blog/a-guide-to-openshift-and-uids
3431
// - https://docs.k8s.io/tasks/configure-pod-container/security-context/
3532
// - https://docs.openshift.com/container-platform/4.8/authentication/managing-security-context-constraints.html
36-
if !kubernetes.Has(ctx, kubernetes.API{
37-
Group: "security.openshift.io", Kind: "SecurityContextConstraints",
38-
}) {
33+
if !openshift {
3934
psc.FSGroup = initialize.Int64(fsgroup)
4035
}
4136

internal/util/pod_security_test.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,48 @@
55
package util
66

77
import (
8-
"context"
98
"testing"
109

1110
"gotest.tools/v3/assert"
1211

13-
"github.com/crunchydata/postgres-operator/internal/kubernetes"
1412
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
1513
)
1614

1715
func TestPodSecurityContext(t *testing.T) {
18-
ctx := context.Background()
19-
assert.Assert(t, cmp.MarshalMatches(PodSecurityContext(ctx, 2, []int64{}), `
16+
t.Run("Non-Openshift", func(t *testing.T) {
17+
assert.Assert(t, cmp.MarshalMatches(PodSecurityContext(2, []int64{}, false), `
2018
fsGroup: 2
2119
fsGroupChangePolicy: OnRootMismatch
2220
`))
2321

24-
supplementalGroups := []int64{3, 4}
25-
assert.Assert(t, cmp.MarshalMatches(PodSecurityContext(ctx, 26, supplementalGroups), `
22+
supplementalGroups := []int64{3, 4}
23+
assert.Assert(t, cmp.MarshalMatches(PodSecurityContext(26, supplementalGroups, false), `
2624
fsGroup: 26
2725
fsGroupChangePolicy: OnRootMismatch
2826
supplementalGroups:
2927
- 3
3028
- 4
3129
`))
30+
})
3231

33-
ctx = kubernetes.NewAPIContext(ctx, kubernetes.NewAPISet(kubernetes.API{
34-
Group: "security.openshift.io", Version: "v1",
35-
Kind: "SecurityContextConstraints",
36-
}))
37-
assert.Assert(t, cmp.MarshalMatches(PodSecurityContext(ctx, 2, []int64{}),
38-
`fsGroupChangePolicy: OnRootMismatch`))
32+
t.Run("OpenShift", func(t *testing.T) {
33+
assert.Assert(t, cmp.MarshalMatches(PodSecurityContext(2, []int64{}, true),
34+
`fsGroupChangePolicy: OnRootMismatch`))
3935

40-
assert.Assert(t, cmp.MarshalMatches(PodSecurityContext(ctx, 2, supplementalGroups), `
36+
supplementalGroups := []int64{3, 4}
37+
assert.Assert(t, cmp.MarshalMatches(PodSecurityContext(2, supplementalGroups, true), `
4138
fsGroupChangePolicy: OnRootMismatch
4239
supplementalGroups:
4340
- 3
4441
- 4
4542
`))
43+
})
4644

4745
t.Run("NoRootGID", func(t *testing.T) {
48-
supplementalGroups = []int64{999, 0, 100, 0}
49-
assert.DeepEqual(t, []int64{999, 100}, PodSecurityContext(ctx, 2, supplementalGroups).SupplementalGroups)
46+
supplementalGroups := []int64{999, 0, 100, 0}
47+
assert.DeepEqual(t, []int64{999, 100}, PodSecurityContext(2, supplementalGroups, false).SupplementalGroups)
5048

5149
supplementalGroups = []int64{0}
52-
assert.Assert(t, PodSecurityContext(ctx, 2, supplementalGroups).SupplementalGroups == nil)
50+
assert.Assert(t, PodSecurityContext(2, supplementalGroups, false).SupplementalGroups == nil)
5351
})
5452
}

0 commit comments

Comments
 (0)