Skip to content

Commit

Permalink
Feature/k8s support (#15)
Browse files Browse the repository at this point in the history
* Minikube and GCP k8s generation in the backend

* UI features to allow platform selection

* Full platform set

* Breaking up structure

* Standard break-up of more code

* Added K8s secret instructions

* Update K8s launch instructions

* Tweak podman/docker instructions to be specific to platform

* Fix typescript types

* Pass through platform and version to generator

* Get rid of ../resources hack

* Import templates from trustgraph, 0.13

* Bedrock works
  • Loading branch information
cybermaggedon authored Oct 15, 2024
1 parent d95f613 commit a4be388
Show file tree
Hide file tree
Showing 27 changed files with 1,345 additions and 419 deletions.
144 changes: 104 additions & 40 deletions config-ui/config_ui/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def __init__(self, **config):
self.app = web.Application(middlewares=[])

self.app.add_routes([web.post("/api/generate", self.generate)])
self.app.add_routes([
web.post("/api/generate/{platform}/{version}", self.generate)
])
self.app.add_routes([web.get("/{tail:.*}", self.everything)])

self.ui = importlib.resources.files().joinpath("ui")
Expand Down Expand Up @@ -84,12 +87,15 @@ async def everything(self, request):
raise web.HTTPInternalServerError()

def process(
self, config, version="0.12.5", platform="docker-compose",
self, config, version="0.0.0", platform="docker-compose",
):

config = config.encode("utf-8")

gen = Generator(config, base=self.templates, version=version)
gen = Generator(
config, templates=self.templates, resources=self.resources,
version=version
)

path = self.templates.joinpath(
f"config-to-{platform}.jsonnet"
Expand All @@ -104,6 +110,18 @@ async def generate(self, request):

logger.info("Generate...")

try:
platform = request.match_info["platform"]
except:
platform = "docker-compose"

try:
version = request.match_info["version"]
except:
version = "0.0.0"

logger.info(f"Generating for platform={platform} version={version}")

try:

config = await request.text()
Expand All @@ -115,62 +133,108 @@ async def generate(self, request):
config = json.dumps(dec)
except:
# Incorrectly formatted stuff is not our problem,
logger.info(f"Bad JSON")
return web.HTTPBadRequest()

logger.info(f"Config: {config}")

processed = self.process(config)
y = yaml.dump(processed)

mem = BytesIO()
if platform in set(["docker-compose", "podman-compose"]):
return await self.generate_docker_compose(
"docker-compose", version, config
)
elif platform in set(["minikube-k8s", "gcp-k8s"]):
return await self.generate_k8s(
platform, version, config
)
else:
return web.HTTPBadRequest()

with zipfile.ZipFile(mem, mode='w') as out:
except Exception as e:
logging.error(f"Exception: {e}")
return web.HTTPInternalServerError()

def output(name, content):
logger.info(f"Adding {name}...")
out.writestr(name, content)
async def generate_docker_compose(self, platform, version, config):

fname = "docker-compose.yaml"
processed = self.process(
config, platform=platform, version=version
)

output(fname, y)
y = yaml.dump(processed)

# Grafana config
path = self.resources.joinpath(
"grafana/dashboards/dashboard.json"
)
res = path.read_text()
output("grafana/dashboards/dashboard.json", res)
mem = BytesIO()

path = self.resources.joinpath(
"grafana/provisioning/dashboard.yml"
)
res = path.read_text()
output("grafana/provisioning/dashboard.yml", res)
with zipfile.ZipFile(mem, mode='w') as out:

path = self.resources.joinpath(
"grafana/provisioning/datasource.yml"
)
res = path.read_text()
output("grafana/provisioning/datasource.yml", res)
def output(name, content):
logger.info(f"Adding {name}...")
out.writestr(name, content)

# Prometheus config
path = self.resources.joinpath(
"prometheus/prometheus.yml"
)
res = path.read_text()
output("prometheus/prometheus.yml", res)
fname = "docker-compose.yaml"

logger.info("Generation complete.")
output(fname, y)

return web.Response(
body=mem.getvalue(),
content_type = "application/octet-stream"
# Grafana config
path = self.resources.joinpath(
"grafana/dashboards/dashboard.json"
)
res = path.read_text()
output("grafana/dashboards/dashboard.json", res)

except Exception as e:
logging.error(f"Exception: {e}")
return web.HTTPInternalServerError()
path = self.resources.joinpath(
"grafana/provisioning/dashboard.yml"
)
res = path.read_text()
output("grafana/provisioning/dashboard.yml", res)

path = self.resources.joinpath(
"grafana/provisioning/datasource.yml"
)
res = path.read_text()
output("grafana/provisioning/datasource.yml", res)

# Prometheus config
path = self.resources.joinpath(
"prometheus/prometheus.yml"
)
res = path.read_text()
output("prometheus/prometheus.yml", res)

logger.info("Generation complete.")

return web.Response(
body=mem.getvalue(),
content_type = "application/octet-stream"
)

async def generate_k8s(self, platform, version, config):

processed = self.process(
config, platform=platform, version=version
)

y = yaml.dump(processed)

mem = BytesIO()

with zipfile.ZipFile(mem, mode='w') as out:

def output(name, content):
logger.info(f"Adding {name}...")
out.writestr(name, content)

fname = "resources.yaml"

output(fname, y)

logger.info("Generation complete.")

return web.Response(
body=mem.getvalue(),
content_type = "application/octet-stream"
)

def run(self):

web.run_app(self.app, port=self.port)

27 changes: 17 additions & 10 deletions config-ui/config_ui/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@
class Generator:

def __init__(
self, config, base=None, resources=None,
self, config, templates=None, resources=None,
version="0.0.0"
):

if base:
self.base = base
if templates:
self.templates = templates
else:
self.base = pathlib.Path("templates")
self.templates = pathlib.Path("templates")

if resources:
self.resources = resources
else:
self.resources = pathlib.Path("resources")

self.config = config
self.version = f"\"{version}\"".encode("utf-8")
Expand All @@ -33,21 +38,23 @@ def load(self, dir, filename):
logger.debug("Request jsonnet: %s %s", dir, filename)

if filename == "config.json" and dir == "":
path = self.base.joinpath(dir, filename)
path = self.templates.joinpath(dir, filename)
return str(path), self.config

if filename == "version.jsonnet":
path = self.base.joinpath(dir, filename)
path = self.templates.joinpath(dir, filename)
return str(path), self.version

if dir:
candidates = [
self.base.joinpath(dir, filename),
self.base.joinpath(filename)
self.templates.joinpath(dir, filename),
self.templates.joinpath(filename),
self.resources.joinpath(dir, filename),
self.resources.joinpath(filename),
]
else:
candidates = [
self.base.joinpath(filename)
self.templates.joinpath(filename)
]

try:
Expand All @@ -70,7 +77,7 @@ def load(self, dir, filename):

except:

path = os.path.join(self.base, filename)
path = os.path.join(self.templates, filename)
logger.debug("Try: %s", path)
with open(path, "rb") as f:
logger.debug("Loaded: %s", path)
Expand Down
2 changes: 2 additions & 0 deletions src/simple-editor/SimpleEditor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
}

.deployment {
max-width: 55rem;
}

.variable {
Expand All @@ -28,3 +29,4 @@ pre {
padding: 1rem;
border: 1px solid #e0e0e0;
}

25 changes: 17 additions & 8 deletions src/simple-editor/deployment/Deployment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Box } from '@mui/material';

//import { useModelParamsStore } from '../state/ModelParams';

import DeploymentPlatform from './DeploymentPlatform';
import DeploymentModel from './DeploymentModel';
import DeploymentConfig from './DeploymentConfig';
import DeploymentInstructions from './DeploymentInstructions';
Expand All @@ -19,16 +20,24 @@ const Deployment: React.FC<DeploymentProps> = ({

<>

<Box>
<DeploymentModel/>
</Box>
<Box className="deployment">

<Box>
<DeploymentConfig/>
</Box>
<Box>
<DeploymentPlatform/>
</Box>

<Box>
<DeploymentModel/>
</Box>

<Box>
<DeploymentConfig/>
</Box>

<Box>
<DeploymentInstructions/>
</Box>

<Box>
<DeploymentInstructions/>
</Box>

</>
Expand Down
18 changes: 18 additions & 0 deletions src/simple-editor/deployment/DeploymentCode.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

import React from 'react'

interface DeploymentCodeProps extends React.PropsWithChildren {
children : React.ReactNode;
};

const DeploymentCode : React.FC<DeploymentCodeProps> =
({children}) => {
return (
<pre>
{children}
</pre>
);
}

export default DeploymentCode;

35 changes: 35 additions & 0 deletions src/simple-editor/deployment/DeploymentEnvVars.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

import React from 'react'

interface DeploymentEnvVarsProps {
variables : {
name : string;
value : string;
}[];
};

const DeploymentEnvVars : React.FC<DeploymentEnvVarsProps> =
({variables}) => {

return (
<pre>
{
variables.map(
(va) => {
return (
<React.Fragment key={va.name}>
export {va.name}=<span className="variable">
{va.value}
</span>
<br/>
</React.Fragment>
);
}
)
}
</pre>
);
}

export default DeploymentEnvVars;

Loading

0 comments on commit a4be388

Please sign in to comment.