11import os
22
33# Define project structure
4- project_name = 'app/media/MyHelm'
5- directories = ['charts/' , 'templates/' ]
6- templates_dir = 'templates/web'
7- files = ['Chart.yaml' , 'values.yaml' ]
8- template_subdirectories = ['web' ]
9- template_files = ['service.yaml' , 'helpers.tpl' ]
10-
11- # Chart.yaml content
12- chart_yaml_content = """apiVersion: v2
4+ project_name = "app/media/MyHelm"
5+ directories = ["charts" , "templates/web" ]
6+ files = ["Chart.yaml" , "values.yaml" ]
7+ template_files = ["service.yaml" , "secret.yaml" ]
8+ chart_content = """apiVersion: v2
139name: my-helm-chart
1410description: A Helm chart for Kubernetes
1511version: 0.1.0
1612"""
17-
18- # values.yaml content
19- values_yaml_content = """web:
13+ values_content = """web:
2014 image: nginx
2115 replicas: 1
2216 service:
23- targetPort : 80
17+ port : 80
2418 persistence:
25- enabled: true
2619 size: 1Gi
2720 accessModes:
2821 - ReadWriteOnce
3427 host: www.example.com
3528"""
3629
37- # Create project structure
30+ # Create project directories and files
3831os .makedirs (project_name , exist_ok = True )
3932for directory in directories :
4033 os .makedirs (os .path .join (project_name , directory ), exist_ok = True )
4134
42- # Create templates/web directory
43- os . makedirs (os .path .join (project_name , templates_dir ), exist_ok = True )
44-
45- # Create files
46- with open ( os . path . join ( project_name , 'Chart.yaml' ), 'w' ) as chart_file :
47- chart_file .write (chart_yaml_content )
35+ for file in files :
36+ with open (os .path .join (project_name , file ), 'w' ) as f :
37+ if file == "Chart.yaml" :
38+ f . write ( chart_content )
39+ elif file == "values.yaml" :
40+ f .write (values_content )
4841
49- with open (os .path .join (project_name , 'values.yaml' ), 'w' ) as values_file :
50- values_file .write (values_yaml_content )
51-
52- # Create template files and directories
53- for template in template_subdirectories :
54- template_path = os .path .join (project_name , 'templates' , template )
55- os .makedirs (template_path , exist_ok = True )
42+ # Create template files
43+ for template in directories [1 :]:
5644 for template_file in template_files :
57- with open (os .path .join (template_path , template_file ), 'w' ) as file :
58- file .write (f"# { template_file } for { template } \n " )
59-
60- # Create secret.yaml if env variable is considered
61- with open (os .path .join (template_path , 'secret.yaml' ), 'w' ) as secret_file :
62- secret_file .write ("# secret.yaml for environment variables\n " )
63-
64- # Create helpers.tpl
65- with open (os .path .join (template_path , 'helpers.tpl' ), 'w' ) as helpers_file :
66- helpers_file .write (f"""{{- define "{ template } .labels" -}}
67- name: {{ .Release.Name }}
68- {{- end -}}
69- // Add additional helper functions if needed
45+ with open (os .path .join (project_name , template , template_file ), 'w' ) as f :
46+ if template_file == "service.yaml" :
47+ f .write ("""apiVersion: v1
48+ kind: Service
49+ metadata:
50+ name: {{ include "{{ .Chart.Name }}.fullname" . }}
51+ spec:
52+ type: ClusterIP
53+ ports:
54+ - port: {{ .Values.web.service.port }}
55+ targetPort: {{ .Values.web.service.port }}
56+ selector:
57+ app: {{ include "{{ .Chart.Name }}.fullname" . }}
58+ """ )
59+ elif template_file == "secret.yaml" :
60+ f .write ("""apiVersion: v1
61+ kind: Secret
62+ metadata:
63+ name: {{ include "{{ .Chart.Name }}.fullname" . }}-secret
64+ type: Opaque
65+ data:
66+ ENV1: {{ .Values.web.env | toJson | b64enc | quote }}
7067""" )
0 commit comments