From 54396a95c2a31a58024bf79fbfb91e7baad402e2 Mon Sep 17 00:00:00 2001 From: Chris Houseknecht Date: Mon, 25 Dec 2017 12:09:31 -0500 Subject: [PATCH] Adds datetime encoder. Fixes Service comparison. (#34223) --- lib/ansible/module_utils/k8s/common.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/ansible/module_utils/k8s/common.py b/lib/ansible/module_utils/k8s/common.py index bb8ee01210..8dee1e3df8 100644 --- a/lib/ansible/module_utils/k8s/common.py +++ b/lib/ansible/module_utils/k8s/common.py @@ -21,6 +21,9 @@ from __future__ import absolute_import, division, print_function import os import re import copy +import json + +from datetime import datetime from ansible.module_utils.six import iteritems from ansible.module_utils.basic import AnsibleModule @@ -53,6 +56,14 @@ except ImportError: HAS_YAML = False +class DateTimeEncoder(json.JSONEncoder): + # When using json.dumps() with K8s object, pass cls=DateTimeEncoder to handle any datetime objects + def default(self, o): + if isinstance(o, datetime): + return o.isoformat() + return json.JSONEncoder.default(self, o) + + class KubernetesAnsibleModuleHelper(AnsibleMixin, KubernetesObjectHelper): pass @@ -196,7 +207,7 @@ class KubernetesAnsibleModule(AnsibleModule): self.helper.object_from_params(self.params, obj=k8s_obj) except KubernetesException as exc: self.fail_json(msg="Failed to patch object: {0}".format(exc.message)) - match, diff = self.helper.objects_match(existing, k8s_obj) + match, diff = self.helper.objects_match(self.helper.fix_serialization(existing), k8s_obj) if match: return_attributes['result'] = existing.to_dict() self.exit_json(**return_attributes)