11import os
22import yaml
33
4- # Project structure
5- project_name = "MyHelm"
6- base_dir = "app/media"
7- project_dir = os .path .join (base_dir , project_name )
8-
9- # Directories to create
10- dirs = ["charts" , "crds" , "templates/web" ]
11-
12- # Creating the directory structure
13- for dir in dirs :
14- os .makedirs (os .path .join (project_dir , dir ), exist_ok = True )
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" ]
158
169# Chart.yaml content
17- chart_yaml = {
18- "apiVersion" : "v1 " ,
19- "name" : project_name ,
10+ chart_yaml_content = {
11+ "apiVersion" : "v2 " ,
12+ "name" : "my-helm-chart" ,
2013 "version" : "0.1.0" ,
21- "description" : "A Helm chart for MyHelm" ,
22- "maintainers" : [{"name" : "Your Name" , "email" : "youremail@example.com" }],
23- "keywords" : ["helm" , "chart" ],
24- "home" : "https://example.com" ,
25- "sources" : ["https://github.com/example/MyHelm" ]
14+ "description" : "A Helm chart for Kubernetes" ,
15+ "type" : "application" ,
2616}
2717
28- # Writing Chart.yaml
29- with open (os .path .join (project_dir , "Chart.yaml" ), 'w' ) as chart_file :
30- yaml .dump (chart_yaml , chart_file )
31-
32- # values.yaml content based on provided information
33- values_yaml = {
18+ # values.yaml content
19+ values_yaml_content = {
3420 "web" : {
3521 "image" : "nginx" ,
22+ "replicaCount" : 1 ,
3623 "service" : {
24+ "port" : 80 ,
25+ },
26+ "persistence" : {
3727 "enabled" : True ,
38- "port" : 80
39- }
28+ "size" : "1Gi" ,
29+ "accessModes" : ["ReadWriteOnce" ],
30+ },
31+ "env" : {
32+ "ENV1" : "Hi" ,
33+ },
4034 }
4135}
4236
43- # Writing values.yaml
44- with open (os .path .join (project_dir , "values.yaml" ), 'w' ) as values_file :
45- yaml .dump (values_yaml , values_file )
37+ # Create the project structure
38+ for directory in dirs :
39+ os .makedirs (os .path .join (project_name , directory ), exist_ok = True )
40+
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
52+ kind: Service
53+ metadata:
54+ name: {{ include "{project_name}.name" . }}
55+ spec:
56+ type: ClusterIP
57+ ports:
58+ - port: {{ .Values.web.service.port }}
59+ 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 )
4664
47- # Template files content
48- deployment_yaml = """apiVersion: apps/v1
65+ # Write deployment.yaml
66+ deployment_yaml_content = """
67+ apiVersion: apps/v1
4968kind: Deployment
5069metadata:
51- name: web
70+ name: {{ include "{project_name}.name" . }}
5271spec:
53- replicas: 1
72+ replicas: {{ .Values.web.replicaCount }}
5473 selector:
5574 matchLabels:
56- app: web
75+ app: {{ include "{project_name}.name" . }}
5776 template:
5877 metadata:
5978 labels:
60- app: web
79+ app: {{ include "{project_name}.name" . }}
6180 spec:
6281 containers:
63- - name: web
64- image: {{ .Values.web.image }}
65- ports:
66- - containerPort: {{ .Values.web.service.port }}
82+ - name: {{ include "{project_name}.name" . }}
83+ image: {{ .Values.web.image }}
84+ 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 }}
6796"""
97+ with open (os .path .join (project_name , "templates/web/deployment.yaml" ), "w" ) as deployment_file :
98+ deployment_file .write (deployment_yaml_content )
6899
69- service_yaml = """apiVersion: v1
70- kind: Service
100+ # Write secret.yaml
101+ secret_yaml_content = """
102+ apiVersion: v1
103+ kind: Secret
71104metadata:
72- name: web
73- spec:
74- type: ClusterIP
75- ports:
76- - port: {{ .Values.web.service.port }}
77- selector:
78- app: web
105+ name: {{ include "{project_name}.name" . }}-secret
106+ type: Opaque
107+ data:
108+ ENV1: {{ .Values.web.env.ENV1 | b64enc | quote }}
79109"""
80-
81- # Creating deployment.yaml and service.yaml in templates/web
82- with open (os .path .join (project_dir , "templates/web/deployment.yaml" ), 'w' ) as dep_file :
83- dep_file .write (deployment_yaml )
84-
85- with open (os .path .join (project_dir , "templates/web/service.yaml" ), 'w' ) as svc_file :
86- svc_file .write (service_yaml )
110+ with open (os .path .join (project_name , "templates/web/secret.yaml" ), "w" ) as secret_file :
111+ secret_file .write (secret_yaml_content )
0 commit comments