Add option pass selectors as `string`
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:
- Either parse my string selector to
LabelSelectorjust so thatbuild_selectorparses it back to a string - 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.
I think the option 1 (parse the string) would be preferable for few reasons:
- It will allow to verify the selector validity before sending it to the remote server
- Will limit the complexity for the signature of labels and fields (it is already complicated)
- The attributes are named
labelsandfields(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 likeparse_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.