Add server_side apply option

Change-Id: I8df0f24d832535c18b77c666dd1fd43968a66a09
This commit is contained in:
Mohammed Naser 2020-07-29 12:42:57 -04:00
parent 59d620115b
commit 961ece2998

View File

@ -18,6 +18,7 @@ The module contains a few useful utilities which we refactor out in order
to be able to use them across all different operators. to be able to use them across all different operators.
""" """
import base64 import base64
import copy
import operator import operator
import json import json
import os import os
@ -69,7 +70,7 @@ ENV.filters['to_yaml'] = to_yaml
ENV.globals['labels'] = labels ENV.globals['labels'] = labels
def create_or_update(template, **kwargs): def create_or_update(template, server_side=True, **kwargs):
"""Create or update a Kubernetes resource. """Create or update a Kubernetes resource.
This function is called with a template and the args to pass to that This function is called with a template and the args to pass to that
@ -80,6 +81,7 @@ def create_or_update(template, **kwargs):
resource = generate_object(template, **kwargs) resource = generate_object(template, **kwargs)
if server_side:
# NOTE(mnaser): The following relies on server-side apply and requires # NOTE(mnaser): The following relies on server-side apply and requires
# at least Kuberentes v1.16+ # at least Kuberentes v1.16+
resp = resource.api.patch( resp = resource.api.patch(
@ -96,6 +98,16 @@ def create_or_update(template, **kwargs):
) )
resource.api.raise_for_status(resp) resource.api.raise_for_status(resp)
resource.set_obj(resp.json()) resource.set_obj(resp.json())
else:
obj = copy.deepcopy(resource.obj)
try:
resource.reload()
resource.obj = obj
resource.update()
except pykube.exceptions.HTTPError as exc:
if exc.code != 404:
raise
resource.create()
return resource return resource