Created
April 2, 2018 07:39
-
-
Save hossainemruz/d0bc2fc112ec9b2bea849e076da4f398 to your computer and use it in GitHub Desktop.
cient-go execute command on pod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"flag" | |
"fmt" | |
"log" | |
"github.com/appscode/go/crypto/rand" | |
"github.com/appscode/go/types" | |
apps "k8s.io/api/apps/v1beta1" | |
core "k8s.io/api/core/v1" | |
extensions "k8s.io/api/extensions/v1beta1" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/apimachinery/pkg/util/intstr" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/kubernetes/scheme" | |
"k8s.io/client-go/rest" | |
"k8s.io/client-go/tools/clientcmd" | |
"k8s.io/client-go/tools/remotecommand" | |
_ "k8s.io/kubernetes/pkg/apis/apps" | |
) | |
var ( | |
masterURL string | |
kubeConfig string | |
) | |
const ( | |
TEST_HEADLESS_SERVICE = "headless" | |
TestSourceDataVolumeName = "source-data" | |
TestSourceDataMountPath = "/source/data" | |
) | |
func init() { | |
flag.StringVar(&kubeConfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.") | |
flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") | |
} | |
func main() { | |
flag.Parse() | |
cfg, err := clientcmd.BuildConfigFromFlags(masterURL, kubeConfig) | |
if err != nil { | |
log.Fatalf("Can't build config. Reason: ", err.Error()) | |
} | |
kubeClient, err := kubernetes.NewForConfig(cfg) | |
if err != nil { | |
fmt.Println(kubeClient.LegacyPrefix) | |
log.Fatalf("Can't build kubeClient. Reason:", err.Error()) | |
} | |
out, err := ExecOnPod(kubeClient, cfg, "stash-demo-b66b9cdfd-cshzf", "default", "ls","/source/data/stash-data") | |
if err != nil { | |
log.Fatalln(err) | |
} | |
fmt.Println(out) | |
//oneliner.PrettyJson(HeadlessService()) | |
//oneliner.PrettyJson(ReplicaSet()) | |
} | |
//ExecOnPod executes arbitrary command inside the pod | |
func ExecOnPod(kubeClient kubernetes.Interface, config *rest.Config, podName string, namespace string, command ...string) (string, error) { | |
var ( | |
execOut bytes.Buffer | |
execErr bytes.Buffer | |
) | |
pod, err := kubeClient.CoreV1().Pods(namespace).Get(podName, metav1.GetOptions{}) | |
if err != nil { | |
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") | |
} | |
req := kubeClient.CoreV1().RESTClient().Post(). | |
Resource("pods"). | |
Name(podName). | |
Namespace(namespace). | |
SubResource("exec") | |
req.VersionedParams(&core.PodExecOptions{ | |
Container: pod.Spec.Containers[0].Name, | |
Command: command, | |
Stdout: true, | |
Stderr: true, | |
}, scheme.ParameterCodec) | |
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL()) | |
if err != nil { | |
return "", fmt.Errorf("failed to init executor: %v", err) | |
} | |
err = exec.Stream(remotecommand.StreamOptions{ | |
Stdout: &execOut, | |
Stderr: &execErr, | |
}) | |
if err != nil { | |
return "", fmt.Errorf("could not execute: %v", err) | |
} | |
if execErr.Len() > 0 { | |
return "", fmt.Errorf("stderr: %v", execErr.String()) | |
} | |
return execOut.String(), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment