-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathasb_encode_binding.py
executable file
·106 lines (90 loc) · 2.93 KB
/
asb_encode_binding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python
ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: asb_encode_binding
short_description: Encodes binding fields for Ansible Service Broker
description:
- Takes a dictionary of fields and makes them available to Ansible Service Broker
to read and create a binding when running the action (provision, bind, etc)
notes: []
requirements: []
author:
- "Red Hat, Inc."
options:
fields:
description:
- 'dictionary of key/value pairs to encode for a binding. Keys will become the injected environment variables.'
required: true
default: {}
env:
- Set via the downward API on the APB Pod
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
'''
EXAMPLES = '''
- name: encode bind credentials
asb_encode_binding:
fields:
POSTGRESQL_HOST: postgresql
POSTGRESQL_PORT: 5432
POSTGRESQL_USER: "{{ postgresql_user }}"
POSTGRESQL_PASSWORD: "{{ postgresql_password }}"
POSTGRESQL_DATABASE: "{{ postgresql_database }}"
'''
RETURN = '''
encoded_fields:
description: string containing encoded fields
returned: success
type: string
sample: eyJURVNUX1ZBUl8xIjogInRlc3QgdmFsdWUgMSIsICJUZXN0VmFsdWUyIjogMn0=
'''
import json
import base64
import os
from ansible.module_utils.basic import AnsibleModule
try:
from kubernetes import client, config
config.load_kube_config()
api = client.CoreV1Api()
except Exception as error:
ansible_module.fail_json(msg="Error attempting to load kubernetes client: {}".format(error))
ENCODED_BINDING_PATH = "/var/tmp/bind-creds"
ENV_NAME = "POD_NAME"
ENV_NAMESPACE = "POD_NAMESPACE"
def main():
argument_spec = dict(
fields=dict(required=True, type='dict')
)
ansible_module = AnsibleModule(argument_spec=argument_spec)
try:
fields_json = json.dumps(ansible_module.params['fields'])
encoded_fields = base64.b64encode(fields_json)
except Exception as error:
ansible_module.fail_json(msg="Error attempting to encode binding: {}".format(error))
try:
name = os.environ[ENV_NAME]
namespace = os.environ[ENV_NAMESPACE]
except Exception as error:
ansible_module.fail_json(msg="Error attempting to get name/namespace from environment: {}".format(error))
try:
api.create_namespaced_secret(
namespace=namespace,
body=client.V1Secret(
metadata=client.V1ObjectMeta(name=name),
data={"fields": encoded_fields}
)
)
except Exception as error:
ansible_module.fail_json(msg="Error attempting to create binding secret: {}".format(error))
ansible_module.exit_json(changed=True, encoded_fields=encoded_fields)
if __name__ == '__main__':
main()