chaostoolkit / chaostoolkit-kubernetes

Kubernetes driver extension of the Chaos Toolkit probes and actions API

Home Page:https://chaostoolkit.org/drivers/kubernetes/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using two different source of truth to filter pods

arkentos opened this issue · comments

Describe the bug
While applying chaos experiments using litmuschaos, which in turn leverages chaostoolkit, we found out the filter to get the pod in namespace vs get the actual list of pods uses two different way

https://github.com/chaostoolkit/chaostoolkit-kubernetes/blob/master/chaosk8s/pod/actions.py#L191-L209

if label_selector:
        ret = v1.list_namespaced_pod(ns, label_selector=label_selector)
        logger.debug("Found {d} pods labelled '{s}' in ns {n}".format(
            d=len(ret.items), s=label_selector, n=ns))
    else:
        ret = v1.list_namespaced_pod(ns)
        logger.debug("Found {d} pods in ns '{n}'".format(
            d=len(ret.items), n=ns))

    pods = []
    if name_pattern:
        pattern = re.compile(name_pattern)
        for p in ret.items:
            if pattern.match(p.metadata.name):
                pods.append(p)
                logger.debug("Pod '{p}' match pattern".format(
                    p=p.metadata.name))

Expected behavior

Usecase 1

metadata:
    name: some-randon-straing-my-app-qal-usw2-1234a1234b-ab1a2
    labels:
        app: my-app

The code is not able to filter correctly

Usecase 2

metadata:
    name: my-app-qal-usw2-1234a1234b-ab1a2
    labels:
        app: my-app

The code is able to filter correctly

Both use-cases able to filter the right pods

Additional context
So the issue is either we update the regex to search instead of match to find the substring pattern anywhere in the p.metadata.name, or just use one source of filter and update to p.metadata.labels[label_selector]

More context: https://docs.python.org/3/library/re.html#search-vs-match

+1

Good catch. This makes sense to me!

Released in 0.25.1

I'm assuming this has been fixed now so I'll close but feel free to open it up again otherwise.