Skip to content

Instantly share code, notes, and snippets.

@ant31
Last active May 12, 2017 20:08
Show Gist options
  • Save ant31/882b642e8f6c29123919d92eca993590 to your computer and use it in GitHub Desktop.
Save ant31/882b642e8f6c29123919d92eca993590 to your computer and use it in GitHub Desktop.
jsonnet + opencompose realexample
local opencompose = import "lib/opencompose.libsonnet";
function(params={}, namespace="default")
opencompose.createServices({
web: {
image: "quay.io/ant31/cookiapp:0.5.0",
domain: "cookieapp.kubespray.com",
env: {"database": "postgres://postgres.default.svc"},
ports: [{name: "http", port: 80}],
},
database: {
image: "postgresql:latest",
ports: [{name: "pg", port: 5432, expose: false}]
},
})
# $ jsonnet cookieapp-opencompose.jsonnet
## WEB-app
- apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations: {}
labels: {}
name: web
spec:
rules:
- host: cookieapp.kubespray.com
http:
backend: {serviceName: web, servicePort: 80}
tls: []
- apiVersion: v1
kind: Service
metadata:
annotations: {}
labels: {}
name: web
spec:
ports: [80]
selector: {app: web}
- apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations: {}
labels: {}
name: web
spec:
replicas: 2
template:
metadata:
annotations: {}
labels: {app: web}
spec:
containers:
- env:
- {name: database, value: postgresql.default.svc.cluster.local}
image: quay.io/ant31/cookiapp:0.5.0
imagePullPolicy: Always
name: web
ports: []
volumeMounts: []
volumes: []
## DATABASE
- apiVersion: v1
kind: Service
metadata:
annotations: {}
labels: {}
name: database
spec:
ports: [5432]
selector: {app: database}
- apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations: {}
labels: {}
name: database
spec:
replicas: 2
template:
metadata:
annotations: {}
labels: {app: database}
spec:
containers:
- env: []
image: postgresql:latest
imagePullPolicy: Always
name: database
ports:
- {containerPort: 5432, name: pg}
volumeMounts: []
volumes: []
local core = import "core.libsonnet";
local kubeUtil = import "util.libsonnet";
local container = core.v1.container;
local deployment = kubeUtil.app.v1beta1.deployment;
local service = core.v1.service;
local ingress = core.extensions.v1beta1.ingress;
local env = core.v1.env + kubeUtil.app.v1.env;
{
local openlib = self,
compact(array):: (
[x for x in array if x != null]
),
createIngress(name, params):: (
if std.objectHas(params, "domain") then
ingress.Default(name) +
ingress.mixin.spec.Rule(params['domain'],
ingress.httpIngressPath.Default(name, params['ports'][0].port))
else
null
),
createSvc(name, params):: (
if std.objectHas(params, 'ports') then
service.Default(name, [params['ports'][0].port],) +
service.mixin.spec.Selector({ app: name })
),
createServices(services)::
openlib.compact(std.flattenArrays(
[openlib.createApp(service_name, services[service_name]),
for service_name in std.objectFields(services)],
)),
createApp(name, params)::
local containerApp =
container.Default(name, params["image"]) +
if std.objectHas(params, 'env') then
container.Env(env.array.FromObj(params["env"])) else {} +
if std.objectHas(params, 'ports') then
container.NamedPort(params['ports'][0].name,
params['ports'][0].port) else {} ;
local deployApp = deployment.FromContainer(name, 2, containerApp);
local svcApp = openlib.createSvc(name, params);
local ingressApp = openlib.createIngress(name, params);
[ingressApp, svcApp, deployApp],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment