Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(helm): fix helm prompt #47

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified app/__pycache__/gpt_services.cpython-311.pyc
Binary file not shown.
Binary file modified app/__pycache__/prompt_generators.cpython-311.pyc
Binary file not shown.
Binary file modified app/__pycache__/services.cpython-311.pyc
Binary file not shown.
202 changes: 116 additions & 86 deletions app/directory_generators/helm_generator.py
Original file line number Diff line number Diff line change
@@ -1,112 +1,142 @@
import os

def create_helm_project_structure(base_path):
project_path = os.path.join(base_path, 'app/media/MyHelm')
os.makedirs(os.path.join(project_path, 'charts'), exist_ok=True)
os.makedirs(os.path.join(project_path, 'templates', 'web'), exist_ok=True)
# Define project structure
project_name = 'app/media/MyHelm'
directories = ['charts', 'templates/web']
files = ['Chart.yaml', 'values.yaml']
template_files = ['service.yaml', 'secrets.yaml', 'helpers.tpl']
stateless = False
persistence = True
docker_images = {'web': 'nginx'}
target_ports = {'web': 80}
replicas = {'web': 1}
pvc = {'web': {'size': '1Gi', 'accessModes': ['ReadWriteOnce']}}
environment = {'web': [{'name': 'ENV1', 'value': 'Hi'}]}
ingress = {'web': {'enabled': False, 'host': 'www.example.com'}}

chart_yaml_content = """apiVersion: v2
name: MyHelm
description: A Helm chart for Kubernetes
version: 0.1.0
"""

values_yaml_content = """web:
image: nginx
service:
targetPort: 80
replicas: 1
persistence:
size: 1Gi
accessModes:
- ReadWriteOnce
env:
- name: ENV1
value: Hi
ingress:
enabled: false
host: www.example.com
"""
# Create directories
for directory in directories:
os.makedirs(os.path.join(project_name, directory), exist_ok=True)

service_yaml_content = """apiVersion: v1
# Create base files
with open(os.path.join(project_name, 'Chart.yaml'), 'w') as chart_file:
chart_file.write("apiVersion: v2\n")

with open(os.path.join(project_name, 'values.yaml'), 'w') as values_file:
values_content = f"web:\n image: {docker_images['web']}\n service:\n port: {target_ports['web']}\n"
values_content += f" replicaCount: {replicas['web']}\n"
if persistence:
values_content += f" persistence:\n size: {pvc['web']['size']}\n accessModes: {pvc['web']['accessModes'][0]}\n"
values_content += " env:\n"
for env in environment['web']:
values_content += f" - name: {env['name']}\n value: {env['value']}\n"
values_content += f" ingress:\n enabled: {ingress['web']['enabled']}\n host: {ingress['web']['host']}\n"
values_content += f" stateless: {stateless}\n"
values_file.write(values_content)

# Create template files
for template in template_files:
with open(os.path.join(project_name, 'templates/web', template), 'w') as temp_file:
if template == 'service.yaml':
temp_file.write("""
apiVersion: v1
kind: Service
metadata:
name: {{ include "MyHelm.fullname" . }}-web
name: {{ .Release.Name }}-web
spec:
type: ClusterIP
ports:
- port: {{ .Values.web.service.targetPort }}
- port: {{ .Values.web.service.port }}
selector:
app: {{ include "MyHelm.name" . }}
"""
app: {{ .Release.Name }}-web
""")
elif template == 'secrets.yaml':
temp_file.write("""
apiVersion: v1
kind: Secret
metadata:
name: {{ .Release.Name }}-web-secrets
type: Opaque
data:
ENV1: {{ .Values.web.env[0].value | b64enc }}
""")
elif template == 'helpers.tpl':
temp_file.write("""
{{- define "app.media.MyHelm.fullname" -}}
{{- .Release.Name }}-web
{{- end -}}
""")

deployment_yaml_content = """apiVersion: apps/v1
# Create StatefulSet or Deployment based on statefulness
if stateless:
with open(os.path.join(project_name, 'templates/web/deployment.yaml'), 'w') as deploy_file:
deploy_file.write("""
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "MyHelm.fullname" . }}-web
name: {{ include "app.media.MyHelm.fullname" . }}
spec:
replicas: {{ .Values.web.replicas }}
replicas: {{ .Values.web.replicaCount }}
selector:
matchLabels:
app: {{ include "app.media.MyHelm.fullname" . }}
template:
metadata:
labels:
app: {{ include "MyHelm.name" . }}
app: {{ include "app.media.MyHelm.fullname" . }}
spec:
containers:
- name: web
image: {{ .Values.web.image }}
ports:
- containerPort: {{ .Values.web.service.targetPort }}
- containerPort: {{ .Values.web.service.port }}
env:
{{- range .Values.web.env }}
{{- range .Values.web.env }}
- name: {{ .name }}
value: {{ .value }}
{{- end }}
"""

secret_yaml_content = """apiVersion: v1
kind: Secret
{{- end }}
""")
else:
with open(os.path.join(project_name, 'templates/web/statefulset.yaml'), 'w') as stateful_file:
stateful_file.write("""
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: {{ include "MyHelm.fullname" . }}-web-env
type: Opaque
data:
ENV1: {{ .Values.web.env | toJson | b64enc | quote }}
"""

helpers_tpl_content = """{{/*
Expand the name of the chart.
*/}}
{{- define "MyHelm.name" -}}
{{- .Chart.Name | replace "-" "_" | lower -}}
{{- end -}}

{{/*
Create a default fully qualified domain name
*/}}
{{- define "MyHelm.fullname" -}}
{{- if .Chart.Name -}}
{{- .Release.Name | lower | replace "-" "_" | trimSuffix "-" | append (include "MyHelm.name" . | lower) | toLower -}}
{{- else -}}
{{- .Release.Name | lower -}}
{{- end -}}
{{- end -}}
"""

with open(os.path.join(project_path, 'Chart.yaml'), 'w') as file:
file.write(chart_yaml_content)

with open(os.path.join(project_path, 'values.yaml'), 'w') as file:
file.write(values_yaml_content)

with open(os.path.join(project_path, 'templates', 'web', 'service.yaml'), 'w') as file:
file.write(service_yaml_content)

with open(os.path.join(project_path, 'templates', 'web', 'deployment.yaml'), 'w') as file:
file.write(deployment_yaml_content)

with open(os.path.join(project_path, 'templates', 'web', 'secret.yaml'), 'w') as file:
file.write(secret_yaml_content)

with open(os.path.join(project_path, 'templates', 'web', 'helpers.tpl'), 'w') as file:
file.write(helpers_tpl_content)
name: {{ include "app.media.MyHelm.fullname" . }}
spec:
serviceName: {{ include "app.media.MyHelm.fullname" . }}
replicas: {{ .Values.web.replicaCount }}
selector:
matchLabels:
app: {{ include "app.media.MyHelm.fullname" . }}
template:
metadata:
labels:
app: {{ include "app.media.MyHelm.fullname" . }}
spec:
containers:
- name: web
image: {{ .Values.web.image }}
ports:
- containerPort: {{ .Values.web.service.port }}
env:
{{- range .Values.web.env }}
- name: {{ .name }}
value: {{ .value }}
{{- end }}
""")

create_helm_project_structure('.')
# Create PVC if persistence is configured
if persistence:
with open(os.path.join(project_name, 'templates/web/pvc.yaml'), 'w') as pvc_file:
pvc_file.write("""
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ include "app.media.MyHelm.fullname" . }}-pvc
spec:
accessModes:
- {{ .Values.web.persistence.accessModes }}
resources:
requests:
storage: {{ .Values.web.persistence.size }}
""")
Loading
Loading