33import yaml
44
55class RenderEngine :
6+ """
7+ The RenderEngine class provides functionality to render K8 templates
8+ and add a service to a kustomization file.
9+ """
610 def __init__ (self , path , kustomization_path , service_name ):
11+ """
12+ Initializes a new instance of the RenderEngine class.
13+
14+ Args:
15+ path (str): The path to the directory containing the templates to render.
16+ kustomization_path (str): The path to the kustomization file.
17+ service_name (str): The name of the service to add to the kustomization file.
18+ """
719 self .path = path
820 self .service_name = service_name
921 self .kustomization_path = kustomization_path
1022 self .remove_template_file = True
1123
1224 def __retrieve_files_to_render (self ):
25+ """
26+ Retrieves a list of files to render.
27+
28+ Returns:
29+ A list of files to render.
30+ """
1331 files_to_render = []
1432 for root , _ , files in os .walk (self .path ):
1533 for file in files :
@@ -18,6 +36,9 @@ def __retrieve_files_to_render(self):
1836 return files_to_render
1937
2038 def render (self ):
39+ """
40+ Renders the templates and adds the service to the kustomization file.
41+ """
2142 files_to_render = self .__retrieve_files_to_render ()
2243 for file in files_to_render :
2344 print (f"Rendering { file } " )
@@ -29,6 +50,15 @@ def render(self):
2950 self .__add_service_to_kustomization_resources ()
3051
3152 def __run_command (self , command ):
53+ """
54+ Runs a shell command.
55+
56+ Args:
57+ command (str): The command to run.
58+
59+ Returns:
60+ The output of the command.
61+ """
3262 try :
3363 output = subprocess .check_output (command , stderr = subprocess .STDOUT , \
3464 universal_newlines = True , shell = True )
@@ -38,27 +68,40 @@ def __run_command(self, command):
3868 return None
3969
4070 def __add_service_to_kustomization_resources (self ):
71+ """
72+ Adds the service to the kustomization file.
73+ """
4174 # verify that the kustomization file exists
4275 if not os .path .exists (self .kustomization_path ):
4376 raise ValueError (
4477 f"The kustomization file { self .kustomization_path } does not exist."
4578 )
4679
47- print (f"Adding { self .service_name } to { self .kustomization_path } " )
48-
4980 with open (self .kustomization_path , "r" , encoding = "utf-8" ) as r_stream :
5081 yaml_dict = yaml .safe_load (r_stream )
5182 # check if the resources key exists
5283
5384 if "resources" not in yaml_dict :
5485 raise ValueError ("No resources found in kustomization yaml file." )
5586
56- yaml_dict ["resources" ].append (f"./services/{ self .service_name } " )
57- # Write the updated yaml to the kustomization file\
58- with open (self .kustomization_path , "w" , encoding = "utf-8" ) as w_stream :
59- yaml .safe_dump (yaml_dict , w_stream , default_flow_style = False , sort_keys = False )
87+ # Check if the service already exists in the resources
88+ if f"./services/{ self .service_name } " not in yaml_dict ["resources" ]:
89+ yaml_dict ["resources" ].append (f"./services/{ self .service_name } " )
90+ # Write the updated yaml to the kustomization file\
91+ with open (self .kustomization_path , "w" , encoding = "utf-8" ) as w_stream :
92+ print (f"Adding { self .service_name } to { self .kustomization_path } " )
93+ yaml .safe_dump (yaml_dict , w_stream , default_flow_style = False , sort_keys = False )
6094
6195 def __run_command (self , command ):
96+ """
97+ Runs a shell command.
98+
99+ Args:
100+ command (str): The command to run.
101+
102+ Returns:
103+ The output of the command.
104+ """
62105 try :
63106 output = subprocess .check_output (command , stderr = subprocess .STDOUT , \
64107 universal_newlines = True , shell = True )
0 commit comments