Skip to content

Commit dd9c390

Browse files
authored
pick first container if postgres is not found (#1505)
* pick first container if postgres is not found * minor change
1 parent 7884af2 commit dd9c390

File tree

6 files changed

+45
-36
lines changed

6 files changed

+45
-36
lines changed

pkg/cluster/cluster.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ func (c *Cluster) compareStatefulSetWith(statefulSet *appsv1.StatefulSet) *compa
462462

463463
if len(c.Statefulset.Spec.Template.Spec.Volumes) != len(statefulSet.Spec.Template.Spec.Volumes) {
464464
needsReplace = true
465-
reasons = append(reasons, fmt.Sprintf("new statefulset's Volumes contains different number of volumes to the old one"))
465+
reasons = append(reasons, "new statefulset's volumes contains different number of volumes to the old one")
466466
}
467467

468468
// we assume any change in priority happens by rolling out a new priority class
@@ -476,7 +476,9 @@ func (c *Cluster) compareStatefulSetWith(statefulSet *appsv1.StatefulSet) *compa
476476

477477
// lazy Spilo update: modify the image in the statefulset itself but let its pods run with the old image
478478
// until they are re-created for other reasons, for example node rotation
479-
if c.OpConfig.EnableLazySpiloUpgrade && !reflect.DeepEqual(c.Statefulset.Spec.Template.Spec.Containers[0].Image, statefulSet.Spec.Template.Spec.Containers[0].Image) {
479+
effectivePodImage := getPostgresContainer(&c.Statefulset.Spec.Template.Spec).Image
480+
desiredImage := getPostgresContainer(&statefulSet.Spec.Template.Spec).Image
481+
if c.OpConfig.EnableLazySpiloUpgrade && !reflect.DeepEqual(effectivePodImage, desiredImage) {
480482
needsReplace = true
481483
reasons = append(reasons, "lazy Spilo update: new statefulset's pod image does not match the current one")
482484
}

pkg/cluster/exec.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"k8s.io/client-go/tools/remotecommand"
1313

1414
"github.com/zalando/postgres-operator/pkg/spec"
15+
"github.com/zalando/postgres-operator/pkg/util/constants"
1516
)
1617

1718
//ExecCommand executes arbitrary command inside the pod
@@ -31,14 +32,14 @@ func (c *Cluster) ExecCommand(podName *spec.NamespacedName, command ...string) (
3132
// iterate through all containers looking for the one running PostgreSQL.
3233
targetContainer := -1
3334
for i, cr := range pod.Spec.Containers {
34-
if cr.Name == c.containerName() {
35+
if cr.Name == constants.PostgresContainerName {
3536
targetContainer = i
3637
break
3738
}
3839
}
3940

4041
if targetContainer < 0 {
41-
return "", fmt.Errorf("could not find %s container to exec to", c.containerName())
42+
return "", fmt.Errorf("could not find %s container to exec to", constants.PostgresContainerName)
4243
}
4344

4445
req := c.KubeClient.RESTClient.Post().

pkg/cluster/k8sres.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ type spiloConfiguration struct {
6666
Bootstrap pgBootstrap `json:"bootstrap"`
6767
}
6868

69-
func (c *Cluster) containerName() string {
70-
return constants.PostgresContainerName
71-
}
72-
7369
func (c *Cluster) statefulSetName() string {
7470
return c.Name
7571
}
@@ -1157,10 +1153,10 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
11571153
c.logger.Debugf("Generating Spilo container, environment variables")
11581154
c.logger.Debugf("%v", spiloEnvVars)
11591155

1160-
spiloContainer := generateContainer(c.containerName(),
1156+
spiloContainer := generateContainer(constants.PostgresContainerName,
11611157
&effectiveDockerImage,
11621158
resourceRequirements,
1163-
deduplicateEnvVars(spiloEnvVars, c.containerName(), c.logger),
1159+
deduplicateEnvVars(spiloEnvVars, constants.PostgresContainerName, c.logger),
11641160
volumeMounts,
11651161
c.OpConfig.Resources.SpiloPrivileged,
11661162
c.OpConfig.Resources.SpiloAllowPrivilegeEscalation,
@@ -1392,6 +1388,9 @@ func (c *Cluster) getNumberOfInstances(spec *acidv1.PostgresSpec) int32 {
13921388
//
13931389
// see https://docs.okd.io/latest/dev_guide/shared_memory.html
13941390
func addShmVolume(podSpec *v1.PodSpec) {
1391+
1392+
postgresContainerIdx := 0
1393+
13951394
volumes := append(podSpec.Volumes, v1.Volume{
13961395
Name: constants.ShmVolumeName,
13971396
VolumeSource: v1.VolumeSource{
@@ -1403,16 +1402,18 @@ func addShmVolume(podSpec *v1.PodSpec) {
14031402

14041403
for i, container := range podSpec.Containers {
14051404
if container.Name == constants.PostgresContainerName {
1406-
mounts := append(container.VolumeMounts,
1407-
v1.VolumeMount{
1408-
Name: constants.ShmVolumeName,
1409-
MountPath: constants.ShmVolumePath,
1410-
})
1411-
1412-
podSpec.Containers[i].VolumeMounts = mounts
1405+
postgresContainerIdx = i
14131406
}
14141407
}
14151408

1409+
mounts := append(podSpec.Containers[postgresContainerIdx].VolumeMounts,
1410+
v1.VolumeMount{
1411+
Name: constants.ShmVolumeName,
1412+
MountPath: constants.ShmVolumePath,
1413+
})
1414+
1415+
podSpec.Containers[postgresContainerIdx].VolumeMounts = mounts
1416+
14161417
podSpec.Volumes = volumes
14171418
}
14181419

@@ -1458,11 +1459,8 @@ func (c *Cluster) addAdditionalVolumes(podSpec *v1.PodSpec,
14581459

14591460
// if no target container is defined assign it to postgres container
14601461
if len(additionalVolume.TargetContainers) == 0 {
1461-
for j := range podSpec.Containers {
1462-
if podSpec.Containers[j].Name == c.containerName() {
1463-
additionalVolumes[i].TargetContainers = []string{c.containerName()}
1464-
}
1465-
}
1462+
postgresContainer := getPostgresContainer(podSpec)
1463+
additionalVolumes[i].TargetContainers = []string{postgresContainer.Name}
14661464
}
14671465

14681466
for _, target := range additionalVolume.TargetContainers {

pkg/cluster/k8sres_test.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@ func TestShmVolume(t *testing.T) {
413413
Volumes: []v1.Volume{},
414414
Containers: []v1.Container{
415415
{
416-
Name: "postgres",
417416
VolumeMounts: []v1.VolumeMount{},
418417
},
419418
},
@@ -438,9 +437,10 @@ func TestShmVolume(t *testing.T) {
438437
}
439438
for _, tt := range tests {
440439
addShmVolume(tt.podSpec)
440+
postgresContainer := getPostgresContainer(tt.podSpec)
441441

442442
volumeName := tt.podSpec.Volumes[tt.shmPos].Name
443-
volumeMountName := tt.podSpec.Containers[0].VolumeMounts[tt.shmPos].Name
443+
volumeMountName := postgresContainer.VolumeMounts[tt.shmPos].Name
444444

445445
if volumeName != constants.ShmVolumeName {
446446
t.Errorf("%s %s: Expected volume %s was not created, have %s instead",
@@ -612,8 +612,9 @@ func TestSecretVolume(t *testing.T) {
612612
for _, tt := range tests {
613613
additionalSecretMount := "aws-iam-s3-role"
614614
additionalSecretMountPath := "/meta/credentials"
615+
postgresContainer := getPostgresContainer(tt.podSpec)
615616

616-
numMounts := len(tt.podSpec.Containers[0].VolumeMounts)
617+
numMounts := len(postgresContainer.VolumeMounts)
617618

618619
addSecretVolume(tt.podSpec, additionalSecretMount, additionalSecretMountPath)
619620

@@ -633,7 +634,8 @@ func TestSecretVolume(t *testing.T) {
633634
}
634635
}
635636

636-
numMountsCheck := len(tt.podSpec.Containers[0].VolumeMounts)
637+
postgresContainer = getPostgresContainer(tt.podSpec)
638+
numMountsCheck := len(postgresContainer.VolumeMounts)
637639

638640
if numMountsCheck != numMounts+1 {
639641
t.Errorf("Unexpected number of VolumeMounts: got %v instead of %v",
@@ -865,7 +867,8 @@ func testEnvs(cluster *Cluster, podSpec *v1.PodTemplateSpec, role PostgresRole)
865867
"CONNECTION_POOLER_PORT": false,
866868
}
867869

868-
envs := podSpec.Spec.Containers[0].Env
870+
container := getPostgresContainer(&podSpec.Spec)
871+
envs := container.Env
869872
for _, env := range envs {
870873
required[env.Name] = true
871874
}
@@ -1045,14 +1048,15 @@ func TestTLS(t *testing.T) {
10451048
}
10461049
assert.Contains(t, sts.Spec.Template.Spec.Volumes, volume, "the pod gets a secret volume")
10471050

1048-
assert.Contains(t, sts.Spec.Template.Spec.Containers[0].VolumeMounts, v1.VolumeMount{
1051+
postgresContainer := getPostgresContainer(&sts.Spec.Template.Spec)
1052+
assert.Contains(t, postgresContainer.VolumeMounts, v1.VolumeMount{
10491053
MountPath: "/tls",
10501054
Name: "my-secret",
10511055
}, "the volume gets mounted in /tls")
10521056

1053-
assert.Contains(t, sts.Spec.Template.Spec.Containers[0].Env, v1.EnvVar{Name: "SSL_CERTIFICATE_FILE", Value: "/tls/tls.crt"})
1054-
assert.Contains(t, sts.Spec.Template.Spec.Containers[0].Env, v1.EnvVar{Name: "SSL_PRIVATE_KEY_FILE", Value: "/tls/tls.key"})
1055-
assert.Contains(t, sts.Spec.Template.Spec.Containers[0].Env, v1.EnvVar{Name: "SSL_CA_FILE", Value: "/tls/ca.crt"})
1057+
assert.Contains(t, postgresContainer.Env, v1.EnvVar{Name: "SSL_CERTIFICATE_FILE", Value: "/tls/tls.crt"})
1058+
assert.Contains(t, postgresContainer.Env, v1.EnvVar{Name: "SSL_PRIVATE_KEY_FILE", Value: "/tls/tls.key"})
1059+
assert.Contains(t, postgresContainer.Env, v1.EnvVar{Name: "SSL_CA_FILE", Value: "/tls/ca.crt"})
10561060
}
10571061

10581062
func TestAdditionalVolume(t *testing.T) {
@@ -1147,7 +1151,7 @@ func TestAdditionalVolume(t *testing.T) {
11471151
}{
11481152
{
11491153
subTest: "checking volume mounts of postgres container",
1150-
container: cluster.containerName(),
1154+
container: constants.PostgresContainerName,
11511155
expectedMounts: []string{"pgdata", "test1", "test3", "test4"},
11521156
},
11531157
{

pkg/cluster/sync.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ func (c *Cluster) syncStatefulSet() error {
357357
// and
358358
// (b) some of the pods were not restarted when the lazy update was still in place
359359
for _, pod := range pods {
360-
effectivePodImage := c.getPostgresContainer(&pod.Spec).Image
361-
stsImage := c.getPostgresContainer(&desiredSts.Spec.Template.Spec).Image
360+
effectivePodImage := getPostgresContainer(&pod.Spec).Image
361+
stsImage := getPostgresContainer(&desiredSts.Spec.Template.Spec).Image
362362

363363
if stsImage != effectivePodImage {
364364
if err = c.markRollingUpdateFlagForPod(&pod, "pod not yet restarted due to lazy update"); err != nil {

pkg/cluster/util.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,17 @@ func (c *Cluster) logServiceChanges(role PostgresRole, old, new *v1.Service, isU
227227
}
228228
}
229229

230-
func (c *Cluster) getPostgresContainer(podSpec *v1.PodSpec) v1.Container {
231-
var pgContainer v1.Container
230+
func getPostgresContainer(podSpec *v1.PodSpec) (pgContainer v1.Container) {
232231
for _, container := range podSpec.Containers {
233232
if container.Name == constants.PostgresContainerName {
234233
pgContainer = container
235234
}
236235
}
236+
237+
// if no postgres container was found, take the first one in the podSpec
238+
if reflect.DeepEqual(pgContainer, v1.Container{}) && len(podSpec.Containers) > 0 {
239+
pgContainer = podSpec.Containers[0]
240+
}
237241
return pgContainer
238242
}
239243

0 commit comments

Comments
 (0)