-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
58 lines (50 loc) · 1.5 KB
/
app.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
import base64
import copy
import http
import json
import random
import logging
import jsonpatch
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route("/mutate", methods=["POST"])
def mutate():
spec = request.json["request"]["object"]
modified_spec = copy.deepcopy(spec)
try:
modified_spec["metadata"]["labels"]["example.com/new-label"] = str(
random.randint(1, 1000)
)
except KeyError:
pass
patch = jsonpatch.JsonPatch.from_diff(spec, modified_spec)
print(jsonify(
{
"response": {
"allowed": True,
"uid": request.json["request"]["uid"],
"patch": base64.b64encode(str(patch).encode()).decode(),
"patchtype": "JSONPatch",
}
}
))
return jsonify(
{
"response": {
"allowed": True,
"uid": request.json["request"]["uid"],
"patch": base64.b64encode(str(patch).encode()).decode(),
"patchtype": "JSONPatch",
}
}
)
@app.route("/health", methods=["GET"])
def health():
return ("", http.HTTPStatus.NO_CONTENT)
if __name__ != '__main__':
# if we are not running directly, we set the loggers
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=False) # pragma: no cover