Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Commit

Permalink
Merge 5c3c897 into 5d9c3b0
Browse files Browse the repository at this point in the history
  • Loading branch information
wnykuang committed Oct 9, 2019
2 parents 5d9c3b0 + 5c3c897 commit 09147e3
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 161 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,4 @@ pykube is now hosted under the Kel Project.
### Bug fixes

* added `DaemonSet` to public API

1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ responses = "*"
sphinx = "*"
sphinx-rtd-theme = "*"
sphinx-autodoc-annotation = "*"
autopep8 = "*"

[requires]
python_version = "3.7"
338 changes: 202 additions & 136 deletions Pipfile.lock

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions pykube/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@


class ObjectManager:
#function all
#x = ObjectManager()
# x(api, namespace)
def __call__(self, api, namespace=None):
if namespace is None and NamespacedAPIObject in getmro(self.api_obj_class):
namespace = api.config.namespace
Expand Down Expand Up @@ -41,12 +44,14 @@ def set_obj(self, obj):
self._original_obj = copy.deepcopy(obj)

def __repr__(self):
#call by 'str' and format.
return "<{kind} {name}>".format(kind=self.kind, name=self.name)

def __str__(self):
return self.name

@property
#property: getter and setter. can called as APIObject.name(like in __str__)
def name(self) -> str:
'''
Name of the Kubernetes resource (metadata.name)
Expand Down Expand Up @@ -82,14 +87,18 @@ def annotations(self) -> dict:
return self.obj["metadata"].setdefault("annotations", {})

def api_kwargs(self, **kwargs):
#**kwargs is a dictionary of arguements to be passed into
kw = {}
# Construct url for api request
obj_list = kwargs.pop("obj_list", False)

# Construct URL
if obj_list:
kw["url"] = self.endpoint
else:
operation = kwargs.pop("operation", "")
kw["url"] = op.normpath(op.join(self.endpoint, self.name, operation))

params = kwargs.pop("params", None)
if params is not None:
query_string = urlencode(params)
Expand Down Expand Up @@ -142,12 +151,16 @@ def patch(self, strategic_merge_patch):
self.api.raise_for_status(r)
self.set_obj(r.json())

def update(self):
def update(self, is_strategic=True):
'''
Update the Kubernetes resource by calling the API (patch)
'''
self.obj = obj_merge(self.obj, self._original_obj)
self.patch(self.obj)
if is_strategic:
self.obj = obj_merge(self.obj, self._original_obj)
self.patch(self.obj)
else:
self.obj = obj_merge(self.obj, self._original_obj, is_strategic)
self.patch(self.obj)

def delete(self, propagation_policy: str = None):
'''
Expand Down
47 changes: 25 additions & 22 deletions pykube/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,42 @@
empty = object()


def obj_merge(a, b):
def obj_merge(obj, original_obj, is_strategic=True):
c = {}
for k, v in a.items():
if k not in b:
for k, v in obj.items():
if k not in original_obj:
c[k] = v
else:
c[k] = obj_check(v, b[k])
for k, v in b.items():
if k not in a:
c[k] = v
c[k] = obj_check(v, original_obj[k])

if is_strategic:
for k, v in original_obj.items():
if k not in obj:
c[k] = v
return c


def obj_check(a, b):
c = None
if not isinstance(a, type(b)):
c = a
def obj_check(obj_value, original_obj_value):
check_result = None
if not isinstance(obj_value, type(original_obj_value)):
check_result = obj_value
else:
if isinstance(a, dict):
c = obj_merge(a, b)
elif isinstance(a, list):
z = []
for x, y in zip_longest(a, b, fillvalue=empty):
if isinstance(obj_value, dict):
check_result = obj_merge(obj_value, original_obj_value)

elif isinstance(obj_value, list):
res_list = []
for x, y in zip_longest(obj_value, original_obj_value, fillvalue=empty):
if x is empty:
z.append(y)
res_list.append(y)
elif y is empty:
z.append(x)
res_list.append(x)
else:
z.append(obj_check(x, y))
c = z
res_list.append(obj_check(x, y))
check_result = res_list
else:
c = a
return c
check_result = obj_value
return check_result


def jsonpath_parse(template, obj):
Expand Down
1 change: 1 addition & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ def test_obj_merge():
assert obj_merge({}, {'b': 2}) == {'b': 2}
assert obj_merge({'a': []}, {'a': []}) == {'a': []}
assert obj_merge({'a': [1, 2]}, {'a': [3, 4]}) == {'a': [1, 2]}
assert obj_merge({'a': [1, 2]}, {'a': [3, 4], 'b': ['1']}, is_strategic=False) == {'a': [1, 2]}

0 comments on commit 09147e3

Please sign in to comment.