Skip to content

Commit

Permalink
Separate rabbitmqcluster_controller into smaller files (#418)
Browse files Browse the repository at this point in the history
- separate according to different k8s/rmq concepts that
the Reconcile function is handling
- all broken down files have a corresponding  *_test.go
file which contains its integration tests
  • Loading branch information
ChunyiLyu committed Oct 28, 2020
1 parent 411eda9 commit 7fdb23c
Show file tree
Hide file tree
Showing 15 changed files with 1,274 additions and 1,077 deletions.
59 changes: 59 additions & 0 deletions controllers/pod_executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package controllers

import (
"bufio"
"bytes"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
)

type PodExecutor interface {
Exec(clientset *kubernetes.Clientset, clusterConfig *rest.Config, namespace, podName, containerName string, command ...string) (string, string, error)
}

func NewPodExecutor() PodExecutor { return &rabbitmqPodExecutor{} }

type rabbitmqPodExecutor struct{}

func (p *rabbitmqPodExecutor) Exec(clientset *kubernetes.Clientset, clusterConfig *rest.Config, namespace, podName, containerName string, command ...string) (string, string, error) {
request := clientset.CoreV1().RESTClient().
Post().
Resource("pods").
Name(podName).
Namespace(namespace).
SubResource("exec").
VersionedParams(&corev1.PodExecOptions{
Container: containerName,
Command: command,
Stdout: true,
Stderr: true,
Stdin: false,
}, scheme.ParameterCodec)

exec, err := remotecommand.NewSPDYExecutor(clusterConfig, "POST", request.URL())
if err != nil {
return "", "", err
}

stdOut := bytes.Buffer{}
stdErr := bytes.Buffer{}

err = exec.Stream(remotecommand.StreamOptions{
Stdout: bufio.NewWriter(&stdOut),
Stderr: bufio.NewWriter(&stdErr),
Stdin: nil,
Tty: false,
})
if err != nil {
return stdOut.String(), stdErr.String(), err
}
if stdErr.Len() > 0 {
return stdOut.String(), stdErr.String(), fmt.Errorf("%v", stdErr)
}

return stdOut.String(), "", nil
}

0 comments on commit 7fdb23c

Please sign in to comment.