Skip to content

Commit 4953b04

Browse files
pooknullegegunes
andauthored
K8SPG-613: replace initImage with initContainer (#1117)
* K8SPG-613: replace initImage with initContainer https://perconadev.atlassian.net/browse/K8SPG-613 * fix unit-tests * fix unit-test * fix unit-test * fix upgrade-consistency test --------- Co-authored-by: Ege Güneş <[email protected]>
1 parent 9d8b028 commit 4953b04

21 files changed

+5522
-119
lines changed

build/crd/crunchy/generated/postgres-operator.crunchydata.com_postgresclusters.yaml

+521-4
Large diffs are not rendered by default.

build/crd/percona/generated/pgv2.percona.com_perconapgclusters.yaml

+521-4
Large diffs are not rendered by default.

config/crd/bases/pgv2.percona.com_perconapgclusters.yaml

+521-4
Large diffs are not rendered by default.

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

+521-4
Large diffs are not rendered by default.

deploy/bundle.yaml

+1,042-8
Large diffs are not rendered by default.

deploy/cr.yaml

+50-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,31 @@ metadata:
1010
# - percona.com/delete-backups
1111
spec:
1212
crVersion: 2.7.0
13-
# initImage: perconalab/percona-postgresql-operator:main
13+
# initContainer:
14+
# image: perconalab/percona-postgresql-operator:main
15+
# resources:
16+
# limits:
17+
# cpu: 2.0
18+
# memory: 4Gi
19+
# containerSecurityContext:
20+
# fsGroup: 1001
21+
# runAsUser: 1001
22+
# runAsNonRoot: true
23+
# fsGroupChangePolicy: "OnRootMismatch"
24+
# runAsGroup: 1001
25+
# seLinuxOptions:
26+
# type: spc_t
27+
# level: s0:c123,c456
28+
# seccompProfile:
29+
# type: Localhost
30+
# localhostProfile: localhost/profile.json
31+
# supplementalGroups:
32+
# - 1001
33+
# sysctls:
34+
# - name: net.ipv4.tcp_keepalive_time
35+
# value: "600"
36+
# - name: net.ipv4.tcp_keepalive_intvl
37+
# value: "60"
1438
# metadata:
1539
# annotations:
1640
# example-annotation: value
@@ -324,8 +348,31 @@ spec:
324348
# metadata:
325349
# labels:
326350
image: perconalab/percona-postgresql-operator:main-pgbackrest17
327-
# initImage: perconalab/percona-postgresql-operator:main
328-
#
351+
# initContainer:
352+
# image: perconalab/percona-postgresql-operator:main
353+
# resources:
354+
# limits:
355+
# cpu: 2.0
356+
# memory: 4Gi
357+
# containerSecurityContext:
358+
# fsGroup: 1001
359+
# runAsUser: 1001
360+
# runAsNonRoot: true
361+
# fsGroupChangePolicy: "OnRootMismatch"
362+
# runAsGroup: 1001
363+
# seLinuxOptions:
364+
# type: spc_t
365+
# level: s0:c123,c456
366+
# seccompProfile:
367+
# type: Localhost
368+
# localhostProfile: localhost/profile.json
369+
# supplementalGroups:
370+
# - 1001
371+
# sysctls:
372+
# - name: net.ipv4.tcp_keepalive_time
373+
# value: "600"
374+
# - name: net.ipv4.tcp_keepalive_intvl
375+
# value: "60"
329376
# containers:
330377
# pgbackrest:
331378
# resources:

deploy/crd.yaml

+1,042-8
Large diffs are not rendered by default.

deploy/cw-bundle.yaml

+1,042-8
Large diffs are not rendered by default.

e2e-tests/tests/upgrade-consistency/01-create-cluster.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ commands:
1111
get_cr \
1212
| yq eval '
1313
.spec.crVersion="2.5.0"' - \
14+
| yq eval ".spec.backups.pgbackrest.image=\"${IMAGE_BASE}:main-ppg$PG_VER-pgbackrest\"" - \
1415
| kubectl -n "${NAMESPACE}" apply -f -

e2e-tests/tests/upgrade-consistency/03-patch-cluster.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ commands:
1111
get_cr \
1212
| yq eval '
1313
.spec.crVersion="2.7.0"' - \
14+
| yq eval ".spec.backups.pgbackrest.image=\"$IMAGE_BACKREST\"" - \
1415
| kubectl -n "${NAMESPACE}" apply -f -
1516
1617
sleep 10

internal/controller/postgrescluster/pgbackrest.go

+32-24
Original file line numberDiff line numberDiff line change
@@ -825,19 +825,42 @@ func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.P
825825
ImagePullPolicy: postgresCluster.Spec.ImagePullPolicy,
826826
Name: naming.PGBackRestRepoContainerName,
827827
SecurityContext: initialize.RestrictedSecurityContext(postgresCluster.CompareVersion("2.5.0") >= 0), // K8SPG-260
828-
// K8SPG-613
829-
VolumeMounts: []corev1.VolumeMount{
830-
{
831-
Name: pNaming.CrunchyBinVolumeName,
832-
MountPath: pNaming.CrunchyBinVolumePath,
833-
},
834-
},
835828
}
836829

837830
if postgresCluster.Spec.Backups.PGBackRest.Jobs != nil {
838831
container.Resources = postgresCluster.Spec.Backups.PGBackRest.Jobs.Resources
839832
}
840833

834+
// K8SPG-613
835+
var initContainers []corev1.Container
836+
volumes := []corev1.Volume{}
837+
if postgresCluster.CompareVersion("2.7.0") >= 0 {
838+
container.VolumeMounts = []corev1.VolumeMount{
839+
{
840+
Name: pNaming.CrunchyBinVolumeName,
841+
MountPath: pNaming.CrunchyBinVolumePath,
842+
},
843+
}
844+
initContainers = []corev1.Container{
845+
k8s.InitContainer(
846+
naming.PGBackRestRepoContainerName,
847+
initImage,
848+
postgresCluster.Spec.ImagePullPolicy,
849+
initialize.RestrictedSecurityContext(true),
850+
container.Resources,
851+
&postgresCluster.Spec.Backups.PGBackRest,
852+
),
853+
}
854+
volumes = []corev1.Volume{
855+
{
856+
Name: pNaming.CrunchyBinVolumeName,
857+
VolumeSource: corev1.VolumeSource{
858+
EmptyDir: &corev1.EmptyDirVolumeSource{},
859+
},
860+
},
861+
}
862+
}
863+
841864
jobSpec := &batchv1.JobSpec{
842865
Template: corev1.PodTemplateSpec{
843866
ObjectMeta: metav1.ObjectMeta{Labels: labels, Annotations: annotations},
@@ -857,24 +880,9 @@ func generateBackupJobSpecIntent(ctx context.Context, postgresCluster *v1beta1.P
857880
SecurityContext: initialize.PodSecurityContext(),
858881
ServiceAccountName: serviceAccountName,
859882
// K8SPG-613
860-
Volumes: []corev1.Volume{
861-
{
862-
Name: pNaming.CrunchyBinVolumeName,
863-
VolumeSource: corev1.VolumeSource{
864-
EmptyDir: &corev1.EmptyDirVolumeSource{},
865-
},
866-
},
867-
},
883+
Volumes: volumes,
868884
// K8SPG-613
869-
InitContainers: []corev1.Container{
870-
k8s.InitContainer(
871-
naming.PGBackRestRepoContainerName,
872-
initImage,
873-
postgresCluster.Spec.ImagePullPolicy,
874-
initialize.RestrictedSecurityContext(postgresCluster.CompareVersion("2.5.0") >= 0),
875-
container.Resources,
876-
),
877-
},
885+
InitContainers: initContainers,
878886
},
879887
},
880888
}

internal/controller/postgrescluster/pgbackrest_test.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,23 @@ func fakePostgresCluster(clusterName, namespace, clusterUID string,
5757
Name: clusterName,
5858
Namespace: namespace,
5959
UID: types.UID(clusterUID),
60+
Labels: map[string]string{
61+
naming.LabelVersion: "2.7.0",
62+
},
6063
},
6164
Spec: v1beta1.PostgresClusterSpec{
6265
Port: initialize.Int32(5432),
6366
Shutdown: initialize.Bool(false),
6467
PostgresVersion: 13,
65-
ImagePullSecrets: []corev1.LocalObjectReference{{
66-
Name: "myImagePullSecret"},
68+
ImagePullSecrets: []corev1.LocalObjectReference{
69+
{
70+
Name: "myImagePullSecret",
71+
},
72+
},
73+
InitContainer: &v1beta1.InitContainerSpec{
74+
Image: "some-init-image",
6775
},
68-
InitImage: "some-init-image",
69-
Image: "example.com/crunchy-postgres-ha:test",
76+
Image: "example.com/crunchy-postgres-ha:test",
7077
InstanceSets: []v1beta1.PostgresInstanceSetSpec{{
7178
Name: "instance1",
7279
DataVolumeClaimSpec: corev1.PersistentVolumeClaimSpec{
@@ -334,6 +341,8 @@ schedulerName: default-scheduler
334341
securityContext:
335342
fsGroup: 26
336343
fsGroupChangePolicy: OnRootMismatch
344+
serviceAccount: hippocluster-pgbackrest
345+
serviceAccountName: hippocluster-pgbackrest
337346
shareProcessNamespace: true
338347
terminationGracePeriodSeconds: 30
339348
tolerations:
@@ -2456,7 +2465,7 @@ func TestGenerateBackupJobIntent(t *testing.T) {
24562465
Name: "",
24572466
Namespace: "",
24582467
Labels: map[string]string{
2459-
naming.LabelVersion: "2.5.0",
2468+
naming.LabelVersion: "2.7.0",
24602469
},
24612470
},
24622471
},
@@ -2778,7 +2787,7 @@ func TestGenerateRestoreJobIntent(t *testing.T) {
27782787
err := r.generateRestoreJobIntent(&v1beta1.PostgresCluster{
27792788
ObjectMeta: metav1.ObjectMeta{
27802789
Labels: map[string]string{
2781-
naming.LabelVersion: "2.5.0",
2790+
naming.LabelVersion: "2.7.0",
27822791
},
27832792
},
27842793
}, "", "",
@@ -2826,7 +2835,7 @@ func TestGenerateRestoreJobIntent(t *testing.T) {
28262835
ObjectMeta: metav1.ObjectMeta{
28272836
Name: "test",
28282837
Labels: map[string]string{
2829-
naming.LabelVersion: "2.5.0",
2838+
naming.LabelVersion: "2.7.0",
28302839
},
28312840
},
28322841
Spec: v1beta1.PostgresClusterSpec{
@@ -2995,7 +3004,7 @@ func TestObserveRestoreEnv(t *testing.T) {
29953004
Name: clusterName,
29963005
Namespace: namespace,
29973006
Labels: map[string]string{
2998-
naming.LabelVersion: "2.5.0",
3007+
naming.LabelVersion: "2.7.0",
29993008
},
30003009
},
30013010
}
@@ -3232,7 +3241,7 @@ func TestPrepareForRestore(t *testing.T) {
32323241
Name: clusterName,
32333242
Namespace: namespace,
32343243
Labels: map[string]string{
3235-
naming.LabelVersion: "2.5.0",
3244+
naming.LabelVersion: "2.7.0",
32363245
},
32373246
},
32383247
}

percona/controller/pgcluster/testutils_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ func readDefaultCR(name, namespace string) (*v2.PerconaPGCluster, error) {
119119
if cr.Annotations == nil {
120120
cr.Annotations = make(map[string]string)
121121
}
122-
cr.Spec.InitImage = "some-image"
122+
cr.Spec.InitContainer = &v1beta1.InitContainerSpec{
123+
Image: "some-image",
124+
}
123125
cr.Annotations[pNaming.AnnotationCustomPatroniVersion] = "4.0.0"
124126
cr.Namespace = namespace
125127
cr.Status.Postgres.Version = cr.Spec.PostgresVersion

percona/k8s/testutils_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ func readDefaultCR(name, namespace string) (*v2.PerconaPGCluster, error) {
8282
if cr.Annotations == nil {
8383
cr.Annotations = make(map[string]string)
8484
}
85-
cr.Spec.InitImage = "some-image"
85+
cr.Spec.InitContainer = &v1beta1.InitContainerSpec{
86+
Image: "some-image",
87+
}
8688
cr.Annotations[pNaming.AnnotationCustomPatroniVersion] = "4.0.0"
8789
cr.Namespace = namespace
8890
cr.Status.Postgres.Version = cr.Spec.PostgresVersion

percona/k8s/util.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,19 @@ func GetWatchNamespace() (string, error) {
3939
return ns, nil
4040
}
4141

42-
func InitContainer(component, image string,
42+
func InitContainer(componentName, image string,
4343
pullPolicy corev1.PullPolicy,
4444
secCtx *corev1.SecurityContext,
4545
resources corev1.ResourceRequirements,
46+
component ComponentWithInit,
4647
) corev1.Container {
48+
if component != nil && component.GetInitContainer() != nil && component.GetInitContainer().Resources != nil {
49+
resources = *component.GetInitContainer().Resources
50+
}
51+
if component != nil && component.GetInitContainer() != nil && component.GetInitContainer().ContainerSecurityContext != nil {
52+
secCtx = component.GetInitContainer().ContainerSecurityContext
53+
}
54+
4755
volumeMounts := []corev1.VolumeMount{
4856
{
4957
Name: naming.CrunchyBinVolumeName,
@@ -52,7 +60,7 @@ func InitContainer(component, image string,
5260
}
5361

5462
return corev1.Container{
55-
Name: component + "-init",
63+
Name: componentName + "-init",
5664
Image: image,
5765
ImagePullPolicy: pullPolicy,
5866
VolumeMounts: volumeMounts,
@@ -65,15 +73,15 @@ func InitContainer(component, image string,
6573
}
6674

6775
type ComponentWithInit interface {
68-
GetInitImage() string
76+
GetInitContainer() *v1beta1.InitContainerSpec
6977
}
7078

7179
func InitImage(ctx context.Context, cl client.Reader, cluster *v1beta1.PostgresCluster, componentWithInit ComponentWithInit) (string, error) {
72-
if componentWithInit != nil && componentWithInit.GetInitImage() != "" {
73-
return componentWithInit.GetInitImage(), nil
80+
if componentWithInit != nil && componentWithInit.GetInitContainer() != nil && componentWithInit.GetInitContainer().Image != "" {
81+
return componentWithInit.GetInitContainer().Image, nil
7482
}
75-
if cluster != nil && len(cluster.Spec.InitImage) > 0 {
76-
return cluster.Spec.InitImage, nil
83+
if cluster != nil && cluster.Spec.InitContainer != nil && len(cluster.Spec.InitContainer.Image) > 0 {
84+
return cluster.Spec.InitContainer.Image, nil
7785
}
7886
return operatorImage(ctx, cl)
7987
}

0 commit comments

Comments
 (0)