Skip to content

Commit 7884af2

Browse files
authored
get postgres container by name, not index (#1504)
1 parent 48cdca6 commit 7884af2

File tree

5 files changed

+49
-33
lines changed

5 files changed

+49
-33
lines changed

pkg/cluster/exec.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ 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"
1615
)
1716

1817
//ExecCommand executes arbitrary command inside the pod
@@ -32,14 +31,14 @@ func (c *Cluster) ExecCommand(podName *spec.NamespacedName, command ...string) (
3231
// iterate through all containers looking for the one running PostgreSQL.
3332
targetContainer := -1
3433
for i, cr := range pod.Spec.Containers {
35-
if cr.Name == constants.PostgresContainerName {
34+
if cr.Name == c.containerName() {
3635
targetContainer = i
3736
break
3837
}
3938
}
4039

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

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

pkg/cluster/k8sres.go

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type spiloConfiguration struct {
6767
}
6868

6969
func (c *Cluster) containerName() string {
70-
return "postgres"
70+
return constants.PostgresContainerName
7171
}
7272

7373
func (c *Cluster) statefulSetName() string {
@@ -1401,14 +1401,18 @@ func addShmVolume(podSpec *v1.PodSpec) {
14011401
},
14021402
})
14031403

1404-
pgIdx := constants.PostgresContainerIdx
1405-
mounts := append(podSpec.Containers[pgIdx].VolumeMounts,
1406-
v1.VolumeMount{
1407-
Name: constants.ShmVolumeName,
1408-
MountPath: constants.ShmVolumePath,
1409-
})
1404+
for i, container := range podSpec.Containers {
1405+
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
1413+
}
1414+
}
14101415

1411-
podSpec.Containers[0].VolumeMounts = mounts
14121416
podSpec.Volumes = volumes
14131417
}
14141418

@@ -1439,54 +1443,58 @@ func (c *Cluster) addAdditionalVolumes(podSpec *v1.PodSpec,
14391443

14401444
volumes := podSpec.Volumes
14411445
mountPaths := map[string]acidv1.AdditionalVolume{}
1442-
for i, v := range additionalVolumes {
1443-
if previousVolume, exist := mountPaths[v.MountPath]; exist {
1446+
for i, additionalVolume := range additionalVolumes {
1447+
if previousVolume, exist := mountPaths[additionalVolume.MountPath]; exist {
14441448
msg := "Volume %+v cannot be mounted to the same path as %+v"
1445-
c.logger.Warningf(msg, v, previousVolume)
1449+
c.logger.Warningf(msg, additionalVolume, previousVolume)
14461450
continue
14471451
}
14481452

1449-
if v.MountPath == constants.PostgresDataMount {
1453+
if additionalVolume.MountPath == constants.PostgresDataMount {
14501454
msg := "Cannot mount volume on postgresql data directory, %+v"
1451-
c.logger.Warningf(msg, v)
1455+
c.logger.Warningf(msg, additionalVolume)
14521456
continue
14531457
}
14541458

1455-
if len(v.TargetContainers) == 0 {
1456-
spiloContainer := podSpec.Containers[0]
1457-
additionalVolumes[i].TargetContainers = []string{spiloContainer.Name}
1459+
// if no target container is defined assign it to postgres container
1460+
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+
}
14581466
}
14591467

1460-
for _, target := range v.TargetContainers {
1461-
if target == "all" && len(v.TargetContainers) != 1 {
1468+
for _, target := range additionalVolume.TargetContainers {
1469+
if target == "all" && len(additionalVolume.TargetContainers) != 1 {
14621470
msg := `Target containers could be either "all" or a list
14631471
of containers, mixing those is not allowed, %+v`
1464-
c.logger.Warningf(msg, v)
1472+
c.logger.Warningf(msg, additionalVolume)
14651473
continue
14661474
}
14671475
}
14681476

14691477
volumes = append(volumes,
14701478
v1.Volume{
1471-
Name: v.Name,
1472-
VolumeSource: v.VolumeSource,
1479+
Name: additionalVolume.Name,
1480+
VolumeSource: additionalVolume.VolumeSource,
14731481
},
14741482
)
14751483

1476-
mountPaths[v.MountPath] = v
1484+
mountPaths[additionalVolume.MountPath] = additionalVolume
14771485
}
14781486

14791487
c.logger.Infof("Mount additional volumes: %+v", additionalVolumes)
14801488

14811489
for i := range podSpec.Containers {
14821490
mounts := podSpec.Containers[i].VolumeMounts
1483-
for _, v := range additionalVolumes {
1484-
for _, target := range v.TargetContainers {
1491+
for _, additionalVolume := range additionalVolumes {
1492+
for _, target := range additionalVolume.TargetContainers {
14851493
if podSpec.Containers[i].Name == target || target == "all" {
14861494
mounts = append(mounts, v1.VolumeMount{
1487-
Name: v.Name,
1488-
MountPath: v.MountPath,
1489-
SubPath: v.SubPath,
1495+
Name: additionalVolume.Name,
1496+
MountPath: additionalVolume.MountPath,
1497+
SubPath: additionalVolume.SubPath,
14901498
})
14911499
}
14921500
}

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 := pod.Spec.Containers[0].Image
361-
stsImage := desiredSts.Spec.Template.Spec.Containers[0].Image
360+
effectivePodImage := c.getPostgresContainer(&pod.Spec).Image
361+
stsImage := c.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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ 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
232+
for _, container := range podSpec.Containers {
233+
if container.Name == constants.PostgresContainerName {
234+
pgContainer = container
235+
}
236+
}
237+
return pgContainer
238+
}
239+
230240
func (c *Cluster) getTeamMembers(teamID string) ([]string, error) {
231241

232242
if teamID == "" {

pkg/util/constants/kubernetes.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import "time"
55
// General kubernetes-related constants
66
const (
77
PostgresContainerName = "postgres"
8-
PostgresContainerIdx = 0
98
K8sAPIPath = "/apis"
109

1110
QueueResyncPeriodPod = 5 * time.Minute

0 commit comments

Comments
 (0)