/
kube.go
97 lines (85 loc) · 2.33 KB
/
kube.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
package api
import (
"bufio"
"context"
"errors"
"flag"
"io"
"path/filepath"
"github.com/aemery-cb/pequod/internal/common"
tea "github.com/charmbracelet/bubbletea"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
type Client struct {
kubeClient *kubernetes.Clientset
}
// Creates a client which wraps the kube client in helpers.
func CreateClient() Client {
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
return Client{kubeClient: clientset}
}
func (c *Client) WatchPods(namespace string, sub chan<- tea.Msg, stop <-chan struct{}) {
watchlist := cache.NewListWatchFromClient(c.kubeClient.CoreV1().RESTClient(), "pods", namespace, fields.Everything())
_, controller := cache.NewInformer(
watchlist,
&v1.Pod{}, 0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
pod := obj.(*v1.Pod)
sub <- common.NewPodMsg{Pod: pod}
},
DeleteFunc: func(obj interface{}) {
pod := obj.(*v1.Pod)
sub <- common.DeletePodMsg{Pod: pod}
},
UpdateFunc: func(old interface{}, new interface{}) {
oldPod := old.(*v1.Pod)
newPod := new.(*v1.Pod)
sub <- common.UpdatePodMsg{Old: oldPod, New: newPod}
},
},
)
go controller.Run(stop)
}
func (c *Client) StreamLogs(ctx context.Context, pod *v1.Pod, sub chan<- tea.Msg) {
req := c.kubeClient.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &v1.PodLogOptions{Follow: true})
podLogs, err := req.Stream(ctx)
if err != nil {
return
}
r := bufio.NewReader(podLogs)
go func() {
defer podLogs.Close()
for {
bytes, err := r.ReadBytes('\n')
sub <- common.NewLogMsg{Pod: pod, Message: string(bytes)}
if err != nil {
if !errors.Is(err, io.EOF) {
return
}
break
}
}
}()
}