-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
283 lines (236 loc) · 8.73 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// SPDX-FileCopyrightText: 2023 Christoph Mewes
// SPDX-License-Identifier: MIT
package main
import (
"context"
"fmt"
"os"
"runtime"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"go.xrstf.de/protokol/pkg/collector"
"go.xrstf.de/protokol/pkg/watcher"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"k8s.io/client-go/tools/clientcmd"
watchtools "k8s.io/client-go/tools/watch"
)
// These variables get set by ldflags during compilation.
var (
BuildTag string
BuildCommit string
BuildDate string // RFC3339 format ("2006-01-02T15:04:05Z07:00")
)
func printVersion() {
// handle empty values in case `go install` was used
if BuildCommit == "" {
fmt.Printf("protokol dev, built with %s\n",
runtime.Version(),
)
} else {
fmt.Printf("protokol %s (%s), built with %s on %s\n",
BuildTag,
BuildCommit[:10],
runtime.Version(),
BuildDate,
)
}
}
type options struct {
kubeconfig string
directory string
namespaces []string
containerNames []string
stream bool
streamPrefix string
labels string
live bool
oneShot bool
flatFiles bool
dumpMetadata bool
dumpEvents bool
dumpRawEvents bool
verbose bool
version bool
}
func main() {
rootCtx := context.Background()
opt := options{
streamPrefix: "[%pN/%pn:%c] >>",
}
pflag.StringVar(&opt.kubeconfig, "kubeconfig", opt.kubeconfig, "kubeconfig file to use (uses $KUBECONFIG by default)")
pflag.StringArrayVarP(&opt.namespaces, "namespace", "n", opt.namespaces, "Kubernetes namespace to watch resources in (supports glob expression) (can be given multiple times)")
pflag.StringArrayVarP(&opt.containerNames, "container", "c", opt.containerNames, "Container names to store logs for (supports glob expression) (can be given multiple times)")
pflag.StringVarP(&opt.labels, "labels", "l", opt.labels, "Label-selector as an alternative to specifying resource names")
pflag.StringVarP(&opt.directory, "output", "o", opt.directory, "Directory where logs should be stored")
pflag.BoolVarP(&opt.flatFiles, "flat", "f", opt.flatFiles, "Do not create directory per namespace, but put all logs in the same directory")
pflag.BoolVar(&opt.live, "live", opt.live, "Only consider running pods, ignore completed/failed pods")
pflag.BoolVar(&opt.stream, "stream", opt.stream, "Do not just dump logs to disk, but also stream them to stdout")
pflag.StringVar(&opt.streamPrefix, "prefix", opt.streamPrefix, "Prefix pattern to put at the beginning of each streamed line (pn = Pod name, pN = Pod namespace, c = container name)")
pflag.BoolVar(&opt.oneShot, "oneshot", opt.oneShot, "Dump logs, but do not tail the containers (i.e. exit after downloading the current state)")
pflag.BoolVar(&opt.dumpMetadata, "metadata", opt.dumpMetadata, "Dump Pods additionally as YAML (note that this can include secrets in environment variables)")
pflag.BoolVar(&opt.dumpEvents, "events", opt.dumpEvents, "Dump events for each matching Pod as a human readable log file (note: label selectors are not respected)")
pflag.BoolVar(&opt.dumpRawEvents, "events-raw", opt.dumpRawEvents, "Dump events for each matching Pod as YAML (note: label selectors are not respected)")
pflag.BoolVarP(&opt.verbose, "verbose", "v", opt.verbose, "Enable more verbose output")
pflag.BoolVarP(&opt.version, "version", "V", opt.version, "Show version info and exit immediately")
pflag.Parse()
if opt.version {
printVersion()
return
}
// //////////////////////////////////////
// setup logging
var log = logrus.New()
log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.RFC1123,
})
if opt.verbose {
log.SetLevel(logrus.DebugLevel)
}
// //////////////////////////////////////
// validate CLI flags
if opt.kubeconfig == "" {
opt.kubeconfig = os.Getenv("KUBECONFIG")
}
var labelSelector labels.Selector
if opt.labels != "" {
var err error
if labelSelector, err = labels.Parse(opt.labels); err != nil {
log.Fatalf("Invalid label selector: %v", err)
}
}
args := pflag.Args()
hasNames := len(args) > 0
if hasNames && opt.labels != "" {
log.Fatal("Cannot specify both resource names and a label selector at the same time.")
}
if !hasNames && len(opt.namespaces) == 0 {
log.Fatal("At least a namespace or a resource name pattern must be given.")
}
if opt.directory == "" {
opt.directory = fmt.Sprintf("protokol-%s", time.Now().Format("2006.01.02T15.04.05"))
}
log.WithField("directory", opt.directory).Info("Storing logs on disk.")
coll, err := collector.NewDiskCollector(opt.directory, opt.flatFiles, opt.dumpEvents, opt.dumpRawEvents)
if err != nil {
log.Fatalf("Failed to create log collector: %v", err)
}
if opt.stream {
stdoutCollector, err := collector.NewStreamCollector(opt.streamPrefix)
if err != nil {
log.Fatalf("Failed to create log collector: %v", err)
}
coll, err = collector.NewMultiplexCollector(coll, stdoutCollector)
if err != nil {
log.Fatalf("Failed to create log collector: %v", err)
}
}
// //////////////////////////////////////
// setup kubernetes client
log.Debug("Creating Kubernetes clientset…")
config, err := clientcmd.BuildConfigFromFlags("", opt.kubeconfig)
if err != nil {
log.Fatalf("Failed to create Kubernetes client: %v", err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatalf("Failed to create Kubernetes clientset: %v", err)
}
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
log.Fatalf("Failed to create dynamic Kubernetes client: %v", err)
}
// //////////////////////////////////////
// start to watch pods & potentially events
podResourceInterface := dynamicClient.Resource(schema.GroupVersionResource{
Version: "v1",
Resource: "pods",
})
eventResourceInterface := dynamicClient.Resource(schema.GroupVersionResource{
Version: "v1",
Resource: "events",
})
if opt.dumpEvents || opt.dumpRawEvents {
log.Debug("Starting to watch pods & events…")
} else {
log.Debug("Starting to watch pods…")
}
// to use the retrywatcher, we need a start revision; setting this to empty or "0"
// is not supported, so we need a real revision; to achieve this we simply create
// a "standard" watcher, takes the first event and its resourceVersion as the
// starting point for the second, longlived retrying watcher
initialPods, resourceVersion, err := getStartPods(rootCtx, clientset, opt.labels)
if err != nil {
log.Fatalf("Failed to determine initial resourceVersion: %v", err)
}
var initialEvents []corev1.Event
if opt.dumpEvents || opt.dumpRawEvents {
initialEvents, err = getStartEvents(rootCtx, clientset, opt.labels)
if err != nil {
log.Fatalf("Failed to retrieve initial events: %v", err)
}
}
var (
podWatcher watch.Interface
eventWatcher watch.Interface
)
if !opt.oneShot {
podWatcher, err = watchtools.NewRetryWatcher(resourceVersion, &watchContextInjector{
ctx: rootCtx,
ri: podResourceInterface,
})
if err != nil {
log.Fatalf("Failed to create watch for pods: %v", err)
}
eventWatcher, err = watchtools.NewRetryWatcher(resourceVersion, &watchContextInjector{
ctx: rootCtx,
ri: eventResourceInterface,
})
if err != nil {
log.Fatalf("Failed to create watch for events: %v", err)
}
}
watcherOpts := watcher.Options{
LabelSelector: labelSelector,
Namespaces: opt.namespaces,
ResourceNames: args,
ContainerNames: opt.containerNames,
RunningOnly: opt.live,
OneShot: opt.oneShot,
DumpMetadata: opt.dumpMetadata,
DumpEvents: opt.dumpEvents || opt.dumpRawEvents,
}
w := watcher.NewWatcher(clientset, coll, log, initialPods, initialEvents, watcherOpts)
w.Watch(rootCtx, podWatcher, eventWatcher)
}
func getStartPods(ctx context.Context, cs *kubernetes.Clientset, labelSelector string) ([]corev1.Pod, string, error) {
pods, err := cs.CoreV1().Pods("").List(ctx, metav1.ListOptions{
LabelSelector: labelSelector,
})
if err != nil {
return nil, "", fmt.Errorf("failed to perform list on Pods: %w", err)
}
return pods.Items, pods.ResourceVersion, nil
}
func getStartEvents(ctx context.Context, cs *kubernetes.Clientset, labelSelector string) ([]corev1.Event, error) {
events, err := cs.CoreV1().Events("").List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("failed to perform list on Events: %w", err)
}
return events.Items, nil
}
type watchContextInjector struct {
ctx context.Context
ri dynamic.ResourceInterface
}
func (cw *watchContextInjector) Watch(options metav1.ListOptions) (watch.Interface, error) {
return cw.ri.Watch(cw.ctx, options)
}