blob: d4a1b6925c59936e6dc00a57241fdab0b2506506 [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,
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040016 tiller_namespace='kube-system', tiller_host=None,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +040017 kube_config=None, gce_service_token=None):
Yuriy Taraday6f826492017-08-16 12:40:24 +040018 tiller_args = {
19 '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,
Yuriy Taraday6f826492017-08-16 12:40:24 +040023 }
24 exists = __salt__['helm.release_exists'](name, namespace, **tiller_args)
Yuriy Taraday84a21032017-06-27 11:13:16 +040025 if not exists:
Yuriy Taraday6618fb92017-08-11 17:11:48 +040026 err = __salt__['helm.release_create'](
Yuriy Taraday6f826492017-08-16 12:40:24 +040027 name, chart_name, namespace, version, values, **tiller_args)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040028 if err:
29 return failure(name, err)
Yuriy Taraday76126042017-08-11 15:27:41 +040030 return {
31 'name': name,
32 'changes': {name: 'CREATED'},
33 'result': True,
34 'comment': 'Release "{}" was created'.format(name),
35 }
36
Yuriy Taraday6f826492017-08-16 12:40:24 +040037 old_values = __salt__['helm.get_values'](name, **tiller_args)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040038 err = __salt__['helm.release_upgrade'](
Yuriy Taraday6f826492017-08-16 12:40:24 +040039 name, chart_name, namespace, version, values, **tiller_args)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040040 if err:
41 return failure(name, err)
Yuriy Taraday76126042017-08-11 15:27:41 +040042
Yuriy Taraday6f826492017-08-16 12:40:24 +040043 new_values = __salt__['helm.get_values'](name, **tiller_args)
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040044 if new_values == old_values:
45 return {
46 'name': name,
47 'changes': {},
48 'result': True,
49 'comment': 'Release "{}" already exists'.format(name),
50 }
Yuriy Taraday76126042017-08-11 15:27:41 +040051
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040052 old_str = yaml.serialize(old_values, default_flow_style=False)
53 new_str = yaml.serialize(new_values, default_flow_style=False)
54 diff = difflib.unified_diff(
55 old_str.split('\n'), new_str.split('\n'), lineterm='')
Yuriy Taraday84a21032017-06-27 11:13:16 +040056 return {
57 'name': name,
Yuriy Taradayaeeaa742017-06-28 15:54:56 +040058 'changes': {'values': '\n'.join(diff)},
Yuriy Taraday84a21032017-06-27 11:13:16 +040059 'result': True,
Yuriy Taraday3c3f63c2017-06-29 20:46:20 +040060 'comment': 'Release "{}" was updated'.format(name),
Yuriy Taraday84a21032017-06-27 11:13:16 +040061 }
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040062
63
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040064def absent(name, namespace, tiller_namespace='kube-system', tiller_host=None,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +040065 kube_config=None, gce_service_token=None):
Yuriy Taraday6f826492017-08-16 12:40:24 +040066 tiller_args = {
67 'tiller_namespace': tiller_namespace,
68 'tiller_host': tiller_host,
Yuriy Taradayf9dd0122017-08-17 16:26:16 +040069 'kube_config': kube_config,
Yuriy Taradaye9f982d2017-08-17 18:06:58 +040070 'gce_service_token': gce_service_token,
Yuriy Taraday6f826492017-08-16 12:40:24 +040071 }
72 exists = __salt__['helm.release_exists'](name, namespace, **tiller_args)
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040073 if not exists:
74 return {
75 'name': name,
76 'changes': {},
77 'result': True,
78 'comment': 'Release "{}" doesn\'t exist'.format(name),
79 }
Yuriy Taraday6f826492017-08-16 12:40:24 +040080 err = __salt__['helm.release_delete'](name, **tiller_args)
Yuriy Taraday6618fb92017-08-11 17:11:48 +040081 if err:
82 return failure(name, err)
Yuriy Taraday893b3fb2017-07-03 16:22:57 +040083 return {
84 'name': name,
85 'changes': {name: 'DELETED'},
86 'result': True,
87 'comment': 'Release "{}" was deleted'.format(name),
88 }