Fix exec into pods to resize volumes for multi-container pods.

The original code assumed only one container per pod.
This commit is contained in:
Oleksii Kliukin
2018-06-04 14:51:39 +02:00
parent 6ee0349536
commit 04b660519a
3 changed files with 14 additions and 4 deletions
+13 -3
View File
@@ -12,6 +12,7 @@ import (
"k8s.io/client-go/tools/remotecommand"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
"github.com/zalando-incubator/postgres-operator/pkg/util/constants"
)
//ExecCommand executes arbitrary command inside the pod
@@ -28,8 +29,17 @@ func (c *Cluster) ExecCommand(podName *spec.NamespacedName, command ...string) (
return "", fmt.Errorf("could not get pod info: %v", err)
}
if len(pod.Spec.Containers) != 1 {
return "", fmt.Errorf("could not determine which container to use")
// iterate through all containers looking for the one running PostgreSQL.
targetContainer := -1
for i, cr := range pod.Spec.Containers {
if cr.Name == constants.PostgresContainerName {
targetContainer = i
break
}
}
if targetContainer < 0 {
return "", fmt.Errorf("could not find %s container to exec to", constants.PostgresContainerName)
}
req := c.KubeClient.RESTClient.Post().
@@ -38,7 +48,7 @@ func (c *Cluster) ExecCommand(podName *spec.NamespacedName, command ...string) (
Namespace(podName.Namespace).
SubResource("exec")
req.VersionedParams(&v1.PodExecOptions{
Container: pod.Spec.Containers[0].Name,
Container: pod.Spec.Containers[targetContainer].Name,
Command: command,
Stdout: true,
Stderr: true,