blob: e60c6dcc68d02a318ca7bf5d90c872d4bdf7c600 [file] [log] [blame]
Yuriy Taradayaeeaa742017-06-28 15:54:56 +04001import difflib
Yuriy Taraday84a21032017-06-27 11:13:16 +04002
Yuriy Taradayaeeaa742017-06-28 15:54:56 +04003from salt.serializers import yaml
4
Yuriy Taraday76126042017-08-11 15:27:41 +04005
6def failure(name, message):
7 return {
8 'name': name,
9 'changes': {},
10 'result': False,
11 'comment': message,
12 }
13
14
Yuriy Taraday6f826492017-08-16 12:40:24 +040015def present(name, chart_name, namespace, version=None, values=None,
16 tiller_namespace='kube-system', tiller_host=None):
17 tiller_args = {
18 'tiller_namespace': tiller_namespace,
19 'tiller_host': tiller_host,
20 }
21 exists = __salt__['helm.release_exists'](name, namespace, **tiller_args)
Yuriy Taraday84a21032017-06-27 11:13:16 +040022 if not exists:
Yuriy Taraday6618fb92017-08-11 17:11:48 +040023 err = __salt__['helm.release_create'](
Yuriy Taraday6f826492017-08-16 12:40:24 +040024 name, chart_name, namespace, version, values, **tiller_args)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040025 if err:
26 return failure(name, err)
Yuriy Taraday76126042017-08-11 15:27:41 +040027 return {
28 'name': name,
29 'changes': {name: 'CREATED'},
30 'result': True,
31 'comment': 'Release "{}" was created'.format(name),
32 }
33
Yuriy Taraday6f826492017-08-16 12:40:24 +040034 old_values = __salt__['helm.get_values'](name, **tiller_args)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040035 err = __salt__['helm.release_upgrade'](
Yuriy Taraday6f826492017-08-16 12:40:24 +040036 name, chart_name, namespace, version, values, **tiller_args)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040037 if err:
38 return failure(name, err)
Yuriy Taraday76126042017-08-11 15:27:41 +040039
Yuriy Taraday6f826492017-08-16 12:40:24 +040040 new_values = __salt__['helm.get_values'](name, **tiller_args)
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040041 if new_values == old_values:
42 return {
43 'name': name,
44 'changes': {},
45 'result': True,
46 'comment': 'Release "{}" already exists'.format(name),
47 }
Yuriy Taraday76126042017-08-11 15:27:41 +040048
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040049 old_str = yaml.serialize(old_values, default_flow_style=False)
50 new_str = yaml.serialize(new_values, default_flow_style=False)
51 diff = difflib.unified_diff(
52 old_str.split('\n'), new_str.split('\n'), lineterm='')
Yuriy Taraday84a21032017-06-27 11:13:16 +040053 return {
54 'name': name,
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040055 'changes': {'values': '\n'.join(diff)},
Yuriy Taraday84a21032017-06-27 11:13:16 +040056 'result': True,
Yuriy Taraday3c3f63c2017-06-29 20:46:20 +040057 'comment': 'Release "{}" was updated'.format(name),
Yuriy Taraday84a21032017-06-27 11:13:16 +040058 }
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040059
60
Yuriy Taraday6f826492017-08-16 12:40:24 +040061def absent(name, namespace, tiller_namespace='kube-system', tiller_host=None):
62 tiller_args = {
63 'tiller_namespace': tiller_namespace,
64 'tiller_host': tiller_host,
65 }
66 exists = __salt__['helm.release_exists'](name, namespace, **tiller_args)
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040067 if not exists:
68 return {
69 'name': name,
70 'changes': {},
71 'result': True,
72 'comment': 'Release "{}" doesn\'t exist'.format(name),
73 }
Yuriy Taraday6f826492017-08-16 12:40:24 +040074 err = __salt__['helm.release_delete'](name, **tiller_args)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040075 if err:
76 return failure(name, err)
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040077 return {
78 'name': name,
79 'changes': {name: 'DELETED'},
80 'result': True,
81 'comment': 'Release "{}" was deleted'.format(name),
82 }