Created
April 22, 2019 22:51
-
-
Save amy/d2dfc8238604130920c853569511d589 to your computer and use it in GitHub Desktop.
This file contains 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
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