UNPKG

k8ts

Version:

Powerful framework for building Kubernetes manifests in TypeScript.

148 lines 5.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Container = void 0; exports.make = make; const instruments_1 = require("@k8ts/instruments"); const adapters_1 = require("../../utils/adapters"); const instruments_2 = require("@k8ts/instruments"); const doddle_1 = require("doddle"); const lodash_1 = require("lodash"); const env_1 = require("../../../env"); const default_1 = require("../../../kinds/default"); const mounts_1 = require("./mounts"); const container_ResourcesSpec = instruments_1.ResourcesSpec.make({ cpu: instruments_1.Unit.Cpu, memory: instruments_1.Unit.Data }); class Container extends instruments_2.Resource_Child { subtype; props; __PORTS__; get kind() { return default_1.v1.Pod.Container._; } __needs__() { const a = this.mounts; return (0, lodash_1.mapValues)((0, lodash_1.mapKeys)(a, x => x.path), x => x.mount.volume); } get mounts() { return (0, doddle_1.seq)(Object.entries(this.props.$mounts ?? {})) .map(([path, mount]) => { return { mount: mount, path: path }; }) .toArray() .pull(); } get volumes() { return (0, doddle_1.seq)(this.mounts.map(x => x.mount.volume)) .uniq() .toArray() .pull(); } get ports() { return instruments_1.PortSet.make(this.props.$ports); } __submanifest__() { const self = this; const { $image, $ports, $command, $env } = self.props; const untaggedProps = (0, lodash_1.omitBy)(self.props, (_, k) => k.startsWith("$")); let resourcesObject = self._resources()?.toObject(); const containerPorts = $ports && (0, doddle_1.seq)((0, adapters_1.toContainerPorts)(instruments_1.PortSet.make($ports)).values()) .toArray() .pull(); const env = (0, env_1.Env)($env); for (const [key, value] of env.entries) { if (typeof value !== "object") { continue; } if (!value) { continue; } const backend = value.$backend; if (backend instanceof instruments_2.Resource_Entity) { if (backend.namespace !== self.namespace) { throw new Error(`Environment variable reference "${key}" must be in the same namespace as the container "${self}", but was ${backend}"`); } } } for (const vol of self.volumes) { if (vol.sourceNamespace !== self.namespace) { throw new Error(`Volume reference "${vol}" must be in the same namespace as the container "${self}"`); } } const envFroms = (self.props.$envFrom ?? []).map(ef => { const source = ef.source; if (source.namespace !== self.namespace) { throw new Error(`EnvFrom source reference "${source}" must be in the same namespace as the container "${self}"`); } if (source.is(default_1.v1.Secret._)) { return { secretRef: { optional: ef.optional, name: source.name } }; } else if (source.is(default_1.v1.ConfigMap._)) { return { configMapRef: { optional: ef.optional, name: source.name } }; } else { throw new Error(`EnvFrom source reference "${source}" must be a ConfigMap or Secret, but was ${source.kind}`); } }); const container = { ...untaggedProps, name: self.name, image: $image.toString(), ports: containerPorts, resources: resourcesObject, command: $command?.toArray(), env: (0, env_1.Env)($env).toEnvVars(), envFrom: envFroms, ...self._groupedMounts() }; return container; } constructor(parent, name, subtype, props) { super(parent, name, props); this.subtype = subtype; this.props = props; } _groupedMounts() { const x = { volumeMounts: [], volumeDevices: [] }; for (const mnt of this.mounts) { const { mount, path } = mnt; if (mount instanceof mounts_1.Container_Mount_Device) { x.volumeDevices.push(mount["__submanifest__"](path)); } else { x.volumeMounts.push(mount["__submanifest__"](path)); } } return x; } _resources() { if (!this.props.$resources) { return undefined; } const result = container_ResourcesSpec.parse(this.props.$resources); return result; } } exports.Container = Container; function make(parent, name, subtype, props) { return new Container(parent, name, subtype, props); } //# sourceMappingURL=container.js.map