From c784302b617bca9493ffb2a44981d51492f5c8d5 Mon Sep 17 00:00:00 2001 From: Juha Kosonen Date: Wed, 30 Oct 2019 08:49:21 +0200 Subject: [PATCH] Along with the v1.16 release a deprecated extensions/v1beta1 API was removed [1]. Migrate to use the apps/v1 API. [1] https://kubernetes.io/docs/setup/release/notes/#kubernetes-v1-16-0-release-notes Signed-off-by: Juha Kosonen --- tests/unit/test_service.py | 47 ++++++++++++++++++++++++------------ xrally_kubernetes/service.py | 40 ++++++++++++++++++------------ 2 files changed, 56 insertions(+), 31 deletions(-) diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py index a45a38d..2acf5e4 100644 --- a/tests/unit/test_service.py +++ b/tests/unit/test_service.py @@ -1101,10 +1101,9 @@ class ReplicaSetServiceTestCase(KubernetesServiceTestCase): def setUp(self): super(ReplicaSetServiceTestCase, self).setUp() - from kubernetes.client.apis import extensions_v1beta1_api + from kubernetes.client.apis import apps_v1_api - p_mock_client = mock.patch.object(extensions_v1beta1_api, - "ExtensionsV1beta1Api") + p_mock_client = mock.patch.object(apps_v1_api, "AppsV1Api") self.client_cls = p_mock_client.start() self.client = self.client_cls.return_value self.addCleanup(p_mock_client.stop) @@ -1123,7 +1122,7 @@ def test_create_replicaset(self): status_wait=False) expected = { - "apiVersion": "extensions/v1beta1", + "apiVersion": "apps/v1", "kind": "ReplicaSet", "metadata": { "name": "name", @@ -1177,7 +1176,7 @@ def test_create_replicaset_with_command(self): status_wait=False) expected = { - "apiVersion": "extensions/v1beta1", + "apiVersion": "apps/v1", "kind": "ReplicaSet", "metadata": { "name": "name", @@ -1841,10 +1840,9 @@ class DeploymentServiceTestCase(KubernetesServiceTestCase): def setUp(self): super(DeploymentServiceTestCase, self).setUp() - from kubernetes.client.apis import extensions_v1beta1_api + from kubernetes.client.apis import apps_v1_api - p_mock_client = mock.patch.object(extensions_v1beta1_api, - "ExtensionsV1beta1Api") + p_mock_client = mock.patch.object(apps_v1_api, "AppsV1Api") self.client_cls = p_mock_client.start() self.client = self.client_cls.return_value self.addCleanup(p_mock_client.stop) @@ -1863,7 +1861,7 @@ def test_create_deployment(self): status_wait=False) expected = { - "apiVersion": "extensions/v1beta1", + "apiVersion": "apps/v1", "kind": "Deployment", "metadata": { "name": "name", @@ -1872,6 +1870,11 @@ def test_create_deployment(self): } }, "spec": { + "selector": { + "matchLabels": { + "app": mock.ANY + } + }, "replicas": 2, "template": { "metadata": { @@ -1912,7 +1915,7 @@ def test_create_deployment_with_command(self): status_wait=False) expected = { - "apiVersion": "extensions/v1beta1", + "apiVersion": "apps/v1", "kind": "Deployment", "metadata": { "name": "name", @@ -1921,6 +1924,11 @@ def test_create_deployment_with_command(self): } }, "spec": { + "selector": { + "matchLabels": { + "app": mock.ANY + } + }, "replicas": 2, "template": { "metadata": { @@ -3165,11 +3173,10 @@ class DaemonSetServiceTestCase(KubernetesServiceTestCase): def setUp(self): super(DaemonSetServiceTestCase, self).setUp() + from kubernetes.client.apis import apps_v1_api from kubernetes.client.apis import core_v1_api - from kubernetes.client.apis import extensions_v1beta1_api - p_mock_client = mock.patch.object(extensions_v1beta1_api, - "ExtensionsV1beta1Api") + p_mock_client = mock.patch.object(apps_v1_api, "AppsV1Api") self.client_cls = p_mock_client.start() self.client = self.client_cls.return_value self.addCleanup(p_mock_client.stop) @@ -3193,12 +3200,17 @@ def test_create_daemonset(self): status_wait=False) expected = { - "apiVersion": "extensions/v1beta1", + "apiVersion": "apps/v1", "kind": "DaemonSet", "metadata": { "name": "name" }, "spec": { + "selector": { + "matchLabels": { + "app": mock.ANY + } + }, "template": { "metadata": { "name": "name", @@ -3238,12 +3250,17 @@ def test_create_daemonset_with_command(self): status_wait=False) expected = { - "apiVersion": "extensions/v1beta1", + "apiVersion": "apps/v1", "kind": "DaemonSet", "metadata": { "name": "name" }, "spec": { + "selector": { + "matchLabels": { + "app": mock.ANY + } + }, "template": { "metadata": { "name": "name", diff --git a/xrally_kubernetes/service.py b/xrally_kubernetes/service.py index 4a898f1..c5f0b7d 100644 --- a/xrally_kubernetes/service.py +++ b/xrally_kubernetes/service.py @@ -19,7 +19,6 @@ from kubernetes.client.apis import apps_v1_api from kubernetes.client.apis import batch_v1_api from kubernetes.client.apis import core_v1_api -from kubernetes.client.apis import extensions_v1beta1_api from kubernetes.client.apis import storage_v1_api from kubernetes.client.apis import version_api from kubernetes.client import rest @@ -204,7 +203,6 @@ def __init__(self, spec, name_generator=None, atomic_inst=None): self.api = api self.v1_client = core_v1_api.CoreV1Api(api) - self.v1beta1_ext = extensions_v1beta1_api.ExtensionsV1beta1Api(api) self.v1_batch = batch_v1_api.BatchV1Api(api) self.v1_apps = apps_v1_api.AppsV1Api(api) self.v1_storage = storage_v1_api.StorageV1Api(api) @@ -609,7 +607,7 @@ def delete_rc(self, name, namespace, status_wait=True): @atomic.action_timer("kubernetes.get_replicaset") def get_replicaset(self, name, namespace, **kwargs): - return self.v1beta1_ext.read_namespaced_replica_set( + return self.v1_apps.read_namespaced_replica_set( name, namespace=namespace ) @@ -639,7 +637,7 @@ def create_replicaset(self, namespace, replicas, image, command=None, container_spec["command"] = list(command) manifest = { - "apiVersion": "extensions/v1beta1", + "apiVersion": "apps/v1", "kind": "ReplicaSet", "metadata": { "name": name, @@ -672,7 +670,7 @@ def create_replicaset(self, namespace, replicas, image, command=None, if not self._spec.get("serviceaccounts"): del manifest["spec"]["template"]["spec"]["serviceAccountName"] - self.v1beta1_ext.create_namespaced_replica_set( + self.v1_apps.create_namespaced_replica_set( namespace=namespace, body=manifest ) @@ -690,7 +688,7 @@ def create_replicaset(self, namespace, replicas, image, command=None, @atomic.action_timer("kubernetes.scale_replicaset") def scale_replicaset(self, name, namespace, replicas, status_wait=True): - self.v1beta1_ext.patch_namespaced_replica_set( + self.v1_apps.patch_namespaced_replica_set( name, namespace=namespace, body={"spec": {"replicas": replicas}} @@ -713,7 +711,7 @@ def delete_replicaset(self, name, namespace, status_wait=True): :param namespace: replicaset namespace :param status_wait: wait for termination if True """ - self.v1beta1_ext.delete_namespaced_replica_set( + self.v1_apps.delete_namespaced_replica_set( name, namespace=namespace, body=k8s_config.V1DeleteOptions() @@ -729,7 +727,7 @@ def delete_replicaset(self, name, namespace, status_wait=True): @atomic.action_timer("kubernetes.get_deployment") def get_deployment(self, name, namespace, **kwargs): - return self.v1beta1_ext.read_namespaced_deployment_status( + return self.v1_apps.read_namespaced_deployment_status( name=name, namespace=namespace ) @@ -771,7 +769,7 @@ def create_deployment(self, namespace, replicas, image, resources=None, container_spec["resources"] = resources manifest = { - "apiVersion": "extensions/v1beta1", + "apiVersion": "apps/v1", "kind": "Deployment", "metadata": { "name": name, @@ -780,6 +778,11 @@ def create_deployment(self, namespace, replicas, image, resources=None, } }, "spec": { + "selector": { + "matchLabels": { + "app": app + } + }, "replicas": replicas, "template": { "metadata": { @@ -799,7 +802,7 @@ def create_deployment(self, namespace, replicas, image, resources=None, if not self._spec.get("serviceaccounts"): del manifest["spec"]["template"]["spec"]["serviceAccountName"] - self.v1beta1_ext.create_namespaced_deployment( + self.v1_apps.create_namespaced_deployment( namespace=namespace, body=manifest ) @@ -843,7 +846,7 @@ def rollout_deployment(self, name, namespace, changes, replicas, "exclusive keys: image, env, resources." ) - self.v1beta1_ext.patch_namespaced_deployment( + self.v1_apps.patch_namespaced_deployment( name=name, namespace=namespace, body=deployment @@ -866,7 +869,7 @@ def delete_deployment(self, name, namespace, status_wait=True): :param namespace: deployment namespace :param status_wait: wait for termination if True """ - self.v1beta1_ext.delete_namespaced_deployment( + self.v1_apps.delete_namespaced_deployment( name=name, namespace=namespace, body=k8s_config.V1DeleteOptions() @@ -1164,7 +1167,7 @@ def list_nodes(self, node_labels=None): @atomic.action_timer("kubernetes.get_daemonset") def get_daemonset(self, name, namespace, **kwargs): - return self.v1beta1_ext.read_namespaced_daemon_set( + return self.v1_apps.read_namespaced_daemon_set( name, namespace=namespace ) @@ -1195,12 +1198,17 @@ def create_daemonset(self, image, namespace, command=None, container_spec["command"] = list(command) manifest = { - "apiVersion": "extensions/v1beta1", + "apiVersion": "apps/v1", "kind": "DaemonSet", "metadata": { "name": name }, "spec": { + "selector": { + "matchLabels": { + "app": app + } + }, "template": { "metadata": { "name": name, @@ -1219,7 +1227,7 @@ def create_daemonset(self, image, namespace, command=None, if not self._spec.get("serviceaccounts"): del manifest["spec"]["template"]["spec"]["serviceAccountName"] - self.v1beta1_ext.create_namespaced_daemon_set( + self.v1_apps.create_namespaced_daemon_set( namespace=namespace, body=manifest ) @@ -1280,7 +1288,7 @@ def delete_daemonset(self, name, namespace, status_wait=True): :param namespace: daemon set namespace :param status_wait: wait for termination if True """ - self.v1beta1_ext.delete_namespaced_daemon_set( + self.v1_apps.delete_namespaced_daemon_set( name, namespace=namespace, body=k8s_config.V1DeleteOptions()