/
pod_log.go
99 lines (81 loc) · 2.28 KB
/
pod_log.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
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0
package logs
import (
"sync"
"carvel.dev/kapp/pkg/kapp/matcher"
"github.com/cppforlife/go-cli-ui/ui"
corev1 "k8s.io/api/core/v1"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
type PodLogOpts struct {
Follow bool
Lines *int64
ContainerNames []string
ContainerTag bool
LinePrefix string
}
type PodLog struct {
pod corev1.Pod
podsClient typedcorev1.PodInterface
tagFunc func(corev1.Container) string
opts PodLogOpts
}
func NewPodLog(
pod corev1.Pod,
podsClient typedcorev1.PodInterface,
tagFunc func(corev1.Container) string,
opts PodLogOpts,
) PodLog {
return PodLog{pod, podsClient, tagFunc, opts}
}
// TailAll will tail all logs from all containers in a single Pod
func (l PodLog) TailAll(ui ui.UI, cancelCh chan struct{}) error {
// Container will not emit any new logs since this is a terminal position
podInTerminalState := l.pod.Status.Phase == corev1.PodSucceeded || l.pod.Status.Phase == corev1.PodFailed
var conts []corev1.Container
for _, cont := range l.pod.Spec.InitContainers {
if !(podInTerminalState && l.isWaitingContainer(cont, l.pod.Status.InitContainerStatuses)) {
if l.isWatchingContainer(cont, l.opts.ContainerNames) {
conts = append(conts, cont)
}
}
}
for _, cont := range l.pod.Spec.Containers {
if !(podInTerminalState && l.isWaitingContainer(cont, l.pod.Status.ContainerStatuses)) {
if l.isWatchingContainer(cont, l.opts.ContainerNames) {
conts = append(conts, cont)
}
}
}
var wg sync.WaitGroup
for _, cont := range conts {
cont := cont
wg.Add(1)
go func() {
NewPodContainerLog(l.pod, cont.Name, l.podsClient, l.tagFunc(cont), l.opts).Tail(ui, cancelCh) // TODO err?
wg.Done()
}()
}
wg.Wait()
return nil
}
func (l PodLog) isWaitingContainer(cont corev1.Container, statuses []corev1.ContainerStatus) bool {
for _, contStatus := range statuses {
if cont.Name == contStatus.Name {
return contStatus.State.Waiting != nil
}
}
return false
}
func (l PodLog) isWatchingContainer(cont corev1.Container, containers []string) bool {
if len(containers) == 0 {
return true
}
for _, n := range containers {
if matcher.NewStringMatcher(n).Matches(cont.Name) {
return true
}
}
return false
}