Files
weave-scope/probe/kubernetes/pod.go
2017-07-27 13:15:53 +00:00

89 lines
1.8 KiB
Go

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)
}