Skip to content

Commit

Permalink
Merge pull request #136 from 2gis/cr-replace
Browse files Browse the repository at this point in the history
Use replace() API call for Custom Resources
  • Loading branch information
seleznev authored Mar 1, 2021
2 parents c5df284 + f314d3f commit 2df08f4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 6 additions & 3 deletions k8s_handle/k8s/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 7 additions & 4 deletions k8s_handle/k8s/provisioner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 2df08f4

Please sign in to comment.