@k8ts/instruments
Version:
A collection of utilities and core components for k8ts.
45 lines (41 loc) • 1.24 kB
text/typescript
import { Map } from "immutable"
import { Ip4 } from "../../_ip"
import { PortSet } from "../set"
import type {
InputPortSetEntry,
InputPortSetRecord,
InputPortSetSpec,
PortSetEntry,
Protocol
} from "../types"
import { parsePortSpec } from "./parse"
function portSetEntry(name: string, value: InputPortSetEntry): PortSetEntry {
return {
name,
port: value.port,
protocol: value.protocol.toUpperCase() as Protocol,
hostIp: value.hostIp ? new Ip4(value.hostIp) : undefined,
hostPort: value.hostPort
}
}
export function parsePortInput(name: string, input: InputPortSetSpec): PortSetEntry {
if (typeof input === "string") {
return portSetEntry(name, parsePortSpec(name, input))
}
if (typeof input === "number") {
return portSetEntry(name, {
port: input,
protocol: "TCP" as const
})
}
return portSetEntry(name, input)
}
export function portRecordInput(
record: InputPortSetRecord<string> | PortSet<string>
): Map<string, PortSetEntry> {
if (record instanceof PortSet) {
return record.values
}
const inputMap = Map(record)
return inputMap.map((v, k) => parsePortInput(k, v))
}