Skip to content

Commit b2652f0

Browse files
committed
Allow kubernetes section of model to use "named object list" format
1 parent 5f173f9 commit b2652f0

File tree

5 files changed

+100
-3
lines changed

5 files changed

+100
-3
lines changed

core/src/main/python/wlsdeploy/tool/extract/domain_resource_extractor.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
DEFAULT_IMAGE_PULL_SECRETS = PASSWORD_TOKEN
5151
DEFAULT_SOURCE_TYPE = 'Image'
5252

53+
# deprecated - used for "named object list" format
54+
OBJECT_NAME_ATTRIBUTES = {
55+
'spec/adminServer/adminService/channels': 'channelName',
56+
'spec/clusters': 'clusterName'
57+
}
58+
5359
_secret_pattern = re.compile("@@SECRET:([\\w.-]+):[\\w.-]+@@")
5460

5561

@@ -146,6 +152,23 @@ def _process_object_array(self, model_value, item_info, schema_path):
146152
:param schema_path: the path of schema elements, used for supported check
147153
"""
148154
child_list = list()
155+
156+
# deprecated "named object list" format - warning was issued in validator
157+
if isinstance(model_value, dict):
158+
for name in model_value:
159+
object_map = model_value[name]
160+
next_target_dict = PyOrderedDict()
161+
self._process_object(object_map, item_info, next_target_dict, schema_path)
162+
163+
# see if the model name should become an attribute in the target dict
164+
mapped_name = get_mapped_key(schema_path)
165+
properties = wko_schema_helper.get_properties(item_info)
166+
if (mapped_name in properties.keys()) and (mapped_name not in next_target_dict.keys()):
167+
_add_to_top(next_target_dict, mapped_name, name)
168+
child_list.append(next_target_dict)
169+
return child_list
170+
# end deprecated
171+
149172
for object_map in model_value:
150173
next_target_dict = PyOrderedDict()
151174
self._process_object(object_map, item_info, next_target_dict, schema_path)
@@ -309,6 +332,22 @@ def _add_secrets(folder, secrets, domain_uid):
309332
secrets.append(secret_name)
310333

311334

335+
# deprecated
336+
def get_mapped_key(schema_path):
337+
"""
338+
For the deprecated "named object list format", the name of each item in a
339+
multiple folder sometimes corresponds to one of its attributes, usually "name".
340+
If a different attribute name is used for the path, return that name.
341+
If the default 'name' is returned, caller should verify that it is an available attribute.
342+
:param schema_path: the slash-delimited path of the elements (no multi-element names)
343+
:return: the attribute key to be used
344+
"""
345+
mapped_key = dictionary_utils.get_element(OBJECT_NAME_ATTRIBUTES, schema_path)
346+
if mapped_key is not None:
347+
return mapped_key
348+
return 'name'
349+
350+
312351
def _add_to_top(dictionary, key, item):
313352
"""
314353
Add an item to the beginning of an ordered dictionary.

core/src/main/python/wlsdeploy/tool/validate/kubernetes_validator.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,25 @@ def _validate_object_array(self, model_value, property_map, schema_path, model_p
109109
:param model_path: the path of model elements (including array indices), used for logging
110110
"""
111111
_method_name = '_validate_object_array'
112+
113+
# deprecated "named object list" format
114+
if isinstance(model_value, dict):
115+
self._logger.warning("WLSDPLY-05091", model_path, class_name=self._class_name, method_name=_method_name)
116+
for name in model_value:
117+
object_map = model_value[name]
118+
next_model_path = model_path + "/" + name
119+
self.validate_folder(object_map, property_map, schema_path, next_model_path)
120+
return
121+
# end deprecated
122+
112123
if not isinstance(model_value, list):
113124
self._logger.severe("WLSDPLY-05040", model_path, class_name=self._class_name, method_name=_method_name)
114125
return
115126

116127
index = 0
117-
for name_map in model_value:
128+
for object_map in model_value:
118129
index_path = '%s[%s]' % (model_path, index)
119-
self.validate_folder(name_map, property_map, schema_path, index_path)
130+
self.validate_folder(object_map, property_map, schema_path, index_path)
120131
index += 1
121132

122133
def _validate_simple_map(self, model_value, property_name, model_path):

core/src/main/resources/oracle/weblogic/deploy/messages/wlsdeploy_rb.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ WLSDPLY-05040=Expected a list of objects in model location {0}
487487

488488
# wlsdeploy/tool/validate/kubernetes_validator.py
489489
WLSDPLY-05090=Model folder {0} is not supported, will be skipped
490+
WLSDPLY-05091=Model folder {0} uses deprecated "named object list" format, should be a hyphenated object list
490491

491492
# wlsdeploy/tools/validate/validation_utils.py
492493
WLSDPLY-05300={0} variable is referenced in the value of the {1} variable, but is not defined in {2}

core/src/test/python/wlsdeploy/tool/extract/extract_test.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@ def __init__(self, *args):
2222

2323
def setUp(self):
2424
BaseTestCase.setUp(self)
25+
self._suspend_logs('wlsdeploy.extract')
2526
self._establish_directory(self.EXTRACT_OUTPUT_DIR)
2627

28+
def tearDown(self):
29+
BaseTestCase.tearDown(self)
30+
self._restore_logs()
31+
2732
def testDefaultModel(self):
2833
"""
2934
Test that default values and information from the model
@@ -58,12 +63,32 @@ def testKubernetesModel(self):
5863
self._match_values("Cluster 0 clusterName", cluster_list[0]['clusterName'], 'CLUSTER_1')
5964
self._match_values("Cluster 1 clusterName", cluster_list[1]['clusterName'], 'CLUSTER_2')
6065

61-
# secrets from the model should be in the domain resource file
66+
# secrets from the kubernetes section should be in the domain resource file
6267
secret_list = self._traverse(resource, 'spec', 'configuration', 'secrets')
6368
self._match_values("Secret count", len(secret_list), 2)
6469
self._match_values("Secret 0", secret_list[0], 'secret-1')
6570
self._match_values("Secret 1", secret_list[1], 'secret-2')
6671

72+
# deprecated
73+
def testNamedObjectListModel(self):
74+
"""
75+
Test that fields using the deprecated "named object list" in the kubernetes section of the model
76+
are transferred to the resulting domain resource file
77+
"""
78+
resource = self._extract_domain_resource('3')
79+
80+
# serverPod/env from the kubernetes section should be in the domain resource file
81+
env_list = self._traverse(resource, 'spec', 'serverPod', 'env')
82+
self._match_values("Env count", len(env_list), 2)
83+
self._match_values("Env 0", env_list[0]['name'], 'JAVA_OPTIONS')
84+
self._match_values("Env 1", env_list[1]['name'], 'USER_MEM_ARGS')
85+
86+
# clusters from kubernetes section should be in the domain resource file
87+
cluster_list = self._traverse(resource, 'spec', 'clusters')
88+
self._match_values("Cluster count", len(cluster_list), 2)
89+
self._match_values("Cluster 0 clusterName", cluster_list[0]['clusterName'], 'CLUSTER_1')
90+
self._match_values("Cluster 1 clusterName", cluster_list[1]['clusterName'], 'CLUSTER_2')
91+
6792
def _extract_domain_resource(self, suffix):
6893
model_file = os.path.join(self.MODELS_DIR, 'model-' + suffix + '.yaml')
6994
translator = FileToPython(model_file, use_ordering=True)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) 2021, Oracle Corporation and/or its affiliates.
2+
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
# Test model for extractDomainResource.
5+
# This will test that the deprecated "named object list" format is still supported in WDT 2.0
6+
7+
kubernetes:
8+
spec:
9+
serverPod:
10+
env:
11+
JAVA_OPTIONS:
12+
value: -good_java
13+
USER_MEM_ARGS:
14+
value: '-XX:+UseContainerSupport -Djava.security.egd=file:/dev/./urandom '
15+
clusters:
16+
CLUSTER_1:
17+
serverStartState: RUNNING
18+
replicas: 1
19+
CLUSTER_2:
20+
serverStartState: RUNNING
21+
replicas: 4

0 commit comments

Comments
 (0)