diff --git a/templates/config-to-k8s.jsonnet b/templates/config-to-k8s.jsonnet new file mode 100644 index 00000000..4ee9d22a --- /dev/null +++ b/templates/config-to-k8s.jsonnet @@ -0,0 +1,19 @@ + +local engine = import "k8s.jsonnet"; +local decode = import "decode-config.jsonnet"; +local components = import "components.jsonnet"; + +// Import config +local config = import "config.json"; + +// Produce patterns from config +local patterns = decode(config); + +// Extract resources usnig the engine +local resources = std.foldl( + function(state, p) state + p.create(engine), + std.objectValues(patterns), + {} +); + +std.manifestYamlDoc(resources, quote_keys=false) diff --git a/templates/k8s.jsonnet b/templates/k8s.jsonnet new file mode 100644 index 00000000..fa981354 --- /dev/null +++ b/templates/k8s.jsonnet @@ -0,0 +1,195 @@ +{ + + container:: function(name) + { + + local container = self, + + name: name, + limits: {}, + reservations: {}, + ports: [], + volumes: [], + + with_image:: function(x) self + { image: x }, + + with_command:: function(x) self + { command: x }, + + with_environment:: function(x) self + { environment: x }, + + with_limits:: function(c, m) self + { limits: { cpus: c, memory: m } }, + + with_reservations:: + function(c, m) self + { reservations: { cpus: c, memory: m } }, + + with_volume_mount:: + function(vol, mnt) + self + { + volumes: super.volumes + [{ + volume: vol.name, mount: mnt + }] + }, + + with_port:: + function(src, dest, name) self + { + ports: super.ports + [ + { src: src, dest: dest, name : name } + ] + }, + + add:: function() { + + resources +: { + [container.name]: { + apiVersion: "apps/v1", + kind: "Deployment", + metadata: { + name: container.name, + namespace: "trustgraph", + labels: { + app: container.name + } + }, + spec: { + replicas: 1, + selector: { + matchLabels: { + app: container.name, + } + } + }, + template: { + metadata: { + labels: { + app: container.name, + } + }, + spec: { + containers: [ + { + name: container.name, + image: container.image, + resources: { + requests: container.reservations, + limits: container.limits + }, + } + ( + if std.length(container.ports) > 0 then + { + ports: [ + { + hostPort: port.src, + containerPort: port.dest, + } + for port in container.ports + ] + } else + {}) + + + (if std.objectHas(container, "command") then + { command: container.command } + else {}) + + (if std.objectHas(container, "environment") then + { environment: [ { + name: e.key, value: e.value + } + for e in + std.objectKeysValues( + container.environment + ) + ] + } + else {}) + + ] + } + }, + } + {} +/* + + + (if std.length(container.volumes) > 0 then + { + volumes: [ + "%s:%s" % [vol.volume, vol.mount] + for vol in container.volumes + ] + } + else {}) +*/ + } + } + + }, + + service:: function(containers) + { + + local service = self, + + name: containers.name, + + with_port:: function(src, dest) self + { port: [src, dest] }, + + add:: function() { + } + + }, + + volume:: function(name) + { + + local volume = self, + + name: name, + + with_size:: function(size) self + { size: size }, + + add:: function() { + volumes +: { + [volume.name]: {} + } + } + + }, + + // FIXME: For K8s + configVolume:: function(name) + { + + local volume = self, + + name: name, + + with_size:: function(size) self + { size: size }, + + add:: function() { + } + + }, + + containers:: function(name, containers) + { + + local cont = self, + + name: name, + containers: containers, + + add:: function() std.foldl( + function(state, c) state + c.add(), + cont.containers, + {} + ), + + }, + + resources:: function(res) + std.foldl( + function(state, c) state + c.add(), + res, + {} + ), + +} +