-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
55 lines (47 loc) · 1.4 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
package logs
import (
"context"
"fmt"
"io"
"log"
"strings"
"github.com/adrianokf/k8s-log-proxy/pkg/security"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
)
type LogTarget struct {
Namespace string
Pod string
Container string
}
// / ParseUrl parses a request URL path and returns its target (identified by namespace, pod, and optional container name)
func ParseUrl(urlPath string) (target LogTarget, err error) {
parts := strings.Split(strings.TrimPrefix(urlPath, "/logs/"), "/")
if len(parts) == 2 {
target.Namespace, target.Pod = parts[0], parts[1]
} else if len(parts) == 3 {
target.Namespace, target.Pod, target.Container = parts[0], parts[1], parts[2]
}
if target.Namespace == "" || target.Pod == "" {
err = fmt.Errorf("namespace or pod name empty: %+v", target)
}
return
}
func ReadLogs(client kubernetes.Interface, namespace, podId string, logOptions *v1.PodLogOptions) (string, error) {
_, err := security.CheckPermissions(namespace, podId)
if err != nil {
panic(err.Error())
}
readCloser, err := client.CoreV1().Pods(namespace).GetLogs(podId, logOptions).Stream(context.TODO())
if err != nil {
log.Printf("Could not query pod logs: %s\n", err.Error())
return "", err
}
defer readCloser.Close()
result, err := io.ReadAll(readCloser)
if err != nil {
log.Printf("Could not read pod logs: %s\n", err.Error())
return "ERROR", err
}
return string(result), nil
}