From 769649003dba4fbdb5330505518f161c798f2583 Mon Sep 17 00:00:00 2001 From: Sylwia_Jakubek Date: Thu, 23 Jul 2020 11:34:53 +0200 Subject: Adapt DCAE CFY K8S Plugin with K8S 1.17 version of APIs Issue-ID: DCAEGEN2-2309 Signed-off-by: Sylwia Jakubek Change-Id: I40db1bcaae65f6d30a71ac7831b6c316e82b5576 --- k8s/ChangeLog.md | 3 +++ k8s/k8sclient/k8sclient.py | 22 ++++++++++++---------- k8s/k8splugin_types.yaml | 2 +- k8s/pom.xml | 2 +- k8s/requirements.txt | 2 +- k8s/setup.py | 6 +++--- k8s/tests/conftest.py | 10 ++++++++++ 7 files changed, 31 insertions(+), 16 deletions(-) diff --git a/k8s/ChangeLog.md b/k8s/ChangeLog.md index 0b96316..16300b0 100644 --- a/k8s/ChangeLog.md +++ b/k8s/ChangeLog.md @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [3.2.0] +* DCAEGEN2-2309 - Adapt with K8S 1.17 version of APIs + ## [3.1.0] * DCAEGEN2-2252 - Add support to request certificates from CMPv2 server in DCAE cloudify blueprints diff --git a/k8s/k8sclient/k8sclient.py b/k8s/k8sclient/k8sclient.py index 628f516..b972ce1 100644 --- a/k8s/k8sclient/k8sclient.py +++ b/k8s/k8sclient/k8sclient.py @@ -205,13 +205,15 @@ def _create_deployment_object(component_name, ) # Define deployment spec - spec = client.ExtensionsV1beta1DeploymentSpec( + spec = client.V1DeploymentSpec( replicas=replicas, + selector=client.V1LabelSelector(match_labels=labels), template=template ) # Create deployment object - deployment = client.ExtensionsV1beta1Deployment( + deployment = client.V1Deployment( + api_version="apps/v1", kind="Deployment", metadata=client.V1ObjectMeta(name=deployment_name), spec=spec @@ -388,13 +390,13 @@ def _patch_deployment(location, namespace, deployment, modify): _configure_api(location) # Get deployment spec - spec = client.ExtensionsV1beta1Api().read_namespaced_deployment(deployment, namespace) + spec = client.AppsV1Api().read_namespaced_deployment(deployment, namespace) # Apply changes to spec spec = modify(spec) # Patch the deploy with updated spec - client.ExtensionsV1beta1Api().patch_namespaced_deployment(deployment, namespace, spec) + client.AppsV1Api().patch_namespaced_deployment(deployment, namespace, spec) def _execute_command_in_pod(location, namespace, pod_name, command): ''' @@ -527,7 +529,7 @@ def deploy(namespace, component_name, image, replicas, always_pull, k8sconfig, * # Get API handles _configure_api(kwargs.get("k8s_location")) core = client.CoreV1Api() - ext = client.ExtensionsV1beta1Api() + k8s_apps_v1_api_client = client.AppsV1Api() # Parse the port mapping container_ports, port_map = parse_ports(kwargs.get("ports", [])) @@ -563,7 +565,7 @@ def deploy(namespace, component_name, image, replicas, always_pull, k8sconfig, * dep = _create_deployment_object(component_name, containers, init_containers, replicas, volumes, labels, pull_secrets=k8sconfig["image_pull_secrets"]) # Have k8s deploy it - ext.create_namespaced_deployment(namespace, dep) + k8s_apps_v1_api_client.create_namespaced_deployment(namespace, dep) deployment_ok = True deployment_description["deployment"] = _create_deployment_name(component_name) @@ -590,7 +592,7 @@ def deploy(namespace, component_name, image, replicas, always_pull, k8sconfig, * core.delete_namespaced_service(_create_service_name(component_name), namespace) # If the deployment was created but not the service, delete the deployment if deployment_ok: - client.ExtensionsV1beta1Api().delete_namespaced_deployment(_create_deployment_name(component_name), namespace, body=client.V1DeleteOptions()) + client.AppsV1Api().delete_namespaced_deployment(_create_deployment_name(component_name), namespace, body=client.V1DeleteOptions()) raise e return dep, deployment_description @@ -606,11 +608,11 @@ def undeploy(deployment_description): # Have k8s delete the underlying pods and replicaset when deleting the deployment. options = client.V1DeleteOptions(propagation_policy="Foreground") - client.ExtensionsV1beta1Api().delete_namespaced_deployment(deployment_description["deployment"], namespace, body=options) + client.AppsV1Api().delete_namespaced_deployment(deployment_description["deployment"], namespace, body=options) def is_available(location, namespace, component_name): _configure_api(location) - dep_status = client.AppsV1beta1Api().read_namespaced_deployment_status(_create_deployment_name(component_name), namespace) + dep_status = client.AppsV1Api().read_namespaced_deployment_status(_create_deployment_name(component_name), namespace) # Check if the number of available replicas is equal to the number requested and that the replicas match the current spec # This check can be used to verify completion of an initial deployment, a scale operation, or an update operation return dep_status.status.available_replicas == dep_status.spec.replicas and dep_status.status.updated_replicas == dep_status.spec.replicas @@ -660,7 +662,7 @@ def rollback(deployment_description, rollback_to=0): client.AppsV1beta1DeploymentRollback(name=deployment, rollback_to=client.AppsV1beta1RollbackConfig(revision=rollback_to))) # Read back the spec for the rolled-back deployment - spec = client.ExtensionsV1beta1Api().read_namespaced_deployment(deployment, namespace) + spec = client.AppsV1Api().read_namespaced_deployment(deployment, namespace) return spec.spec.template.spec.containers[0].image, spec.spec.replicas def execute_command_in_deployment(deployment_description, command): diff --git a/k8s/k8splugin_types.yaml b/k8s/k8splugin_types.yaml index 0568186..ff649df 100644 --- a/k8s/k8splugin_types.yaml +++ b/k8s/k8splugin_types.yaml @@ -23,7 +23,7 @@ plugins: k8s: executor: 'central_deployment_agent' package_name: k8splugin - package_version: 3.1.0 + package_version: 3.2.0 data_types: diff --git a/k8s/pom.xml b/k8s/pom.xml index 6ba215d..8a541d4 100644 --- a/k8s/pom.xml +++ b/k8s/pom.xml @@ -28,7 +28,7 @@ limitations under the License. org.onap.dcaegen2.platform.plugins k8s k8s-plugin - 3.1.0-SNAPSHOT + 3.2.0-SNAPSHOT http://maven.apache.org UTF-8 diff --git a/k8s/requirements.txt b/k8s/requirements.txt index fc23d8e..b8e6663 100644 --- a/k8s/requirements.txt +++ b/k8s/requirements.txt @@ -1,6 +1,6 @@ setuptools<45.0.0 python-consul>=0.6.0 onap-dcae-dcaepolicy-lib>=2.4.1 -kubernetes==10.0.0 +kubernetes==11.0.0 cloudify-common>=5.0.0; python_version<"3" cloudify-common @ git+https://github.com/cloudify-cosmo/cloudify-common@cy-1374-python3#egg=cloudify-common==5.0.0; python_version>="3" diff --git a/k8s/setup.py b/k8s/setup.py index fbe623c..da3a7d3 100644 --- a/k8s/setup.py +++ b/k8s/setup.py @@ -23,14 +23,14 @@ from setuptools import setup setup( name='k8splugin', description='Cloudify plugin for containerized components deployed using Kubernetes', - version="3.1.0", - author='J. F. Lucas, Michael Hwang, Tommy Carpenter, Joanna Jeremicz', + version="3.2.0", + author='J. F. Lucas, Michael Hwang, Tommy Carpenter, Joanna Jeremicz, Sylwia Jakubek', packages=['k8splugin','k8sclient','configure'], zip_safe=False, install_requires=[ 'python-consul>=0.6.0', 'onap-dcae-dcaepolicy-lib>=2.4.1', - 'kubernetes==10.0.0', + 'kubernetes==11.0.0', 'cloudify-common>=5.0.0', ] ) diff --git a/k8s/tests/conftest.py b/k8s/tests/conftest.py index ae09da1..442e03c 100644 --- a/k8s/tests/conftest.py +++ b/k8s/tests/conftest.py @@ -2,6 +2,7 @@ # org.onap.dcae # ================================================================================ # Copyright (c) 2018-2020 AT&T Intellectual Property. All rights reserved. +# Copyright (c) 2020 Nokia. All rights reserved. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -39,6 +40,7 @@ def mockk8sapi(monkeypatch): # to get an API object core = client.CoreV1Api() ext = client.ExtensionsV1beta1Api() + appsv1 = client.AppsV1Api() def pseudo_deploy(namespace, dep): return dep @@ -60,9 +62,17 @@ def mockk8sapi(monkeypatch): monkeypatch.setattr(ext,"create_namespaced_deployment", pseudo_deploy) return ext + # patched_appsv1 returns an AppsV1Api object with the + # create_namespaced_deployment method stubbed out so that there + # is no attempt to call the k8s API server + def patched_appsv1(): + monkeypatch.setattr(ext,"create_namespaced_deployment", pseudo_deploy) + return ext + def pseudo_configure(loc): pass monkeypatch.setattr(k8sclient.k8sclient,"_configure_api", pseudo_configure) monkeypatch.setattr(client, "CoreV1Api", patched_core) monkeypatch.setattr(client,"ExtensionsV1beta1Api", patched_ext) + monkeypatch.setattr(client,"AppsV1Api", patched_appsv1) -- cgit 1.2.3-korg