f081453b3c
This commit adds the command "subcloud deploy abort" to dcmanager. It allows the user to abort the on-going execution of a playbook against the subcloud. Any task will be aborted immediately if the unabortable flag is not set by the playbook [1]. If current operation is install, a shutdown command will also be issued. Test Plan: Success cases: - PASS: Trigger an abort during installation and verify that the playbook execution was aborted immediately, the subcloud was shut dows after it and the RVMC pod and job were terminated. - PASS: Trigger an abort during config and verify that the playbook execution was aborted immediately. - PASS: Trigger an abort during bootstrap without the presence of unabortable flag and verify that the playbook execution was aborted immidiately. - PASS: Trigger an abort during bootstrap with the presence of unabortable flag and verify that the playbook execution was aborted only after the flag was deleted. - PASS: Trigger an abort directly calling the API (using CURL instead of using the CLI. Failure cases: - PASS: Verify that the abort request is rejected if deploy state is not 'installing', 'bootstrapping' or 'configuring'. - PASS: Abort when an unabortable task is running and then force an external error during this task restarting dcmanager-manager service, verify that deploy state is set to a failed state e.g. bootstrap-failed. - PASS: Abort when an unabortable task is running and then force an internal error during this task using ansible.builtin.fail module, verify that deploy state is set to a failed state e.g. bootstrap-failed. - PASS: Abort when an unabortable task is running and then force the playbook to halt the execution during this task using ansible.builtin.pause module, verify that deploy state is set to a failed state after 10 minutes e.g. bootstrap-failed. Story: 2010756 Task: 48102 Co-Authored-By: Gustavo Herzmann <gustavo.herzmann@windriver.com> Signed-off-by: Victor Romano <victor.gluzromano@windriver.com> Change-Id: Ic5311324a76bf7ce1215692e934d5577ff82868e
207 lines
7.0 KiB
Python
207 lines
7.0 KiB
Python
#
|
|
# Copyright (c) 2020-2023 Wind River Systems, Inc.
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
#
|
|
|
|
from kubernetes import __version__ as K8S_MODULE_VERSION
|
|
from kubernetes import client
|
|
from kubernetes.client import Configuration
|
|
from kubernetes.client.rest import ApiException
|
|
from kubernetes import config
|
|
from oslo_log import log as logging
|
|
from six.moves import http_client as httplib
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
K8S_MODULE_MAJOR_VERSION = int(K8S_MODULE_VERSION.split('.')[0])
|
|
KUBE_CONFIG_PATH = '/etc/kubernetes/admin.conf'
|
|
|
|
CERT_MANAGER_GROUP = 'cert-manager.io'
|
|
CERT_MANAGER_VERSION = 'v1'
|
|
CERT_MANAGER_CERTIFICATE = 'certificates'
|
|
|
|
|
|
class KubeOperator(object):
|
|
|
|
def __init__(self):
|
|
self._kube_client_batch = None
|
|
self._kube_client_core = None
|
|
self._kube_client_custom_objects = None
|
|
|
|
def _load_kube_config(self):
|
|
config.load_kube_config(KUBE_CONFIG_PATH)
|
|
if K8S_MODULE_MAJOR_VERSION < 12:
|
|
c = Configuration()
|
|
else:
|
|
c = Configuration().get_default_copy()
|
|
# Workaround: Turn off SSL/TLS verification
|
|
c.verify_ssl = False
|
|
Configuration.set_default(c)
|
|
|
|
def _get_kubernetesclient_batch(self):
|
|
if not self._kube_client_batch:
|
|
self._load_kube_config()
|
|
self._kube_client_batch = client.BatchV1Api()
|
|
return self._kube_client_batch
|
|
|
|
def _get_kubernetesclient_core(self):
|
|
if not self._kube_client_core:
|
|
self._load_kube_config()
|
|
self._kube_client_core = client.CoreV1Api()
|
|
return self._kube_client_core
|
|
|
|
def _get_kubernetesclient_custom_objects(self):
|
|
if not self._kube_client_custom_objects:
|
|
self._load_kube_config()
|
|
self._kube_client_custom_objects = client.CustomObjectsApi()
|
|
return self._kube_client_custom_objects
|
|
|
|
def kube_get_secret(self, name, namespace):
|
|
c = self._get_kubernetesclient_core()
|
|
try:
|
|
return c.read_namespaced_secret(name, namespace)
|
|
except ApiException as e:
|
|
if e.status == httplib.NOT_FOUND:
|
|
return None
|
|
else:
|
|
LOG.error("Failed to get Secret %s under "
|
|
"Namespace %s: %s" % (name, namespace, e.body))
|
|
raise
|
|
except Exception as e:
|
|
LOG.error("Kubernetes exception in kube_get_secret: %s" % e)
|
|
raise
|
|
|
|
def kube_delete_secret(self, name, namespace, **kwargs):
|
|
body = {}
|
|
|
|
if kwargs:
|
|
body.update(kwargs)
|
|
|
|
c = self._get_kubernetesclient_core()
|
|
try:
|
|
c.delete_namespaced_secret(name, namespace, body=body)
|
|
except ApiException as e:
|
|
if e.status == httplib.NOT_FOUND:
|
|
LOG.warn("Secret %s under Namespace %s "
|
|
"not found." % (name, namespace))
|
|
else:
|
|
LOG.error("Failed to clean up Secret %s under "
|
|
"Namespace %s: %s" % (name, namespace, e.body))
|
|
raise
|
|
except Exception as e:
|
|
LOG.error("Kubernetes exception in kube_delete_secret: %s" % e)
|
|
raise
|
|
|
|
def get_cert_manager_certificate(self, namespace, name):
|
|
custom_object_api = self._get_kubernetesclient_custom_objects()
|
|
|
|
try:
|
|
cert = custom_object_api.get_namespaced_custom_object(
|
|
CERT_MANAGER_GROUP,
|
|
CERT_MANAGER_VERSION,
|
|
namespace,
|
|
CERT_MANAGER_CERTIFICATE,
|
|
name)
|
|
except ApiException as e:
|
|
if e.status == httplib.NOT_FOUND:
|
|
return None
|
|
else:
|
|
LOG.error("Fail to access %s:%s. %s" % (namespace, name, e))
|
|
raise
|
|
else:
|
|
return cert
|
|
|
|
def apply_cert_manager_certificate(self, namespace, name, body):
|
|
custom_object_api = self._get_kubernetesclient_custom_objects()
|
|
|
|
cert = self.get_cert_manager_certificate(namespace, name)
|
|
if cert:
|
|
custom_object_api.patch_namespaced_custom_object(
|
|
CERT_MANAGER_GROUP,
|
|
CERT_MANAGER_VERSION,
|
|
namespace,
|
|
CERT_MANAGER_CERTIFICATE,
|
|
name,
|
|
body
|
|
)
|
|
else:
|
|
custom_object_api.create_namespaced_custom_object(
|
|
CERT_MANAGER_GROUP,
|
|
CERT_MANAGER_VERSION,
|
|
namespace,
|
|
CERT_MANAGER_CERTIFICATE,
|
|
body)
|
|
|
|
def delete_cert_manager_certificate(self, namespace, name):
|
|
custom_object_api = self._get_kubernetesclient_custom_objects()
|
|
|
|
try:
|
|
custom_object_api.delete_namespaced_custom_object(
|
|
CERT_MANAGER_GROUP,
|
|
CERT_MANAGER_VERSION,
|
|
namespace,
|
|
CERT_MANAGER_CERTIFICATE,
|
|
name,
|
|
body={}
|
|
)
|
|
except ApiException as e:
|
|
if e.status != httplib.NOT_FOUND:
|
|
LOG.error("Fail to delete %s:%s. %s" % (namespace, name, e))
|
|
raise
|
|
|
|
def get_pods_by_namespace(self, namespace):
|
|
c = self._get_kubernetesclient_core()
|
|
try:
|
|
pods = c.list_namespaced_pod(namespace)
|
|
return [pod.metadata.name for pod in pods.items]
|
|
except ApiException as e:
|
|
if e.status == httplib.NOT_FOUND:
|
|
return []
|
|
LOG.error("Failed to get pod name under "
|
|
"Namespace %s." % (namespace))
|
|
raise
|
|
except Exception as e:
|
|
LOG.error("Kubernetes exception in get_pods_by_namespace: %s" % e)
|
|
raise
|
|
|
|
def pod_exists(self, pod, namespace):
|
|
pods = self.get_pods_by_namespace(namespace)
|
|
if pod in pods:
|
|
return True
|
|
return False
|
|
|
|
def kube_delete_job(self, name, namespace, **kwargs):
|
|
body = {}
|
|
if kwargs:
|
|
body.update(kwargs)
|
|
|
|
b = self._get_kubernetesclient_batch()
|
|
try:
|
|
b.delete_namespaced_job(name, namespace, body=body)
|
|
except ApiException as e:
|
|
if e.status != httplib.NOT_FOUND:
|
|
LOG.error("Failed to delete job %s under "
|
|
"Namespace %s: %s" % (name, namespace, e.body))
|
|
raise
|
|
except Exception as e:
|
|
LOG.error("Kubernetes exception in kube_delete_job: %s" % e)
|
|
raise
|
|
|
|
def kube_delete_pod(self, name, namespace, **kwargs):
|
|
body = {}
|
|
if kwargs:
|
|
body.update(kwargs)
|
|
|
|
c = self._get_kubernetesclient_core()
|
|
try:
|
|
c.delete_namespaced_pod(name, namespace, body=body)
|
|
except ApiException as e:
|
|
if e.status != httplib.NOT_FOUND:
|
|
LOG.error("Failed to delete pod %s under "
|
|
"Namespace %s: %s" % (name, namespace, e.body))
|
|
raise
|
|
except Exception as e:
|
|
LOG.error("Kubernetes exception in kube_delete_pod: %s" % e)
|
|
raise
|