Skip to content

Commit 43f2c3a

Browse files
committed
Convert container security context test to kuttl
This creates a Kuttl test to verify a PostgresCluster's containers' security context settings which replaces the current EnvTest implementation. Issue: [sc-14262]
1 parent c13154e commit 43f2c3a

File tree

3 files changed

+212
-81
lines changed

3 files changed

+212
-81
lines changed

internal/controller/postgrescluster/cluster_test.go

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ package postgrescluster
2020

2121
import (
2222
"context"
23-
"os"
24-
"strings"
2523
"testing"
26-
"time"
2724

2825
"github.com/pkg/errors"
2926
"go.opentelemetry.io/otel"
@@ -36,7 +33,6 @@ import (
3633
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3734
"k8s.io/apimachinery/pkg/runtime"
3835
"k8s.io/apimachinery/pkg/runtime/schema"
39-
"k8s.io/apimachinery/pkg/util/wait"
4036
"k8s.io/client-go/tools/record"
4137
"sigs.k8s.io/controller-runtime/pkg/client"
4238
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -600,83 +596,6 @@ func TestCustomAnnotations(t *testing.T) {
600596
})
601597
}
602598

603-
func TestContainerSecurityContext(t *testing.T) {
604-
if !strings.EqualFold(os.Getenv("USE_EXISTING_CLUSTER"), "true") {
605-
t.Skip("Test requires pods to be created")
606-
}
607-
608-
ctx := context.Background()
609-
env, cc := setupKubernetes(t)
610-
require.ParallelCapacity(t, 1)
611-
612-
reconciler := &Reconciler{
613-
Client: cc,
614-
Owner: client.FieldOwner(t.Name()),
615-
Recorder: new(record.FakeRecorder),
616-
Tracer: otel.Tracer(t.Name()),
617-
}
618-
619-
var err error
620-
reconciler.PodExec, err = newPodExecutor(env.Config)
621-
assert.NilError(t, err)
622-
623-
cluster := testCluster()
624-
cluster.Namespace = setupNamespace(t, cc).Name
625-
626-
assert.NilError(t, errors.WithStack(reconciler.Client.Create(ctx, cluster)))
627-
t.Cleanup(func() {
628-
// Remove finalizers, if any, so the namespace can terminate.
629-
assert.Check(t, client.IgnoreNotFound(
630-
reconciler.Client.Patch(ctx, cluster, client.RawPatch(
631-
client.Merge.Type(), []byte(`{"metadata":{"finalizers":[]}}`)))))
632-
})
633-
634-
pods := &corev1.PodList{}
635-
assert.NilError(t, wait.Poll(time.Second, Scale(2*time.Minute), func() (bool, error) {
636-
// Reconcile the cluster
637-
result, err := reconciler.Reconcile(ctx, reconcile.Request{
638-
NamespacedName: client.ObjectKeyFromObject(cluster),
639-
})
640-
if err != nil {
641-
return false, err
642-
}
643-
if result.Requeue {
644-
return false, nil
645-
}
646-
647-
err = reconciler.Client.List(ctx, pods,
648-
client.InNamespace(cluster.Namespace),
649-
client.MatchingLabels{
650-
naming.LabelCluster: cluster.Name,
651-
})
652-
if err != nil {
653-
return false, err
654-
}
655-
656-
// Can expect 4 pods from a cluster
657-
// instance, repo-host, pgbouncer, backup(s)
658-
if len(pods.Items) < 4 {
659-
return false, nil
660-
}
661-
return true, nil
662-
}))
663-
664-
// Once we have a pod list with pods of each type, check that the
665-
// pods containers have the expected Security Context options
666-
for _, pod := range pods.Items {
667-
for _, container := range pod.Spec.Containers {
668-
assert.Equal(t, *container.SecurityContext.Privileged, false)
669-
assert.Equal(t, *container.SecurityContext.ReadOnlyRootFilesystem, true)
670-
assert.Equal(t, *container.SecurityContext.AllowPrivilegeEscalation, false)
671-
}
672-
for _, initContainer := range pod.Spec.InitContainers {
673-
assert.Equal(t, *initContainer.SecurityContext.Privileged, false)
674-
assert.Equal(t, *initContainer.SecurityContext.ReadOnlyRootFilesystem, true)
675-
assert.Equal(t, *initContainer.SecurityContext.AllowPrivilegeEscalation, false)
676-
}
677-
}
678-
}
679-
680599
func TestGenerateClusterPrimaryService(t *testing.T) {
681600
_, cc := setupKubernetes(t)
682601
require.ParallelCapacity(t, 0)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apiVersion: postgres-operator.crunchydata.com/v1beta1
2+
kind: PostgresCluster
3+
metadata:
4+
name: security-context
5+
labels: { postgres-operator-test: kuttl }
6+
spec:
7+
postgresVersion: ${KUTTL_PG_VERSION}
8+
instances:
9+
- name: instance1
10+
replicas: 1
11+
dataVolumeClaimSpec: { accessModes: [ReadWriteOnce], resources: { requests: { storage: 1Gi } } }
12+
backups:
13+
pgbackrest:
14+
repos:
15+
- name: repo1
16+
volume:
17+
volumeClaimSpec: { accessModes: [ReadWriteOnce], resources: { requests: { storage: 1Gi } } }
18+
proxy:
19+
pgBouncer:
20+
replicas: 1
21+
userInterface:
22+
pgAdmin:
23+
dataVolumeClaimSpec: { accessModes: [ReadWriteOnce], resources: { requests: { storage: 1Gi } } }
24+
monitoring:
25+
pgmonitor:
26+
exporter: {}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
apiVersion: postgres-operator.crunchydata.com/v1beta1
2+
kind: PostgresCluster
3+
metadata:
4+
name: security-context
5+
status:
6+
instances:
7+
- name: instance1
8+
readyReplicas: 1
9+
replicas: 1
10+
updatedReplicas: 1
11+
---
12+
apiVersion: batch/v1
13+
kind: Job
14+
metadata:
15+
labels:
16+
postgres-operator.crunchydata.com/cluster: security-context
17+
postgres-operator.crunchydata.com/pgbackrest-backup: replica-create
18+
status:
19+
succeeded: 1
20+
---
21+
# initial pgBackRest backup
22+
apiVersion: v1
23+
kind: Pod
24+
metadata:
25+
labels:
26+
postgres-operator.crunchydata.com/cluster: security-context
27+
postgres-operator.crunchydata.com/pgbackrest: ""
28+
postgres-operator.crunchydata.com/pgbackrest-backup: replica-create
29+
postgres-operator.crunchydata.com/pgbackrest-repo: repo1
30+
spec:
31+
containers:
32+
- name: pgbackrest
33+
securityContext:
34+
allowPrivilegeEscalation: false
35+
privileged: false
36+
readOnlyRootFilesystem: true
37+
runAsNonRoot: true
38+
---
39+
# instance
40+
apiVersion: v1
41+
kind: Pod
42+
metadata:
43+
labels:
44+
postgres-operator.crunchydata.com/cluster: security-context
45+
postgres-operator.crunchydata.com/data: postgres
46+
postgres-operator.crunchydata.com/instance-set: instance1
47+
postgres-operator.crunchydata.com/patroni: security-context-ha
48+
postgres-operator.crunchydata.com/role: master
49+
spec:
50+
containers:
51+
- name: database
52+
securityContext:
53+
allowPrivilegeEscalation: false
54+
privileged: false
55+
readOnlyRootFilesystem: true
56+
runAsNonRoot: true
57+
- name: replication-cert-copy
58+
securityContext:
59+
allowPrivilegeEscalation: false
60+
privileged: false
61+
readOnlyRootFilesystem: true
62+
runAsNonRoot: true
63+
- name: pgbackrest
64+
securityContext:
65+
allowPrivilegeEscalation: false
66+
privileged: false
67+
readOnlyRootFilesystem: true
68+
runAsNonRoot: true
69+
- name: pgbackrest-config
70+
securityContext:
71+
allowPrivilegeEscalation: false
72+
privileged: false
73+
readOnlyRootFilesystem: true
74+
runAsNonRoot: true
75+
- name: exporter
76+
securityContext:
77+
allowPrivilegeEscalation: false
78+
privileged: false
79+
readOnlyRootFilesystem: true
80+
runAsNonRoot: true
81+
initContainers:
82+
- name: postgres-startup
83+
securityContext:
84+
allowPrivilegeEscalation: false
85+
privileged: false
86+
readOnlyRootFilesystem: true
87+
runAsNonRoot: true
88+
- name: nss-wrapper-init
89+
securityContext:
90+
allowPrivilegeEscalation: false
91+
privileged: false
92+
readOnlyRootFilesystem: true
93+
runAsNonRoot: true
94+
---
95+
# pgAdmin
96+
apiVersion: v1
97+
kind: Pod
98+
metadata:
99+
labels:
100+
postgres-operator.crunchydata.com/cluster: security-context
101+
postgres-operator.crunchydata.com/data: pgadmin
102+
postgres-operator.crunchydata.com/role: pgadmin
103+
statefulset.kubernetes.io/pod-name: security-context-pgadmin-0
104+
name: security-context-pgadmin-0
105+
spec:
106+
containers:
107+
- name: pgadmin
108+
securityContext:
109+
allowPrivilegeEscalation: false
110+
privileged: false
111+
readOnlyRootFilesystem: true
112+
runAsNonRoot: true
113+
initContainers:
114+
- name: pgadmin-startup
115+
securityContext:
116+
allowPrivilegeEscalation: false
117+
privileged: false
118+
readOnlyRootFilesystem: true
119+
runAsNonRoot: true
120+
- name: nss-wrapper-init
121+
securityContext:
122+
allowPrivilegeEscalation: false
123+
privileged: false
124+
readOnlyRootFilesystem: true
125+
runAsNonRoot: true
126+
---
127+
# pgBouncer
128+
apiVersion: v1
129+
kind: Pod
130+
metadata:
131+
labels:
132+
postgres-operator.crunchydata.com/cluster: security-context
133+
postgres-operator.crunchydata.com/role: pgbouncer
134+
spec:
135+
containers:
136+
- name: pgbouncer
137+
securityContext:
138+
allowPrivilegeEscalation: false
139+
privileged: false
140+
readOnlyRootFilesystem: true
141+
runAsNonRoot: true
142+
- name: pgbouncer-config
143+
securityContext:
144+
allowPrivilegeEscalation: false
145+
privileged: false
146+
readOnlyRootFilesystem: true
147+
runAsNonRoot: true
148+
---
149+
# pgBackRest repo
150+
apiVersion: v1
151+
kind: Pod
152+
metadata:
153+
labels:
154+
postgres-operator.crunchydata.com/cluster: security-context
155+
postgres-operator.crunchydata.com/data: pgbackrest
156+
postgres-operator.crunchydata.com/pgbackrest: ""
157+
postgres-operator.crunchydata.com/pgbackrest-dedicated: ""
158+
statefulset.kubernetes.io/pod-name: security-context-repo-host-0
159+
name: security-context-repo-host-0
160+
spec:
161+
containers:
162+
- name: pgbackrest
163+
securityContext:
164+
allowPrivilegeEscalation: false
165+
privileged: false
166+
readOnlyRootFilesystem: true
167+
runAsNonRoot: true
168+
- name: pgbackrest-config
169+
securityContext:
170+
allowPrivilegeEscalation: false
171+
privileged: false
172+
readOnlyRootFilesystem: true
173+
runAsNonRoot: true
174+
initContainers:
175+
- name: pgbackrest-log-dir
176+
securityContext:
177+
allowPrivilegeEscalation: false
178+
privileged: false
179+
readOnlyRootFilesystem: true
180+
runAsNonRoot: true
181+
- name: nss-wrapper-init
182+
securityContext:
183+
allowPrivilegeEscalation: false
184+
privileged: false
185+
readOnlyRootFilesystem: true
186+
runAsNonRoot: true

0 commit comments

Comments
 (0)