Improvement of k8s Deployments update

* Deployment will be updated only if service-related configuration
  was changed
* SilentUndefined handler to ignore undefined variables of jinja
  templates was added

Change-Id: Iad384ee447268cba44cbfacba81118ec44ca31c3
This commit is contained in:
Andrey Pavlov
2016-09-16 13:08:55 +03:00
parent 6a864bb4ba
commit ae770e795f
5 changed files with 105 additions and 23 deletions

View File

@@ -3,13 +3,31 @@ import os
import jinja2
class SilentUndefined(jinja2.Undefined):
def _fail_with_undefined_error(self, *args, **kwargs):
return ''
__add__ = __radd__ = __mul__ = __rmul__ = __div__ = __rdiv__ = \
__truediv__ = __rtruediv__ = __floordiv__ = __rfloordiv__ = \
__mod__ = __rmod__ = __pos__ = __neg__ = __call__ = \
__getitem__ = __lt__ = __le__ = __gt__ = __ge__ = __int__ = \
__float__ = __complex__ = __pow__ = __rpow__ = \
_fail_with_undefined_error
def str_to_bool(text):
return text is not None and text.lower() in ['true', 'yes']
def jinja_render(path, context, functions=()):
def jinja_render(path, context, functions=(), ignore_undefined=False):
kwargs = {}
if ignore_undefined:
kwargs['undefined'] = SilentUndefined
else:
kwargs['undefined'] = jinja2.StrictUndefined
env = jinja2.Environment(loader=jinja2.FileSystemLoader(
os.path.dirname(path)), undefined=jinja2.StrictUndefined)
os.path.dirname(path)), **kwargs)
env.filters['bool'] = str_to_bool
for func in functions:
env.globals[func.__name__] = func