Skip to content

ListOptions step on each other, HasLabels and MatchingLabels compete #2098

@jsilvela

Description

@jsilvela

When trying to get a List using the variadic ListOptions argument, I ran to unexpected behavior if applying both HasLabels and MatchingLabels.

List(ctx, podList, client.InNamespace(namespace)
    client.MatchingLabels{
        utils.ClusterLabelName: clusterName,
    },
    client.HasLabels{"role"}, // this ensures we are getting instance pods only
)

When watching the code in action, more pods than expected were being returned.
It seemed as if the ListOptions were being OR'ed rather than AND'ed.

Looking into the implementation of MatchingLabels and HasLabels, in their ApplyToList(opts *ListOptions),
both will overwrite whatever values the LabelSelector had before.

opts.LabelSelector = sel

This means, likely, that if we apply both HasLabels and MatchingLabels, the last one applied will "win".
This is rather un-intuitive behavior.
If it is so by design, I think it should be documented.
If possible, I would think a logical AND would be the intuitive expectation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugCategorizes issue or PR as related to a bug.lifecycle/rottenDenotes an issue or PR that has aged beyond stale and will be auto-closed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions