Python: Pod creation not working after upgrading to 4.0

Created on 23 Nov 2017  路  4Comments  路  Source: kubernetes-client/python

I have upgraded to api version 4.0 and my pod creation is not working after this upgrade. it gives me error as :
'
kubernetes\client\models\v1_container_port.py", line 92, in container_port
raise ValueError("Invalid value for container_port, must not be None")
ValueError: Invalid value for container_port, must not be None

`

And my code is :

client.Configuration().host = "<my host>"
v1 = client.CoreV1Api()
pod = client.V1ReplicationController()
pod.kind = "ReplicationController"
podTemplate = client.V1PodTemplateSpec()
pod.metadata = client.V1ObjectMeta(name="server")
repSec = client.V1ReplicationControllerSpec()
repSec.replicas = 1
repSec.selector = {'name': 'server'}
container = client.V1Container(name='server')
container.name = "server"
container.image = "<my images>"
port = client.V1ContainerPort()
port.container_port = 8080
# port.name="client"
port_list = []
port_list.append(port)
port.container_port = 8081
# port.name="server-port"
port_list.append(port)
container.ports = port_list
spec = client.V1PodSpec(containers=[container])
podTemplate.spec = spec
podTemplate.metadata = client.V1ObjectMeta(labels={'name': 'server'})
repSec.template = podTemplate
pod.spec = repSec
v1.create_namespaced_replication_controller(namespace="default", body=pod)

Most helpful comment

Yes, sounds good. Thanks for the help @tomplus

All 4 comments

@mbohlool I just looked around the code and found a if check missing in vi_container_port for container_port. Can you please look around it and i can work on it as well and create a PR if it's required.

Hi @codefetcher I've checked it and you are right - it's kind of incompatibility between versions. You can compare file: kubernetes/client/models/v1_container_port.py

version 3.0

class V1ContainerPort(object):

    def __init__(self, container_port=None, host_ip=None, host_port=None, name=None, protocol=None):

        self._container_port = container_port
        self._host_ip = host_ip
        self._host_port = host_port
        self._name = name
        self._protocol = protocol

version 4.0

    def __init__(self, container_port=None, host_ip=None, host_port=None, name=None, protocol=None):

        self._container_port = None
        self._host_ip = None
        self._host_port = None
        self._name = None
        self._protocol = None
        self.discriminator = None

        self.container_port = container_port    << use setter and it's required
        if host_ip is not None:
          self.host_ip = host_ip
        if host_port is not None:
          self.host_port = host_port
        if name is not None:
          self.name = name
        if protocol is not None:
          self.protocol = protocol

The latest version of swagger-codegen (which is used to generate code from specification) does it in this way. If fields are mandatory you'll have to pass it via a constructor. IMO it's better because object without container_port defined is invalid.

You can fix your code easily:

port = client.V1ContainerPort(container_port=8080)

How does it look ?

BTW, your list of ports could be incorrect, because you add a reference to the same object twice.

>>> port = kubernetes.client.models.V1ContainerPort(8080)
>>> ports = []
>>> ports.append(port)
>>> port.container_port = 8081
>>> ports.append(port)
>>> ports
[{'container_port': 8081,
 'host_ip': None,
 'host_port': None,
 'name': None,
 'protocol': None}, {'container_port': 8081,
 'host_ip': None,
 'host_port': None,
 'name': None,
 'protocol': None}]
>>> 

/cc @yliaog

Yes, sounds good. Thanks for the help @tomplus

Was this page helpful?
0 / 5 - 0 ratings