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-prompt): fix a little problem of statefull or deployment gen… #52

Merged
merged 1 commit into from
Nov 10, 2024
Merged
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__/prompt_generators.cpython-311.pyc
Binary file not shown.
272 changes: 113 additions & 159 deletions app/directory_generators/helm_generator.py
Original file line number Diff line number Diff line change
@@ -1,162 +1,116 @@
import os

project_name = "app/media/MyHelm"
chart_structure = {
"charts": {},
"templates": {
"web": {}
}
}

values_data = {
'web': {
'image': 'nginx',
'targetPort': 80,
'replicaCount': 1,
'persistence': {
'size': '1Gi',
'accessModes': 'ReadWriteOnce'
},
'env': [
{'name': 'ENV1', 'value': 'Hi'}
],
'ingress': {
'enabled': False,
'host': 'www.example.com'
},
'stateless': True
}
}

def create_file(path, content=""):
with open(path, 'w') as file:
file.write(content)

os.makedirs(os.path.join(project_name, "charts"), exist_ok=True)
os.makedirs(os.path.join(project_name, "templates", "web"), exist_ok=True)

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

values_yaml_content = """web:
image: nginx
targetPort: 80
replicaCount: 1
persistence:
size: 1Gi
accessModes:
- ReadWriteOnce
env:
- name: ENV1
value: Hi
ingress:
enabled: false
host: www.example.com
stateless: true
"""

create_file(os.path.join(project_name, "Chart.yaml"), chart_yaml_content)
create_file(os.path.join(project_name, "values.yaml"), values_yaml_content)

deployment_yaml_content = """apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-web
spec:
replicas: {{ .Values.web.replicaCount }}
selector:
matchLabels:
app: {{ .Release.Name }}-web
template:
metadata:
labels:
app: {{ .Release.Name }}-web
spec:
containers:
- name: web
image: {{ .Values.web.image }}
ports:
- containerPort: {{ .Values.web.targetPort }}
env:
{{- range .Values.web.env }}
- name: {{ .name }}
value: {{ .value | quote }}
{{- end }}
"""

service_yaml_content = """apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}-web
spec:
ports:
- port: {{ .Values.web.targetPort }}
selector:
app: {{ .Release.Name }}-web
"""

secrets_yaml_content = """apiVersion: v1
kind: Secret
metadata:
name: {{ .Release.Name }}-secret
type: Opaque
data:
example-key: {{ .Values.secret.exampleKey | b64enc | quote }}
"""

create_file(os.path.join(project_name, "templates", "web", "deployment.yaml"), deployment_yaml_content)
create_file(os.path.join(project_name, "templates", "web", "service.yaml"), service_yaml_content)
create_file(os.path.join(project_name, "templates", "web", "secrets.yaml"), secrets_yaml_content)

if values_data['web']['stateless']:
create_file(os.path.join(project_name, "templates", "web", "statefulset.yaml"), "")
else:
create_file(os.path.join(project_name, "templates", "web", "statefulset.yaml"), "")

if values_data['web']['ingress']['enabled']:
ingress_yaml_content = """apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ .Release.Name }}-web
spec:
rules:
- host: {{ .Values.web.ingress.host }}
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: {{ .Release.Name }}-web
port:
number: {{ .Values.web.targetPort }}
"""
create_file(os.path.join(project_name, "templates", "web", "ingress.yaml"), ingress_yaml_content)

pvc_yaml_content = """apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ .Release.Name }}-web
spec:
accessModes:
- {{ .Values.web.persistence.accessModes | first }}
resources:
requests:
storage: {{ .Values.web.persistence.size }}
"""

create_file(os.path.join(project_name, "templates", "web", "pvc.yaml"), pvc_yaml_content)

helpers_tpl_content = """{{/*
Common utility functions for templates
*/}}

{{- define "mychart.name" -}}
{{ .Release.Name }}-{{ .Chart.Name }}
{{- end -}}
"""

create_file(os.path.join(project_name, "templates", "web", "helpers.tpl"), helpers_tpl_content)
charts_dir = os.path.join(project_name, "charts")
templates_dir = os.path.join(project_name, "templates")
web_dir = os.path.join(templates_dir, "web")

# Create project directories
os.makedirs(charts_dir, exist_ok=True)
os.makedirs(templates_dir, exist_ok=True)
os.makedirs(web_dir, exist_ok=True)

# Create Chart.yaml
with open(os.path.join(project_name, "Chart.yaml"), "w") as chart_file:
chart_file.write("apiVersion: v2\n")
chart_file.write("name: MyHelm\n")
chart_file.write("description: A Helm chart for Kubernetes\n")
chart_file.write("version: 0.1.0\n")

# Create values.yaml
with open(os.path.join(project_name, "values.yaml"), "w") as values_file:
values_file.write("web:\n")
values_file.write(" image: nginx\n")
values_file.write(" targetPort: 80\n")
values_file.write(" replicas: 1\n")
values_file.write(" persistence:\n")
values_file.write(" size: 1Gi\n")
values_file.write(" accessModes:\n")
values_file.write(" - ReadWriteOnce\n")
values_file.write(" env:\n")
values_file.write(" - name: ENV1\n")
values_file.write(" value: Hi\n")
values_file.write(" ingress:\n")
values_file.write(" enabled: false\n")
values_file.write(" host: www.example.com\n")
values_file.write(" stateless:\n")
values_file.write(" enabled: false\n")

# Create service.yaml
with open(os.path.join(web_dir, "service.yaml"), "w") as service_file:
service_file.write("apiVersion: v1\n")
service_file.write("kind: Service\n")
service_file.write("metadata:\n")
service_file.write(" name: web-service\n")
service_file.write("spec:\n")
service_file.write(" type: ClusterIP\n")
service_file.write(" ports:\n")
service_file.write(" - port: 80\n")
service_file.write(" targetPort: {{ .Values.web.targetPort }}\n")
service_file.write(" selector:\n")
service_file.write(" app: web\n")

# Create statefulset.yaml (since stateless is false)
with open(os.path.join(web_dir, "statefulset.yaml"), "w") as statefulset_file:
statefulset_file.write("apiVersion: apps/v1\n")
statefulset_file.write("kind: StatefulSet\n")
statefulset_file.write("metadata:\n")
statefulset_file.write(" name: web\n")
statefulset_file.write("spec:\n")
statefulset_file.write(" serviceName: web-service\n")
statefulset_file.write(" replicas: {{ .Values.web.replicas }}\n")
statefulset_file.write(" selector:\n")
statefulset_file.write(" matchLabels:\n")
statefulset_file.write(" app: web\n")
statefulset_file.write(" template:\n")
statefulset_file.write(" metadata:\n")
statefulset_file.write(" labels:\n")
statefulset_file.write(" app: web\n")
statefulset_file.write(" spec:\n")
statefulset_file.write(" containers:\n")
statefulset_file.write(" - name: web\n")
statefulset_file.write(" image: {{ .Values.web.image }}\n")
statefulset_file.write(" ports:\n")
statefulset_file.write(" - containerPort: {{ .Values.web.targetPort }}\n")
statefulset_file.write(" env:\n")
statefulset_file.write(" - name: {{ .Values.web.env[0].name }}\n")
statefulset_file.write(" value: {{ .Values.web.env[0].value }}\n")
statefulset_file.write(" volumeClaimTemplates:\n")
statefulset_file.write(" - metadata:\n")
statefulset_file.write(" name: web-pvc\n")
statefulset_file.write(" spec:\n")
statefulset_file.write(" accessModes:\n")
statefulset_file.write(" - {{ .Values.web.persistence.accessModes[0] }}\n")
statefulset_file.write(" resources:\n")
statefulset_file.write(" requests:\n")
statefulset_file.write(" storage: {{ .Values.web.persistence.size }}\n")

# Create pvc.yaml
with open(os.path.join(web_dir, "pvc.yaml"), "w") as pvc_file:
pvc_file.write("apiVersion: v1\n")
pvc_file.write("kind: PersistentVolumeClaim\n")
pvc_file.write("metadata:\n")
pvc_file.write(" name: web-pvc\n")
pvc_file.write("spec:\n")
pvc_file.write(" accessModes:\n")
pvc_file.write(" - {{ .Values.web.persistence.accessModes[0] }}\n")
pvc_file.write(" resources:\n")
pvc_file.write(" requests:\n")
pvc_file.write(" storage: {{ .Values.web.persistence.size }}\n")

# Create secrets.yaml
with open(os.path.join(web_dir, "secrets.yaml"), "w") as secrets_file:
secrets_file.write("apiVersion: v1\n")
secrets_file.write("kind: Secret\n")
secrets_file.write("metadata:\n")
secrets_file.write(" name: web-secrets\n")
secrets_file.write("type: Opaque\n")
secrets_file.write("data:\n")
secrets_file.write(" # Add your base64 encoded secrets here\n")

# Create helpers.tpl
with open(os.path.join(web_dir, "helpers.tpl"), "w") as helpers_file:
helpers_file.write("# Define any helpers here\n")
helpers_file.write("define \"web.fullname\" \n")
helpers_file.write(" {{ .Release.Name }}-{{ .Values.web.name }} \n")
helpers_file.write("end\n")
2 changes: 1 addition & 1 deletion app/media/MyHelm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: v2
name: mychart
name: MyHelm
description: A Helm chart for Kubernetes
version: 0.1.0
24 changes: 0 additions & 24 deletions app/media/MyHelm/templates/web/deployment.yaml

This file was deleted.

11 changes: 4 additions & 7 deletions app/media/MyHelm/templates/web/helpers.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{{/*
Common utility functions for templates
*/}}

{{- define "mychart.name" -}}
{{ .Release.Name }}-{{ .Chart.Name }}
{{- end -}}
# Define any helpers here
define "web.fullname"
{{ .Release.Name }}-{{ .Values.web.name }}
end
4 changes: 2 additions & 2 deletions app/media/MyHelm/templates/web/pvc.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ .Release.Name }}-web
name: web-pvc
spec:
accessModes:
- {{ .Values.web.persistence.accessModes | first }}
- {{ .Values.web.persistence.accessModes[0] }}
resources:
requests:
storage: {{ .Values.web.persistence.size }}
4 changes: 2 additions & 2 deletions app/media/MyHelm/templates/web/secrets.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: {{ .Release.Name }}-secret
name: web-secrets
type: Opaque
data:
example-key: {{ .Values.secret.exampleKey | b64enc | quote }}
# Add your base64 encoded secrets here
8 changes: 5 additions & 3 deletions app/media/MyHelm/templates/web/service.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}-web
name: web-service
spec:
type: ClusterIP
ports:
- port: {{ .Values.web.targetPort }}
- port: 80
targetPort: {{ .Values.web.targetPort }}
selector:
app: {{ .Release.Name }}-web
app: web
32 changes: 32 additions & 0 deletions app/media/MyHelm/templates/web/statefulset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: web-service
replicas: {{ .Values.web.replicas }}
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: {{ .Values.web.image }}
ports:
- containerPort: {{ .Values.web.targetPort }}
env:
- name: {{ .Values.web.env[0].name }}
value: {{ .Values.web.env[0].value }}
volumeClaimTemplates:
- metadata:
name: web-pvc
spec:
accessModes:
- {{ .Values.web.persistence.accessModes[0] }}
resources:
requests:
storage: {{ .Values.web.persistence.size }}
Loading
Loading