This repository has been archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (64 loc) · 1.52 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
package main
import (
"time"
"runtime"
"os"
"io/ioutil"
"github.com/Difrex/kubeat/beater"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
version = "0.1"
namespacePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
)
func main() {
var client *kubernetes.Clientset
var config *rest.Config
if isInK8S() {
log.Info("K8S launch detected")
c, err := rest.InClusterConfig()
handleError(err)
config = c
} else {
log.Info("Outside K8S launch detected")
c, err := clientcmd.BuildConfigFromFlags("", configPath)
handleError(err)
config = c
}
client, err := kubernetes.NewForConfig(config)
handleError(err)
podLogs := beater.NewPodLogs(getNamespace(), client, config)
podLogs.SkipVerify = kubeSkipTLSVerify
podLogs.Ignored = ignorePod
go podLogs.PodTicker()
ticker := time.NewTicker(time.Duration(tickTime) * time.Second)
for t := range ticker.C {
log.Info(t.Unix(), " Num of logwatchers: ", podLogs.Len())
log.Info(t.Unix(), " Num of CGOCalls: ", runtime.NumCgoCall())
log.Info(t.Unix(), " Num of goroutines ", runtime.NumGoroutine())
}
}
func handleError(err error) {
if err != nil {
panic(err)
}
}
func isInK8S() bool {
if os.Getenv("KUBERNETES_SERVICE_HOST") != "" {
return true
}
return false
}
func getNamespace() string {
f, err := os.Open(namespacePath)
if os.IsExist(err) {
data, err := ioutil.ReadAll(f)
handleError(err)
return string(data)
}
f.Close()
return namespace
}