Skip to content

Commit

Permalink
fix: Merge pull request #19 from abolfazl8131/develop
Browse files Browse the repository at this point in the history
update helm prompt
  • Loading branch information
abolfazl8131 authored Oct 31, 2024
2 parents 4c8ec5d + b21f30c commit 15b354c
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 43 deletions.
Binary file modified app/__pycache__/models.cpython-311.pyc
Binary file not shown.
Binary file modified app/__pycache__/prompt_generators.cpython-311.pyc
Binary file not shown.
107 changes: 79 additions & 28 deletions app/directory_generators/helm_generator.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,86 @@
import os
import yaml

# Project structure
project_name = "MyHelm"
base_dir = "app/media"
project_path = os.path.join(base_dir, project_name)

# Define directories and files
dirs = [
os.path.join(project_path, "charts"),
os.path.join(project_path, "crds"),
os.path.join(project_path, "templates", "web"),
]

files = {
"Chart.yaml": """apiVersion: v1
name: MyHelm
description: A Helm chart for Kubernetes
version: 0.1.0
""",
"values.yaml": """image:
repository: rembg
tag: latest
pullPolicy: IfNotPresent
""",
project_dir = os.path.join(base_dir, project_name)

# Directories to create
dirs = ["charts", "crds", "templates/web"]

# Creating the directory structure
for dir in dirs:
os.makedirs(os.path.join(project_dir, dir), exist_ok=True)

# Chart.yaml content
chart_yaml = {
"apiVersion": "v1",
"name": project_name,
"version": "0.1.0",
"description": "A Helm chart for MyHelm",
"maintainers": [{"name": "Your Name", "email": "youremail@example.com"}],
"keywords": ["helm", "chart"],
"home": "https://example.com",
"sources": ["https://github.com/example/MyHelm"]
}

# Create project structure
os.makedirs(project_path, exist_ok=True)
for d in dirs:
os.makedirs(d, exist_ok=True)
# Writing Chart.yaml
with open(os.path.join(project_dir, "Chart.yaml"), 'w') as chart_file:
yaml.dump(chart_yaml, chart_file)

# values.yaml content based on provided information
values_yaml = {
"web": {
"image": "nginx",
"service": {
"enabled": True,
"port": 80
}
}
}

# Writing values.yaml
with open(os.path.join(project_dir, "values.yaml"), 'w') as values_file:
yaml.dump(values_yaml, values_file)

# Template files content
deployment_yaml = """apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: {{ .Values.web.image }}
ports:
- containerPort: {{ .Values.web.service.port }}
"""

service_yaml = """apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: ClusterIP
ports:
- port: {{ .Values.web.service.port }}
selector:
app: web
"""

# Creating deployment.yaml and service.yaml in templates/web
with open(os.path.join(project_dir, "templates/web/deployment.yaml"), 'w') as dep_file:
dep_file.write(deployment_yaml)

# Create files with default content
for file_name, content in files.items():
with open(os.path.join(project_path, file_name), 'w') as f:
f.write(content)
with open(os.path.join(project_dir, "templates/web/service.yaml"), 'w') as svc_file:
svc_file.write(service_yaml)
11 changes: 10 additions & 1 deletion app/media/MyHelm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
apiVersion: v1
description: A Helm chart for MyHelm
home: https://example.com
keywords:
- helm
- chart
maintainers:
- email: youremail@example.com
name: Your Name
name: MyHelm
description: A Helm chart for Kubernetes
sources:
- https://github.com/example/MyHelm
version: 0.1.0
19 changes: 19 additions & 0 deletions app/media/MyHelm/templates/web/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: {{ .Values.web.image }}
ports:
- containerPort: {{ .Values.web.service.port }}
10 changes: 10 additions & 0 deletions app/media/MyHelm/templates/web/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: ClusterIP
ports:
- port: {{ .Values.web.service.port }}
selector:
app: web
9 changes: 5 additions & 4 deletions app/media/MyHelm/values.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
image:
repository: rembg
tag: latest
pullPolicy: IfNotPresent
web:
image: nginx
service:
enabled: true
port: 80
14 changes: 8 additions & 6 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from pydantic import BaseModel
from typing import Optional

from typing import List, Optional

class BasicInput(BaseModel):

Expand All @@ -26,12 +25,15 @@ class IaCTemplateGeneration(BaseModel):
base_config:str = 'ec2'
service:str = 'terraform'

class Pod(BaseModel):
name:str
image:str
target_port:int

class HelmTemplateGeneration(BaseModel):
CI_integration:bool = True
api_version:int = 1
templates:list[str]
images:list[str]

pods:List[Pod]




Expand Down
13 changes: 9 additions & 4 deletions app/prompt_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,23 @@ def IaC_template_generator(input : IaCTemplateGeneration) -> str:
return prompt

def helm_template_generator(input : HelmTemplateGeneration) -> str:

templates = [i.name for i in input.pods]
docker_images = [{i.name:i.image} for i in input.pods]
target_ports = [{i.name:i.target_port} for i in input.pods]
prompt = f"""
generate a correct python code to generate a helm project structure (project name: app/media/MyHelm)
based on the latest version of helm chart.
just generate a code to generate a folder as project template. don't consider base_dir
CI integrated (using github actions) = {input.CI_integration}.
consider these directories : [charts/, crds/, templates/]
consider these files : Chart.yaml & values.yaml
in the templates/ directory create these directories: {input.templates}.
in the templates/ directory create these directories: {templates}.
set the api_version in the Chart.yaml : v{input.api_version}.
initialize values.yaml based on these docker images : {input.images}
initialize values.yaml based on these dict of templates and docker images,
please provide other informations related to values.yaml : {docker_images},
the target port of pods in the dict format are here : {target_ports}
for each template, initialize this files [deployment.yaml ,service.yaml].
please set a something default in chart.yaml and values.yaml
Expand Down

0 comments on commit 15b354c

Please sign in to comment.