/
logs.go
72 lines (59 loc) · 1.51 KB
/
logs.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
package cmd
import (
"context"
"io"
"os"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"github.com/guettli/check-conditions/pkg/checkconditions"
"github.com/spf13/cobra"
)
var logsCmd = &cobra.Command{
Use: "logs",
Short: "Check all logs of all pods",
Long: `...`,
Run: func(cmd *cobra.Command, args []string) {
runLogs(arguments)
},
}
func init() {
rootCmd.AddCommand(logsCmd)
}
func runLogs(args checkconditions.Arguments) {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
config, err := kubeconfig.ClientConfig()
if err != nil {
panic(err.Error())
}
config.QPS = 1000
config.Burst = 1000
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// List all pods
pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
for _, pod := range pods.Items {
for _, container := range pod.Spec.Containers {
req := clientset.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &corev1.PodLogOptions{
Container: container.Name,
})
podLogs, err := req.Stream(context.TODO())
if err != nil {
panic(err.Error())
}
defer podLogs.Close()
_, err = io.Copy(os.Stdout, podLogs)
if err != nil {
panic(err.Error())
}
}
}
}