11import os
22
3- # Define the project structure
4- project_name = "MyHelm"
5- base_path = f"app/media/{ project_name } "
3+ def create_helm_project_structure (base_path ):
4+ project_path = os .path .join (base_path , 'app/media/MyHelm' )
5+ os .makedirs (os .path .join (project_path , 'charts' ), exist_ok = True )
6+ os .makedirs (os .path .join (project_path , 'templates' , 'web' ), exist_ok = True )
67
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
8+ chart_yaml_content = """apiVersion: v2
9+ name: MyHelm
2710description: A Helm chart for Kubernetes
2811version: 0.1.0
2912"""
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:
13+
14+ values_yaml_content = """web:
3515 image: nginx
3616 service:
3717 targetPort: 80
3818 replicas: 1
3919 persistence:
40- enabled: true
4120 size: 1Gi
4221 accessModes:
4322 - ReadWriteOnce
4827 enabled: false
4928 host: www.example.com
5029"""
51- with open (os .path .join (base_path , "values.yaml" ), "w" ) as values_file :
52- values_file .write (values_yaml_content )
5330
54- # Create service.yaml file
55- service_yaml_content = """apiVersion: v1
31+ service_yaml_content = """apiVersion: v1
5632kind: Service
5733metadata:
58- name: web
34+ name: {{ include "MyHelm.fullname" . }}- web
5935spec:
6036 type: ClusterIP
6137 ports:
62- - port: 80
63- targetPort: {{ .Values.web.service.targetPort }}
38+ - port: {{ .Values.web.service.targetPort }}
6439 selector:
65- app: {{ .Release.Name }}
40+ app: {{ include "MyHelm.name" . }}
6641"""
6742
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
43+ deployment_yaml_content = """apiVersion: apps/v1
7344kind: Deployment
7445metadata:
75- name: web
46+ name: {{ include "MyHelm.fullname" . }}- web
7647spec:
7748 replicas: {{ .Values.web.replicas }}
78- selector:
79- matchLabels:
80- app: {{ .Release.Name }}
8149 template:
8250 metadata:
8351 labels:
84- app: {{ .Release.Name }}
52+ app: {{ include "MyHelm.name" . }}
8553 spec:
8654 containers:
8755 - name: web
8856 image: {{ .Values.web.image }}
8957 ports:
9058 - containerPort: {{ .Values.web.service.targetPort }}
9159 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 }}
60+ {{- range .Values.web.env }}
61+ - name: {{ .name }}
62+ value: {{ .value }}
63+ {{- end }}
10264"""
10365
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
66+ secret_yaml_content = """apiVersion: v1
10967kind: Secret
11068metadata:
111- name: web-secret
69+ name: {{ include "MyHelm.fullname" . }}- web-env
11270type: Opaque
11371data:
114- ENV1: aGl
72+ ENV1: {{ .Values.web.env | toJson | b64enc | quote }}
73+ """
74+
75+ helpers_tpl_content = """{{/*
76+ Expand the name of the chart.
77+ */}}
78+ {{- define "MyHelm.name" -}}
79+ {{- .Chart.Name | replace "-" "_" | lower -}}
80+ {{- end -}}
81+
82+ {{/*
83+ Create a default fully qualified domain name
84+ */}}
85+ {{- define "MyHelm.fullname" -}}
86+ {{- if .Chart.Name -}}
87+ {{- .Release.Name | lower | replace "-" "_" | trimSuffix "-" | append (include "MyHelm.name" . | lower) | toLower -}}
88+ {{- else -}}
89+ {{- .Release.Name | lower -}}
90+ {{- end -}}
91+ {{- end -}}
11592"""
11693
117- with open (os .path .join (base_path , "templates/web/secret.yaml" ), "w" ) as secret_file :
118- secret_file .write (secret_yaml_content )
94+ with open (os .path .join (project_path , 'Chart.yaml' ), 'w' ) as file :
95+ file .write (chart_yaml_content )
96+
97+ with open (os .path .join (project_path , 'values.yaml' ), 'w' ) as file :
98+ file .write (values_yaml_content )
99+
100+ with open (os .path .join (project_path , 'templates' , 'web' , 'service.yaml' ), 'w' ) as file :
101+ file .write (service_yaml_content )
102+
103+ with open (os .path .join (project_path , 'templates' , 'web' , 'deployment.yaml' ), 'w' ) as file :
104+ file .write (deployment_yaml_content )
105+
106+ with open (os .path .join (project_path , 'templates' , 'web' , 'secret.yaml' ), 'w' ) as file :
107+ file .write (secret_yaml_content )
108+
109+ with open (os .path .join (project_path , 'templates' , 'web' , 'helpers.tpl' ), 'w' ) as file :
110+ file .write (helpers_tpl_content )
111+
112+ create_helm_project_structure ('.' )
0 commit comments