Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use replace() API call for Custom Resources #136

Merged
merged 1 commit into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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