Skip to content

Conversation

@XploY04
Copy link

@XploY04 XploY04 commented Oct 23, 2025

What this PR does / why we need it:

This PR adds support for label intersection mode when selecting target pods for chaos experiments, enabling precise pod targeting by matching ALL specified labels (AND logic) instead of the default union behavior that matches ANY label (OR logic).

Problem: Currently, multiple labels use UNION (OR) logic - selecting pods that match ANY label. This causes unintended pod selection when precise targeting is needed (e.g., "primary role AND specific cluster"). As noted in issue #774, when working with CloudNativePG clusters that have multiple instances with different roles, the union behavior selects too many pods.

Solution: Adds optional 4th parameter to TARGETS env variable:

TARGETS="kind:namespace:[labels]:mode"

Where mode can be union (default) or intersection

Example:

# Union (default): Gets pods with app=web OR tier=frontend
- name: TARGETS
  value: "deployment:default:[app=web,tier=frontend]:union"

# Intersection: Gets pods with app=web AND tier=frontend
- name: TARGETS
  value: "deployment:default:[app=web,tier=frontend]:intersection"

Changes:

  • Added LabelMatchMode field to AppDetails struct
  • Added getPodsWithIntersectionLabels() function
  • Updated GetTargets() to parse optional mode parameter
  • Added comprehensive unit tests (140 lines)
  • Added documentation and examples

Backward Compatibility: Fully compatible - defaults to union mode

Which issue this PR fixes : fixes #774

Use Cases:

  • Any scenario requiring AND logic for labels

Checklist:

  • Fixes Experiments for CloudNativePG project #774
  • PR messages has document related information
  • Labelled this PR & related issue with breaking-changes tag - Not applicable (backward compatible)
  • PR messages has breaking changes related information - Not applicable
  • Labelled this PR & related issue with requires-upgrade tag - Not applicable
  • PR messages has upgrade related information - Not applicable
  • Commit has unit tests - Yes (pkg/types/types_test.go)
  • Commit has integration tests - No (covered by unit tests)
  • E2E run Required for the changes - Recommended (test both union and intersection modes)

…l selector for intersection matching

Signed-off-by: XploY04 <[email protected]>
@XploY04 XploY04 requested a review from ispeakc0de October 29, 2025 13:47
@ispeakc0de
Copy link
Member

ispeakc0de commented Nov 11, 2025

HI @XploY04 ,

We need to first add the below pre-requisites changes in chaos-operator:

The TARGETS environment variable is set here in the chaos-operator.

First we need to update the workload schema to include a new field named operation (or conjunction) that supports values such as union and intersection (defaulting to union).

Based on this field, we should modify the logic to append the value after the colon (:) in the TARGETS environment variable.

Finally, regenerate the CRDs to reflect these schema changes by running make generate.

@XploY04
Copy link
Author

XploY04 commented Nov 11, 2025

Hey @ispeakc0de,

I've created the prerequisite PR in chaos-operator as requested: litmuschaos/chaos-operator#510

This adds the labelMatchMode field to the CRD schema and updates getTargets() to append it to the TARGETS environment variable.

I initially thought users could simply override TARGETS via env vars (which does work), but I understand now that a schema-based approach provides better UX, validation, and maintainability.

Please let me know if any changes are needed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Experiments for CloudNativePG project

2 participants