mirror of
https://github.com/zalando/postgres-operator.git
synced 2026-10-04 19:53:09 +02:00
Command exec inside the Pod method
This commit is contained in:
@@ -18,6 +18,7 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
RestConfig *rest.Config
|
||||
KubeClient *kubernetes.Clientset
|
||||
RestClient *rest.RESTClient
|
||||
EtcdClient etcdclient.KeysAPI
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"k8s.io/client-go/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/client/unversioned/remotecommand"
|
||||
remotecommandconsts "k8s.io/apimachinery/pkg/util/remotecommand"
|
||||
|
||||
"github.bus.zalan.do/acid/postgres-operator/pkg/spec"
|
||||
)
|
||||
|
||||
func (c *Controller) ExecCommand(podName spec.NamespacedName, command []string) (string, error) {
|
||||
var (
|
||||
execOut bytes.Buffer
|
||||
execErr bytes.Buffer
|
||||
)
|
||||
|
||||
pod, err := c.KubeClient.Pods(podName.Namespace).Get(podName.Name)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Can't get Pod info: %s", err)
|
||||
}
|
||||
|
||||
if len(pod.Spec.Containers) != 1 {
|
||||
return "", fmt.Errorf("Can't determine which container to use")
|
||||
}
|
||||
|
||||
req := c.RestClient.Post().
|
||||
Resource("pods").
|
||||
Name(podName.Name).
|
||||
Namespace(podName.Namespace).
|
||||
SubResource("exec")
|
||||
req.VersionedParams(&api.PodExecOptions{
|
||||
Container: pod.Spec.Containers[0].Name,
|
||||
Command: command,
|
||||
Stdout: true,
|
||||
Stderr: true,
|
||||
}, api.ParameterCodec)
|
||||
|
||||
exec, err := remotecommand.NewExecutor(c.RestConfig, "POST", req.URL())
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Failed to init executor: %s", err)
|
||||
}
|
||||
|
||||
err = exec.Stream(remotecommand.StreamOptions{
|
||||
SupportedProtocols: remotecommandconsts.SupportedStreamingProtocols,
|
||||
Stdout: &execOut,
|
||||
Stderr: &execErr,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Can't execute: %s", err)
|
||||
}
|
||||
|
||||
if execErr.Len() > 0 {
|
||||
return "", fmt.Errorf("Stderr: %s", execErr.String())
|
||||
}
|
||||
|
||||
return execOut.String(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user