From f314d3f7f86e8ef50dbf35d198d6eed78d58715c Mon Sep 17 00:00:00 2001 From: Aleksandr Seleznev Date: Wed, 24 Feb 2021 15:19:01 +0300 Subject: [PATCH] Use `replace()` API call for Custom Resources --- k8s_handle/k8s/adapters.py | 9 ++++++--- k8s_handle/k8s/provisioner.py | 11 +++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/k8s_handle/k8s/adapters.py b/k8s_handle/k8s/adapters.py index 402fb6c..f09442a 100644 --- a/k8s_handle/k8s/adapters.py +++ b/k8s_handle/k8s/adapters.py @@ -294,16 +294,19 @@ def delete(self): '{}'.format(add_indent(e.body))) raise ProvisioningError(e) - def replace(self, _): + def replace(self, parameters): self._validate() + if 'resourceVersion' in parameters: + self.body['metadata']['resourceVersion'] = parameters['resourceVersion'] + try: if self.namespace: - return self.api.patch_namespaced_custom_object( + return self.api.replace_namespaced_custom_object( self.group, self.version, self.namespace, self.plural, self.name, self.body ) - return self.api.patch_cluster_custom_object( + return self.api.replace_cluster_custom_object( self.group, self.version, self.plural, self.name, self.body ) except ApiException as e: diff --git a/k8s_handle/k8s/provisioner.py b/k8s_handle/k8s/provisioner.py index bbc998f..c361c72 100644 --- a/k8s_handle/k8s/provisioner.py +++ b/k8s_handle/k8s/provisioner.py @@ -94,6 +94,13 @@ def _deploy(self, template_body, file_path): log.info('{} "{}" already exists, replace it'.format(template_body['kind'], kube_client.name)) parameters = {} + if hasattr(resource, 'metadata'): + if hasattr(resource.metadata, 'resource_version'): + parameters['resourceVersion'] = resource.metadata.resource_version + elif 'metadata' in resource: + if 'resourceVersion' in resource['metadata']: + parameters['resourceVersion'] = resource['metadata']['resourceVersion'] + if template_body['kind'] == 'Service': if hasattr(resource.spec, 'cluster_ip'): parameters['clusterIP'] = resource.spec.cluster_ip @@ -108,10 +115,6 @@ def _deploy(self, template_body, file_path): log.warning('PersistentVolume has "{}" status, skip replacing'.format(resource.status.phase)) return - if template_body['kind'] in ['Service', 'CustomResourceDefinition', 'PodDisruptionBudget']: - if hasattr(resource.metadata, 'resource_version'): - parameters['resourceVersion'] = resource.metadata.resource_version - kube_client.replace(parameters) if self.sync_mode: