Skip to content

Commit

Permalink
Print logs from one goroutine
Browse files Browse the repository at this point in the history
Based on:
oandrew@8723308

Fixes: https://github.com/wercker/stern/issues/96

Also introduce GitHub actions

Signed-off-by: Andrew Seigner <[email protected]>
  • Loading branch information
siggy committed Sep 7, 2019
1 parent 57d17f4 commit 3bee4a6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CI

on:
pull_request: {}
push:
branches:
- master
tags:
- "*"

jobs:
go_unit_tests:
name: Go unit tests
runs-on: ubuntu-18.04
container:
image: golang:1.12.9
steps:
- name: Checkout code
uses: actions/checkout@v1
- name: Go unit tests
run: |
go test -cover -race -v -mod=readonly ./...
16 changes: 15 additions & 1 deletion stern/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package stern

import (
"context"
"fmt"
"os"

"github.com/pkg/errors"
"github.com/wercker/stern/kubernetes"
Expand Down Expand Up @@ -49,6 +51,18 @@ func Run(ctx context.Context, config *Config) error {
}

tails := make(map[string]*Tail)
logC := make(chan string, 1024)

go func() {
for {
select {
case str := <-logC:
fmt.Fprintf(os.Stdout, str)
case <-ctx.Done():
break
}
}
}()

go func() {
for p := range added {
Expand All @@ -67,7 +81,7 @@ func Run(ctx context.Context, config *Config) error {
})
tails[id] = tail

tail.Start(ctx, clientset.CoreV1().Pods(p.Namespace))
tail.Start(ctx, clientset.CoreV1().Pods(p.Namespace), logC)
}
}()

Expand Down
22 changes: 14 additions & 8 deletions stern/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package stern

import (
"bufio"
"bytes"
"context"
"fmt"
"hash/fnv"
Expand All @@ -26,7 +27,7 @@ import (
"github.com/fatih/color"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/typed/core/v1"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
)

Expand Down Expand Up @@ -82,17 +83,17 @@ func determineColor(podName string) (podColor, containerColor *color.Color) {
}

// Start starts tailing
func (t *Tail) Start(ctx context.Context, i v1.PodInterface) {
func (t *Tail) Start(ctx context.Context, i v1.PodInterface, logC chan<- string) {
t.podColor, t.containerColor = determineColor(t.PodName)

go func() {
g := color.New(color.FgHiGreen, color.Bold).SprintFunc()
p := t.podColor.SprintFunc()
c := t.containerColor.SprintFunc()
if t.Options.Namespace {
fmt.Fprintf(os.Stderr, "%s %s %s › %s\n", g("+"), p(t.Namespace), p(t.PodName), c(t.ContainerName))
logC <- fmt.Sprintf("%s %s %s › %s\n", g("+"), p(t.Namespace), p(t.PodName), c(t.ContainerName))
} else {
fmt.Fprintf(os.Stderr, "%s %s › %s\n", g("+"), p(t.PodName), c(t.ContainerName))
logC <- fmt.Sprintf("%s %s › %s\n", g("+"), p(t.PodName), c(t.ContainerName))
}

req := i.GetLogs(t.PodName, &corev1.PodLogOptions{
Expand Down Expand Up @@ -140,12 +141,12 @@ func (t *Tail) Start(ctx context.Context, i v1.PodInterface) {
break
}
}
if !matches {
if !matches {
continue OUTER
}
}

t.Print(str)
logC <- t.Print(str)
}
}()

Expand All @@ -168,7 +169,7 @@ func (t *Tail) Close() {
}

// Print prints a color coded log message with the pod and container names
func (t *Tail) Print(msg string) {
func (t *Tail) Print(msg string) string {
vm := Log{
Message: msg,
Namespace: t.Namespace,
Expand All @@ -177,10 +178,15 @@ func (t *Tail) Print(msg string) {
PodColor: t.podColor,
ContainerColor: t.containerColor,
}
err := t.tmpl.Execute(os.Stdout, vm)

var buf bytes.Buffer
err := t.tmpl.Execute(&buf, vm)
if err != nil {
os.Stderr.WriteString(fmt.Sprintf("expanding template failed: %s", err))
return ""
}

return buf.String()
}

// Log is the object which will be used together with the template to generate
Expand Down

0 comments on commit 3bee4a6

Please sign in to comment.