Skip to content

Instantly share code, notes, and snippets.

@amy
Created April 22, 2019 22:51
Show Gist options
  • Save amy/d2dfc8238604130920c853569511d589 to your computer and use it in GitHub Desktop.
Save amy/d2dfc8238604130920c853569511d589 to your computer and use it in GitHub Desktop.
list, err := c.K8sClientset.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
logrus.Errorf("err1: %v", err)
return err
}
logrus.Infoln(list)
req := c.K8sClientset.RESTClient().Post().
Resource("pods").
Namespace("default").
Name("nginx-7cdbd8cdc9-wdsd8").
SubResource("exec")
req.VersionedParams(&v1.PodExecOptions{
Container: "nginx",
Command: []string{"/bin/sh", "-c", "ls"},
Stdout: true,
Stderr: true,
}, scheme.ParameterCodec)
logrus.Infoln("URL: ", req.URL().String())
executor, err := remotecommand.NewSPDYExecutor(c.K8sRestConfig, "POST", req.URL())
if err != nil {
logrus.Error(err)
return err
}
//////
var stdout, stderr bytes.Buffer
streamOptions := remotecommand.StreamOptions{
Stdout: &stdout,
Stderr: &stderr,
}
errCh := make(chan error)
go func() {
err := executor.Stream(streamOptions)
errCh <- err
logrus.Errorf("Error 1: %v", err)
logrus.Infoln("ERROR 2")
spew.Dump(err)
}()
timeoutCh := time.After(time.Minute * 1)
select {
case err := <-errCh:
x := err.(*k8serrors.StatusError)
logrus.Infoln(x.DebugError())
logrus.Infoln("START DUMP")
spew.Dump(err)
spew.Dump(x.ErrStatus)
return err
case <-timeoutCh:
return errors.Errorf("timed out after %v", time.Minute*1)
}
///////
logrus.Infof("stdout: %s", stdout.String())
logrus.Errorf("stderr: %s", stderr.String())
logrus.Errorf("err2: %v", err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment