forked from kubernetes/kube-state-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpod.go
199 lines (178 loc) · 6.79 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/client-go/1.5/pkg/api/v1"
)
var (
descPodInfo = prometheus.NewDesc(
"kube_pod_info",
"Information about pod.",
[]string{"namespace", "pod", "host_ip", "pod_ip", "node"}, nil,
)
descPodStatusPhase = prometheus.NewDesc(
"kube_pod_status_phase",
"The pods current phase.",
[]string{"namespace", "pod", "phase"}, nil,
)
descPodStatusReady = prometheus.NewDesc(
"kube_pod_status_ready",
"Describes whether the pod is ready to serve requests.",
[]string{"namespace", "pod", "condition"}, nil,
)
descPodStatusScheduled = prometheus.NewDesc(
"kube_pod_status_scheduled",
"Describes the status of the scheduling process for the pod.",
[]string{"namespace", "pod", "condition"}, nil,
)
descPodContainerInfo = prometheus.NewDesc(
"kube_pod_container_info",
"Information about a container in a pod.",
[]string{"namespace", "pod", "container", "image", "image_id", "container_id"}, nil,
)
descPodContainerStatusWaiting = prometheus.NewDesc(
"kube_pod_container_status_waiting",
"Describes whether the container is currently in waiting state.",
[]string{"namespace", "pod", "container"}, nil,
)
descPodContainerStatusRunning = prometheus.NewDesc(
"kube_pod_container_status_running",
"Describes whether the container is currently in running state.",
[]string{"namespace", "pod", "container"}, nil,
)
descPodContainerStatusTerminated = prometheus.NewDesc(
"kube_pod_container_status_terminated",
"Describes whether the container is currently in terminated state.",
[]string{"namespace", "pod", "container"}, nil,
)
descPodContainerStatusReady = prometheus.NewDesc(
"kube_pod_container_status_ready",
"Describes whether the containers readiness check succeeded.",
[]string{"namespace", "pod", "container"}, nil,
)
descPodContainerStatusRestarts = prometheus.NewDesc(
"kube_pod_container_status_restarts",
"The number of container restarts per container.",
[]string{"namespace", "pod", "container"}, nil,
)
descPodContainerResourceRequestsCpuCores = prometheus.NewDesc(
"kube_pod_container_resource_requests_cpu_cores",
"The number of requested cpu cores by a container.",
[]string{"namespace", "pod", "container", "node"}, nil,
)
descPodContainerResourceRequestsMemoryBytes = prometheus.NewDesc(
"kube_pod_container_resource_requests_memory_bytes",
"The number of requested memory bytes by a container.",
[]string{"namespace", "pod", "container", "node"}, nil,
)
descPodContainerResourceLimitsCpuCores = prometheus.NewDesc(
"kube_pod_container_resource_limits_cpu_cores",
"The limit on cpu cores to be used by a container.",
[]string{"namespace", "pod", "container", "node"}, nil,
)
descPodContainerResourceLimitsMemoryBytes = prometheus.NewDesc(
"kube_pod_container_resource_limits_memory_bytes",
"The limit on memory to be used by a container in bytes.",
[]string{"namespace", "pod", "container", "node"}, nil,
)
)
type podStore interface {
List() (pods []v1.Pod, err error)
}
// podCollector collects metrics about all pods in the cluster.
type podCollector struct {
store podStore
}
// Describe implements the prometheus.Collector interface.
func (pc *podCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- descPodInfo
ch <- descPodStatusPhase
ch <- descPodStatusReady
ch <- descPodStatusScheduled
ch <- descPodContainerInfo
ch <- descPodContainerStatusWaiting
ch <- descPodContainerStatusRunning
ch <- descPodContainerStatusTerminated
ch <- descPodContainerStatusReady
ch <- descPodContainerStatusRestarts
ch <- descPodContainerResourceRequestsCpuCores
ch <- descPodContainerResourceRequestsMemoryBytes
ch <- descPodContainerResourceLimitsCpuCores
ch <- descPodContainerResourceLimitsMemoryBytes
}
// Collect implements the prometheus.Collector interface.
func (pc *podCollector) Collect(ch chan<- prometheus.Metric) {
pods, err := pc.store.List()
if err != nil {
glog.Errorf("listing pods failed: %s", err)
return
}
for _, p := range pods {
pc.collectPod(ch, p)
}
}
func (pc *podCollector) collectPod(ch chan<- prometheus.Metric, p v1.Pod) {
nodeName := p.Spec.NodeName
addConstMetric := func(desc *prometheus.Desc, t prometheus.ValueType, v float64, lv ...string) {
lv = append([]string{p.Namespace, p.Name}, lv...)
ch <- prometheus.MustNewConstMetric(desc, t, v, lv...)
}
addGauge := func(desc *prometheus.Desc, v float64, lv ...string) {
addConstMetric(desc, prometheus.GaugeValue, v, lv...)
}
addCounter := func(desc *prometheus.Desc, v float64, lv ...string) {
addConstMetric(desc, prometheus.CounterValue, v, lv...)
}
addGauge(descPodInfo, 1, p.Status.HostIP, p.Status.PodIP, nodeName)
addGauge(descPodStatusPhase, 1, string(p.Status.Phase))
for _, c := range p.Status.Conditions {
switch c.Type {
case v1.PodReady:
addConditionMetrics(ch, descPodStatusReady, c.Status, p.Namespace, p.Name)
case v1.PodScheduled:
addConditionMetrics(ch, descPodStatusScheduled, c.Status, p.Namespace, p.Name)
}
}
for _, cs := range p.Status.ContainerStatuses {
addGauge(descPodContainerInfo, 1,
cs.Name, cs.Image, cs.ImageID, cs.ContainerID,
)
addGauge(descPodContainerStatusWaiting, boolFloat64(cs.State.Waiting != nil), cs.Name)
addGauge(descPodContainerStatusRunning, boolFloat64(cs.State.Running != nil), cs.Name)
addGauge(descPodContainerStatusTerminated, boolFloat64(cs.State.Terminated != nil), cs.Name)
addGauge(descPodContainerStatusReady, boolFloat64(cs.Ready), cs.Name)
addCounter(descPodContainerStatusRestarts, float64(cs.RestartCount), cs.Name)
}
for _, c := range p.Spec.Containers {
req := c.Resources.Requests
lim := c.Resources.Limits
if cpu, ok := req[v1.ResourceCPU]; ok {
addGauge(descPodContainerResourceRequestsCpuCores, float64(cpu.MilliValue())/1000,
c.Name, nodeName)
}
if mem, ok := req[v1.ResourceMemory]; ok {
addGauge(descPodContainerResourceRequestsMemoryBytes, float64(mem.Value()),
c.Name, nodeName)
}
if cpu, ok := lim[v1.ResourceCPU]; ok {
addGauge(descPodContainerResourceLimitsCpuCores, float64(cpu.MilliValue())/1000,
c.Name, nodeName)
}
if mem, ok := lim[v1.ResourceMemory]; ok {
addGauge(descPodContainerResourceLimitsMemoryBytes, float64(mem.Value()),
c.Name, nodeName)
}
}
}