forked from Azure/azure-cli-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagednamespace.py
More file actions
370 lines (287 loc) · 12.3 KB
/
Copy pathmanagednamespace.py
File metadata and controls
370 lines (287 loc) · 12.3 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# pylint: disable=too-many-lines
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
from azure.cli.core.azclierror import (
InvalidArgumentValueError,
RequiredArgumentMissingError,
)
from azure.cli.core.util import (
sdk_no_wait,
)
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from azure.mgmt.containerservice import ContainerServiceClient
from azext_aks_preview._consts import (
CONST_NAMESPACE_NETWORK_POLICY_RULE_DENYALL,
CONST_NAMESPACE_NETWORK_POLICY_RULE_ALLOWALL,
CONST_NAMESPACE_NETWORK_POLICY_RULE_ALLOWSAMENAMESPACE,
CONST_NAMESPACE_ADOPTION_POLICY_NEVER,
CONST_NAMESPACE_ADOPTION_POLICY_IFIDENTICAL,
CONST_NAMESPACE_ADOPTION_POLICY_ALWAYS,
CONST_NAMESPACE_DELETE_POLICY_KEEP,
CONST_NAMESPACE_DELETE_POLICY_DELETE
)
from azext_aks_preview._client_factory import CUSTOM_MGMT_AKS_PREVIEW
def get_cluster_location(cmd, resource_group_name, cluster_name):
containerservice_client = get_mgmt_service_client(cmd.cli_ctx, ContainerServiceClient)
cluster = containerservice_client.managed_clusters.get(resource_group_name, cluster_name)
return cluster.location
def aks_managed_namespace_add(cmd, client, raw_parameters, headers, no_wait):
resource_group_name = raw_parameters.get("resource_group_name")
cluster_name = raw_parameters.get("cluster_name")
namespace_name = raw_parameters.get("name")
namespace_config = constructNamespace(cmd, raw_parameters, namespace_name)
namespace_config.location = get_cluster_location(cmd, resource_group_name, cluster_name)
return sdk_no_wait(
no_wait,
client.begin_create_or_update,
resource_group_name,
cluster_name,
namespace_name,
namespace_config,
headers=headers,
)
def constructNamespace(cmd, raw_parameters, namespace_name):
tags = raw_parameters.get("tags", {})
labels_raw = raw_parameters.get("labels")
labels = parse_key_value_list(labels_raw)
annotations_raw = raw_parameters.get("annotations")
annotations = parse_key_value_list(annotations_raw)
NamespaceProperties = cmd.get_models(
"NamespaceProperties",
resource_type=CUSTOM_MGMT_AKS_PREVIEW,
operation_group="managed_namespaces"
)
namespace_properties = NamespaceProperties(
labels=labels,
annotations=annotations,
default_resource_quota=setResourceQuota(cmd, raw_parameters),
default_network_policy=setNetworkPolicyRule(cmd, raw_parameters),
adoption_policy=setAdoptionPolicy(raw_parameters),
delete_policy=setDeletePolicy(raw_parameters)
)
Namespace = cmd.get_models(
"ManagedNamespace",
resource_type=CUSTOM_MGMT_AKS_PREVIEW,
operation_group="managed_namespaces"
)
namespace_config = Namespace()
namespace_config.name = namespace_name
namespace_config.tags = tags
namespace_config.properties = namespace_properties
return namespace_config
def setResourceQuota(cmd, raw_parameters):
cpu_request = raw_parameters.get("cpu_request")
cpu_limit = raw_parameters.get("cpu_limit")
memory_request = raw_parameters.get("memory_request")
memory_limit = raw_parameters.get("memory_limit")
if any(param is None for param in [cpu_request, cpu_limit, memory_request, memory_limit]):
raise RequiredArgumentMissingError(
"Please specify --cpu-request, --cpu-limit, --memory-request, and --memory-limit."
)
ResourceQuota = cmd.get_models(
"ResourceQuota",
resource_type=CUSTOM_MGMT_AKS_PREVIEW,
operation_group="managed_namespaces"
)
rq = ResourceQuota(
cpu_request=cpu_request,
cpu_limit=cpu_limit,
memory_request=memory_request,
memory_limit=memory_limit
)
return rq
def setNetworkPolicyRule(cmd, raw_parameters):
ingress_policy = raw_parameters.get("ingress_policy") or CONST_NAMESPACE_NETWORK_POLICY_RULE_ALLOWSAMENAMESPACE
egress_policy = raw_parameters.get("egress_policy") or CONST_NAMESPACE_NETWORK_POLICY_RULE_ALLOWALL
valid_network_policy_rules = {
CONST_NAMESPACE_NETWORK_POLICY_RULE_DENYALL,
CONST_NAMESPACE_NETWORK_POLICY_RULE_ALLOWSAMENAMESPACE,
CONST_NAMESPACE_NETWORK_POLICY_RULE_ALLOWALL
}
if ingress_policy not in valid_network_policy_rules:
raise InvalidArgumentValueError(
f"Invalid ingress_policy '{ingress_policy}'. Must be one of: "
f"{', '.join(valid_network_policy_rules)}"
)
if egress_policy not in valid_network_policy_rules:
raise InvalidArgumentValueError(
f"Invalid egress_policy '{egress_policy}'. Must be one of: "
f"{', '.join(valid_network_policy_rules)}"
)
NetworkPolicies = cmd.get_models(
"NetworkPolicies",
resource_type=CUSTOM_MGMT_AKS_PREVIEW,
operation_group="managed_namespaces"
)
np = NetworkPolicies(
ingress=ingress_policy,
egress=egress_policy
)
return np
def setAdoptionPolicy(raw_parameters):
adoption_policy = raw_parameters.get("adoption_policy") or CONST_NAMESPACE_ADOPTION_POLICY_NEVER
valid_adoption_policy = {
CONST_NAMESPACE_ADOPTION_POLICY_NEVER,
CONST_NAMESPACE_ADOPTION_POLICY_IFIDENTICAL,
CONST_NAMESPACE_ADOPTION_POLICY_ALWAYS
}
if adoption_policy not in valid_adoption_policy:
raise InvalidArgumentValueError(
f"Invalid adoption policy '{adoption_policy}'. Must be one of: "
f"{', '.join(valid_adoption_policy)}"
)
return adoption_policy
def setDeletePolicy(raw_parameters):
delete_policy = raw_parameters.get("delete_policy") or CONST_NAMESPACE_DELETE_POLICY_KEEP
valid_delete_policy = {
CONST_NAMESPACE_DELETE_POLICY_KEEP,
CONST_NAMESPACE_DELETE_POLICY_DELETE
}
if delete_policy not in valid_delete_policy:
raise InvalidArgumentValueError(
f"Invalid delete policy '{delete_policy}'. Must be one of: "
f"{', '.join(valid_delete_policy)}"
)
return delete_policy
def parse_key_value_list(pairs):
result = {}
if pairs is None:
return result
for pair in pairs:
if "=" not in pair:
raise ValueError(f"Invalid format '{pair}'. Expected format key=value.")
key, value = pair.split("=", 1)
result[key.strip()] = value.strip()
return result
def aks_managed_namespace_update(cmd, client, raw_parameters, headers, existedNamespace, no_wait):
resource_group_name = raw_parameters.get("resource_group_name")
cluster_name = raw_parameters.get("cluster_name")
namespace_name = raw_parameters.get("name")
namespace_config = updateNamespace(cmd, raw_parameters, existedNamespace)
namespace_config.location = get_cluster_location(cmd, resource_group_name, cluster_name)
return sdk_no_wait(
no_wait,
client.begin_create_or_update,
resource_group_name,
cluster_name,
namespace_name,
namespace_config,
headers=headers,
)
def updateNamespace(cmd, raw_parameters, existedNamespace):
tags = raw_parameters.get("tags", {})
labels_raw = raw_parameters.get("labels")
labels = parse_key_value_list(labels_raw)
annotations_raw = raw_parameters.get("annotations")
annotations = parse_key_value_list(annotations_raw)
NamespaceProperties = cmd.get_models(
"NamespaceProperties",
resource_type=CUSTOM_MGMT_AKS_PREVIEW,
operation_group="managed_namespaces"
)
namespace_properties = NamespaceProperties(
labels=labels,
annotations=annotations,
default_resource_quota=updateResourceQuota(cmd, raw_parameters, existedNamespace),
default_network_policy=updateNetworkPolicyRule(cmd, raw_parameters, existedNamespace),
adoption_policy=updateAdoptionPolicy(raw_parameters, existedNamespace),
delete_policy=updateDeletePolicy(raw_parameters, existedNamespace)
)
Namespace = cmd.get_models(
"ManagedNamespace",
resource_type=CUSTOM_MGMT_AKS_PREVIEW,
operation_group="managed_namespaces"
)
namespace_config = Namespace()
namespace_config.name = existedNamespace.name
namespace_config.tags = tags
namespace_config.properties = namespace_properties
return namespace_config
def updateResourceQuota(cmd, raw_parameters, existedNamespace):
cpu_request = raw_parameters.get("cpu_request")
cpu_limit = raw_parameters.get("cpu_limit")
memory_request = raw_parameters.get("memory_request")
memory_limit = raw_parameters.get("memory_limit")
if cpu_request is None:
cpu_request = existedNamespace.properties.default_resource_quota.cpu_request
if cpu_limit is None:
cpu_limit = existedNamespace.properties.default_resource_quota.cpu_limit
if memory_request is None:
memory_request = existedNamespace.properties.default_resource_quota.memory_request
if memory_limit is None:
memory_limit = existedNamespace.properties.default_resource_quota.memory_limit
ResourceQuota = cmd.get_models(
"ResourceQuota",
resource_type=CUSTOM_MGMT_AKS_PREVIEW,
operation_group="managed_namespaces"
)
rq = ResourceQuota(
cpu_request=cpu_request,
cpu_limit=cpu_limit,
memory_request=memory_request,
memory_limit=memory_limit
)
return rq
def updateNetworkPolicyRule(cmd, raw_parameters, existedNamespace):
ingress_policy = raw_parameters.get("ingress_policy")
egress_policy = raw_parameters.get("egress_policy")
valid_network_policy_rules = {
CONST_NAMESPACE_NETWORK_POLICY_RULE_DENYALL,
CONST_NAMESPACE_NETWORK_POLICY_RULE_ALLOWSAMENAMESPACE,
CONST_NAMESPACE_NETWORK_POLICY_RULE_ALLOWALL
}
if ingress_policy is not None and ingress_policy not in valid_network_policy_rules:
raise InvalidArgumentValueError(
f"Invalid ingress_policy '{ingress_policy}'. Must be one of: "
f"{', '.join(valid_network_policy_rules)}"
)
if egress_policy is not None and egress_policy not in valid_network_policy_rules:
raise InvalidArgumentValueError(
f"Invalid egress_policy '{egress_policy}'. Must be one of: "
f"{', '.join(valid_network_policy_rules)}"
)
if ingress_policy is None:
ingress_policy = existedNamespace.properties.default_network_policy.ingress
if egress_policy is None:
egress_policy = existedNamespace.properties.default_network_policy.egress
NetworkPolicies = cmd.get_models(
"NetworkPolicies",
resource_type=CUSTOM_MGMT_AKS_PREVIEW,
operation_group="managed_namespaces"
)
np = NetworkPolicies(
ingress=ingress_policy,
egress=egress_policy
)
return np
def updateAdoptionPolicy(raw_parameters, existedNamespace):
adoption_policy = raw_parameters.get("adoption_policy")
valid_adoption_policy = {
CONST_NAMESPACE_ADOPTION_POLICY_NEVER,
CONST_NAMESPACE_ADOPTION_POLICY_IFIDENTICAL,
CONST_NAMESPACE_ADOPTION_POLICY_ALWAYS
}
if adoption_policy is not None and adoption_policy not in valid_adoption_policy:
raise InvalidArgumentValueError(
f"Invalid adoption policy '{adoption_policy}'. Must be one of: "
f"{', '.join(valid_adoption_policy)}"
)
if adoption_policy is None:
adoption_policy = existedNamespace.properties.adoption_policy
return adoption_policy
def updateDeletePolicy(raw_parameters, existedNamespace):
delete_policy = raw_parameters.get("delete_policy")
valid_delete_policy = {
CONST_NAMESPACE_DELETE_POLICY_KEEP,
CONST_NAMESPACE_DELETE_POLICY_DELETE
}
if delete_policy is not None and delete_policy not in valid_delete_policy:
raise InvalidArgumentValueError(
f"Invalid delete policy '{delete_policy}'. Must be one of: "
f"{', '.join(valid_delete_policy)}"
)
if delete_policy is None:
delete_policy = existedNamespace.properties.delete_policy
return delete_policy