3

I'm pretty new to Kubernetes and I have to create a pod using Kubernetes python-client. So to experiment around I'm trying to run examples notebooks provided by the project without any change to see how things works.

Starting with intro_notebook.ipynb at 3rd step I get an error:

ValueError: Invalid value for `selector`, must not be `None`

Here is the code:

from kubernetes import client, config

The only part I've changed is the second cell, because I'm running Kubernetes using Ubuntu's microk8s:

config.load_kube_config('/var/snap/microk8s/current/credentials/client.config')
api_instance = client.AppsV1Api()
dep = client.V1Deployment()
spec = client.V1DeploymentSpec() # <<< At this line I hit the error!

Complete Traceback:

ValueError                                Traceback (most recent call last)
<ipython-input-9-f155901e8381> in <module>
      1 api_instance = client.AppsV1Api()
      2 dep = client.V1Deployment()
----> 3 spec = client.V1DeploymentSpec()

~/.local/share/virtualenvs/jupyter-2hJZ4kgI/lib/python3.8/site-packages/kubernetes/client/models/v1_deployment_spec.py in __init__(self, min_ready_seconds, paused, progress_deadline_seconds, replicas, revision_history_limit, selector, strategy, template)
     76         if revision_history_limit is not None:
     77             self.revision_history_limit = revision_history_limit
---> 78         self.selector = selector
     79         if strategy is not None:
     80             self.strategy = strategy

~/.local/share/virtualenvs/jupyter-2hJZ4kgI/lib/python3.8/site-packages/kubernetes/client/models/v1_deployment_spec.py in selector(self, selector)
    215         """
    216         if selector is None:
--> 217             raise ValueError("Invalid value for `selector`, must not be `None`")  # noqa: E501
    218 
    219         self._selector = selector

ValueError: Invalid value for `selector`, must not be `None`

I'm running python3.8:

$ pip freeze | grep -e kuber
kubernetes==11.0.0

$ snap list microk8s 
Name      Version  Rev   Tracking       Publisher   Notes
microk8s  v1.18.6  1551  latest/stable  canonical✓  classic

Update

Downgrading microk8s to v1.15.11 did not solve the issue.

FooBar
  • 55
  • 1
  • 9
  • Are you sure that was the error? Usually there is an entire traceback including the line that failed. – tdelaney Aug 02 '20 at 17:19
  • Kindly include the whole error prompt, and the code as well. – Astrian_72954 Aug 02 '20 at 17:21
  • @FooBar Can you please share the Kubernetes version you cluster run? – hilsenrat Aug 02 '20 at 17:39
  • @hilsenrat Version added: `v1.18.6`. Is this a compatibility issue? I'm able to list pods... – FooBar Aug 02 '20 at 18:34
  • @FooBar It probably is.. as you can see [in that issue](https://github.com/kubernetes-client/python/issues/1170), the Python client supports now only up until Kubernetes v1.15. – hilsenrat Aug 02 '20 at 18:43
  • @hilsenrat I've downgrade it to version v1.15.11, (the closest version available). Still getting the same result. – FooBar Aug 02 '20 at 18:57
  • @FooBar I see... I checked the [client spec](https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1DeploymentSpec.md) and looks like the `selector` and `template` properties are not optional. – hilsenrat Aug 02 '20 at 19:03
  • Sorry for leading you to the wrong way, I glad that @mdaniel was able to assist you eventually. – hilsenrat Aug 02 '20 at 19:22
  • @hilsenrat NP, Thanks for trying to help :) – FooBar Aug 02 '20 at 19:26

1 Answers1

1

It's because they changed the validation to happen in the constructor, rather that later -- which is not what the notebook was expecting. They You just need to move those inner assignments up to be valid before constructing the Deployment:

name = "my-busybox"
spec = client.V1DeploymentSpec(
    selector=client.V1LabelSelector(match_labels={"app":"busybox"}),
    template=client.V1PodTemplateSpec(),
)
container = client.V1Container(
    image="busybox:1.26.1",
    args=["sleep", "3600"],
    name=name,
)
spec.template.metadata = client.V1ObjectMeta(
    name="busybox",
    labels={"app":"busybox"},
)
spec.template.spec = client.V1PodSpec(containers = [container])
dep = client.V1Deployment(
    metadata=client.V1ObjectMeta(name=name),
    spec=spec,
)
mdaniel
  • 31,240
  • 5
  • 55
  • 58
  • Thanks, I'm so lost... I mean, really LOST! – FooBar Aug 02 '20 at 19:10
  • 1
    if the kubernetes python client is your first contact with python, that's a rough place to start. If the kubernetes python client is your first contact with kubernetes, that is **for sure** the wrong place to start – mdaniel Aug 02 '20 at 19:20
  • It's the second one. "Kubernetes python client is **almost** my first contact with Kubernetes". I only have basic knowledge of Kubernetes, what's a cluster, what's a node or pod. How to run a pod... that's pretty much all I know from a beginner course... – FooBar Aug 02 '20 at 19:29