11import os
2- import yaml
32
4- # Define the project directories and files
5- project_name = "app/media/ MyHelm"
6- dirs = [ "charts" , "templates/web" ]
7- files = [ "Chart.yaml" , "values.yaml" , "templates/web/service.yaml" , "templates/web/deployment.yaml" , "templates/web/secret.yaml" ]
3+ # Define the project structure
4+ project_name = "MyHelm"
5+ base_path = "app/media/"
6+ project_path = os . path . join ( base_path , project_name )
87
9- # Chart.yaml content
10- chart_yaml_content = {
11- "apiVersion" : "v2" ,
12- "name" : "my-helm-chart" ,
13- "version" : "0.1.0" ,
14- "description" : "A Helm chart for Kubernetes" ,
15- "type" : "application" ,
16- }
17-
18- # values.yaml content
19- values_yaml_content = {
20- "web" : {
21- "image" : "nginx" ,
22- "replicaCount" : 1 ,
23- "service" : {
24- "port" : 80 ,
25- },
26- "persistence" : {
27- "enabled" : True ,
28- "size" : "1Gi" ,
29- "accessModes" : ["ReadWriteOnce" ],
30- },
31- "env" : {
32- "ENV1" : "Hi" ,
33- },
34- }
35- }
36-
37- # Create the project structure
38- for directory in dirs :
39- os .makedirs (os .path .join (project_name , directory ), exist_ok = True )
8+ directories = [
9+ "charts" ,
10+ "templates/web"
11+ ]
4012
41- # Write Chart.yaml
42- with open (os .path .join (project_name , "Chart.yaml" ), "w" ) as chart_file :
43- yaml .dump (chart_yaml_content , chart_file )
44-
45- # Write values.yaml
46- with open (os .path .join (project_name , "values.yaml" ), "w" ) as values_file :
47- yaml .dump (values_yaml_content , values_file )
48-
49- # Write service.yaml
50- service_yaml_content = """
51- apiVersion: v1
13+ files_and_contents = {
14+ "Chart.yaml" : """apiVersion: v2
15+ name: MyHelm
16+ description: A Helm chart for Kubernetes
17+ version: 0.1.0
18+ """ ,
19+ "values.yaml" : """web:
20+ image: nginx
21+ targetPort: 80
22+ replicas: 1
23+ persistence:
24+ size: 1Gi
25+ accessModes:
26+ - ReadWriteOnce
27+ env:
28+ - name: ENV1
29+ value: Hi
30+ ingress:
31+ enabled: true
32+ stateless: true
33+ """ ,
34+ "templates/web/service.yaml" : """apiVersion: v1
5235kind: Service
5336metadata:
54- name: {{ include "{project_name}.name" . }}
37+ name: {{ .Release.Name }}-web
5538spec:
5639 type: ClusterIP
5740 ports:
58- - port: {{ .Values.web.service.port }}
41+ - port: 80
42+ targetPort: {{ .Values.web.targetPort }}
5943 selector:
60- app: {{ include "{project_name}.name" . }}
61- """
62- with open (os .path .join (project_name , "templates/web/service.yaml" ), "w" ) as service_file :
63- service_file .write (service_yaml_content )
64-
65- # Write deployment.yaml
66- deployment_yaml_content = """
67- apiVersion: apps/v1
44+ app: {{ .Release.Name }}-web
45+ """ ,
46+ "templates/web/deployment.yaml" : """apiVersion: apps/v1
6847kind: Deployment
6948metadata:
70- name: {{ include "{project_name}.name" . }}
49+ name: {{ .Release.Name }}-web
7150spec:
72- replicas: {{ .Values.web.replicaCount }}
51+ replicas: {{ .Values.web.replicas }}
7352 selector:
7453 matchLabels:
75- app: {{ include "{project_name}.name" . }}
54+ app: {{ .Release.Name }}-web
7655 template:
7756 metadata:
7857 labels:
79- app: {{ include "{project_name}.name" . }}
58+ app: {{ .Release.Name }}-web
8059 spec:
8160 containers:
82- - name: {{ include "{project_name}.name" . }}
61+ - name: web
8362 image: {{ .Values.web.image }}
8463 ports:
85- - containerPort: {{ .Values.web.service.port }}
86- {{- if .Values.web.persistence.enabled }}
87- volumeClaimTemplates:
88- - metadata:
89- name: {{ include "{project_name}.name" . }}-pvc
90- spec:
91- accessModes: {{ .Values.web.persistence.accessModes | toJson }}
92- resources:
93- requests:
94- storage: {{ .Values.web.persistence.size }}
95- {{- end }}
96- """
97- with open (os .path .join (project_name , "templates/web/deployment.yaml" ), "w" ) as deployment_file :
98- deployment_file .write (deployment_yaml_content )
99-
100- # Write secret.yaml
101- secret_yaml_content = """
102- apiVersion: v1
64+ - containerPort: {{ .Values.web.targetPort }}
65+ env:
66+ - name: {{ .Values.web.env[0].name }}
67+ value: {{ .Values.web.env[0].value }}
68+ """ ,
69+ "templates/web/secret.yaml" : """apiVersion: v1
10370kind: Secret
10471metadata:
105- name: {{ include "{project_name}.name" . }}-secret
72+ name: {{ .Release.Name }}-web -secret
10673type: Opaque
10774data:
108- ENV1: {{ .Values.web.env.ENV1 | b64enc | quote }}
75+ ENV1: {{ .Values.web.env[0].value | b64enc | quote }}
10976"""
110- with open (os .path .join (project_name , "templates/web/secret.yaml" ), "w" ) as secret_file :
111- secret_file .write (secret_yaml_content )
77+ }
78+
79+ # Create directories and files
80+ os .makedirs (project_path , exist_ok = True )
81+ for directory in directories :
82+ os .makedirs (os .path .join (project_path , directory ), exist_ok = True )
83+
84+ for file_path , content in files_and_contents .items ():
85+ with open (os .path .join (project_path , file_path ), 'w' ) as f :
86+ f .write (content )
0 commit comments