From 8f17284b308617ba626d34c897879918d157d1b7 Mon Sep 17 00:00:00 2001 From: abolfazl1381 Date: Sun, 10 Nov 2024 10:40:48 +0330 Subject: [PATCH] fix(helm-prompt): fix a little problem of statefull or deployment generation --- .../prompt_generators.cpython-311.pyc | Bin 8163 -> 8179 bytes app/directory_generators/helm_generator.py | 272 ++++++++---------- app/media/MyHelm/Chart.yaml | 2 +- .../MyHelm/templates/web/deployment.yaml | 24 -- app/media/MyHelm/templates/web/helpers.tpl | 11 +- app/media/MyHelm/templates/web/pvc.yaml | 4 +- app/media/MyHelm/templates/web/secrets.yaml | 4 +- app/media/MyHelm/templates/web/service.yaml | 8 +- .../MyHelm/templates/web/statefulset.yaml | 32 +++ app/media/MyHelm/values.yaml | 7 +- app/prompt_generators.py | 2 +- 11 files changed, 164 insertions(+), 202 deletions(-) delete mode 100644 app/media/MyHelm/templates/web/deployment.yaml diff --git a/app/__pycache__/prompt_generators.cpython-311.pyc b/app/__pycache__/prompt_generators.cpython-311.pyc index 8add4ddb69f9469fc9e843781caa43ea2b9680c1..bcbcb9dfa6fb73b30c7196e0027a3cd93c35e9b3 100644 GIT binary patch delta 55 zcmaEC|Jj~*IWI340}vdFHb`&V$eSq1_-Asy!+7(`_GDZR~(Qkz%--s20f diff --git a/app/directory_generators/helm_generator.py b/app/directory_generators/helm_generator.py index d3ef64c2..4b8dde49 100644 --- a/app/directory_generators/helm_generator.py +++ b/app/directory_generators/helm_generator.py @@ -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) \ No newline at end of file +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") \ No newline at end of file diff --git a/app/media/MyHelm/Chart.yaml b/app/media/MyHelm/Chart.yaml index 59be7b67..95e441a0 100644 --- a/app/media/MyHelm/Chart.yaml +++ b/app/media/MyHelm/Chart.yaml @@ -1,4 +1,4 @@ apiVersion: v2 -name: mychart +name: MyHelm description: A Helm chart for Kubernetes version: 0.1.0 diff --git a/app/media/MyHelm/templates/web/deployment.yaml b/app/media/MyHelm/templates/web/deployment.yaml deleted file mode 100644 index 84e27dcb..00000000 --- a/app/media/MyHelm/templates/web/deployment.yaml +++ /dev/null @@ -1,24 +0,0 @@ -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 }} diff --git a/app/media/MyHelm/templates/web/helpers.tpl b/app/media/MyHelm/templates/web/helpers.tpl index 25e51e5c..506945bd 100644 --- a/app/media/MyHelm/templates/web/helpers.tpl +++ b/app/media/MyHelm/templates/web/helpers.tpl @@ -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 diff --git a/app/media/MyHelm/templates/web/pvc.yaml b/app/media/MyHelm/templates/web/pvc.yaml index 3121c2b3..94f1548e 100644 --- a/app/media/MyHelm/templates/web/pvc.yaml +++ b/app/media/MyHelm/templates/web/pvc.yaml @@ -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 }} diff --git a/app/media/MyHelm/templates/web/secrets.yaml b/app/media/MyHelm/templates/web/secrets.yaml index 811b2d09..4f1b6f8f 100644 --- a/app/media/MyHelm/templates/web/secrets.yaml +++ b/app/media/MyHelm/templates/web/secrets.yaml @@ -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 diff --git a/app/media/MyHelm/templates/web/service.yaml b/app/media/MyHelm/templates/web/service.yaml index d4b1b25a..ced154e4 100644 --- a/app/media/MyHelm/templates/web/service.yaml +++ b/app/media/MyHelm/templates/web/service.yaml @@ -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 diff --git a/app/media/MyHelm/templates/web/statefulset.yaml b/app/media/MyHelm/templates/web/statefulset.yaml index e69de29b..6b98c7d2 100644 --- a/app/media/MyHelm/templates/web/statefulset.yaml +++ b/app/media/MyHelm/templates/web/statefulset.yaml @@ -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 }} diff --git a/app/media/MyHelm/values.yaml b/app/media/MyHelm/values.yaml index 5bea2e8b..db28af7c 100644 --- a/app/media/MyHelm/values.yaml +++ b/app/media/MyHelm/values.yaml @@ -1,10 +1,10 @@ web: image: nginx targetPort: 80 - replicaCount: 1 + replicas: 1 persistence: size: 1Gi - accessModes: + accessModes: - ReadWriteOnce env: - name: ENV1 @@ -12,4 +12,5 @@ web: ingress: enabled: false host: www.example.com - stateless: true + stateless: + enabled: false diff --git a/app/prompt_generators.py b/app/prompt_generators.py index 94fb501e..f6285cd4 100644 --- a/app/prompt_generators.py +++ b/app/prompt_generators.py @@ -93,7 +93,7 @@ def helm_template_generator(input : HelmTemplateGeneration) -> str: Based on values.yaml, create all necessary Kubernetes templates in the templates directory: - if stateless is true, create deployment.yaml; if stateless is false, create statefulset.yaml. + if stateless.enabled is true, create deployment.yaml; if stateless.enabled is false, create statefulset.yaml. If a persistence block exists, include pvc.yaml. If the ingress block is defined and ingress.enabled is true, create ingress.yaml. if ingress.enabled is false, do not create ingress.yaml. Always create secrets.yaml for secure data storage.