Skip to content

Commit f62d0c4

Browse files
Merge pull request #353 from jgreat/smarter-strip-log-headers
revert dropping header, for only setting RawTerminal when container tty
2 parents 2df0ea0 + b425d7d commit f62d0c4

File tree

4 files changed

+18
-46
lines changed

4 files changed

+18
-46
lines changed

glide.lock

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ import:
77
- package: golang.org/x/net
88
subpackages:
99
- websocket
10-
- package: github.com/hashicorp/go-version

router/pump.go

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
"github.com/fsouza/go-dockerclient"
14-
"github.com/hashicorp/go-version"
1514
)
1615

1716
var allowTTY bool
@@ -181,10 +180,6 @@ func (p *LogsPump) Run() error {
181180

182181
func (p *LogsPump) pumpLogs(event *docker.APIEvents, backlog bool, inactivityTimeout time.Duration) {
183182
id := normalID(event.ID)
184-
185-
dockerVersion, err := p.client.Version()
186-
assert(err, "Could not get Docker Version endpoint")
187-
188183
container, err := p.client.InspectContainer(id)
189184
assert(err, "pump")
190185
if ignoreContainerTTY(container) {
@@ -214,9 +209,16 @@ func (p *LogsPump) pumpLogs(event *docker.APIEvents, backlog bool, inactivityTim
214209
debug("pump.pumpLogs():", id, "pump exists")
215210
return
216211
}
212+
213+
// RawTerminal with container Tty=false injects binary headers into
214+
// the log stream that show up as garbage unicode characters
215+
rawTerminal := false
216+
if allowTTY && container.Config.Tty {
217+
rawTerminal = true
218+
}
217219
outrd, outwr := io.Pipe()
218220
errrd, errwr := io.Pipe()
219-
p.pumps[id] = newContainerPump(container, dockerVersion, outrd, errrd)
221+
p.pumps[id] = newContainerPump(container, outrd, errrd)
220222
p.mu.Unlock()
221223
p.update(event)
222224
go func() {
@@ -232,7 +234,7 @@ func (p *LogsPump) pumpLogs(event *docker.APIEvents, backlog bool, inactivityTim
232234
Tail: tail,
233235
Since: sinceTime.Unix(),
234236
InactivityTimeout: inactivityTimeout,
235-
RawTerminal: allowTTY,
237+
RawTerminal: rawTerminal,
236238
})
237239
if err != nil {
238240
debug("pump.pumpLogs():", id, "stopped with error:", err)
@@ -344,32 +346,14 @@ type containerPump struct {
344346
logstreams map[chan *Message]*Route
345347
}
346348

347-
func newContainerPump(container *docker.Container, dockerVersion *docker.Env, stdout, stderr io.Reader) *containerPump {
348-
apiVersion := dockerVersion.Get("ApiVersion")
349-
debug("API Version: ", apiVersion)
350-
debug("TTY is: ", container.Config.Tty)
351-
earliestAPI, _ := version.NewVersion("1.13")
352-
currentAPI, err := version.NewVersion(apiVersion)
353-
assert(err, "current docker api version is not valid version string")
354-
hasHeaders := false
355-
if container.Config.Tty == false {
356-
if currentAPI.Compare(earliestAPI) >= 0 {
357-
hasHeaders = true
358-
}
359-
}
360-
debug("hasHeaders set to: ", hasHeaders)
361-
349+
func newContainerPump(container *docker.Container, stdout, stderr io.Reader) *containerPump {
362350
cp := &containerPump{
363351
container: container,
364352
logstreams: make(map[chan *Message]*Route),
365353
}
366-
pump := func(source string, hasHeaders bool, input io.Reader) {
354+
pump := func(source string, input io.Reader) {
367355
buf := bufio.NewReader(input)
368356
for {
369-
// Discard header bytes from log message if TTY is false and api > 1.13.
370-
if hasHeaders {
371-
buf.Discard(8)
372-
}
373357
line, err := buf.ReadString('\n')
374358
if err != nil {
375359
if err != io.EOF {
@@ -385,8 +369,8 @@ func newContainerPump(container *docker.Container, dockerVersion *docker.Env, st
385369
})
386370
}
387371
}
388-
go pump("stdout", hasHeaders, stdout)
389-
go pump("stderr", hasHeaders, stderr)
372+
go pump("stdout", stdout)
373+
go pump("stderr", stderr)
390374
return cp
391375
}
392376

router/pump_test.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,7 @@ func TestPumpContainerRename(t *testing.T) {
135135
Name: "foo",
136136
Config: config,
137137
}
138-
version := &docker.Env{
139-
"ApiVersion=1.33",
140-
}
141-
p.pumps["8dfafdbc3a40"] = newContainerPump(container, version, os.Stdout, os.Stderr)
138+
p.pumps["8dfafdbc3a40"] = newContainerPump(container, os.Stdout, os.Stderr)
142139
if name := p.pumps["8dfafdbc3a40"].container.Name; name != "foo" {
143140
t.Errorf("containerPump should have name: 'foo' got name: '%s'", name)
144141
}
@@ -156,10 +153,7 @@ func TestPumpNewContainerPump(t *testing.T) {
156153
ID: "8dfafdbc3a40",
157154
Config: config,
158155
}
159-
version := &docker.Env{
160-
"ApiVersion=1.33",
161-
}
162-
pump := newContainerPump(container, version, os.Stdout, os.Stderr)
156+
pump := newContainerPump(container, os.Stdout, os.Stderr)
163157
if pump == nil {
164158
t.Error("pump nil")
165159
return
@@ -174,10 +168,7 @@ func TestPumpContainerPump(t *testing.T) {
174168
ID: "8dfafdbc3a40",
175169
Config: config,
176170
}
177-
version := &docker.Env{
178-
"ApiVersion=1.33",
179-
}
180-
pump := newContainerPump(container, version, os.Stdout, os.Stderr)
171+
pump := newContainerPump(container, os.Stdout, os.Stderr)
181172
logstream, route := make(chan *Message), &Route{}
182173
go func() {
183174
for msg := range logstream {

0 commit comments

Comments
 (0)