lightkube icon indicating copy to clipboard operation
lightkube copied to clipboard

Add option pass selectors as `string`

Open wochinge opened this issue 1 year ago • 1 comments

Problem

First of all, I really like syntax to create selectors. Very clean and pythonic 💯

In our case we'd like to pass in the selector via environment variable. This is quite convenient as we can dynamically change the selector in quite advanced ways. Lightkube doesn't support an already rendered string selector. That leaves me with the following options:

  1. Either parse my string selector to LabelSelector just so that build_selector parses it back to a string
  2. Directly use the client under the hood. Example
    request = self._kubernetes_client._client.prepare_request(
            'list', res=Namespace,
            params={
                'limit': None,
                'labelSelector': string_selector,
                'fieldSelector': None
            }
        )
    namespaces = self._kubernetes_client.list(request)
    

A simple fix for this would be to change the build_selector function to just return the passed selector (and not parse it) in case the passed selector is already a string.

wochinge avatar Jul 03 '24 15:07 wochinge

I think the option 1 (parse the string) would be preferable for few reasons:

  1. It will allow to verify the selector validity before sending it to the remote server
  2. Will limit the complexity for the signature of labels and fields (it is already complicated)
  3. The attributes are named labels and fields (plural), so it would be odd to allow a single string as a value. We could include this functionality as a function in operators.py like parse_selector(..).

However, I do see the advantage of allowing a prepared string, which will reduce the need for serialize and deserialize the selector at each call..

Feel free to create a PR with the approach you would like to see.

gtsystem avatar Jul 08 '24 08:07 gtsystem