Skip to content

Commit

Permalink
add new metric kube_pod_service_account
Browse files Browse the repository at this point in the history
  • Loading branch information
swarup-stripe committed Jun 13, 2023
1 parent 31d6e8f commit 8a8b7b4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/pod-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
| 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 | - |
| 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 | - |
| 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 | - |
| 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 | - |

## Useful metrics queries

Expand Down
22 changes: 22 additions & 0 deletions internal/store/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func podMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
createPodStatusUnschedulableFamilyGenerator(),
createPodTolerationsFamilyGenerator(),
createPodNodeSelectorsFamilyGenerator(),
createPodServiceAccountFamilyGenerator(),
}
}

Expand Down Expand Up @@ -1652,6 +1653,27 @@ func createPodNodeSelectorsFamilyGenerator() generator.FamilyGenerator {
)
}

func createPodServiceAccountFamilyGenerator() generator.FamilyGenerator {
return *generator.NewFamilyGeneratorWithStability(
"kube_pod_service_account",
"The service account for a pod.",
metric.Gauge,
basemetrics.ALPHA,
"",
wrapPodFunc(func(p *v1.Pod) *metric.Family {
m := metric.Metric{
LabelKeys: []string{"service_account"},
LabelValues: []string{p.Spec.ServiceAccountName},
Value: 1,
}

return &metric.Family{
Metrics: []*metric.Metric{&m},
}
}),
)
}

func wrapPodFunc(f func(*v1.Pod) *metric.Family) func(interface{}) *metric.Family {
return func(obj interface{}) *metric.Family {
pod := obj.(*v1.Pod)
Expand Down
22 changes: 21 additions & 1 deletion internal/store/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,26 @@ func TestPodStore(t *testing.T) {
"kube_pod_tolerations",
},
},
{
Obj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
Namespace: "ns1",
UID: "uid1",
},
Spec: v1.PodSpec{
ServiceAccountName: "service-account-name",
},
},
Want: `
# HELP kube_pod_service_account The service account for a pod.
# TYPE kube_pod_service_account gauge
kube_pod_service_account{namespace="ns1",pod="pod1",service_account="service-account-name",uid="uid1"} 1
`,
MetricNames: []string{
"kube_pod_service_account",
},
},
}

for i, c := range cases {
Expand Down Expand Up @@ -2161,7 +2181,7 @@ func BenchmarkPodStore(b *testing.B) {
},
}

expectedFamilies := 50
expectedFamilies := 51
for n := 0; n < b.N; n++ {
families := f(pod)
if len(families) != expectedFamilies {
Expand Down
3 changes: 3 additions & 0 deletions pkg/app/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func TestFullScrapeCycle(t *testing.T) {
# HELP kube_pod_overhead_cpu_cores The pod overhead in regards to cpu cores associated with running a pod.
# HELP kube_pod_overhead_memory_bytes The pod overhead in regards to memory associated with running a pod.
# HELP kube_pod_runtimeclass_name_info The runtimeclass associated with the pod.
# HELP kube_pod_service_account The service account for a pod.
# HELP kube_pod_owner [STABLE] Information about the Pod's owner.
# HELP kube_pod_restart_policy [STABLE] Describes the restart policy in use by this pod.
# HELP kube_pod_spec_volumes_persistentvolumeclaims_info [STABLE] Information about persistentvolumeclaim volumes in a pod.
Expand Down Expand Up @@ -284,6 +285,7 @@ func TestFullScrapeCycle(t *testing.T) {
# TYPE kube_pod_overhead_cpu_cores gauge
# TYPE kube_pod_overhead_memory_bytes gauge
# TYPE kube_pod_runtimeclass_name_info gauge
# TYPE kube_pod_service_account gauge
# TYPE kube_pod_owner gauge
# TYPE kube_pod_restart_policy gauge
# TYPE kube_pod_spec_volumes_persistentvolumeclaims_info gauge
Expand Down Expand Up @@ -334,6 +336,7 @@ kube_pod_info{namespace="default",pod="pod0",uid="abc-0",host_ip="1.1.1.1",pod_i
kube_pod_labels{namespace="default",pod="pod0",uid="abc-0"} 1
kube_pod_owner{namespace="default",pod="pod0",uid="abc-0",owner_kind="",owner_name="",owner_is_controller=""} 1
kube_pod_restart_policy{namespace="default",pod="pod0",uid="abc-0",type="Always"} 1
kube_pod_service_account{namespace="default",pod="pod0",uid="abc-0",service_account=""} 1
kube_pod_status_phase{namespace="default",pod="pod0",uid="abc-0",phase="Failed"} 0
kube_pod_status_phase{namespace="default",pod="pod0",uid="abc-0",phase="Pending"} 0
kube_pod_status_phase{namespace="default",pod="pod0",uid="abc-0",phase="Running"} 1
Expand Down

0 comments on commit 8a8b7b4

Please sign in to comment.