-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (104 loc) · 3.02 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
package main
import (
"net/http"
"os"
"github.com/gin-gonic/gin"
"context"
"fmt"
"strings"
"strconv"
"io"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
func setupRouter() *gin.Engine {
// Disable Console Color
gin.DisableConsoleColor()
if os.Getenv("DEBUG") != "" {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
logkey := os.Getenv("LOGKEY")
fmt.Println("Logkey is: ", logkey)
// k8s client setup
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// get the namespace we're in.
namespaceByte, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
namespace := fmt.Sprintf("%s", namespaceByte)
if err != nil {
panic(err)
}
r := gin.New()
r.Use(
gin.LoggerWithWriter(gin.DefaultWriter, "/healthcheck"),
gin.Recovery(),
)
// Ping
r.GET("/healthcheck", func(c *gin.Context) {
c.String(http.StatusOK, "still alive")
})
r.GET("/logs", func(c *gin.Context) {
// check if there's a key in the environment, if so, make sure it's in the request
if os.Getenv("LOGKEY") != "" {
if os.Getenv("LOGKEY") != strings.Join(c.Request.URL.Query()["key"], " ") {
c.String(http.StatusForbidden, "Key Required")
c.Abort()
return
}
}
var output = ""
var loglines = int64(20)
loglinesval, err := strconv.Atoi(strings.Join(c.Request.URL.Query()["lines"], " "))
if err == nil {
loglines = int64(loglinesval)
}
// get all pods in our namespace
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
for i, pod := range pods.Items {
poddetails, err := clientset.CoreV1().Pods(namespace).Get(context.TODO(), pod.Name, metav1.GetOptions{})
if err != nil {
panic(err.Error())
}
for j, container := range poddetails.Spec.Containers {
podLogOpts := corev1.PodLogOptions{
Container: container.Name,
TailLines: &loglines,
}
buf := new(strings.Builder)
// get the logs here
req := clientset.CoreV1().Pods(namespace).GetLogs(pod.Name, &podLogOpts)
//
output += "\n\n\n-----------------------------\n"
output += fmt.Sprintf("ID: %d %d, \n Namespace: %s \n Pod: %s:\n Container: %s\n", i, j, namespace, pod.Name, container.Name)
output += "-----------------------------\n"
logoutput, err := req.Stream(context.TODO())
if err != nil {
panic(err.Error())
}
io.Copy(buf,logoutput)
output += buf.String()
output += "-----------------------------\n\n\n\n\n"
}
}
c.String(http.StatusOK, output)
})
return r
}
func main() {
r := setupRouter()
// Listen and Server in 0.0.0.0:8080
r.Run(":8080")
}