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

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,

View File

@ -15,7 +15,6 @@ import (
"k8s.io/client-go/pkg/apis/apps/v1beta1"
policyv1beta1 "k8s.io/client-go/pkg/apis/policy/v1beta1"
"k8s.io/client-go/rest"
)
// EventType contains type of the events for the TPRs and Pods received from Kubernetes

View File

@ -4,6 +4,7 @@ import "time"
// General kubernetes-related constants
const (
PostgresContainerName = "postgres"
K8sAPIPath = "/apis"
StatefulsetDeletionInterval = 1 * time.Second
StatefulsetDeletionTimeout = 30 * time.Second