Skip to content

Conversation

@ljluestc
Copy link

Summary

Use client-go / apimachinery label selector utilities instead of custom label matching helpers in the PDB controller.

Fixes #8.


Background / Motivation

The controller previously implemented its own label matching helpers (containLabels, labelsIntersect) to:

  • Decide which PDBs are managed vs unmanaged (based on heritage: pdb-controller).
  • Determine whether existing unmanaged PDBs already match a workload’s pod template labels (getMatchedPDBs).

Kubernetes already provides robust label selector types and matching logic via client-go / k8s.io/apimachinery, so this PR replaces our ad‑hoc implementations with those built‑in utilities. This reduces duplicated logic and keeps behavior consistent with other Kubernetes components.


What this PR changes

  • Replace custom intersection logic in getMatchedPDBs

    • Before: getMatchedPDBs relied on a custom labelsIntersect function comparing two map[string]string.
    • After: it now uses Kubernetes’ label selector APIs:
      • Convert each PDB’s Spec.Selector (*metav1.LabelSelector) using metav1.LabelSelectorAsSelector.
      • Convert the resource’s pod template labels to a labels.Set.
      • Use selector.Matches(labels.Set(resourceLabels)) to decide whether the PDB matches the workload.
  • Use label selectors to find managed PDBs

    • Before: filterPDBs used containLabels(pdb.Labels, ownerLabels) to decide whether a PDB is managed.
    • After: filterPDBs uses labels.SelectorFromSet(ownerLabels).Matches(labels.Set(pdb.Labels)), delegating the match semantics to apimachinery.
  • Remove custom helpers that duplicate selector behavior

    • labelsIntersect and the custom subset logic in containLabels are no longer used for controller behavior.
    • Managed/unmanaged classification and matching now go through the selector utilities, which are well‑tested and idiomatic in Kubernetes code.
  • Tests

    • Existing behavior tests in controller_test.go continue to exercise:
      • Classification of managed vs unmanaged PDBs.
      • Matching between workloads and PDBs (including the parent‑resource‑hash paths).
    • No changes to the public API or configuration flags.

Behavior notes

  • Matching semantics now exactly follow Kubernetes label selector behavior:
    • PDB selectors can use the standard LabelSelector form (we already use MatchLabels, but this prepares us for more complex selectors).
    • Any invalid LabelSelector will be safely skipped (logged and ignored) rather than leading to undefined behavior.
  • Managed PDB detection still uses the same ownerLabels (heritage: pdb-controller); only the implementation is switched to selectors.

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.

Look into client-go matchLabel functions

1 participant