The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | /** |
| 2 | * Internal Kubernetes object model + ordering/path helpers powering |
| 3 | * `Kubernetes.Manifest` and the workload platforms (`Deployment`, `Job`). |
| 4 | * Not part of the public surface — the public manifest shape is the literal |
| 5 | * `KubernetesManifest` on `Kubernetes.Manifest`. |
| 6 | */ |
| 7 | |
| 8 | export interface KubernetesObjectMetadata { |
| 9 | name: string; |
| 10 | namespace?: string; |
| 11 | labels?: Record<string, string>; |
| 12 | annotations?: Record<string, string>; |
| 13 | } |
| 14 | |
| 15 | export type KubernetesObjectDefinition = { |
| 16 | apiVersion: string; |
| 17 | kind: string; |
| 18 | metadata: KubernetesObjectMetadata; |
| 19 | } & Record<string, unknown>; |
| 20 | |
| 21 | export interface KubernetesObjectRef { |
| 22 | apiVersion: string; |
| 23 | kind: string; |
| 24 | name: string; |
| 25 | namespace?: string; |
| 26 | /** |
| 27 | * Fingerprint of the declared fields last applied. Reads compare a GET |
| 28 | * projected through {@link driftMask} to this. |
| 29 | */ |
| 30 | baselineHash?: string; |
| 31 | /** |
| 32 | * Declared-field mask from the last apply. Absent on state written before |
| 33 | * selections existed; those rows still detect a deleted object. |
| 34 | */ |
| 35 | driftMask?: unknown; |
| 36 | } |
| 37 | |
| 38 | export interface KubernetesObjectBinding { |
| 39 | type: "kubernetes-object"; |
| 40 | object: KubernetesObjectDefinition; |
| 41 | } |
| 42 | |
| 43 | export type KubernetesObjectScope = "Cluster" | "Namespaced"; |
| 44 | |
| 45 | export interface KubernetesObjectKindSpec { |
| 46 | plural: string; |
| 47 | scope: KubernetesObjectScope; |
| 48 | applyRank: number; |
| 49 | } |
| 50 | |
| 51 | /** Apply rank for kinds not in the static table (applied last, deleted first). */ |
| 52 | export const DEFAULT_APPLY_RANK = 100; |
| 53 | |
| 54 | const supportedKinds: Record<string, KubernetesObjectKindSpec> = { |
| 55 | "v1/Namespace": { |
| 56 | plural: "namespaces", |
| 57 | scope: "Cluster", |
| 58 | applyRank: 10, |
| 59 | }, |
| 60 | // CRDs apply right after namespaces so a chart's custom resources |
| 61 | // (default rank 100) always find their definition registered. |
| 62 | "apiextensions.k8s.io/v1/CustomResourceDefinition": { |
| 63 | plural: "customresourcedefinitions", |
| 64 | scope: "Cluster", |
| 65 | applyRank: 15, |
| 66 | }, |
| 67 | "v1/ServiceAccount": { |
| 68 | plural: "serviceaccounts", |
| 69 | scope: "Namespaced", |
| 70 | applyRank: 20, |
| 71 | }, |
| 72 | "v1/ConfigMap": { |
| 73 | plural: "configmaps", |
| 74 | scope: "Namespaced", |
| 75 | applyRank: 30, |
| 76 | }, |
| 77 | "v1/Secret": { |
| 78 | plural: "secrets", |
| 79 | scope: "Namespaced", |
| 80 | applyRank: 30, |
| 81 | }, |
| 82 | "v1/Service": { |
| 83 | plural: "services", |
| 84 | scope: "Namespaced", |
| 85 | applyRank: 40, |
| 86 | }, |
| 87 | "apps/v1/Deployment": { |
| 88 | plural: "deployments", |
| 89 | scope: "Namespaced", |
| 90 | applyRank: 50, |
| 91 | }, |
| 92 | "apps/v1/StatefulSet": { |
| 93 | plural: "statefulsets", |
| 94 | scope: "Namespaced", |
| 95 | applyRank: 50, |
| 96 | }, |
| 97 | "apps/v1/DaemonSet": { |
| 98 | plural: "daemonsets", |
| 99 | scope: "Namespaced", |
| 100 | applyRank: 50, |
| 101 | }, |
| 102 | "batch/v1/Job": { |
| 103 | plural: "jobs", |
| 104 | scope: "Namespaced", |
| 105 | applyRank: 60, |
| 106 | }, |
| 107 | "batch/v1/CronJob": { |
| 108 | plural: "cronjobs", |
| 109 | scope: "Namespaced", |
| 110 | applyRank: 60, |
| 111 | }, |
| 112 | "v1/Pod": { |
| 113 | plural: "pods", |
| 114 | scope: "Namespaced", |
| 115 | applyRank: 60, |
| 116 | }, |
| 117 | }; |
| 118 | |
| 119 | const objectTypeKey = (input: Pick<KubernetesObjectRef, "apiVersion" | "kind">) => |
| 120 | `${input.apiVersion}/${input.kind}`; |
| 121 | |
| 122 | /** Look up the static kind table; `undefined` for kinds needing discovery. */ |
| 123 | export const lookupKubernetesKindSpec = ( |
| 124 | input: Pick<KubernetesObjectRef, "apiVersion" | "kind">, |
| 125 | ): KubernetesObjectKindSpec | undefined => supportedKinds[objectTypeKey(input)]; |
| 126 | |
| 127 | export const getKubernetesKindSpec = (input: Pick<KubernetesObjectRef, "apiVersion" | "kind">) => { |
| 128 | const spec = lookupKubernetesKindSpec(input); |
| 129 | if (!spec) { |
| 130 | throw new Error(`Unsupported Kubernetes object ${input.apiVersion}/${input.kind}`); |
| 131 | } |
| 132 | return spec; |
| 133 | }; |
| 134 | |
| 135 | const applyRankOf = (input: Pick<KubernetesObjectRef, "apiVersion" | "kind">) => |
| 136 | lookupKubernetesKindSpec(input)?.applyRank ?? DEFAULT_APPLY_RANK; |
| 137 | |
| 138 | export const toKubernetesObjectRef = (object: KubernetesObjectDefinition): KubernetesObjectRef => ({ |
| 139 | apiVersion: object.apiVersion, |
| 140 | kind: object.kind, |
| 141 | name: object.metadata.name, |
| 142 | namespace: object.metadata.namespace, |
| 143 | }); |
| 144 | |
| 145 | export const kubernetesObjectKey = ( |
| 146 | input: Pick<KubernetesObjectRef, "apiVersion" | "kind" | "name" | "namespace">, |
| 147 | ) => [input.apiVersion, input.kind, input.namespace ?? "_cluster", input.name].join("/"); |
| 148 | |
| 149 | const compareRefs = (a: KubernetesObjectRef, b: KubernetesObjectRef) => |
| 150 | kubernetesObjectKey(a).localeCompare(kubernetesObjectKey(b)); |
| 151 | |
| 152 | export const sortObjectsForApply = (objects: ReadonlyArray<KubernetesObjectDefinition>) => |
| 153 | [...objects].sort( |
| 154 | (a, b) => |
| 155 | applyRankOf(a) - applyRankOf(b) || |
| 156 | compareRefs(toKubernetesObjectRef(a), toKubernetesObjectRef(b)), |
| 157 | ); |
| 158 | |
| 159 | export const sortRefsForDelete = (objects: ReadonlyArray<KubernetesObjectRef>) => |
| 160 | [...objects].sort((a, b) => applyRankOf(b) - applyRankOf(a) || compareRefs(a, b)); |
| 161 | |
| 162 | export const chunkByApplyRank = (objects: ReadonlyArray<KubernetesObjectDefinition>) => { |
| 163 | const chunks: KubernetesObjectDefinition[][] = []; |
| 164 | |
| 165 | for (const object of sortObjectsForApply(objects)) { |
| 166 | const rank = applyRankOf(object); |
| 167 | const current = chunks[chunks.length - 1]; |
| 168 | if (!current) { |
| 169 | chunks.push([object]); |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | const currentRank = applyRankOf(current[0]); |
| 174 | if (currentRank === rank) { |
| 175 | current.push(object); |
| 176 | } else { |
| 177 | chunks.push([object]); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return chunks; |
| 182 | }; |
| 183 | |
| 184 | /** |
| 185 | * Build the REST path for an object given its (statically-known or |
| 186 | * discovered) kind spec. |
| 187 | */ |
| 188 | export const buildKubernetesObjectPathWithSpec = ( |
| 189 | input: Pick<KubernetesObjectRef, "apiVersion" | "kind" | "name" | "namespace">, |
| 190 | spec: KubernetesObjectKindSpec, |
| 191 | ) => { |
| 192 | const [group, version] = input.apiVersion.includes("/") |
| 193 | ? input.apiVersion.split("/", 2) |
| 194 | : [undefined, input.apiVersion]; |
| 195 | |
| 196 | const base = group ? `/apis/${group}/${version}` : `/api/${version}`; |
| 197 | |
| 198 | if (spec.scope === "Namespaced") { |
| 199 | if (!input.namespace) { |
| 200 | throw new Error( |
| 201 | `Kubernetes object ${input.apiVersion}/${input.kind}/${input.name} requires a namespace`, |
| 202 | ); |
| 203 | } |
| 204 | |
| 205 | return `${base}/namespaces/${input.namespace}/${spec.plural}/${input.name}`; |
| 206 | } |
| 207 | |
| 208 | return `${base}/${spec.plural}/${input.name}`; |
| 209 | }; |
| 210 | |
| 211 | export const buildKubernetesObjectPath = ( |
| 212 | input: Pick<KubernetesObjectRef, "apiVersion" | "kind" | "name" | "namespace">, |
| 213 | ) => buildKubernetesObjectPathWithSpec(input, getKubernetesKindSpec(input)); |