-
Notifications
You must be signed in to change notification settings - Fork 712
/
Copy pathpod.go
88 lines (72 loc) · 1.77 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
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
package kubernetes
import (
"strconv"
"github.com/weaveworks/scope/report"
apiv1 "k8s.io/client-go/pkg/api/v1"
)
// These constants are keys used in node metadata
const (
State = "kubernetes_state"
IsInHostNetwork = "kubernetes_is_in_host_network"
RestartCount = "kubernetes_restart_count"
StateDeleted = "deleted"
)
// Pod represents a Kubernetes pod
type Pod interface {
Meta
AddParent(topology, id string)
NodeName() string
GetNode(probeID string) report.Node
RestartCount() uint
}
type pod struct {
*apiv1.Pod
Meta
parents report.Sets
Node *apiv1.Node
}
// NewPod creates a new Pod
func NewPod(p *apiv1.Pod) Pod {
return &pod{
Pod: p,
Meta: meta{p.ObjectMeta},
parents: report.MakeSets(),
}
}
func (p *pod) UID() string {
// Work around for master pod not reporting the right UID.
if hash, ok := p.ObjectMeta.Annotations["kubernetes.io/config.hash"]; ok {
return hash
}
return p.Meta.UID()
}
func (p *pod) AddParent(topology, id string) {
p.parents = p.parents.Add(topology, report.MakeStringSet(id))
}
func (p *pod) State() string {
return string(p.Status.Phase)
}
func (p *pod) NodeName() string {
return p.Spec.NodeName
}
func (p *pod) RestartCount() uint {
count := uint(0)
for _, cs := range p.Status.ContainerStatuses {
count += uint(cs.RestartCount)
}
return count
}
func (p *pod) GetNode(probeID string) report.Node {
latests := map[string]string{
State: p.State(),
IP: p.Status.PodIP,
report.ControlProbeID: probeID,
RestartCount: strconv.FormatUint(uint64(p.RestartCount()), 10),
}
if p.Pod.Spec.HostNetwork {
latests[IsInHostNetwork] = "true"
}
return p.MetaNode(report.MakePodNodeID(p.UID())).WithLatests(latests).
WithParents(p.parents).
WithLatestActiveControls(GetLogs, DeletePod)
}