forked from weaveworks/scope
/
pod.go
74 lines (61 loc) · 1.49 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
package kubernetes
import (
"github.com/weaveworks/scope/report"
"k8s.io/kubernetes/pkg/api"
)
// These constants are keys used in node metadata
const (
State = "kubernetes_state"
IsInHostNetwork = "kubernetes_is_in_host_network"
StateDeleted = "deleted"
)
// Pod represents a Kubernetes pod
type Pod interface {
Meta
AddParent(topology, id string)
NodeName() string
GetNode(probeID string) report.Node
}
type pod struct {
*api.Pod
Meta
parents report.Sets
Node *api.Node
}
// NewPod creates a new Pod
func NewPod(p *api.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) GetNode(probeID string) report.Node {
latests := map[string]string{
State: p.State(),
IP: p.Status.PodIP,
report.ControlProbeID: probeID,
}
if sc := p.Pod.Spec.SecurityContext; sc != nil && sc.HostNetwork {
latests[IsInHostNetwork] = "true"
}
return p.MetaNode(report.MakePodNodeID(p.UID())).WithLatests(latests).
WithParents(p.parents).
WithLatestActiveControls(GetLogs, DeletePod)
}