Skip to content

Commit 59795d4

Browse files
Merge pull request zalando#314 from zalando-incubator/volume_resize_with_multiple_containers
Fix exec into pods to resize volumes for multi-container pods.
2 parents 6ee0349 + 04b6605 commit 59795d4

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

pkg/cluster/exec.go

Lines changed: 13 additions & 3 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-incubator/postgres-operator/pkg/spec"
15+
"github.com/zalando-incubator/postgres-operator/pkg/util/constants"
1516
)
1617

1718
//ExecCommand executes arbitrary command inside the pod
@@ -28,8 +29,17 @@ func (c *Cluster) ExecCommand(podName *spec.NamespacedName, command ...string) (
2829
return "", fmt.Errorf("could not get pod info: %v", err)
2930
}
3031

31-
if len(pod.Spec.Containers) != 1 {
32-
return "", fmt.Errorf("could not determine which container to use")
32+
// iterate through all containers looking for the one running PostgreSQL.
33+
targetContainer := -1
34+
for i, cr := range pod.Spec.Containers {
35+
if cr.Name == constants.PostgresContainerName {
36+
targetContainer = i
37+
break
38+
}
39+
}
40+
41+
if targetContainer < 0 {
42+
return "", fmt.Errorf("could not find %s container to exec to", constants.PostgresContainerName)
3343
}
3444

3545
req := c.KubeClient.RESTClient.Post().
@@ -38,7 +48,7 @@ func (c *Cluster) ExecCommand(podName *spec.NamespacedName, command ...string) (
3848
Namespace(podName.Namespace).
3949
SubResource("exec")
4050
req.VersionedParams(&v1.PodExecOptions{
41-
Container: pod.Spec.Containers[0].Name,
51+
Container: pod.Spec.Containers[targetContainer].Name,
4252
Command: command,
4353
Stdout: true,
4454
Stderr: true,

pkg/spec/types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"k8s.io/client-go/pkg/apis/apps/v1beta1"
1616
policyv1beta1 "k8s.io/client-go/pkg/apis/policy/v1beta1"
1717
"k8s.io/client-go/rest"
18-
1918
)
2019

2120
// EventType contains type of the events for the TPRs and Pods received from Kubernetes

pkg/util/constants/kubernetes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import "time"
44

55
// General kubernetes-related constants
66
const (
7+
PostgresContainerName = "postgres"
78
K8sAPIPath = "/apis"
89
StatefulsetDeletionInterval = 1 * time.Second
910
StatefulsetDeletionTimeout = 30 * time.Second

0 commit comments

Comments
 (0)