Skip to content

Commit 0785f33

Browse files
committed
add kube_deployment_status_replicas_terminating and kube_replicaset_status_terminating_replicas metrics
1 parent 31f1aa7 commit 0785f33

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

internal/store/deployment.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,26 @@ func deploymentMetricFamilies(allowAnnotationsList, allowLabelsList []string) []
143143
}
144144
}),
145145
),
146+
*generator.NewFamilyGeneratorWithStability(
147+
"kube_deployment_status_replicas_terminating",
148+
"The number of terminating replicas per deployment.",
149+
metric.Gauge,
150+
basemetrics.ALPHA,
151+
"",
152+
wrapDeploymentFunc(func(r *v1.Deployment) *metric.Family {
153+
ms := []*metric.Metric{}
154+
155+
if r.Status.TerminatingReplicas != nil {
156+
ms = append(ms, &metric.Metric{
157+
Value: float64(*r.Status.TerminatingReplicas),
158+
})
159+
}
160+
161+
return &metric.Family{
162+
Metrics: ms,
163+
}
164+
}),
165+
),
146166
*generator.NewFamilyGeneratorWithStability(
147167
"kube_deployment_status_observed_generation",
148168
"The generation observed by the deployment controller.",

internal/store/deployment_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
corev1 "k8s.io/api/core/v1"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/util/intstr"
27+
"k8s.io/utils/ptr"
2728

2829
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
2930
)
@@ -57,6 +58,8 @@ func TestDeploymentStore(t *testing.T) {
5758
# TYPE kube_deployment_status_replicas gauge
5859
# HELP kube_deployment_status_replicas_ready [STABLE] The number of ready replicas per deployment.
5960
# TYPE kube_deployment_status_replicas_ready gauge
61+
# HELP kube_deployment_status_replicas_terminating The number of terminating replicas per deployment.
62+
# TYPE kube_deployment_status_replicas_terminating gauge
6063
# HELP kube_deployment_status_replicas_available [STABLE] The number of available replicas per deployment.
6164
# TYPE kube_deployment_status_replicas_available gauge
6265
# HELP kube_deployment_status_replicas_unavailable [STABLE] The number of unavailable replicas per deployment.
@@ -96,6 +99,7 @@ func TestDeploymentStore(t *testing.T) {
9699
AvailableReplicas: 10,
97100
UnavailableReplicas: 5,
98101
UpdatedReplicas: 2,
102+
TerminatingReplicas: ptr.To[int32](3),
99103
ObservedGeneration: 111,
100104
Conditions: []v1.DeploymentCondition{
101105
{Type: v1.DeploymentAvailable, Status: corev1.ConditionTrue},
@@ -126,6 +130,7 @@ func TestDeploymentStore(t *testing.T) {
126130
kube_deployment_status_replicas_updated{deployment="depl1",namespace="ns1"} 2
127131
kube_deployment_status_replicas{deployment="depl1",namespace="ns1"} 15
128132
kube_deployment_status_replicas_ready{deployment="depl1",namespace="ns1"} 10
133+
kube_deployment_status_replicas_terminating{deployment="depl1",namespace="ns1"} 3
129134
kube_deployment_status_condition{deployment="depl1",namespace="ns1",condition="Available",status="true"} 1
130135
kube_deployment_status_condition{deployment="depl1",namespace="ns1",condition="Progressing",status="true"} 1
131136
kube_deployment_status_condition{deployment="depl1",namespace="ns1",condition="Available",status="false"} 0
@@ -150,6 +155,7 @@ func TestDeploymentStore(t *testing.T) {
150155
AvailableReplicas: 5,
151156
UnavailableReplicas: 0,
152157
UpdatedReplicas: 1,
158+
TerminatingReplicas: nil,
153159
ObservedGeneration: 1111,
154160
Conditions: []v1.DeploymentCondition{
155161
{Type: v1.DeploymentAvailable, Status: corev1.ConditionFalse},

internal/store/replicaset.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,26 @@ func replicaSetMetricFamilies(allowAnnotationsList, allowLabelsList []string) []
112112
}
113113
}),
114114
),
115+
*generator.NewFamilyGeneratorWithStability(
116+
"kube_replicaset_status_terminating_replicas",
117+
"The number of terminating replicas per ReplicaSet.",
118+
metric.Gauge,
119+
basemetrics.ALPHA,
120+
"",
121+
wrapReplicaSetFunc(func(r *v1.ReplicaSet) *metric.Family {
122+
ms := []*metric.Metric{}
123+
124+
if r.Status.TerminatingReplicas != nil {
125+
ms = append(ms, &metric.Metric{
126+
Value: float64(*r.Status.TerminatingReplicas),
127+
})
128+
}
129+
130+
return &metric.Family{
131+
Metrics: ms,
132+
}
133+
}),
134+
),
115135
*generator.NewFamilyGeneratorWithStability(
116136
"kube_replicaset_status_observed_generation",
117137
"The generation observed by the ReplicaSet controller.",

internal/store/replicaset_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
v1 "k8s.io/api/apps/v1"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/utils/ptr"
2526

2627
generator "k8s.io/kube-state-metrics/v2/pkg/metric_generator"
2728
)
@@ -43,11 +44,13 @@ func TestReplicaSetStore(t *testing.T) {
4344
# HELP kube_replicaset_metadata_generation [STABLE] Sequence number representing a specific generation of the desired state.
4445
# TYPE kube_replicaset_metadata_generation gauge
4546
# HELP kube_replicaset_status_replicas [STABLE] The number of replicas per ReplicaSet.
47+
# HELP kube_replicaset_status_terminating_replicas The number of terminating replicas per ReplicaSet.
4648
# TYPE kube_replicaset_status_replicas gauge
4749
# HELP kube_replicaset_status_fully_labeled_replicas [STABLE] The number of fully labeled replicas per ReplicaSet.
4850
# TYPE kube_replicaset_status_fully_labeled_replicas gauge
4951
# HELP kube_replicaset_status_ready_replicas [STABLE] The number of ready replicas per ReplicaSet.
5052
# TYPE kube_replicaset_status_ready_replicas gauge
53+
# TYPE kube_replicaset_status_terminating_replicas gauge
5154
# HELP kube_replicaset_status_observed_generation [STABLE] The generation observed by the ReplicaSet controller.
5255
# TYPE kube_replicaset_status_observed_generation gauge
5356
# HELP kube_replicaset_spec_replicas [STABLE] Number of desired pods for a ReplicaSet.
@@ -80,6 +83,7 @@ func TestReplicaSetStore(t *testing.T) {
8083
Replicas: 5,
8184
FullyLabeledReplicas: 10,
8285
ReadyReplicas: 5,
86+
TerminatingReplicas: ptr.To[int32](3),
8387
ObservedGeneration: 1,
8488
},
8589
Spec: v1.ReplicaSetSpec{
@@ -93,6 +97,7 @@ func TestReplicaSetStore(t *testing.T) {
9397
kube_replicaset_status_observed_generation{namespace="ns1",replicaset="rs1"} 1
9498
kube_replicaset_status_fully_labeled_replicas{namespace="ns1",replicaset="rs1"} 10
9599
kube_replicaset_status_ready_replicas{namespace="ns1",replicaset="rs1"} 5
100+
kube_replicaset_status_terminating_replicas{namespace="ns1",replicaset="rs1"} 3
96101
kube_replicaset_spec_replicas{namespace="ns1",replicaset="rs1"} 5
97102
kube_replicaset_owner{namespace="ns1",owner_is_controller="true",owner_kind="Deployment",owner_name="dp-name",replicaset="rs1"} 1
98103
`,
@@ -112,6 +117,7 @@ func TestReplicaSetStore(t *testing.T) {
112117
Replicas: 0,
113118
FullyLabeledReplicas: 5,
114119
ReadyReplicas: 0,
120+
TerminatingReplicas: nil,
115121
ObservedGeneration: 5,
116122
},
117123
Spec: v1.ReplicaSetSpec{

0 commit comments

Comments
 (0)