11import os
2+ import yaml
23
3- # Define the project structure
4- project_name = "MyHelm"
5- base_path = "app/media/"
6- project_path = os .path .join (base_path , project_name )
4+ # Define directory and file structure
5+ project_name = 'MyHelm'
6+ base_path = os .path .join ('app' , 'media' , project_name )
77
88directories = [
9- " charts" ,
10- " templates/ web"
9+ os . path . join ( base_path , ' charts' ) ,
10+ os . path . join ( base_path , ' templates' , ' web' )
1111]
1212
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
13+ files = {
14+ 'Chart.yaml' : {
15+ 'apiVersion' : 'v2' ,
16+ 'name' : project_name ,
17+ 'description' : 'A Helm chart for Kubernetes' ,
18+ 'version' : '0.1.0' ,
19+ 'appVersion' : '1.0.0'
20+ },
21+ 'values.yaml' : {
22+ 'web' : {
23+ 'image' : {
24+ 'repository' : 'nginx' ,
25+ 'tag' : 'latest'
26+ },
27+ 'service' : {
28+ 'enabled' : True ,
29+ 'port' : 80
30+ },
31+ 'replicaCount' : 1 ,
32+ 'persistence' : {
33+ 'enabled' : True ,
34+ 'size' : '1Gi' ,
35+ 'accessModes' : ['ReadWriteOnce' ]
36+ },
37+ 'env' : [{
38+ 'name' : 'ENV1' ,
39+ 'value' : 'Hi'
40+ }],
41+ 'ingress' : {
42+ 'enabled' : False ,
43+ 'host' : 'www.example.com'
44+ }
45+ }
46+ }
47+ }
48+
49+ # Create directories
50+ for directory in directories :
51+ os .makedirs (directory , exist_ok = True )
52+
53+ # Create files with initial content
54+ for filename , content in files .items ():
55+ with open (os .path .join (base_path , filename ), 'w' ) as file :
56+ yaml .dump (content , file )
57+
58+ # Create service.yaml template
59+ service_yaml_content = """apiVersion: v1
3560kind: Service
3661metadata:
37- name: {{ .Release.Name }}-web
62+ name: {{ include "{project_name}.fullname" . }}
3863spec:
3964 type: ClusterIP
4065 ports:
41- - port: 80
42- targetPort: {{ .Values.web.targetPort }}
43- selector:
44- app: {{ .Release.Name }}-web
45- """ ,
46- "templates/web/deployment.yaml" : """apiVersion: apps/v1
47- kind: Deployment
48- metadata:
49- name: {{ .Release.Name }}-web
50- spec:
51- replicas: {{ .Values.web.replicas }}
66+ - port: {{ .Values.web.service.port }}
5267 selector:
53- matchLabels:
54- app: {{ .Release.Name }}-web
55- template:
56- metadata:
57- labels:
58- app: {{ .Release.Name }}-web
59- spec:
60- containers:
61- - name: web
62- image: {{ .Values.web.image }}
63- ports:
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
68+ app: {{ include "{project_name}.name" . }}
69+ """
70+
71+ with open (os .path .join (base_path , 'templates' , 'web' , 'service.yaml' ), 'w' ) as service_file :
72+ service_file .write (service_yaml_content .format (project_name = project_name ))
73+
74+ # Create secret.yaml template if environment variables exist
75+ if files ['values.yaml' ]['web' ].get ('env' ):
76+ secret_yaml_content = """apiVersion: v1
7077kind: Secret
7178metadata:
72- name: {{ .Release.Name }}-web-secret
79+ name: {{ include "{project_name}.fullname" . }}-env
7380type: Opaque
7481data:
7582 ENV1: {{ .Values.web.env[0].value | b64enc | quote }}
7683"""
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 )
8384
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 )
85+ with open (os .path .join (base_path , 'templates' , 'web' , 'secret.yaml' ), 'w' ) as secret_file :
86+ secret_file .write (secret_yaml_content .format (project_name = project_name ))
0 commit comments