All files / core/blong-gogo/src ResolutionK8s.ts

85.91% Statements 61/71
100% Branches 3/3
50% Functions 3/6
85.91% Lines 61/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 721x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x                     1x 1x 1x 1x 1x 1x 1x  
import {Internal, type IManifest} from '@feasibleone/blong/types';
 
import type {IResolution} from './Resolution.ts';
 
interface IConfig {
    /** The port `RpcServer` serves an orchestrator namespace on. */
    portRpc: number;
    /** The gateway port (unused for namespace resolution, kept for symmetry). */
    portGateway: number;
    /** The cluster DNS domain the Service suffix is built from. */
    domain: string;
    /** The namespace the suite is deployed into. */
    namespace: string;
}
 
/**
 * Kubernetes resolution: a service id resolves to the Service of the same name
 * in the suite's namespace, instead of to a local port.
 *
 * `GatewayCodec` asks for `rpc-<namespace>` and `RpcServer` serves namespaces on
 * the RPC port, so the `rpc-` prefix is stripped and the port is the
 * namespace's, not the gateway's. `blong-kustomize` generates one Service per
 * orchestrator namespace with that name, which is what makes the namespace the
 * suite's abstract name for a process (see the PRD and the deployment docs).
 *
 * The namespace comes from config, then from `BLONG_NAMESPACE` /
 * `KUBERNETES_NAMESPACE` — the downward API that pins a pod to its own
 * namespace — and only then falls back to `default`.
 *
 * Pinned down with `resolution.impl: kubernetes` in the suite's config; the
 * loader then loads this module instead of `ResolutionLocal` (see the
 * `resolution` component in `load.ts`).
 */
export default class ResolutionK8s extends Internal implements IResolution {
    #config: IConfig = {
        portRpc: 8091,
        portGateway: 8080,
        domain: 'svc.cluster.local',
        namespace: '',
    };
    #manifest: IManifest | undefined;
 
    public constructor(config: IConfig, {manifest}: {manifest?: IManifest} = {}) {
        super();
        this.merge(this.#config, config);
        this.#manifest = manifest;
        if (!this.#config.namespace) {
            const env = (globalThis as {process?: {env?: Record<string, string | undefined>}})
                .process?.env;
            this.#config.namespace = env?.BLONG_NAMESPACE ?? env?.KUBERNETES_NAMESPACE ?? 'default';
        }
    }
 
    public async resolve(service: string): ReturnType<IResolution['resolve']> {
        const isRpc = service.startsWith('rpc-');
        const name = service.replace(/^rpc-/, '');
        // The manifest port is deliberately NOT preferred here: a random dev port
        // would point at a Service port the cluster does not publish. In the
        // cluster the Service is the address, and its port is the component's.
        return {
            hostname: `${name}.${this.#config.namespace}.${this.#config.domain}`,
            port: `${isRpc ? this.#config.portRpc : this.#config.portGateway}`,
        };
    }
 
    public announce(): void {}
 
    public async start(): Promise<void> {}
 
    public async stop(): Promise<void> {}
}