Skip to content

fix(deployment): guard nil Spec.Replicas before dereference#2971

Open
immanuwell wants to merge 1 commit into
kubernetes:mainfrom
immanuwell:fix/deployment-nil-replicas-guard
Open

fix(deployment): guard nil Spec.Replicas before dereference#2971
immanuwell wants to merge 1 commit into
kubernetes:mainfrom
immanuwell:fix/deployment-nil-replicas-guard

Conversation

@immanuwell
Copy link
Copy Markdown
Contributor

@immanuwell immanuwell commented May 28, 2026

What this PR does / why we need it:

Spec.Replicas is *int32 - a pointer. Three metrics dereference it without a nil check:

  • kube_deployment_spec_replicas
  • kube_deployment_spec_strategy_rollingupdate_max_unavailable
  • kube_deployment_spec_strategy_rollingupdate_max_surge

replicaset.go and statefulset.go both guard this same field already. Deployment was just missing the guards, so it would straight up panic on a nil pointer if a deployment with unset Spec.Replicas came through (e.g. via direct API/etcd write, old migrated objects, or tests).

Reproduce:

d := &v1.Deployment{
    ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "default"},
    Spec: v1.DeploymentSpec{
        Strategy: v1.DeploymentStrategy{
            RollingUpdate: &v1.RollingUpdateDeployment{
                MaxUnavailable: &maxUnavailable,
            },
        },
        // Replicas intentionally nil
    },
}
// kube-state-metrics panics when generating metrics for this object

Fix follows the existing pattern from replicaset.go:160 and statefulset.go:170.

How does this change affect the cardinality of KSM: does not change cardinality

Which issue(s) this PR fixes:

@k8s-ci-robot k8s-ci-robot added needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels May 28, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

This issue is currently awaiting triage.

If kube-state-metrics contributors determine this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: immanuwell
Once this PR has been reviewed and has the lgtm label, please assign catherinef-dev for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot requested review from bhope and nmn3m May 28, 2026 04:56
@github-project-automation github-project-automation Bot moved this to Needs Triage in SIG Instrumentation May 28, 2026
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

2 participants