11import os
22
3- # Define project structure
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
9- name: my-helm-chart
3+ # Define the project structure
4+ project_name = "MyHelm"
5+ base_path = f"app/media/{ project_name } "
6+
7+ directories = [
8+ "charts/" ,
9+ "templates/web/"
10+ ]
11+
12+ files = [
13+ "Chart.yaml" ,
14+ "values.yaml" ,
15+ "templates/web/service.yaml" ,
16+ "templates/web/deployment.yaml" ,
17+ "templates/web/secret.yaml" # Only if there are environment variables
18+ ]
19+
20+ # Create the directories
21+ for directory in directories :
22+ os .makedirs (os .path .join (base_path , directory ), exist_ok = True )
23+
24+ # Create the Chart.yaml file
25+ chart_yaml_content = """apiVersion: v2
26+ name: mychart
1027description: A Helm chart for Kubernetes
1128version: 0.1.0
1229"""
13- values_content = """web:
30+ with open (os .path .join (base_path , "Chart.yaml" ), "w" ) as chart_file :
31+ chart_file .write (chart_yaml_content )
32+
33+ # Create the values.yaml file
34+ values_yaml_content = """web:
1435 image: nginx
15- replicas: 1
1636 service:
17- port: 80
37+ targetPort: 80
38+ replicas: 1
1839 persistence:
40+ enabled: true
1941 size: 1Gi
2042 accessModes:
2143 - ReadWriteOnce
2648 enabled: false
2749 host: www.example.com
2850"""
51+ with open (os .path .join (base_path , "values.yaml" ), "w" ) as values_file :
52+ values_file .write (values_yaml_content )
2953
30- # Create project directories and files
31- os .makedirs (project_name , exist_ok = True )
32- for directory in directories :
33- os .makedirs (os .path .join (project_name , directory ), exist_ok = True )
34-
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 )
41-
42- # Create template files
43- for template in directories [1 :]:
44- for template_file in template_files :
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
54+ # Create service.yaml file
55+ service_yaml_content = """apiVersion: v1
4856kind: Service
4957metadata:
50- name: {{ include "{{ .Chart.Name }}.fullname" . }}
58+ name: web
5159spec:
5260 type: ClusterIP
5361 ports:
54- - port: {{ .Values.web.service.port }}
55- targetPort: {{ .Values.web.service.port }}
62+ - port: 80
63+ targetPort: {{ .Values.web.service.targetPort }}
5664 selector:
57- app: {{ include "{{ .Chart.Name }}.fullname" . }}
58- """ )
59- elif template_file == "secret.yaml" :
60- f .write ("""apiVersion: v1
65+ app: {{ .Release.Name }}
66+ """
67+
68+ with open (os .path .join (base_path , "templates/web/service.yaml" ), "w" ) as service_file :
69+ service_file .write (service_yaml_content )
70+
71+ # Create deployment.yaml file
72+ deployment_yaml_content = """apiVersion: apps/v1
73+ kind: Deployment
74+ metadata:
75+ name: web
76+ spec:
77+ replicas: {{ .Values.web.replicas }}
78+ selector:
79+ matchLabels:
80+ app: {{ .Release.Name }}
81+ template:
82+ metadata:
83+ labels:
84+ app: {{ .Release.Name }}
85+ spec:
86+ containers:
87+ - name: web
88+ image: {{ .Values.web.image }}
89+ ports:
90+ - containerPort: {{ .Values.web.service.targetPort }}
91+ env:
92+ - name: ENV1
93+ value: {{ .Values.web.env[0].value }}
94+ volumeClaimTemplates:
95+ - metadata:
96+ name: web-pvc
97+ spec:
98+ accessModes: {{ .Values.web.persistence.accessModes | toYaml }}
99+ resources:
100+ requests:
101+ storage: {{ .Values.web.persistence.size }}
102+ """
103+
104+ with open (os .path .join (base_path , "templates/web/deployment.yaml" ), "w" ) as deployment_file :
105+ deployment_file .write (deployment_yaml_content )
106+
107+ # Create secret.yaml file
108+ secret_yaml_content = """apiVersion: v1
61109kind: Secret
62110metadata:
63- name: {{ include "{{ .Chart.Name }}.fullname" . }} -secret
111+ name: web -secret
64112type: Opaque
65113data:
66- ENV1: {{ .Values.web.env | toJson | b64enc | quote }}
67- """ )
114+ ENV1: aGl
115+ """
116+
117+ with open (os .path .join (base_path , "templates/web/secret.yaml" ), "w" ) as secret_file :
118+ secret_file .write (secret_yaml_content )
0 commit comments