Skip to content

Commit 56a3a0a

Browse files
authored
Merge pull request #2096 from swarup-stripe/add-SA-to-kube-pod-info
feat: Adds new metric `kube_pod_service_account`
2 parents 7189f39 + 8a8b7b4 commit 56a3a0a

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

docs/pod-metrics.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
| kube_pod_status_scheduled_time | Gauge | Unix timestamp when pod moved into scheduled status | seconds |`pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `uid`=&lt;pod-uid&gt; | STABLE | - |
5353
| kube_pod_status_unschedulable | Gauge | Describes the unschedulable status for the pod | |`pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `uid`=&lt;pod-uid&gt; | STABLE | - |
5454
| kube_pod_tolerations | Gauge | Information about the pod tolerations | | `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `uid`=&lt;pod-uid&gt; <br> `key`=&lt;toleration-key&gt; <br> `operator`=&lt;toleration-operator&gt; <br> `value`=&lt;toleration-value&gt; <br> `effect`=&lt;toleration-effect&gt; `toleration_seconds`=&lt;toleration-seconds&gt; | EXPERIMENTAL | - |
55+
| kube_pod_service_account | Gauge | The service account for a pod | | `pod`=&lt;pod-name&gt; <br> `namespace`=&lt;pod-namespace&gt; <br> `uid`=&lt;pod-uid&gt; <br> `service_account`=&lt;service_account&gt; | EXPERIMENTAL | - |
5556

5657
## Useful metrics queries
5758

internal/store/pod.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func podMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
9292
createPodStatusUnschedulableFamilyGenerator(),
9393
createPodTolerationsFamilyGenerator(),
9494
createPodNodeSelectorsFamilyGenerator(),
95+
createPodServiceAccountFamilyGenerator(),
9596
}
9697
}
9798

@@ -1652,6 +1653,27 @@ func createPodNodeSelectorsFamilyGenerator() generator.FamilyGenerator {
16521653
)
16531654
}
16541655

1656+
func createPodServiceAccountFamilyGenerator() generator.FamilyGenerator {
1657+
return *generator.NewFamilyGeneratorWithStability(
1658+
"kube_pod_service_account",
1659+
"The service account for a pod.",
1660+
metric.Gauge,
1661+
basemetrics.ALPHA,
1662+
"",
1663+
wrapPodFunc(func(p *v1.Pod) *metric.Family {
1664+
m := metric.Metric{
1665+
LabelKeys: []string{"service_account"},
1666+
LabelValues: []string{p.Spec.ServiceAccountName},
1667+
Value: 1,
1668+
}
1669+
1670+
return &metric.Family{
1671+
Metrics: []*metric.Metric{&m},
1672+
}
1673+
}),
1674+
)
1675+
}
1676+
16551677
func wrapPodFunc(f func(*v1.Pod) *metric.Family) func(interface{}) *metric.Family {
16561678
return func(obj interface{}) *metric.Family {
16571679
pod := obj.(*v1.Pod)

internal/store/pod_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2066,6 +2066,26 @@ func TestPodStore(t *testing.T) {
20662066
"kube_pod_tolerations",
20672067
},
20682068
},
2069+
{
2070+
Obj: &v1.Pod{
2071+
ObjectMeta: metav1.ObjectMeta{
2072+
Name: "pod1",
2073+
Namespace: "ns1",
2074+
UID: "uid1",
2075+
},
2076+
Spec: v1.PodSpec{
2077+
ServiceAccountName: "service-account-name",
2078+
},
2079+
},
2080+
Want: `
2081+
# HELP kube_pod_service_account The service account for a pod.
2082+
# TYPE kube_pod_service_account gauge
2083+
kube_pod_service_account{namespace="ns1",pod="pod1",service_account="service-account-name",uid="uid1"} 1
2084+
`,
2085+
MetricNames: []string{
2086+
"kube_pod_service_account",
2087+
},
2088+
},
20692089
}
20702090

20712091
for i, c := range cases {
@@ -2161,7 +2181,7 @@ func BenchmarkPodStore(b *testing.B) {
21612181
},
21622182
}
21632183

2164-
expectedFamilies := 50
2184+
expectedFamilies := 51
21652185
for n := 0; n < b.N; n++ {
21662186
families := f(pod)
21672187
if len(families) != expectedFamilies {

pkg/app/server_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ func TestFullScrapeCycle(t *testing.T) {
235235
# HELP kube_pod_overhead_cpu_cores The pod overhead in regards to cpu cores associated with running a pod.
236236
# HELP kube_pod_overhead_memory_bytes The pod overhead in regards to memory associated with running a pod.
237237
# HELP kube_pod_runtimeclass_name_info The runtimeclass associated with the pod.
238+
# HELP kube_pod_service_account The service account for a pod.
238239
# HELP kube_pod_owner [STABLE] Information about the Pod's owner.
239240
# HELP kube_pod_restart_policy [STABLE] Describes the restart policy in use by this pod.
240241
# HELP kube_pod_spec_volumes_persistentvolumeclaims_info [STABLE] Information about persistentvolumeclaim volumes in a pod.
@@ -284,6 +285,7 @@ func TestFullScrapeCycle(t *testing.T) {
284285
# TYPE kube_pod_overhead_cpu_cores gauge
285286
# TYPE kube_pod_overhead_memory_bytes gauge
286287
# TYPE kube_pod_runtimeclass_name_info gauge
288+
# TYPE kube_pod_service_account gauge
287289
# TYPE kube_pod_owner gauge
288290
# TYPE kube_pod_restart_policy gauge
289291
# TYPE kube_pod_spec_volumes_persistentvolumeclaims_info gauge
@@ -334,6 +336,7 @@ kube_pod_info{namespace="default",pod="pod0",uid="abc-0",host_ip="1.1.1.1",pod_i
334336
kube_pod_labels{namespace="default",pod="pod0",uid="abc-0"} 1
335337
kube_pod_owner{namespace="default",pod="pod0",uid="abc-0",owner_kind="",owner_name="",owner_is_controller=""} 1
336338
kube_pod_restart_policy{namespace="default",pod="pod0",uid="abc-0",type="Always"} 1
339+
kube_pod_service_account{namespace="default",pod="pod0",uid="abc-0",service_account=""} 1
337340
kube_pod_status_phase{namespace="default",pod="pod0",uid="abc-0",phase="Failed"} 0
338341
kube_pod_status_phase{namespace="default",pod="pod0",uid="abc-0",phase="Pending"} 0
339342
kube_pod_status_phase{namespace="default",pod="pod0",uid="abc-0",phase="Running"} 1

0 commit comments

Comments
 (0)