11import os
22
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 )
3+ project_name = "app/media/MyHelm"
4+ chart_structure = {
5+ "charts" : {},
6+ "templates" : {
7+ "web" : {}
8+ }
9+ }
710
8- chart_yaml_content = """apiVersion: v2
9- name: MyHelm
11+ values_data = {
12+ 'web' : {
13+ 'image' : 'nginx' ,
14+ 'targetPort' : 80 ,
15+ 'replicaCount' : 1 ,
16+ 'persistence' : {
17+ 'size' : '1Gi' ,
18+ 'accessModes' : 'ReadWriteOnce'
19+ },
20+ 'env' : [
21+ {'name' : 'ENV1' , 'value' : 'Hi' }
22+ ],
23+ 'ingress' : {
24+ 'enabled' : False ,
25+ 'host' : 'www.example.com'
26+ },
27+ 'stateless' : True
28+ }
29+ }
30+
31+ def create_file (path , content = "" ):
32+ with open (path , 'w' ) as file :
33+ file .write (content )
34+
35+ os .makedirs (os .path .join (project_name , "charts" ), exist_ok = True )
36+ os .makedirs (os .path .join (project_name , "templates" , "web" ), exist_ok = True )
37+
38+ chart_yaml_content = """apiVersion: v2
39+ name: mychart
1040description: A Helm chart for Kubernetes
1141version: 0.1.0
1242"""
13-
14- values_yaml_content = """web:
43+
44+ values_yaml_content = """web:
1545 image: nginx
16- service:
17- targetPort: 80
18- replicas: 1
46+ targetPort: 80
47+ replicaCount: 1
1948 persistence:
2049 size: 1Gi
21- accessModes:
50+ accessModes:
2251 - ReadWriteOnce
2352 env:
2453 - name: ENV1
2554 value: Hi
2655 ingress:
2756 enabled: false
2857 host: www.example.com
58+ stateless: true
2959"""
3060
31- service_yaml_content = """apiVersion: v1
32- kind: Service
33- metadata:
34- name: {{ include "MyHelm.fullname" . }}-web
35- spec:
36- type: ClusterIP
37- ports:
38- - port: {{ .Values.web.service.targetPort }}
39- selector:
40- app: {{ include "MyHelm.name" . }}
41- """
61+ create_file (os .path .join (project_name , "Chart.yaml" ), chart_yaml_content )
62+ create_file (os .path .join (project_name , "values.yaml" ), values_yaml_content )
4263
43- deployment_yaml_content = """apiVersion: apps/v1
64+ deployment_yaml_content = """apiVersion: apps/v1
4465kind: Deployment
4566metadata:
46- name: {{ include "MyHelm.fullname" . }}-web
67+ name: {{ .Release.Name }}-web
4768spec:
48- replicas: {{ .Values.web.replicas }}
69+ replicas: {{ .Values.web.replicaCount }}
70+ selector:
71+ matchLabels:
72+ app: {{ .Release.Name }}-web
4973 template:
5074 metadata:
5175 labels:
52- app: {{ include "MyHelm.name" . }}
76+ app: {{ .Release.Name }}-web
5377 spec:
5478 containers:
5579 - name: web
5680 image: {{ .Values.web.image }}
5781 ports:
58- - containerPort: {{ .Values.web.service. targetPort }}
82+ - containerPort: {{ .Values.web.targetPort }}
5983 env:
60- {{- range .Values.web.env }}
84+ {{- range .Values.web.env }}
6185 - name: {{ .name }}
62- value: {{ .value }}
63- {{- end }}
86+ value: {{ .value | quote }}
87+ {{- end }}
88+ """
89+
90+ service_yaml_content = """apiVersion: v1
91+ kind: Service
92+ metadata:
93+ name: {{ .Release.Name }}-web
94+ spec:
95+ ports:
96+ - port: {{ .Values.web.targetPort }}
97+ selector:
98+ app: {{ .Release.Name }}-web
6499"""
65100
66- secret_yaml_content = """apiVersion: v1
101+ secrets_yaml_content = """apiVersion: v1
67102kind: Secret
68103metadata:
69- name: {{ include "MyHelm.fullname" . }}-web-env
104+ name: {{ .Release.Name }}-secret
70105type: Opaque
71106data:
72- ENV1 : {{ .Values.web.env | toJson | b64enc | quote }}
107+ example-key : {{ .Values.secret.exampleKey | b64enc | quote }}
73108"""
74109
75- helpers_tpl_content = """{{/*
76- Expand the name of the chart.
77- */}}
78- {{- define "MyHelm.name" -}}
79- {{- .Chart.Name | replace "-" "_" | lower -}}
80- {{- end -}}
110+ create_file (os .path .join (project_name , "templates" , "web" , "deployment.yaml" ), deployment_yaml_content )
111+ create_file (os .path .join (project_name , "templates" , "web" , "service.yaml" ), service_yaml_content )
112+ create_file (os .path .join (project_name , "templates" , "web" , "secrets.yaml" ), secrets_yaml_content )
81113
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 -}}
92- """
114+ if values_data ['web' ]['stateless' ]:
115+ create_file (os .path .join (project_name , "templates" , "web" , "statefulset.yaml" ), "" )
116+ else :
117+ create_file (os .path .join (project_name , "templates" , "web" , "statefulset.yaml" ), "" )
93118
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 )
119+ if values_data ['web' ]['ingress' ]['enabled' ]:
120+ ingress_yaml_content = """apiVersion: networking.k8s.io/v1
121+ kind: Ingress
122+ metadata:
123+ name: {{ .Release.Name }}-web
124+ spec:
125+ rules:
126+ - host: {{ .Values.web.ingress.host }}
127+ http:
128+ paths:
129+ - path: /
130+ pathType: Prefix
131+ backend:
132+ service:
133+ name: {{ .Release.Name }}-web
134+ port:
135+ number: {{ .Values.web.targetPort }}
136+ """
137+ create_file (os .path .join (project_name , "templates" , "web" , "ingress.yaml" ), ingress_yaml_content )
99138
100- with open (os .path .join (project_path , 'templates' , 'web' , 'service.yaml' ), 'w' ) as file :
101- file .write (service_yaml_content )
139+ pvc_yaml_content = """apiVersion: v1
140+ kind: PersistentVolumeClaim
141+ metadata:
142+ name: {{ .Release.Name }}-web
143+ spec:
144+ accessModes:
145+ - {{ .Values.web.persistence.accessModes | first }}
146+ resources:
147+ requests:
148+ storage: {{ .Values.web.persistence.size }}
149+ """
102150
103- with open (os .path .join (project_path , 'templates' , 'web' , 'deployment.yaml' ), 'w' ) as file :
104- file .write (deployment_yaml_content )
151+ create_file (os .path .join (project_name , "templates" , "web" , "pvc.yaml" ), pvc_yaml_content )
105152
106- with open (os .path .join (project_path , 'templates' , 'web' , 'secret.yaml' ), 'w' ) as file :
107- file .write (secret_yaml_content )
153+ helpers_tpl_content = """{{/*
154+ Common utility functions for templates
155+ */}}
108156
109- with open (os .path .join (project_path , 'templates' , 'web' , 'helpers.tpl' ), 'w' ) as file :
110- file .write (helpers_tpl_content )
157+ {{- define "mychart.name" -}}
158+ {{ .Release.Name }}-{{ .Chart.Name }}
159+ {{- end -}}
160+ """
111161
112- create_helm_project_structure ( '.' )
162+ create_file ( os . path . join ( project_name , "templates" , "web" , "helpers.tpl" ), helpers_tpl_content )
0 commit comments