kubernetes.core icon indicating copy to clipboard operation
kubernetes.core copied to clipboard

k8s_info Invalid label selector for version string (X.X.X)

Open mhtgrwl5 opened this issue 1 year ago • 1 comments

I am trying to filter out the the deployments which are not of my current version using ansible.

- name: Filter and get old deployment
  kubernetes.core.k8s_info:
    api_version: v1
    kind: Deployment
    namespace: "my_namespace"
    label_selectors:
      - curr_version notin (1.1.0)
  register: old_deployments

Expected the output to give the list of deployments not having curr_version equal to 1.1.0

But I am getting this error

{"level":"error","ts":1665557104.5018141,"logger":"proxy","msg":"Unable to convert label selectors for the client","error":"invalid selector: [curr_version notin (1.1.0)]","stacktrace":"net/http.serverHandler.ServeHTTP\n\t/usr/lib/golang/src/net/http/server.go:2879\nnet/http.(*conn).serve\n\t/usr/lib/golang/src/net/http/server.go:1930"}

I referenced the pattern matching from here - https://github.com/abikouo/kubernetes.core/blob/08596fd05ba7190a04e7112270a38a0ce32095dd/plugins/module_utils/selector.py#L39

According to the pattern the above selector looks fine.

Even I tried to change the selector line as this (for testing purpose) -

      - curr_version notin ("1.1.0")

But getting error as below.

{"level":"error","ts":1665555657.2939646,"logger":"requestfactory","msg":"Could not parse request","error":"unable to parse requirement: values[0][curr_version]: Invalid value: \"\\\"1.1.0\\\"\": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue',  or 'my_value',  or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')","stacktrace":"net/http.serverHandler.ServeHTTP\n\t/usr/lib/golang/src/net/http/server.go:2879\nnet/http.(*conn).serve\n\t/usr/lib/golang/src/net/http/server.go:1930"}
{"level":"error","ts":1665555657.2940943,"logger":"proxy","msg":"Unable to convert label selectors for the client","error":"invalid selector: [curr_version notin (\"1.1.0\")]","stacktrace":"net/http.serverHandler.ServeHTTP\n\t/usr/lib/golang/src/net/http/server.go:2879\nnet/http.(*conn).serve\n\t/usr/lib/golang/src/net/http/server.go:1930"}

I am not sure where am I wrong. I tried to find out the possible workaround but was not able to find it anywhere.

Although I am guessing second issue is just because the label selector is a string and the pattern doesn't allows to have quotes in the string. Which is understood.

Can someone help with this?

Other information which might be useful

kubernetes.core.k8s version - 2.2.3
operator-sdk version - 1.23.0
ansible version - 2.9.27
python - 3.6.8

mhtgrwl5 avatar Oct 12 '22 07:10 mhtgrwl5

I am unable to reproduce this. The following playbook works as expected for me:

- hosts: localhost
  gather_facts: false
  tasks:
    - kubernetes.core.k8s:
        kind: Namespace
        name: testing

    - kubernetes.core.k8s:
        definition:
          kind: Deployment
          apiVersion: apps/v1
          metadata:
            namespace: testing
            name: mydeploy
            labels:
              version: 1.1.0
          spec:
            replicas: 2
            selector:
              matchLabels:
                app: busypod
            template:
              metadata:
                labels:
                  app: busypod
              spec:
                containers:
                  - image: busybox
                    name: busybox
                    command:
                      - /bin/sh
                      - -c
                      - while true; do sleep 5; done

    - kubernetes.core.k8s_info:
        kind: Deployment
        api_version: apps/v1
        namespace: testing
        label_selectors:
          - version notin (1.1.1)

Tested with:

kubernetes.core 2.2.3
ansible 2.9.27
python 3.6.12

gravesm avatar Oct 12 '22 13:10 gravesm

@gravesm You are correct. That works in my machine. I guess I need to find out why that is not working when I am using it inside my operator.

mhtgrwl5 avatar Oct 31 '22 05:10 mhtgrwl5

I will keep the issue open for a while during my investigation.

mhtgrwl5 avatar Oct 31 '22 05:10 mhtgrwl5

@gravesm I am facing issue only inside the operator. The image of REDHAT that I am using is ose-ansible-operator:v4.10 I am unable to find any difference between the two environments. Will you be able to help me look into what's the difference between the two. On that image we are modifying below packages to these versions.

PyYaml==5.4.1
ansible-runner==2.2.1
oauthlib==3.2.1

Image source description. https://catalog.redhat.com/software/containers/openshift4/ose-ansible-operator/5cdc9a53bed8bd5717d6345e?tag=v4.10.0-202210211005.p0.g1064979.assembly.stream&push_date=1667372978000&container-tabs=packages

mhtgrwl5 avatar Nov 08 '22 07:11 mhtgrwl5

There's nothing that jumps out to me as being the culprit. You may have better luck filing an issue with https://github.com/operator-framework/operator-sdk/issues.

gravesm avatar Nov 08 '22 14:11 gravesm

@gravesm Thanks. Things are looking good with this package. I have requested support from operator-sdk management team. I am closing this issue. If I find something, I will add as comment on this issue. So that if someone faces the same issue, he will be able to find the solution.

mhtgrwl5 avatar Nov 28 '22 05:11 mhtgrwl5