-
Notifications
You must be signed in to change notification settings - Fork 288
/
pod.go
58 lines (49 loc) · 1.46 KB
/
pod.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
package k8s
import (
"context"
"fmt"
"io"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"github.com/windmilleng/tilt/internal/container"
)
func (k K8sClient) WatchPod(ctx context.Context, pod *v1.Pod) (watch.Interface, error) {
podAPI := k.core.Pods(NamespaceFromPod(pod).String())
podID := PodIDFromPod(pod)
fieldSelector := fmt.Sprintf("metadata.name=%s", podID)
watchOptions := metav1.ListOptions{
FieldSelector: fieldSelector,
Watch: true,
ResourceVersion: pod.ObjectMeta.ResourceVersion,
}
return podAPI.Watch(watchOptions)
}
func (k K8sClient) ContainerLogs(ctx context.Context, pID PodID, cName container.Name, n Namespace, startWatchTime time.Time) (io.ReadCloser, error) {
options := &v1.PodLogOptions{
Container: cName.String(),
Follow: true,
SinceTime: &metav1.Time{
Time: startWatchTime,
},
}
req := k.core.Pods(n.String()).GetLogs(pID.String(), options)
return req.Stream()
}
func (k K8sClient) PodByID(ctx context.Context, pID PodID, n Namespace) (*v1.Pod, error) {
pod, err := k.core.Pods(n.String()).Get(pID.String(), metav1.GetOptions{})
if pod != nil {
FixContainerStatusImages(pod)
}
return pod, err
}
func PodIDFromPod(pod *v1.Pod) PodID {
return PodID(pod.ObjectMeta.Name)
}
func NamespaceFromPod(pod *v1.Pod) Namespace {
return Namespace(pod.ObjectMeta.Namespace)
}
func NodeIDFromPod(pod *v1.Pod) NodeID {
return NodeID(pod.Spec.NodeName)
}