diff --git a/pkg/cluster/exec.go b/pkg/cluster/exec.go index f11ceb457..81843aca6 100644 --- a/pkg/cluster/exec.go +++ b/pkg/cluster/exec.go @@ -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, diff --git a/pkg/spec/types.go b/pkg/spec/types.go index 66d6a73fa..204d16aa7 100644 --- a/pkg/spec/types.go +++ b/pkg/spec/types.go @@ -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 diff --git a/pkg/util/constants/kubernetes.go b/pkg/util/constants/kubernetes.go index 7f25bb9e7..2604f124d 100644 --- a/pkg/util/constants/kubernetes.go +++ b/pkg/util/constants/kubernetes.go @@ -4,6 +4,7 @@ import "time" // General kubernetes-related constants const ( + PostgresContainerName = "postgres" K8sAPIPath = "/apis" StatefulsetDeletionInterval = 1 * time.Second StatefulsetDeletionTimeout = 30 * time.Second