Skip to content

Commit

Permalink
Merge pull request #2 from fntlnz/feature/run-tty
Browse files Browse the repository at this point in the history
feat(pkg/attacher): attach with a TTY
  • Loading branch information
fntlnz committed Nov 24, 2018
2 parents 827c417 + 06dac2b commit c88b3ca
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module github.com/fntlnz/kubectl-trace

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/docker/docker v1.13.1 // indirect
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96 // indirect
github.com/elazarl/goproxy v0.0.0-20181111060418-2ce16c963a8a // indirect
github.com/ghodss/yaml v1.0.0 // indirect
Expand All @@ -16,6 +17,7 @@ require (
github.com/json-iterator/go v1.1.5 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/mitchellh/go-homedir v1.0.0
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
Expand All @@ -42,4 +44,5 @@ require (
k8s.io/api v0.0.0-20181004124137-fd83cbc87e76
k8s.io/apimachinery v0.0.0-20180913025736-6dd46049f395
k8s.io/client-go v9.0.0+incompatible
k8s.io/kubernetes v1.12.2
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/docker v1.13.1 h1:5VBhsO6ckUxB0A8CE5LlUJdXzik9cbEbBTQ/ggeml7M=
github.com/docker/docker v1.13.1/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96 h1:cenwrSVm+Z7QLSV/BsnenAOcDXdX4cMv4wP0B/5QbPg=
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
github.com/elazarl/goproxy v0.0.0-20181111060418-2ce16c963a8a h1:A4wNiqeKqU56ZhtnzJCTyPZ1+cyu8jKtIchQ3TtxHgw=
Expand Down Expand Up @@ -43,6 +45,8 @@ github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDe
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/mapstructure v1.0.0 h1:vVpGvMXJPqSDh2VYHF7gsfQj8Ncx+Xw5Y1KHeTRY+7I=
github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
Expand Down Expand Up @@ -114,3 +118,5 @@ k8s.io/apimachinery v0.0.0-20180913025736-6dd46049f395 h1:X+c9tYTDc9Pmt+Z1YSMqmU
k8s.io/apimachinery v0.0.0-20180913025736-6dd46049f395/go.mod h1:ccL7Eh7zubPUSh9A3USN90/OzHNSVN6zxzde07TDCL0=
k8s.io/client-go v9.0.0+incompatible h1:NXWpDuPFeVB5lYP1fTqJUtwigjtmRXJNtndnN53ldGI=
k8s.io/client-go v9.0.0+incompatible/go.mod h1:7vJpHMYJwNQCWgzmNV+VYUl1zCObLyodBc8nIyt8L5s=
k8s.io/kubernetes v1.12.2 h1:J94sY8nziFyzMNhnbD5CiSZTo7yWytir0ia+vJuwYfc=
k8s.io/kubernetes v1.12.2/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk=
29 changes: 23 additions & 6 deletions pkg/attacher/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes/scheme"
tcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/kubernetes/pkg/kubectl/util/term"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -83,9 +84,12 @@ func (a *Attacher) Attach(selector, namespace string) {
restClient := a.CoreV1Client.RESTClient().(*restclient.RESTClient)
containerName := pod.Spec.Containers[0].Name

attfn := defaultAttachFunc(restClient, pod.Name, containerName, pod.Namespace, a.Config)
t, err := setupTTY(os.Stdout, os.Stdin)
if err != nil {
return false, err
}
err = t.Safe(defaultAttachFunc(restClient, pod.Name, containerName, pod.Namespace, a.Config, t))

err = attfn()
if err != nil {
a.logger.Warn("attach retry", zap.Error(err))
return false, nil
Expand All @@ -96,8 +100,7 @@ func (a *Attacher) Attach(selector, namespace string) {
<-a.ctx.Done()
}

func defaultAttachFunc(restClient *restclient.RESTClient, podName string, containerName string, namespace string, config *restclient.Config) func() error {
raw := false
func defaultAttachFunc(restClient *restclient.RESTClient, podName string, containerName string, namespace string, config *restclient.Config, t term.TTY) func() error {
return func() error {
req := restClient.Post().
Resource("pods").
Expand All @@ -109,11 +112,11 @@ func defaultAttachFunc(restClient *restclient.RESTClient, podName string, contai
Stdin: true,
Stdout: true,
Stderr: true,
TTY: raw,
TTY: t.Raw,
}, scheme.ParameterCodec)

att := &defaultRemoteAttach{}
return att.Attach("POST", req.URL(), config, os.Stdin, os.Stdout, os.Stderr, raw, nil)
return att.Attach("POST", req.URL(), config, t.In, t.Out, os.Stderr, t.Raw, t.MonitorSize(t.GetSize()))
}
}

Expand All @@ -132,3 +135,17 @@ func (*defaultRemoteAttach) Attach(method string, url *url.URL, config *restclie
TerminalSizeQueue: terminalSizeQueue,
})
}

func setupTTY(out io.Writer, in io.Reader) (term.TTY, error) {
t := term.TTY{
Out: out,
In: in,
Raw: true,
}

if !t.IsTerminalIn() {
return t, fmt.Errorf("unable to use a TTY if the input is not a terminal")
}

return t, nil
}
2 changes: 2 additions & 0 deletions pkg/tracejob/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ func (t *TraceJobClient) CreateJob(nj TraceJob) (*batchv1.Job, error) {
Name: nj.Name,
Image: "quay.io/fntlnz/kubectl-trace-bpftrace:master", //TODO(fntlnz): yes this should be configurable!
Command: bpfTraceCmd,
TTY: true,
Stdin: true,
VolumeMounts: []apiv1.VolumeMount{
apiv1.VolumeMount{
Name: "program",
Expand Down

0 comments on commit c88b3ca

Please sign in to comment.