44import os
55from pathlib import Path
66
7- def patch_yaml_file (file_path ):
8- """Patch a YAML file to add namespace and Istio labels """
7+ def patch_yaml_file (file_path , component ):
8+ """Patch a YAML file to add conditional rendering and namespace """
99 with open (file_path , 'r' ) as f :
1010 content = f .read ()
1111
12- if '{{- if .Values.sparkOperator.enabled }}' in content :
12+ component_map = {
13+ 'spark-operator' : 'sparkOperator.enabled' ,
14+ 'cert-manager' : 'certManager.enabled' ,
15+ }
16+
17+ condition = component_map .get (component , f'{ component } .enabled' )
18+ condition_check = f'{{{{- if .Values.{ condition } }}}}'
19+
20+ if condition_check in content :
1321 return
1422
1523 try :
@@ -21,9 +29,16 @@ def patch_yaml_file(file_path):
2129 if 'metadata' in doc and doc .get ('kind' ) in [
2230 'Deployment' , 'Service' , 'ServiceAccount' , 'Role' , 'RoleBinding'
2331 ]:
24- doc ['metadata' ]['namespace' ] = '{{ include "kubeflow.namespace" . }}'
32+ if not isinstance (doc ['metadata' ].get ('namespace' ), str ) or '{{' not in doc ['metadata' ].get ('namespace' , '' ):
33+ if component == 'cert-manager' :
34+ if doc .get ('kind' ) in ['Role' , 'RoleBinding' ] and 'leaderelection' in doc ['metadata' ].get ('name' , '' ):
35+ doc ['metadata' ]['namespace' ] = 'kube-system'
36+ else :
37+ doc ['metadata' ]['namespace' ] = '{{ .Values.global.certManagerNamespace }}'
38+ else :
39+ doc ['metadata' ]['namespace' ] = '{{ include "kubeflow.namespace" . }}'
2540
26- if doc .get ('kind' ) == 'Deployment' and 'spec' in doc :
41+ if doc .get ('kind' ) == 'Deployment' and 'spec' in doc and component == 'spark-operator' :
2742 if 'template' in doc ['spec' ] and 'metadata' in doc ['spec' ]['template' ]:
2843 template_meta = doc ['spec' ]['template' ]['metadata' ]
2944 if 'labels' not in template_meta :
@@ -33,7 +48,7 @@ def patch_yaml_file(file_path):
3348 patched_docs .append (doc )
3449
3550 with open (file_path , 'w' ) as f :
36- f .write ('{{- if .Values.sparkOperator.enabled } }\n ' )
51+ f .write (f' { condition_check } \n ' )
3752 for doc in patched_docs :
3853 f .write ('---\n ' )
3954 yaml .dump (doc , f , default_flow_style = False , sort_keys = False )
@@ -42,11 +57,16 @@ def patch_yaml_file(file_path):
4257 except Exception as e :
4358 print (f"Warning: Could not patch { file_path } : { e } " )
4459 with open (file_path , 'w' ) as f :
45- f .write ('{{- if .Values.sparkOperator.enabled } }\n ' )
60+ f .write (f' { condition_check } \n ' )
4661 f .write (content )
4762 f .write ('{{- end }}\n ' )
4863
4964if __name__ == "__main__" :
65+ if len (sys .argv ) < 3 :
66+ print ("Usage: patch-templates.py <templates_dir> <component>" )
67+ sys .exit (1 )
68+
5069 templates_dir = sys .argv [1 ]
70+ component = sys .argv [2 ]
5171 for yaml_file in Path (templates_dir ).rglob ("*.yaml" ):
52- patch_yaml_file (str (yaml_file ))
72+ patch_yaml_file (str (yaml_file ), component )
0 commit comments