This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
podsetting_operator.py
160 lines (138 loc) · 5.39 KB
/
podsetting_operator.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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os
from typing import Any, Dict
import kopf
from orbit_controller import ORBIT_API_GROUP, ORBIT_API_VERSION, dynamic_client
from orbit_controller.utils import poddefault_utils
@kopf.on.startup()
def configure(settings: kopf.OperatorSettings, logger: kopf.Logger, **_: Any) -> None:
settings.persistence.progress_storage = kopf.MultiProgressStorage(
[
kopf.AnnotationsProgressStorage(prefix="orbit.aws"),
kopf.StatusProgressStorage(field="status.orbit-aws"),
]
)
settings.persistence.finalizer = "podsetting-operator.orbit.aws/kopf-finalizer"
settings.posting.level = logging.getLevelName(os.environ.get("EVENT_LOG_LEVEL", "INFO"))
def _should_index_namespaces(labels: kopf.Labels, **_: Any) -> bool:
return labels.get("orbit/space", None) == "user" and "orbit/team" in labels
@kopf.index("namespaces", when=_should_index_namespaces) # type: ignore
def namespaces_idx(
name: str, annotations: kopf.Annotations, labels: kopf.Labels, **_: Any
) -> Dict[str, Dict[str, Any]]:
"""Index of user namespaces by team"""
return {
labels["orbit/team"]: {
"name": name,
"annotations": annotations,
"labels": labels,
}
}
def _should_process_podsetting(labels: kopf.Labels, status: kopf.Status, **_: Any) -> bool:
return (
labels.get("orbit/space") == "team"
and "orbit/disable-watcher" not in labels
and "podDefaultsCreation" not in status
)
@kopf.on.resume(ORBIT_API_GROUP, ORBIT_API_VERSION, "podsettings", when=_should_process_podsetting) # type: ignore
@kopf.on.create(ORBIT_API_GROUP, ORBIT_API_VERSION, "podsettings", when=_should_process_podsetting)
def create_poddefaults(
namespace: str,
name: str,
labels: kopf.Labels,
spec: kopf.Spec,
status: kopf.Status,
patch: kopf.Patch,
logger: kopf.Logger,
namespaces_idx: kopf.Index[str, Dict[str, Any]],
**_: Any,
) -> str:
team = labels.get("orbit/team", None)
if team is None:
logger.error("Missing required orbit/team label")
patch["status"] = {"podDefaultsCreation": "MissingTeam"}
return "MissingTeam"
# Contruct a pseudo poddefault for the team to be copied to users
poddefault = poddefault_utils.construct(
name=name,
desc=spec.get("desc", ""),
labels={"orbit/space": "team", "orbit/team": team},
)
user_namespaces = [ns.get("name") for ns in namespaces_idx.get(team, [])]
poddefault_utils.copy_poddefaults_to_user_namespaces(
poddefaults=[poddefault],
user_namespaces=user_namespaces,
client=dynamic_client(),
logger=logger,
)
patch["status"] = {"podDefaultsCreation": "Complete"}
return "PodDefaultsCreated"
@kopf.on.update(ORBIT_API_GROUP, ORBIT_API_VERSION, "podsettings", when=_should_process_podsetting) # type: ignore
def update_poddefaults(
namespace: str,
name: str,
labels: kopf.Labels,
spec: kopf.Spec,
logger: kopf.Logger,
namespaces_idx: kopf.Index[str, Dict[str, Any]],
**_: Any,
) -> str:
team = labels.get("orbit/team", None)
if team is None:
logger.error("Missing required orbit/team label")
return "MissingTeam"
# Contruct a pseudo poddefault for the team to be copied to users
poddefault = poddefault_utils.construct(
name=name,
desc=spec.get("desc", ""),
labels={"orbit/space": "team", "orbit/team": team},
)
user_namespaces = [namespace["name"] for namespace in namespaces_idx.get(team, [])]
poddefault_utils.modify_poddefaults_in_user_namespaces(
poddefaults=[poddefault],
user_namespaces=user_namespaces,
client=dynamic_client(),
logger=logger,
)
return "PodDefaultsUpdated"
@kopf.on.delete(ORBIT_API_GROUP, ORBIT_API_VERSION, "podsettings", when=_should_process_podsetting) # type: ignore
def delete_poddefaults(
namespace: str,
name: str,
labels: kopf.Labels,
spec: kopf.Spec,
logger: kopf.Logger,
namespaces_idx: kopf.Index[str, Dict[str, Any]],
**_: Any,
) -> str:
team = labels.get("orbit/team", None)
if team is None:
logger.error("Missing required orbit/team label")
return "MissingTeam"
# Contruct a pseudo poddefault for the team to be deleted from users
poddefault = poddefault_utils.construct(
name=name,
desc=spec.get("desc", ""),
labels={"orbit/space": "team", "orbit/team": team},
)
user_namespaces = [namespace["name"] for namespace in namespaces_idx.get(team, [])]
poddefault_utils.delete_poddefaults_from_user_namespaces(
poddefaults=[poddefault],
user_namespaces=user_namespaces,
client=dynamic_client(),
logger=logger,
)
return "PodDefaultsDeleted"