-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
187 lines (150 loc) · 3.62 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"context"
"fmt"
"log"
"math"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"time"
flag "github.com/spf13/pflag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"
)
const (
debugAddress = ":6060"
logSecondsOffset = 10
)
var (
sinceSeconds = int64(math.Ceil(float64(logSecondsOffset) / float64(time.Second)))
)
func main() {
var (
debug bool
kubeContext string
kubeconfig string
labels string
namespace string
timestamps bool
version bool
)
flags := flag.NewFlagSet("k8stail", flag.ExitOnError)
flags.Usage = func() {
flags.PrintDefaults()
}
flags.StringVar(&kubeContext, "context", "", "Kubernetes context")
flags.BoolVar(&debug, "debug", false, "Debug mode using pprof (http://localhost:6060)")
flags.StringVar(&kubeconfig, "kubeconfig", "", "Path of kubeconfig")
flags.StringVarP(&labels, "labels", "l", "", "Label filter query")
flags.StringVarP(&namespace, "namespace", "n", "", "Kubernetes namespace")
flags.BoolVarP(×tamps, "timestamps", "t", false, "Include timestamps on each line")
flags.BoolVarP(&version, "version", "v", false, "Print version")
if err := flags.Parse(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if kubeconfig == "" {
if os.Getenv("KUBECONFIG") != "" {
kubeconfig = os.Getenv("KUBECONFIG")
} else {
kubeconfig = clientcmd.RecommendedHomeFile
}
}
if version {
printVersion()
os.Exit(0)
}
if debug {
go func() {
log.Println(http.ListenAndServe(debugAddress, nil))
}()
}
clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
&clientcmd.ConfigOverrides{CurrentContext: kubeContext})
config, err := clientConfig.ClientConfig()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
rawConfig, err := clientConfig.RawConfig()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
var currentContext string
if kubeContext == "" {
currentContext = rawConfig.CurrentContext
} else {
currentContext = kubeContext
}
if namespace == "" {
if rawConfig.Contexts[currentContext].Namespace == "" {
namespace = metav1.NamespaceDefault
} else {
namespace = rawConfig.Contexts[currentContext].Namespace
}
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt)
logger := NewLogger()
logger.PrintHeader(currentContext, namespace, labels)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
watcher, err := clientset.CoreV1().Pods(namespace).Watch(ctx, metav1.ListOptions{
LabelSelector: labels,
})
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
added, finished, deleted := Watch(ctx, watcher)
tails := NewTailMap()
go func() {
for target := range added {
id := target.GetID()
if _, ok := tails.Get(id); ok {
continue
}
tail := NewTail(target.Namespace, target.Pod, target.Container, logger, sinceSeconds, timestamps)
tails.Set(id, tail)
tail.Start(ctx, clientset)
}
}()
go func() {
for target := range finished {
id := target.GetID()
t, ok := tails.Get(id)
if !ok {
continue
}
if t.Finished {
continue
}
t.Finish()
tails.Delete(id)
}
}()
go func() {
for target := range deleted {
id := target.GetID()
t, ok := tails.Get(id)
if !ok {
continue
}
t.Delete()
tails.Delete(id)
}
}()
<-sigCh
cancel()
}