Skip to content

Commit

Permalink
Merge pull request #1079 from fabianvf/fix-py3-hang-10
Browse files Browse the repository at this point in the history
cherry-pick of #1073 to release-10.0: Fix python3 hang
  • Loading branch information
k8s-ci-robot authored Feb 21, 2020
2 parents df6fdd2 + 629c747 commit 9ca8780
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 101 deletions.
4 changes: 2 additions & 2 deletions examples/create_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def main():

with open(path.join(path.dirname(__file__), "nginx-deployment.yaml")) as f:
dep = yaml.safe_load(f)
k8s_beta = client.ExtensionsV1beta1Api()
resp = k8s_beta.create_namespaced_deployment(
k8s_apps_v1 = client.AppsV1Api()
resp = k8s_apps_v1.create_namespaced_deployment(
body=dep, namespace="default")
print("Deployment created. status='%s'" % str(resp.status))

Expand Down
2 changes: 1 addition & 1 deletion examples/create_deployment_from_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def main():
config.load_kube_config()
k8s_client = client.ApiClient()
utils.create_from_yaml(k8s_client, "nginx-deployment.yaml")
k8s_api = client.ExtensionsV1beta1Api(k8s_client)
k8s_api = client.AppsV1Api(k8s_client)
deps = k8s_api.read_namespaced_deployment("nginx-deployment", "default")
print("Deployment {0} created".format(deps.metadata.name))

Expand Down
18 changes: 9 additions & 9 deletions examples/deployment_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ def create_deployment_object():
# Configureate Pod template container
container = client.V1Container(
name="nginx",
image="nginx:1.7.9",
image="nginx:1.15.4",
ports=[client.V1ContainerPort(container_port=80)])
# Create and configurate a spec section
template = client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(labels={"app": "nginx"}),
spec=client.V1PodSpec(containers=[container]))
# Create the specification of deployment
spec = client.ExtensionsV1beta1DeploymentSpec(
spec = client.AppsV1beta1DeploymentSpec(
replicas=3,
template=template)
# Instantiate the deployment object
deployment = client.ExtensionsV1beta1Deployment(
api_version="extensions/v1beta1",
deployment = client.AppsV1beta1Deployment(
api_version="apps/v1beta1",
kind="Deployment",
metadata=client.V1ObjectMeta(name=DEPLOYMENT_NAME),
spec=spec)
Expand All @@ -55,7 +55,7 @@ def create_deployment(api_instance, deployment):

def update_deployment(api_instance, deployment):
# Update container image
deployment.spec.template.spec.containers[0].image = "nginx:1.9.1"
deployment.spec.template.spec.containers[0].image = "nginx:1.16.0"
# Update the deployment
api_response = api_instance.patch_namespaced_deployment(
name=DEPLOYMENT_NAME,
Expand All @@ -80,16 +80,16 @@ def main():
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
extensions_v1beta1 = client.ExtensionsV1beta1Api()
apps_v1beta1 = client.AppsV1beta1Api()
# Create a deployment object with client-python API. The deployment we
# created is same as the `nginx-deployment.yaml` in the /examples folder.
deployment = create_deployment_object()

create_deployment(extensions_v1beta1, deployment)
create_deployment(apps_v1beta1, deployment)

update_deployment(extensions_v1beta1, deployment)
update_deployment(apps_v1beta1, deployment)

delete_deployment(extensions_v1beta1)
delete_deployment(apps_v1beta1)


if __name__ == '__main__':
Expand Down
10 changes: 7 additions & 3 deletions examples/nginx-deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
image: nginx:1.15.4
ports:
- containerPort: 80

18 changes: 9 additions & 9 deletions examples/notebooks/create_deployment.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"outputs": [],
"source": [
"config.load_kube_config()\n",
"extension = client.ExtensionsV1beta1Api()"
"apps_api = client.AppsV1beta1Api()"
]
},
{
Expand All @@ -70,7 +70,7 @@
},
"outputs": [],
"source": [
"deployment = client.ExtensionsV1beta1Deployment()"
"deployment = client.AppsV1beta1Deployment()"
]
},
{
Expand All @@ -93,7 +93,7 @@
},
"outputs": [],
"source": [
"deployment.api_version = \"extensions/v1beta1\"\n",
"deployment.api_version = \"apps/v1beta1\"\n",
"deployment.kind = \"Deployment\"\n",
"deployment.metadata = client.V1ObjectMeta(name=\"nginx-deployment\")"
]
Expand All @@ -118,7 +118,7 @@
},
"outputs": [],
"source": [
"spec = client.ExtensionsV1beta1DeploymentSpec()\n",
"spec = client.AppsV1beta1DeploymentSpec()\n",
"spec.replicas = 3"
]
},
Expand Down Expand Up @@ -207,7 +207,7 @@
},
"outputs": [],
"source": [
"extension.create_namespaced_deployment(namespace=\"default\", body=deployment)"
"apps_api.create_namespaced_deployment(namespace=\"default\", body=deployment)"
]
},
{
Expand Down Expand Up @@ -253,7 +253,7 @@
},
"outputs": [],
"source": [
"extension.replace_namespaced_deployment(name=\"nginx-deployment\", namespace=\"default\", body=deployment)"
"apps_api.replace_namespaced_deployment(name=\"nginx-deployment\", namespace=\"default\", body=deployment)"
]
},
{
Expand All @@ -277,10 +277,10 @@
},
"outputs": [],
"source": [
"rollback = client.ExtensionsV1beta1DeploymentRollback()\n",
"rollback.api_version = \"extensions/v1beta1\"\n",
"rollback = client.AppsV1beta1DeploymentRollback()\n",
"rollback.api_version = \"apps/v1beta1\"\n",
"rollback.kind = \"DeploymentRollback\"\n",
"rollback.rollback_to = client.ExtensionsV1beta1RollbackConfig()\n",
"rollback.rollback_to = client.AppsV1beta1RollbackConfig()\n",
"rollback.rollback_to.revision = 0\n",
"rollback.name = \"nginx-deployment\""
]
Expand Down
6 changes: 3 additions & 3 deletions examples/notebooks/intro_notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@
},
"outputs": [],
"source": [
"api_instance = client.ExtensionsV1beta1Api()\n",
"dep = client.ExtensionsV1beta1Deployment()\n",
"spec = client.ExtensionsV1beta1DeploymentSpec()"
"api_instance = client.AppsV1beta1Api()\n",
"dep = client.AppsV1beta1Deployment()\n",
"spec = client.AppsV1beta1DeploymentSpec()"
]
},
{
Expand Down
14 changes: 12 additions & 2 deletions kubernetes/client/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import os
import re
import json
import atexit
import mimetypes
import tempfile
from multiprocessing.pool import ThreadPool
Expand Down Expand Up @@ -74,19 +75,28 @@ def __init__(self, configuration=None, header_name=None, header_value=None,
self.cookie = cookie
# Set default User-Agent.
self.user_agent = 'Swagger-Codegen/10.0.1/python'

def __del__(self):

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
self.close()

def close(self):
if self._pool:
self._pool.close()
self._pool.join()
self._pool = None
if hasattr(atexit, 'unregister'):
atexit.unregister(self.close)

@property
def pool(self):
"""Create thread pool on first request
avoids instantiating unused threadpool for blocking clients.
"""
if self._pool is None:
atexit.register(self.close)
self._pool = ThreadPool(self.pool_threads)
return self._pool

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,38 @@
import yaml

from kubernetes.client import api_client
from kubernetes.client.apis import extensions_v1beta1_api
from kubernetes.client.apis import apps_v1_api
from kubernetes.client.models import v1_delete_options
from kubernetes.e2e_test import base


class TestClientExtensions(unittest.TestCase):
class TestClientApps(unittest.TestCase):

@classmethod
def setUpClass(cls):
cls.config = base.get_e2e_configuration()

def test_create_deployment(self):
client = api_client.ApiClient(configuration=self.config)
api = extensions_v1beta1_api.ExtensionsV1beta1Api(client)
api = apps_v1_api.AppsV1Api(client)
name = 'nginx-deployment-' + str(uuid.uuid4())
deployment = '''apiVersion: extensions/v1beta1
deployment = '''apiVersion: apps/v1
kind: Deployment
metadata:
name: %s
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
image: nginx:1.15.4
ports:
- containerPort: 80
'''
Expand All @@ -60,24 +63,27 @@ def test_create_deployment(self):

def test_create_daemonset(self):
client = api_client.ApiClient(configuration=self.config)
api = extensions_v1beta1_api.ExtensionsV1beta1Api(client)
api = apps_v1_api.AppsV1Api(client)
name = 'nginx-app-' + str(uuid.uuid4())
daemonset = {
'apiVersion': 'extensions/v1beta1',
'apiVersion': 'apps/v1',
'kind': 'DaemonSet',
'metadata': {
'labels': {'app': 'nginx'},
'name': '%s' % name,
},
'spec': {
'selector': {
'matchLabels': {'app': 'nginx'},
},
'template': {
'metadata': {
'labels': {'app': 'nginx'},
'name': name},
'spec': {
'containers': [
{'name': 'nginx-app',
'image': 'nginx:1.10'},
'image': 'nginx:1.15.4'},
],
},
},
Expand All @@ -91,4 +97,4 @@ def test_create_daemonset(self):
self.assertIsNotNone(resp)

options = v1_delete_options.V1DeleteOptions()
resp = api.delete_namespaced_daemon_set(name, 'default', body=options)
resp = api.delete_namespaced_daemon_set(name, 'default', body=options)
Loading

0 comments on commit 9ca8780

Please sign in to comment.