Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matchExpressions ignored on pod selection #2770

Open
t4lz opened this issue Sep 20, 2024 · 1 comment
Open

matchExpressions ignored on pod selection #2770

t4lz opened this issue Sep 20, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@t4lz
Copy link
Member

t4lz commented Sep 20, 2024

When targeting target types that have multiple pods, we select a pod using the target object's label selector, however, it seems we are currently ignoring the matchExpressions field of the label selector, and only take the matchLabels field.

Impact

Those two fields are ANDed, so if a user defines a condition using both matchLabels and matchExpressions, we would ignore the restriction defined by the matchExpressions on selected pods, and in the worse case scenario we would select a pod from a different deployment/rollout/...

Also, we return an error if there are no matchLabels, but I think that's a valid case - when the label selector is defined based on match expressions alone.

Example for one of the places we only use selector.match_labels:

async fn get_labels(resource: &Self::Resource) -> Result<BTreeMap<String, String>> {
resource
.spec
.as_ref()
.and_then(|spec| spec.selector.match_labels.clone())
.ok_or_else(|| KubeApiError::missing_field(resource, ".spec.selector.matchLabels"))
}

Reproduction

no-expressions.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: no-exps
  labels:
    app: py-serv
spec:
  replicas: 1
  selector:
    matchLabels:
      app: py-serv
  template:
    metadata:
      labels:
        app: py-serv
    spec:
      containers:
        - name: py-serv
          image: ghcr.io/metalbear-co/mirrord-pytest:latest

has-expressions.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: has-exps
  labels:
    app: py-serv
spec:
  replicas: 1
  selector:
    matchLabels:
      app: py-serv
    matchExpressions:
      - { key: gotcha, operator: Exists }
  template:
    metadata:
      labels:
        app: py-serv
        gotcha: haha
    spec:
      containers:
        - name: py-serv
          image: ghcr.io/metalbear-co/mirrord-pytest:latest

If you define those two deployments, and run mirrord with the operator, targeting deploy/has-exps, agents will be created for both pods.

If you run without the operator, targeting deploy/has-exps, you might target the pod from the wrong deployment:

MIRRORD_PROGRESS_MODE=off MIRRORD_OPERATOR_ENABLED=false mirrord exec -t deploy/has-exps hostname
no-exps-7cdc865574-mpxtx
@t4lz t4lz added the bug Something isn't working label Sep 20, 2024
Copy link

linear bot commented Sep 20, 2024

@t4lz t4lz changed the title matchExpressions ignored on pod selection matchExpressions ignored on pod selection Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant