Skip to content

fix: ignore apps.kubernetes.io/pod-index when generating network policies - #396

Merged
matthyx merged 1 commit into
kubescape:mainfrom
ANAMASGARD:fix-942-statefulset-peer-selectors
Sep 7, 2026
Merged

fix: ignore apps.kubernetes.io/pod-index when generating network policies#396
matthyx merged 1 commit into
kubescape:mainfrom
ANAMASGARD:fix-942-statefulset-peer-selectors

Conversation

@ANAMASGARD

Copy link
Copy Markdown
Contributor

Sanitize legacy ContainerProfile neighbors that still carry per-replica StatefulSet labels so GenerateNetworkPolicy collapses them into a single workload-level peer rule that remains valid after scale-up.

Related: kubescape/node-agent#942

Overview

Current behavior: IsIgnoredLabel already strips statefulset.kubernetes.io/pod-name, but not apps.kubernetes.io/pod-index. Legacy learned neighbors that differ only by per-replica identity labels therefore become one GeneratedNetworkPolicy peer rule per StatefulSet replica, so scaling up denies traffic to new pods.

Future behavior: apps.kubernetes.io/pod-index is ignored in removeLabels() (via IsIgnoredLabel). Three otherwise-identical neighbors (replicas 0/1/2) collapse into one ingress peer and one egress peer with only workload labels (e.g. app.kubernetes.io/name=db).

This is the generation-time / backward-compatibility half of the fix. New profiles should stop learning replica-specific selectors in the companion kubescape/node-agent change.

How to Test

go test ./pkg/apis/softwarecomposition/networkpolicy/v2/ \
  -run 'TestRemoveLabels|TestGenerateNetworkPolicy_StatefulSetPeerCollapsesReplicas' \
  -count=1

Expected:

  • TestRemoveLabels strips both apps.kubernetes.io/pod-index and statefulset.kubernetes.io/pod-name
  • TestGenerateNetworkPolicy_StatefulSetPeerCollapsesReplicas ingress and egress subtests each assert len(rules)==1, one peer, selector {"app.kubernetes.io/name":"db"}, no identity labels

Related issues/PRs

  • Related: kubescape/node-agent#942
  • Companion: kubescape/node-agent PR — strip StatefulSet pod-identity labels from network peer selectors (link after opening)

Checklist before requesting a review

  • My code follows the style guidelines of this project
  • I have commented on my code, particularly in hard-to-understand areas
  • I have performed a self-review of my code
  • If it is a core feature, I have added thorough tests.
  • New and existing unit tests pass locally with my changes (targeted #942 regressions verified; full go test ./... not re-run in this environment)

…cies

Sanitize legacy ContainerProfile neighbors that still carry per-replica
StatefulSet labels so GenerateNetworkPolicy collapses them into a single
workload-level peer rule valid after scale-up.

Related: kubescape/node-agent#942
Signed-off-by: Gaurav Chaudhary <chaudharygaurav2004@gmail.com>
@coderabbitai

coderabbitai Bot commented Sep 4, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: 386731e0-bd54-46c8-b4d8-2561d4fd2d02


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@matthyx matthyx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the generation-time compatibility fix. Ignoring apps.kubernetes.io/pod-index alongside statefulset.kubernetes.io/pod-name correctly collapses legacy per-replica selectors, and the ingress/egress regression coverage exercises the intended behavior. I found no blockers. Verified with go test ./... -count=1.

@matthyx
matthyx merged commit d9d4f10 into kubescape:main Sep 7, 2026
7 checks passed
@matthyx matthyx moved this to To Archive in KS PRs tracking Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To Archive

Development

Successfully merging this pull request may close these issues.

2 participants