Skip to content

Commit 244fbcf

Browse files
authored
Create operator resource file for WKO target (#756)
* JIRA WDT-461 - Add model output for wko target; expose template files in lib/targets directory * JIRA WDT-461 - Correct exception handling for reading file template * JIRA WDT-461 - Added some comments from WKO template * JIRA WDT-461 - Added new lines at end of JSON files * JIRA WDT-461 - Combine two output dir getters for the same value * JIRA WDT-461 - Added a comment for "replicas"
1 parent f9332f5 commit 244fbcf

File tree

15 files changed

+138
-46
lines changed

15 files changed

+138
-46
lines changed

core/src/main/python/compare_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ def main():
642642

643643
try:
644644
model_context = __process_args(sys.argv)
645-
_outputdir = model_context.get_compare_model_output_dir()
645+
_outputdir = model_context.get_output_dir()
646646
model1 = model_context.get_trailing_argument(0)
647647
model2 = model_context.get_trailing_argument(1)
648648

core/src/main/python/prepare_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ def main():
379379

380380
try:
381381
model_context = __process_args(sys.argv)
382-
_outputdir = model_context.get_kubernetes_output_dir()
382+
_outputdir = model_context.get_output_dir()
383383
model1 = model_context.get_model_file()
384384
# for f in [ model1 ]:
385385
# if not os.path.exists(f):

core/src/main/python/wlsdeploy/tool/util/credential_map_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def create_default_init_file(self, default_mapping_nodes):
9696

9797
self._logger.info('WLSDPLY-01790', output_file, class_name=self._class_name, method_name=_method_name)
9898

99-
file_template_helper.create_file(template_path, template_hash, output_file, self._exception_type)
99+
file_template_helper.create_file_from_resource(template_path, template_hash, output_file, self._exception_type)
100100

101101
def _build_default_template_hash(self, mapping_section_nodes):
102102
"""

core/src/main/python/wlsdeploy/tool/util/targets/vz_config_helper.py renamed to core/src/main/python/wlsdeploy/tool/util/targets/additional_output_helper.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818
from wlsdeploy.tool.util import k8s_helper
1919
from wlsdeploy.tool.util.targets import file_template_helper
2020
from wlsdeploy.util import dictionary_utils
21+
from wlsdeploy.util import path_utils
2122
from wlsdeploy.util import target_configuration_helper
2223

2324
__class_name = 'vz_config_helper'
2425
__logger = PlatformLogger('wlsdeploy.tool.util')
2526

26-
TEMPLATE_PATH = 'oracle/weblogic/deploy/targets/vz'
27-
2827
# substitution keys used in the templates
2928
CLUSTER_NAME = 'clusterName'
3029
CLUSTERS = 'clusters'
@@ -43,42 +42,46 @@
4342
WEBLOGIC_CREDENTIALS_SECRET = 'webLogicCredentialsSecret'
4443

4544

46-
def create_vz_configuration(model, model_context, aliases, exception_type):
45+
def create_additional_output(model, model_context, aliases, exception_type):
4746
"""
48-
Create and write the Kubernetes resource configuration files for Verrazzano.
49-
:param model: Model object, used to derive some values in the configurations
50-
:param model_context: used to determine location and content for the configurations
47+
Create and write additional output for the configured target type.
48+
:param model: Model object, used to derive some values in the output
49+
:param model_context: used to determine location and content for the output
5150
:param aliases: used to derive secret names
5251
:param exception_type: the type of exception to throw if needed
5352
"""
5453

5554
# -output_dir argument was previously verified
56-
output_dir = model_context.get_kubernetes_output_dir()
55+
output_dir = model_context.get_output_dir()
5756

57+
# all current output types use this hash, and process a set of template files
5858
template_hash = _build_template_hash(model, model_context, aliases)
5959

60-
_create_file('model.yaml', template_hash, output_dir, exception_type)
61-
62-
_create_file('binding.yaml', template_hash, output_dir, exception_type)
60+
file_names = model_context.get_target_configuration().get_additional_output_types()
61+
for file_name in file_names:
62+
_create_file(file_name, template_hash, model_context, output_dir, exception_type)
6363

6464

65-
def _create_file(template_name, template_hash, output_dir, exception_type):
65+
def _create_file(template_name, template_hash, model_context, output_dir, exception_type):
6666
"""
6767
Read the template from the resource stream, perform any substitutions,
6868
and write it to a file with the same name in the output directory.
6969
:param template_name: the name of the template file, and the output file
7070
:param template_hash: a dictionary of substitution values
71+
:param model_context: used to determine location and content for the output
7172
:param output_dir: the directory to write the output file
7273
:param exception_type: the type of exception to throw if needed
7374
"""
7475
_method_name = '_create_file'
7576

76-
template_path = TEMPLATE_PATH + '/' + template_name
77+
target_key = model_context.get_target()
78+
template_subdir = "targets/" + target_key + "/" + template_name
79+
template_path = path_utils.find_config_path(template_subdir)
7780
output_file = File(output_dir, template_name)
7881

7982
__logger.info('WLSDPLY-01662', output_file, class_name=__class_name, method_name=_method_name)
8083

81-
file_template_helper.create_file(template_path, template_hash, output_file, exception_type)
84+
file_template_helper.create_file_from_file(template_path, template_hash, output_file, exception_type)
8285

8386

8487
def _build_template_hash(model, model_context, aliases):

core/src/main/python/wlsdeploy/tool/util/targets/file_template_helper.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import re
88

99
from java.io import BufferedReader
10+
from java.io import IOException
1011
from java.io import InputStreamReader
12+
from java.lang import IllegalArgumentException
1113
from oracle.weblogic.deploy.util import FileUtils
1214

1315
from wlsdeploy.exception import exception_helper
@@ -22,7 +24,7 @@
2224
_block_end_pattern = re.compile("({{/(.*)}})")
2325

2426

25-
def create_file(resource_path, template_hash, output_file, exception_type):
27+
def create_file_from_resource(resource_path, template_hash, output_file, exception_type):
2628
"""
2729
Read the template from the resource stream, perform any substitutions,
2830
and write it to the output file.
@@ -31,17 +33,41 @@ def create_file(resource_path, template_hash, output_file, exception_type):
3133
:param output_file: the file to write
3234
:param exception_type: the type of exception to throw if needed
3335
"""
34-
_method_name = 'create_file'
36+
_method_name = 'create_file_from_resource'
3537

36-
file_writer = open(output_file.getPath(), "w")
37-
38-
template_stream = FileUtils.getResourceAsStream(resource_path)
38+
template_stream = FileUtils.getFileAsStream(resource_path)
3939
if template_stream is None:
4040
ex = exception_helper.create_exception(exception_type, 'WLSDPLY-01661', resource_path)
4141
__logger.throwing(ex, class_name=__class_name, method_name=_method_name)
4242
raise ex
4343

44-
template_reader = BufferedReader(InputStreamReader(FileUtils.getResourceAsStream(resource_path)))
44+
_create_file_from_stream(template_stream, template_hash, output_file, exception_type)
45+
46+
47+
def create_file_from_file(file_path, template_hash, output_file, exception_type):
48+
"""
49+
Read the template from the template file, perform any substitutions,
50+
and write it to the output file.
51+
:param file_path: the absolute file path of the source template
52+
:param template_hash: a dictionary of substitution values
53+
:param output_file: the file to write
54+
:param exception_type: the type of exception to throw if needed
55+
"""
56+
_method_name = 'create_file_from_file'
57+
58+
try:
59+
template_stream = FileUtils.getFileAsStream(file_path)
60+
if template_stream is not None:
61+
_create_file_from_stream(template_stream, template_hash, output_file, exception_type)
62+
except (IOException, IllegalArgumentException), ie:
63+
ex = exception_helper.create_exception(exception_type, 'WLSDPLY-01666', file_path, ie)
64+
__logger.throwing(ex, class_name=__class_name, method_name=_method_name)
65+
raise ex
66+
67+
68+
def _create_file_from_stream(template_stream, template_hash, output_file, exception_type):
69+
template_reader = BufferedReader(InputStreamReader(template_stream))
70+
file_writer = open(output_file.getPath(), "w")
4571

4672
current_block_key = None
4773
block_lines = []

core/src/main/python/wlsdeploy/util/model_context.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -594,16 +594,9 @@ def get_encryption_passphrase(self):
594594
"""
595595
return self._encryption_passphrase
596596

597-
def get_compare_model_output_dir(self):
597+
def get_output_dir(self):
598598
"""
599-
Return the compare model output dir
600-
:return: output dir for compare models tool
601-
"""
602-
return self._output_dir
603-
604-
def get_kubernetes_output_dir(self):
605-
"""
606-
Return the output directory for generated k8s target.
599+
Return the output directory.
607600
:return: output directory
608601
"""
609602
return self._output_dir

core/src/main/python/wlsdeploy/util/target_configuration_helper.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from wlsdeploy.logging.platform_logger import PlatformLogger
1818
from wlsdeploy.tool.util import k8s_helper
1919
from wlsdeploy.tool.util import variable_injector_functions
20-
from wlsdeploy.tool.util.targets import vz_config_helper
20+
from wlsdeploy.tool.util.targets import additional_output_helper
2121
from wlsdeploy.util import dictionary_utils
2222
from wlsdeploy.util.cla_utils import CommandLineArgUtil
2323

@@ -103,7 +103,7 @@ def generate_k8s_script(model_context, token_dictionary, model_dictionary):
103103
domain_uid = k8s_helper.get_domain_uid(domain_name)
104104

105105
nl = '\n'
106-
file_location = model_context.get_kubernetes_output_dir()
106+
file_location = model_context.get_output_dir()
107107
k8s_file = os.path.join(file_location, "create_k8s_secrets.sh")
108108
k8s_script = open(k8s_file, 'w')
109109

@@ -239,13 +239,7 @@ def create_additional_output(model, model_context, aliases, exception_type):
239239
"""
240240
_method_name = 'create_additional_output'
241241

242-
output_types = model_context.get_target_configuration().get_additional_output_types()
243-
for output_type in output_types:
244-
if output_type == VZ_EXTRA_CONFIG:
245-
vz_config_helper.create_vz_configuration(model, model_context, aliases, exception_type)
246-
else:
247-
__logger.warning('WLSDPLY-01660', output_type, model_context.get_target(), class_name=__class_name,
248-
method_name=_method_name)
242+
additional_output_helper.create_additional_output(model, model_context, aliases, exception_type)
249243

250244

251245
def create_secret_name(variable_name, suffix=None):

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ WLSDPLY-01662=Creating target configuration file {0}
301301
WLSDPLY-01663=Update {0} for {1}
302302
WLSDPLY-01664=Update {0} and {1} for {2}
303303
WLSDPLY-01665=Edit these values to change the namespace or domain UID
304-
304+
WLSDPLY-01666=Unable to open file template {0}: {1}
305305

306306
# wlsdeploy/util/enum.py
307307
WLSDPLY-01700=The value {0} is not a valid value of the Enum type {1}

core/src/main/targetconfigs/k8s/target.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
"variable_injectors" : {"PORT": {},"HOST": {},"URL": {}},
88
"validation_method" : "lax",
99
"credentials_method" : "secrets"
10-
}
10+
}

0 commit comments

Comments
 (0)