-
Notifications
You must be signed in to change notification settings - Fork 90
/
k8s_event.py
343 lines (298 loc) · 9.98 KB
/
k8s_event.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2019 Emily Moss
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {
"metadata_version": "1.1",
"status": ["preview"],
"supported_by": "community",
}
DOCUMENTATION = """
module: k8s_events
short_description: Create Kubernetes Events
version_added: "2.8"
author: Emily Moss for Red Hat
description:
- Create Kubernetes Events for Metering
extends_documentation_fragment:
- k8s_auth_options
options:
resource_definition:
description:
- A partial YAML definition of the Event object being created/updated. Here you can define Kubernetes
Event Resource parameters not covered by this module's parameters.
- "NOTE: I(resource_definition) has lower priority than module parameters. If you try to define e.g.
I(metadata.namespace) here, that value will be ignored and I(metadata) used instead."
aliases:
- definition
- inline
type: dict
state:
description:
- Determines if an object should be created, patched, or deleted. When set to C(present), an object will be
created, if it does not already exist. If set to C(absent), an existing object will be deleted. If set to
C(present), an existing object will be patched, if its attributes differ from those specified using
module options and I(resource_definition).
default: present
choices:
- present
- absent
force:
description:
- If set to C(True), and I(state) is C(present), an existing object will be replaced.
default: false
type: bool
merge_type:
description:
- Whether to override the default patch merge approach with a specific type. By default, the strategic
merge will typically be used.
- For example, Custom Resource Definitions typically aren't updatable by the usual strategic merge. You may
want to use C(merge) if you see "strategic merge patch format is not supported"
- See U(https://kubernetes.io/docs/tasks/run-application/update-api-object-kubectl-patch/#use-a-json-merge-patch-to-update-a-deployment)
- Requires openshift >= 0.6.2
- If more than one merge_type is given, the merge_types will be tried in order
- If openshift >= 0.6.2, this defaults to C(['strategic-merge', 'merge']), which is ideal for using the same parameters
on resource kinds that combine Custom Resources and built-in resources. For openshift < 0.6.2, the default
is simply C(strategic-merge).
choices:
- json
- merge
- strategic-merge
type: list
name:
description:
- Use to specify a Event object name.
required: true
type: str
namespace:
description:
- Use to specify a Event object namespace.
required: true
type: str
message:
description:
- Status for Operation
required: true
type: str
reason:
description:
- Reason for the transition into the objects current status
required: true
type: str
reportingComponent:
description:
- Component responsible for event
required: true
type: str
type:
description:
- Type of Event
choices:
- Warning
- Normal
source:
description: EventSource
- Component for reporting this Event
- component
required: true
type: string
involvedObject:
description: ObjectReference
- Object event is reporting on. ApiVersion, kind, name and namespace are of the involvedObject.
- apiVersion
required: true
type: string
- kind
required: true
type: string
- name
required: true
type: string
- namespace
required: true
type: string
requirements:
- python >= 2.7
- openshift >= 0.6.2
"""
EXAMPLES = """
- name: Create Kubernetes Event
k8s_event:
state: present
name: test-https-emily109
namespace: default
message: Event created
reason: Created
reportingComponent: Reporting components
type: Normal
source:
component: Metering components
involvedObject:
apiVersion: v1
kind: Service
name: test-https-emily107
namespace: default
"""
RETURN = """
result:
description:
- The created, patched, or otherwise present Event object. Will be empty in the case of a deletion.
returned: success
type: complex
contains:
api_version:
description: The versioned schema of this representation of an object.
returned: success
type: str
count:
description: Count of Event occurance
returned: success
type: int
firstTimestamp:
description: Timestamp of first occurance of Event
returned: success
type: timestamp
involvedObject:
description: Object event is reporting on. Includes apiVersion, kind, name, namespace, resourceVersion and uid.
returned: success
type: complex
kind:
description: Always 'Event'.
returned: success
type: str
lastTimestamp:
description: Timestamp of last occurance of Event
returned: success
type: timestamp
message:
description: Status for operation
returned: success
type: str
metadata:
description: Standard object metadata. Includes name, namespace, annotations, labels, etc.
returned: success
type: complex
reason:
description: Reason for the transition into the objects current status
returned: success
type: str
reportingComponent:
description: Component responsible for event
returned: success
type: str
source:
description: Source contains information for an Event
returned: success
type: complex
type:
description: Type of Event. Either Normal or Warning
returned: success
type: string
"""
import copy
import traceback
import datetime
import kubernetes.config.dateutil
import openshift
from collections import defaultdict
from ansible.module_utils.k8s.common import AUTH_ARG_SPEC
from ansible.module_utils.k8s.raw import KubernetesRawModule
EVENT_ARG_SPEC = {
"state": {"default": "present", "choices": ["present", "absent"]},
"name": {"required": True},
"namespace": {"required": True},
"merge_type": {"type": "list", "choices": ["json", "merge", "strategic-merge"]},
"message": {"type": "str", "required": True},
"reason": {"type": "str", "required": True},
"reportingComponent": {"type": "str"},
"type": {"choices": ["Normal", "Warning"]},
"source": {
"type": "dict",
"component": {"type": "str", "required": True}
},
"involvedObject": {
"type": "dict",
"apiVersion": {"type": "str", "required": True},
"kind": {"type": "str", "required": True},
"name": {"type": "str", "required": True},
"namespace": {"type": "str", "required": True},
},
}
class KubernetesEvent(KubernetesRawModule):
def __init__(self, *args, **kwargs):
super(KubernetesEvent, self).__init__(*args, k8s_kind="Event", **kwargs)
@property
def argspec(self):
""" argspec property builder """
argumentSpec = copy.deepcopy(AUTH_ARG_SPEC)
argumentSpec.update(EVENT_ARG_SPEC)
return argumentSpec
def execute_module(self):
""" Module execution """
self.client = self.get_api_client()
message = self.params.get("message")
reason = self.params.get("reason")
reporting_component = self.params.get("reportingComponent")
event_type = self.params.get("type")
source = self.params.get("source")
definition = defaultdict(defaultdict)
meta = definition["metadata"]
meta["name"] = self.params.get("name")
meta["namespace"] = self.params.get("namespace")
involved_obj = self.params.get("involvedObject")
resource = self.find_resource("Event", "v1", fail=True)
prior_event = None
try:
prior_event = resource.get(
name=meta["name"],
namespace=meta["namespace"])
except openshift.dynamic.exceptions.NotFoundError:
pass
prior_count = 1
now = datetime.datetime.now()
rfc = kubernetes.config.dateutil.format_rfc3339(now)
first_timestamp = rfc
last_timestamp = rfc
if prior_event and prior_event["reason"] == reason:
prior_count = prior_event["count"] + 1
first_timestamp = prior_event["firstTimestamp"]
last_timestamp = rfc
involved_object_resource = self.find_resource(involved_obj["kind"], "v1", fail=True)
if involved_object_resource:
try:
api_involved_object = involved_object_resource.get(
name=involved_obj["name"], namespace=involved_obj["namespace"])
involved_obj["uid"] = api_involved_object["metadata"]["uid"]
involved_obj["resourceVersion"] = api_involved_object["metadata"]["resourceVersion"]
except openshift.dynamic.exceptions.NotFoundError:
pass
event = {
"kind": "Event",
"count": prior_count,
"eventTime": None,
"firstTimestamp": first_timestamp,
"involvedObject": involved_obj,
"lastTimestamp": last_timestamp,
"message": message,
"metadata": {
"name": meta["name"],
"namespace": meta["namespace"]
},
"reason": reason,
"reportingComponent": reporting_component,
"reportingInstance": "",
"source": source,
"type": event_type,
}
result = self.perform_action(resource, event)
self.exit_json(**result)
def main():
module = KubernetesEvent()
try:
module.execute_module()
except Exception as e:
module.fail_json(msg=str(e), exception=traceback.format_exc())
if __name__ == "__main__":
main()