-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
111 lines (92 loc) · 3.12 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
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
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.
from typing import Any
from fastapi import FastAPI, Body
import logging
import base64
from pydantic import BaseModel, TypeAdapter
import os
# Default to 1 year
YEAR = 31_556_952
GRACE_PERIOD: int = int(os.getenv("GRACE_PERIOD_SECONDS", YEAR))
class Patch(BaseModel):
op: str
path: str = "/spec/template/spec/terminationGracePeriodSeconds"
value: int
app = FastAPI()
webhook = logging.getLogger(__name__)
webhook.setLevel(logging.INFO)
logging.basicConfig(format="[%(asctime)s] %(levelname)s: %(message)s")
ADAPTER = TypeAdapter(list[Patch])
def patch_termination(existing_value: bool) -> str:
op = "replace" if existing_value else "add"
webhook.info(f"Updating terminationGracePeriodSeconds, replacing it ({op = })")
patch_operations = [
Patch(
op=op,
value=GRACE_PERIOD,
)
]
return base64.b64encode(ADAPTER.dump_json(patch_operations)).decode()
def admission_review(uid: str, message: str, existing_value: bool) -> dict:
if existing_value:
return {
"apiVersion": "admission.k8s.io/v1",
"kind": "AdmissionReview",
"response": {
"uid": uid,
"allowed": True,
"patchType": "JSONPatch",
"status": {"message": message},
"patch": patch_termination(existing_value),
},
}
return {
"apiVersion": "admission.k8s.io/v1",
"kind": "AdmissionReview",
"response": {
"uid": uid,
"allowed": True,
"status": {"message": "No value provided, continue."},
},
}
def admission_validation(uid: str, current_value: int | None):
if not current_value or current_value > 30:
return {
"apiVersion": "admission.k8s.io/v1",
"kind": "AdmissionReview",
"response": {
"uid": uid,
"allowed": True,
"status": {
"message": f"Valid value has been provided ({current_value})"
},
},
}
return {
"apiVersion": "admission.k8s.io/v1",
"kind": "AdmissionReview",
"response": {
"uid": uid,
"allowed": False,
"status": {
"code": 403,
"message": f"Termination period lower than 30s is not allowed (given {current_value})",
},
},
}
@app.post("/mutate")
def mutate_request(request: dict = Body(...)):
uid = request["request"]["uid"]
selector = request["request"]["object"]["spec"]["template"]["spec"]
return admission_review(
uid,
"Successfully updated terminationGracePeriodSeconds.",
True if "terminationGracePeriodSeconds" in selector else False,
)
@app.post("/validate")
def validate_request(request: dict = Body(...)):
uid = request["request"]["uid"]
selector = request["request"]["object"]["spec"]["template"]["spec"]
period_value = selector.get("terminationGracePeriodSeconds")
return admission_validation(uid, period_value)