Skip to content

Commit 8be39c1

Browse files
authored
fix: Merge pull request #23 from abolfazl8131/fix/helm
fix helm prompt for secret generation
2 parents b3193dc + ce5a082 commit 8be39c1

7 files changed

Lines changed: 64 additions & 52 deletions

File tree

193 Bytes
Binary file not shown.
Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
import os
22

33
# Define project structure
4-
project_name = 'app/media/MyHelm'
5-
directories = ['charts/', 'templates/']
6-
templates_dir = 'templates/web'
7-
files = ['Chart.yaml', 'values.yaml']
8-
template_subdirectories = ['web']
9-
template_files = ['service.yaml', 'helpers.tpl']
10-
11-
# Chart.yaml content
12-
chart_yaml_content = """apiVersion: v2
4+
project_name = "app/media/MyHelm"
5+
directories = ["charts", "templates/web"]
6+
files = ["Chart.yaml", "values.yaml"]
7+
template_files = ["service.yaml", "secret.yaml"]
8+
chart_content = """apiVersion: v2
139
name: my-helm-chart
1410
description: A Helm chart for Kubernetes
1511
version: 0.1.0
1612
"""
17-
18-
# values.yaml content
19-
values_yaml_content = """web:
13+
values_content = """web:
2014
image: nginx
2115
replicas: 1
2216
service:
23-
targetPort: 80
17+
port: 80
2418
persistence:
25-
enabled: true
2619
size: 1Gi
2720
accessModes:
2821
- ReadWriteOnce
@@ -34,37 +27,41 @@
3427
host: www.example.com
3528
"""
3629

37-
# Create project structure
30+
# Create project directories and files
3831
os.makedirs(project_name, exist_ok=True)
3932
for directory in directories:
4033
os.makedirs(os.path.join(project_name, directory), exist_ok=True)
4134

42-
# Create templates/web directory
43-
os.makedirs(os.path.join(project_name, templates_dir), exist_ok=True)
44-
45-
# Create files
46-
with open(os.path.join(project_name, 'Chart.yaml'), 'w') as chart_file:
47-
chart_file.write(chart_yaml_content)
35+
for file in files:
36+
with open(os.path.join(project_name, file), 'w') as f:
37+
if file == "Chart.yaml":
38+
f.write(chart_content)
39+
elif file == "values.yaml":
40+
f.write(values_content)
4841

49-
with open(os.path.join(project_name, 'values.yaml'), 'w') as values_file:
50-
values_file.write(values_yaml_content)
51-
52-
# Create template files and directories
53-
for template in template_subdirectories:
54-
template_path = os.path.join(project_name, 'templates', template)
55-
os.makedirs(template_path, exist_ok=True)
42+
# Create template files
43+
for template in directories[1:]:
5644
for template_file in template_files:
57-
with open(os.path.join(template_path, template_file), 'w') as file:
58-
file.write(f"# {template_file} for {template}\n")
59-
60-
# Create secret.yaml if env variable is considered
61-
with open(os.path.join(template_path, 'secret.yaml'), 'w') as secret_file:
62-
secret_file.write("# secret.yaml for environment variables\n")
63-
64-
# Create helpers.tpl
65-
with open(os.path.join(template_path, 'helpers.tpl'), 'w') as helpers_file:
66-
helpers_file.write(f"""{{- define "{template}.labels" -}}
67-
name: {{ .Release.Name }}
68-
{{- end -}}
69-
// Add additional helper functions if needed
45+
with open(os.path.join(project_name, template, template_file), 'w') as f:
46+
if template_file == "service.yaml":
47+
f.write("""apiVersion: v1
48+
kind: Service
49+
metadata:
50+
name: {{ include "{{ .Chart.Name }}.fullname" . }}
51+
spec:
52+
type: ClusterIP
53+
ports:
54+
- port: {{ .Values.web.service.port }}
55+
targetPort: {{ .Values.web.service.port }}
56+
selector:
57+
app: {{ include "{{ .Chart.Name }}.fullname" . }}
58+
""")
59+
elif template_file == "secret.yaml":
60+
f.write("""apiVersion: v1
61+
kind: Secret
62+
metadata:
63+
name: {{ include "{{ .Chart.Name }}.fullname" . }}-secret
64+
type: Opaque
65+
data:
66+
ENV1: {{ .Values.web.env | toJson | b64enc | quote }}
7067
""")

app/media/MyHelm/templates/web/helpers.tpl

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
# secret.yaml for environment variables
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: {{ include "{{ .Chart.Name }}.fullname" . }}-secret
5+
type: Opaque
6+
data:
7+
ENV1: {{ .Values.web.env | toJson | b64enc | quote }}
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
# service.yaml for web
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: {{ include "{{ .Chart.Name }}.fullname" . }}
5+
spec:
6+
type: ClusterIP
7+
ports:
8+
- port: {{ .Values.web.service.port }}
9+
targetPort: {{ .Values.web.service.port }}
10+
selector:
11+
app: {{ include "{{ .Chart.Name }}.fullname" . }}

app/media/MyHelm/values.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ web:
22
image: nginx
33
replicas: 1
44
service:
5-
targetPort: 80
5+
port: 80
66
persistence:
7-
enabled: true
87
size: 1Gi
98
accessModes:
109
- ReadWriteOnce

app/prompt_generators.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,18 @@ def helm_template_generator(input : HelmTemplateGeneration) -> str:
8282
for each template, initialize this file => service.yaml.
8383
set replicas of pods following this dict format : {replicas_}.
8484
set persistance (pvc) of pods following this dict fomrat : {persistance}
85-
set environment variables of pods like this dict format : {envs}
85+
set environment variables of pods following this dict format : {envs} based on helm standard environment setting.(
86+
for example something like that:
87+
env:
88+
name=value
89+
)
8690
initialize ingress with a default host for pod if the pod ingress is true in here {ingress_}.
8791
8892
if environment variable is considered for pod, then create secret.yaml in the related template.
93+
creating secret.yaml based on environemt variables in the {envs} in the template is very important.
8994
9095
please set a something default in chart.yaml and values.yaml based on the requirement.
91-
please generate helpers.tpl for each template and initialize resources and set a label for reach resource.
92-
96+
9397
just Generate a python code without any additional notes or ```python3 entry
9498
"""
9599
return prompt

0 commit comments

Comments
 (0)