blob: 3c1be901a93f7988ab6f5816d333765fbe116c09 [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
tmeneau94bf68e2017-10-17 15:55:34 -040015def present(name, chart_name, namespace, version=None, values_file=None,
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040016 tiller_namespace='kube-system', tiller_host=None,
tmeneau8cf4fce2017-10-17 15:05:35 -040017 kube_config=None, gce_service_token=None, helm_home=None):
18 kwargs = {
Yuriy Taraday6f826492017-08-16 12:40:24 +040019 'tiller_namespace': tiller_namespace,
20 'tiller_host': tiller_host,
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040021 'kube_config': kube_config,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +040022 'gce_service_token': gce_service_token,
tmeneau8cf4fce2017-10-17 15:05:35 -040023 'helm_home': helm_home
Yuriy Taraday6f826492017-08-16 12:40:24 +040024 }
tmeneau8cf4fce2017-10-17 15:05:35 -040025 exists = __salt__['helm.release_exists'](name, namespace, **kwargs)
Yuriy Taraday84a21032017-06-27 11:13:16 +040026 if not exists:
Yuriy Taraday6618fb92017-08-11 17:11:48 +040027 err = __salt__['helm.release_create'](
tmeneau94bf68e2017-10-17 15:55:34 -040028 name, chart_name, namespace, version, values_file, **kwargs)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040029 if err:
30 return failure(name, err)
Yuriy Taraday76126042017-08-11 15:27:41 +040031 return {
32 'name': name,
33 'changes': {name: 'CREATED'},
34 'result': True,
35 'comment': 'Release "{}" was created'.format(name),
36 }
37
tmeneau8cf4fce2017-10-17 15:05:35 -040038 old_values = __salt__['helm.get_values'](name, **kwargs)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040039 err = __salt__['helm.release_upgrade'](
tmeneau94bf68e2017-10-17 15:55:34 -040040 name, chart_name, namespace, version, values_file, **kwargs)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040041 if err:
42 return failure(name, err)
Yuriy Taraday76126042017-08-11 15:27:41 +040043
tmeneau8cf4fce2017-10-17 15:05:35 -040044 new_values = __salt__['helm.get_values'](name, **kwargs)
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040045 if new_values == old_values:
46 return {
47 'name': name,
48 'changes': {},
49 'result': True,
50 'comment': 'Release "{}" already exists'.format(name),
51 }
Yuriy Taraday76126042017-08-11 15:27:41 +040052
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040053 old_str = yaml.serialize(old_values, default_flow_style=False)
54 new_str = yaml.serialize(new_values, default_flow_style=False)
55 diff = difflib.unified_diff(
56 old_str.split('\n'), new_str.split('\n'), lineterm='')
Yuriy Taraday84a21032017-06-27 11:13:16 +040057 return {
58 'name': name,
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040059 'changes': {'values': '\n'.join(diff)},
Yuriy Taraday84a21032017-06-27 11:13:16 +040060 'result': True,
Yuriy Taraday3c3f63c2017-06-29 20:46:20 +040061 'comment': 'Release "{}" was updated'.format(name),
Yuriy Taraday84a21032017-06-27 11:13:16 +040062 }
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040063
64
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040065def absent(name, namespace, tiller_namespace='kube-system', tiller_host=None,
tmeneau8cf4fce2017-10-17 15:05:35 -040066 kube_config=None, gce_service_token=None, helm_home=None):
67 kwargs = {
Yuriy Taraday6f826492017-08-16 12:40:24 +040068 'tiller_namespace': tiller_namespace,
69 'tiller_host': tiller_host,
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040070 'kube_config': kube_config,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +040071 'gce_service_token': gce_service_token,
tmeneau8cf4fce2017-10-17 15:05:35 -040072 'helm_home': helm_home
Yuriy Taraday6f826492017-08-16 12:40:24 +040073 }
tmeneau8cf4fce2017-10-17 15:05:35 -040074 exists = __salt__['helm.release_exists'](name, namespace, **kwargs)
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040075 if not exists:
76 return {
77 'name': name,
78 'changes': {},
79 'result': True,
80 'comment': 'Release "{}" doesn\'t exist'.format(name),
81 }
tmeneau8cf4fce2017-10-17 15:05:35 -040082 err = __salt__['helm.release_delete'](name, **kwargs)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040083 if err:
84 return failure(name, err)
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040085 return {
86 'name': name,
87 'changes': {name: 'DELETED'},
88 'result': True,
89 'comment': 'Release "{}" was deleted'.format(name),
90 }