The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Kubernetes from "@alchemy.run/kubernetes"; |
| 2 | import { |
| 3 | appliedObjectsMatch, |
| 4 | driftMask, |
| 5 | hashDriftSelection, |
| 6 | } from "@alchemy.run/kubernetes/internal/declared"; |
| 7 | import { expect } from "@alchemy.run/test"; |
| 8 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 9 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 10 | import { toYamlDisplayValue } from "alchemy/Cli/PropertyDiff"; |
| 11 | import { encodeState, reviveStateRecursive } from "alchemy/State/StateEncoding"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Layer from "effect/Layer"; |
| 14 | import * as Redacted from "effect/Redacted"; |
| 15 | |
| 16 | const { test } = Test.make({ |
| 17 | providers: Layer.mergeAll(Kubernetes.providers(), NodeServices.layer), |
| 18 | }); |
| 19 | |
| 20 | test( |
| 21 | "declared lists with a merge key compare by that key", |
| 22 | Effect.sync(() => { |
| 23 | const desired = { |
| 24 | apiVersion: "apps/v1", |
| 25 | kind: "Deployment", |
| 26 | metadata: { name: "app" }, |
| 27 | spec: { |
| 28 | template: { |
| 29 | spec: { |
| 30 | containers: [ |
| 31 | { name: "a", image: "a:1" }, |
| 32 | { name: "b", image: "b:1" }, |
| 33 | ], |
| 34 | }, |
| 35 | }, |
| 36 | }, |
| 37 | }; |
| 38 | const preview = { |
| 39 | ...desired, |
| 40 | metadata: { name: "app", uid: "u", resourceVersion: "9" }, |
| 41 | }; |
| 42 | const reordered = { |
| 43 | ...preview, |
| 44 | spec: { |
| 45 | template: { |
| 46 | spec: { |
| 47 | containers: [ |
| 48 | { name: "b", image: "b:1" }, |
| 49 | { name: "a", image: "a:1" }, |
| 50 | ], |
| 51 | }, |
| 52 | }, |
| 53 | }, |
| 54 | }; |
| 55 | expect(appliedObjectsMatch(reordered, preview, desired)).toBe(true); |
| 56 | const edited = { |
| 57 | ...reordered, |
| 58 | spec: { |
| 59 | template: { |
| 60 | spec: { |
| 61 | containers: [ |
| 62 | { name: "b", image: "b:2" }, |
| 63 | { name: "a", image: "a:1" }, |
| 64 | ], |
| 65 | }, |
| 66 | }, |
| 67 | }, |
| 68 | }; |
| 69 | expect(appliedObjectsMatch(edited, preview, desired)).toBe(false); |
| 70 | const tolerations = { |
| 71 | apiVersion: "v1", |
| 72 | kind: "Pod", |
| 73 | metadata: { name: "app" }, |
| 74 | spec: { |
| 75 | tolerations: [ |
| 76 | { |
| 77 | key: "disk", |
| 78 | operator: "Equal", |
| 79 | value: "ssd", |
| 80 | effect: "NoSchedule", |
| 81 | }, |
| 82 | { key: "disk", operator: "Exists", effect: "NoExecute" }, |
| 83 | ], |
| 84 | }, |
| 85 | }; |
| 86 | expect( |
| 87 | appliedObjectsMatch( |
| 88 | { |
| 89 | ...tolerations, |
| 90 | spec: { |
| 91 | tolerations: [ |
| 92 | { key: "disk", operator: "Exists", effect: "NoExecute" }, |
| 93 | { |
| 94 | key: "disk", |
| 95 | operator: "Equal", |
| 96 | value: "ssd", |
| 97 | effect: "NoSchedule", |
| 98 | }, |
| 99 | ], |
| 100 | }, |
| 101 | }, |
| 102 | tolerations, |
| 103 | tolerations, |
| 104 | ), |
| 105 | ).toBe(false); |
| 106 | expect( |
| 107 | appliedObjectsMatch( |
| 108 | { |
| 109 | ...tolerations, |
| 110 | spec: { |
| 111 | tolerations: [ |
| 112 | { |
| 113 | key: "disk", |
| 114 | operator: "Equal", |
| 115 | value: "ssd", |
| 116 | effect: "NoExecute", |
| 117 | }, |
| 118 | { key: "disk", operator: "Exists", effect: "NoExecute" }, |
| 119 | ], |
| 120 | }, |
| 121 | }, |
| 122 | tolerations, |
| 123 | tolerations, |
| 124 | ), |
| 125 | ).toBe(false); |
| 126 | expect( |
| 127 | appliedObjectsMatch( |
| 128 | { |
| 129 | ...tolerations, |
| 130 | spec: { |
| 131 | tolerations: [ |
| 132 | ...tolerations.spec.tolerations, |
| 133 | { key: "gpu", operator: "Exists", effect: "NoSchedule" }, |
| 134 | ], |
| 135 | }, |
| 136 | }, |
| 137 | tolerations, |
| 138 | tolerations, |
| 139 | ), |
| 140 | ).toBe(false); |
| 141 | const bare = { |
| 142 | apiVersion: "v1", |
| 143 | kind: "Pod", |
| 144 | metadata: { name: "app" }, |
| 145 | spec: { tolerations: [{ key: "gpu" }] }, |
| 146 | }; |
| 147 | const defaulted = { |
| 148 | ...bare, |
| 149 | spec: { tolerations: [{ key: "gpu", operator: "Equal" }] }, |
| 150 | }; |
| 151 | expect(appliedObjectsMatch(defaulted, bare, bare)).toBe(true); |
| 152 | expect( |
| 153 | appliedObjectsMatch( |
| 154 | { |
| 155 | ...bare, |
| 156 | spec: { tolerations: [{ key: "tpu", operator: "Equal" }] }, |
| 157 | }, |
| 158 | defaulted, |
| 159 | bare, |
| 160 | ), |
| 161 | ).toBe(false); |
| 162 | const service = { |
| 163 | apiVersion: "v1", |
| 164 | kind: "Service", |
| 165 | metadata: { name: "app" }, |
| 166 | spec: { |
| 167 | ports: [ |
| 168 | { name: "http", port: 80 }, |
| 169 | { name: "https", port: 443 }, |
| 170 | ], |
| 171 | }, |
| 172 | }; |
| 173 | expect( |
| 174 | appliedObjectsMatch( |
| 175 | { |
| 176 | ...service, |
| 177 | spec: { |
| 178 | ports: [ |
| 179 | { name: "https", port: 443 }, |
| 180 | { name: "http", port: 80 }, |
| 181 | ], |
| 182 | }, |
| 183 | }, |
| 184 | service, |
| 185 | service, |
| 186 | ), |
| 187 | ).toBe(true); |
| 188 | const secret = { |
| 189 | apiVersion: "v1", |
| 190 | kind: "Secret", |
| 191 | metadata: { name: "token" }, |
| 192 | stringData: Redacted.make({ token: "s3cr3t" }), |
| 193 | }; |
| 194 | const encode = (value: string) => Buffer.from(value).toString("base64"); |
| 195 | expect(JSON.stringify(driftMask(secret))).not.toContain("s3cr3t"); |
| 196 | expect( |
| 197 | appliedObjectsMatch( |
| 198 | { |
| 199 | ...secret, |
| 200 | stringData: undefined, |
| 201 | data: { token: encode("s3cr3t"), extra: encode("other-value") }, |
| 202 | }, |
| 203 | { |
| 204 | ...secret, |
| 205 | stringData: undefined, |
| 206 | data: { token: encode("s3cr3t") }, |
| 207 | }, |
| 208 | secret, |
| 209 | ), |
| 210 | ).toBe(true); |
| 211 | const indexed = { |
| 212 | apiVersion: "v1", |
| 213 | kind: "ConfigMap", |
| 214 | metadata: { name: "c" }, |
| 215 | data: { items: ["a", "b"] }, |
| 216 | }; |
| 217 | expect(appliedObjectsMatch({ ...indexed, data: { items: ["b", "a"] } }, indexed, indexed)).toBe( |
| 218 | false, |
| 219 | ); |
| 220 | expect( |
| 221 | appliedObjectsMatch({ ...indexed, data: { items: ["a", "b", "c"] } }, indexed, indexed), |
| 222 | ).toBe(false); |
| 223 | const named = { |
| 224 | apiVersion: "example.com/v1", |
| 225 | kind: "Widget", |
| 226 | metadata: { name: "w" }, |
| 227 | spec: { |
| 228 | items: [ |
| 229 | { name: "a", image: "a:1" }, |
| 230 | { name: "b", image: "b:1" }, |
| 231 | ], |
| 232 | }, |
| 233 | }; |
| 234 | expect( |
| 235 | appliedObjectsMatch( |
| 236 | { |
| 237 | ...named, |
| 238 | spec: { |
| 239 | items: [ |
| 240 | { name: "b", image: "b:1" }, |
| 241 | { name: "a", image: "a:1" }, |
| 242 | ], |
| 243 | }, |
| 244 | }, |
| 245 | named, |
| 246 | named, |
| 247 | ), |
| 248 | ).toBe(false); |
| 249 | const crdContainers = { |
| 250 | apiVersion: "example.com/v1", |
| 251 | kind: "Widget", |
| 252 | metadata: { name: "w" }, |
| 253 | spec: { |
| 254 | containers: [ |
| 255 | { name: "a", image: "a:1" }, |
| 256 | { name: "b", image: "b:1" }, |
| 257 | ], |
| 258 | }, |
| 259 | }; |
| 260 | expect( |
| 261 | appliedObjectsMatch( |
| 262 | { |
| 263 | ...crdContainers, |
| 264 | spec: { |
| 265 | containers: [ |
| 266 | { name: "b", image: "b:1" }, |
| 267 | { name: "a", image: "a:1" }, |
| 268 | ], |
| 269 | }, |
| 270 | }, |
| 271 | crdContainers, |
| 272 | crdContainers, |
| 273 | ), |
| 274 | ).toBe(false); |
| 275 | expect( |
| 276 | appliedObjectsMatch( |
| 277 | { |
| 278 | ...crdContainers, |
| 279 | spec: { |
| 280 | containers: [ |
| 281 | { name: "a", image: "a:1" }, |
| 282 | { name: "b", image: "b:1" }, |
| 283 | { name: "c", image: "c:1" }, |
| 284 | ], |
| 285 | }, |
| 286 | }, |
| 287 | crdContainers, |
| 288 | crdContainers, |
| 289 | ), |
| 290 | ).toBe(false); |
| 291 | const mapItems = { |
| 292 | ...named, |
| 293 | metadata: { |
| 294 | name: "w", |
| 295 | managedFields: [ |
| 296 | { |
| 297 | manager: "alchemy", |
| 298 | fieldsV1: { |
| 299 | "f:spec": { |
| 300 | "f:items": { |
| 301 | 'k:{"name":"a"}': { ".": {} }, |
| 302 | }, |
| 303 | }, |
| 304 | }, |
| 305 | }, |
| 306 | ], |
| 307 | }, |
| 308 | }; |
| 309 | const mapReordered = { |
| 310 | ...mapItems, |
| 311 | spec: { |
| 312 | items: [ |
| 313 | { name: "b", image: "b:1" }, |
| 314 | { name: "a", image: "a:1" }, |
| 315 | ], |
| 316 | }, |
| 317 | }; |
| 318 | expect(appliedObjectsMatch(mapReordered, mapItems, mapItems)).toBe(true); |
| 319 | expect(JSON.stringify(driftMask(mapItems))).toContain('"spec.items"'); |
| 320 | const k8sIoCrd = { |
| 321 | apiVersion: "widgets.acme.k8s.io/v1", |
| 322 | kind: "Widget", |
| 323 | metadata: { name: "w" }, |
| 324 | spec: { |
| 325 | containers: [ |
| 326 | { name: "a", image: "a:1" }, |
| 327 | { name: "b", image: "b:1" }, |
| 328 | ], |
| 329 | }, |
| 330 | }; |
| 331 | expect( |
| 332 | appliedObjectsMatch( |
| 333 | { |
| 334 | ...k8sIoCrd, |
| 335 | spec: { |
| 336 | containers: [ |
| 337 | { name: "b", image: "b:1" }, |
| 338 | { name: "a", image: "a:1" }, |
| 339 | ], |
| 340 | }, |
| 341 | }, |
| 342 | k8sIoCrd, |
| 343 | k8sIoCrd, |
| 344 | ), |
| 345 | ).toBe(false); |
| 346 | const codes = { |
| 347 | apiVersion: "example.com/v1", |
| 348 | kind: "Widget", |
| 349 | metadata: { |
| 350 | name: "w", |
| 351 | managedFields: [ |
| 352 | { |
| 353 | manager: "alchemy", |
| 354 | fieldsV1: { "f:spec": { "f:codes": { "v:1": {} } } }, |
| 355 | }, |
| 356 | ], |
| 357 | }, |
| 358 | spec: { codes: [1, true] }, |
| 359 | }; |
| 360 | expect( |
| 361 | appliedObjectsMatch( |
| 362 | { ...codes, spec: { codes: [true, 1, 2] } }, |
| 363 | { ...codes, spec: { codes: [1, true] } }, |
| 364 | codes, |
| 365 | ), |
| 366 | ).toBe(true); |
| 367 | expect(appliedObjectsMatch({ ...codes, spec: { codes: [true] } }, codes, codes)).toBe(false); |
| 368 | const settings = { |
| 369 | apiVersion: "example.com/v1", |
| 370 | kind: "Widget", |
| 371 | metadata: { name: "w" }, |
| 372 | spec: { settings: { a: 1 } }, |
| 373 | }; |
| 374 | // Undeclared map keys are not part of the selection. |
| 375 | expect( |
| 376 | appliedObjectsMatch({ ...settings, spec: { settings: { a: 1, b: 2 } } }, settings, settings), |
| 377 | ).toBe(true); |
| 378 | expect( |
| 379 | appliedObjectsMatch({ ...settings, spec: { settings: { a: 2 } } }, settings, settings), |
| 380 | ).toBe(false); |
| 381 | }), |
| 382 | { tags: ["provider:kubernetes", "local"] }, |
| 383 | ); |
| 384 | |
| 385 | type DriftMeta = { |
| 386 | name?: string; |
| 387 | namespace?: string; |
| 388 | labels?: Record<string, string>; |
| 389 | annotations?: Record<string, string>; |
| 390 | uid?: string; |
| 391 | resourceVersion?: string; |
| 392 | }; |
| 393 | |
| 394 | type DriftDeployment = { |
| 395 | apiVersion: string; |
| 396 | kind: string; |
| 397 | metadata: DriftMeta; |
| 398 | spec: { |
| 399 | replicas?: number; |
| 400 | revisionHistoryLimit?: number; |
| 401 | selector?: { matchLabels: Record<string, string> }; |
| 402 | template: { metadata: DriftMeta; spec: Record<string, unknown> }; |
| 403 | }; |
| 404 | }; |
| 405 | |
| 406 | const deployment = (name: string, spec: DriftDeployment["spec"]): DriftDeployment => ({ |
| 407 | apiVersion: "apps/v1", |
| 408 | kind: "Deployment", |
| 409 | metadata: { name, namespace: "default" }, |
| 410 | spec, |
| 411 | }); |
| 412 | |
| 413 | const podSpec = (spec: Record<string, unknown>): DriftDeployment["spec"] => ({ |
| 414 | selector: { matchLabels: { app: "web" } }, |
| 415 | template: { metadata: { labels: { app: "web" } }, spec }, |
| 416 | }); |
| 417 | |
| 418 | test( |
| 419 | "drift selection keeps declared fields and ignores server-owned ones", |
| 420 | Effect.gen(function* () { |
| 421 | const declared = deployment("app", { |
| 422 | ...podSpec({ containers: [{ name: "app", image: "app:1" }] }), |
| 423 | }); |
| 424 | const canonical = { |
| 425 | ...declared, |
| 426 | metadata: { ...declared.metadata, uid: "u", resourceVersion: "1" }, |
| 427 | spec: { |
| 428 | replicas: 1, |
| 429 | revisionHistoryLimit: 10, |
| 430 | ...declared.spec, |
| 431 | template: { |
| 432 | ...declared.spec.template, |
| 433 | spec: { |
| 434 | containers: [{ name: "app", image: "app:1", imagePullPolicy: "IfNotPresent" }], |
| 435 | }, |
| 436 | }, |
| 437 | }, |
| 438 | }; |
| 439 | const mask = driftMask(declared); |
| 440 | const baseline = yield* hashDriftSelection(declared, canonical); |
| 441 | expect(yield* hashDriftSelection(mask, canonical)).toBe(baseline); |
| 442 | // Older state has no canonical hash. The same selection against the |
| 443 | // declaration ignores apiserver defaults. |
| 444 | expect(yield* hashDriftSelection(mask, declared)).toBe( |
| 445 | yield* hashDriftSelection(mask, canonical), |
| 446 | ); |
| 447 | const resized = { ...canonical, spec: { ...canonical.spec, replicas: 5 } }; |
| 448 | expect(yield* hashDriftSelection(mask, resized)).toBe(baseline); |
| 449 | |
| 450 | const pinned = deployment("pinned", { |
| 451 | replicas: 2, |
| 452 | ...podSpec({ containers: [{ name: "app", image: "app:1" }] }), |
| 453 | }); |
| 454 | const pinnedMask = driftMask(pinned); |
| 455 | const pinnedBaseline = yield* hashDriftSelection(pinned, pinned); |
| 456 | expect( |
| 457 | yield* hashDriftSelection(pinnedMask, { ...pinned, spec: { ...pinned.spec, replicas: 3 } }), |
| 458 | ).not.toBe(pinnedBaseline); |
| 459 | |
| 460 | const noted = deployment("noted", { |
| 461 | ...podSpec({ containers: [{ name: "app", image: "app:1" }] }), |
| 462 | }); |
| 463 | noted.metadata = { |
| 464 | ...noted.metadata, |
| 465 | annotations: { app: "web" }, |
| 466 | }; |
| 467 | ( |
| 468 | noted.spec.template as { |
| 469 | metadata: { labels: Record<string, string>; annotations?: Record<string, string> }; |
| 470 | } |
| 471 | ).metadata = { |
| 472 | labels: { app: "web" }, |
| 473 | annotations: { "prometheus.io/scrape": "true" }, |
| 474 | }; |
| 475 | const notedMask = driftMask(noted); |
| 476 | const notedBaseline = yield* hashDriftSelection(noted, noted); |
| 477 | const controller = { |
| 478 | ...noted, |
| 479 | metadata: { |
| 480 | ...noted.metadata, |
| 481 | annotations: { app: "web", "deployment.kubernetes.io/revision": "4" }, |
| 482 | }, |
| 483 | }; |
| 484 | expect(yield* hashDriftSelection(notedMask, controller)).toBe(notedBaseline); |
| 485 | const edited = { |
| 486 | ...noted, |
| 487 | metadata: { ...noted.metadata, annotations: { app: "api" } }, |
| 488 | }; |
| 489 | expect(yield* hashDriftSelection(notedMask, edited)).not.toBe(notedBaseline); |
| 490 | const dropped = { |
| 491 | ...noted, |
| 492 | metadata: { ...noted.metadata, annotations: {} }, |
| 493 | }; |
| 494 | expect(yield* hashDriftSelection(notedMask, dropped)).not.toBe(notedBaseline); |
| 495 | const template = noted.spec.template as { |
| 496 | metadata: { labels: Record<string, string>; annotations: Record<string, string> }; |
| 497 | spec: unknown; |
| 498 | }; |
| 499 | const templateEdited = { |
| 500 | ...noted, |
| 501 | spec: { |
| 502 | ...noted.spec, |
| 503 | template: { |
| 504 | ...template, |
| 505 | metadata: { ...template.metadata, annotations: { "prometheus.io/scrape": "false" } }, |
| 506 | }, |
| 507 | }, |
| 508 | }; |
| 509 | expect(yield* hashDriftSelection(notedMask, templateEdited)).not.toBe(notedBaseline); |
| 510 | const templateDropped = { |
| 511 | ...noted, |
| 512 | spec: { |
| 513 | ...noted.spec, |
| 514 | template: { ...template, metadata: { labels: { app: "web" } } }, |
| 515 | }, |
| 516 | }; |
| 517 | expect(yield* hashDriftSelection(notedMask, templateDropped)).not.toBe(notedBaseline); |
| 518 | |
| 519 | const ordered = deployment("ordered", { |
| 520 | ...podSpec({ |
| 521 | initContainers: [ |
| 522 | { name: "a", image: "a:1" }, |
| 523 | { name: "b", image: "b:1" }, |
| 524 | ], |
| 525 | containers: [ |
| 526 | { name: "app", image: "app:1" }, |
| 527 | { name: "sidecar", image: "sidecar:1" }, |
| 528 | ], |
| 529 | }), |
| 530 | }); |
| 531 | const orderedMask = driftMask(ordered); |
| 532 | const orderedBaseline = yield* hashDriftSelection(ordered, ordered); |
| 533 | const reversedInit = { |
| 534 | ...ordered, |
| 535 | spec: { |
| 536 | ...ordered.spec, |
| 537 | template: { |
| 538 | ...ordered.spec.template, |
| 539 | spec: { |
| 540 | initContainers: [ |
| 541 | { name: "b", image: "b:1" }, |
| 542 | { name: "a", image: "a:1" }, |
| 543 | ], |
| 544 | containers: [ |
| 545 | { name: "sidecar", image: "sidecar:1" }, |
| 546 | { name: "app", image: "app:1" }, |
| 547 | ], |
| 548 | }, |
| 549 | }, |
| 550 | }, |
| 551 | }; |
| 552 | // initContainers is a map list: order is not part of the merge key. |
| 553 | expect(yield* hashDriftSelection(orderedMask, reversedInit)).toBe(orderedBaseline); |
| 554 | const reversedContainers = { |
| 555 | ...ordered, |
| 556 | spec: { |
| 557 | ...ordered.spec, |
| 558 | template: { |
| 559 | ...ordered.spec.template, |
| 560 | spec: { |
| 561 | initContainers: [ |
| 562 | { name: "a", image: "a:1" }, |
| 563 | { name: "b", image: "b:1" }, |
| 564 | ], |
| 565 | containers: [ |
| 566 | { name: "sidecar", image: "sidecar:1" }, |
| 567 | { name: "app", image: "app:1" }, |
| 568 | ], |
| 569 | }, |
| 570 | }, |
| 571 | }, |
| 572 | }; |
| 573 | expect(yield* hashDriftSelection(orderedMask, reversedContainers)).toBe(orderedBaseline); |
| 574 | |
| 575 | const dns = { |
| 576 | apiVersion: "v1", |
| 577 | kind: "Service", |
| 578 | metadata: { name: "dns" }, |
| 579 | spec: { |
| 580 | ports: [ |
| 581 | { name: "dns-tcp", port: 53, protocol: "TCP", targetPort: 53 }, |
| 582 | { name: "dns-udp", port: 53, protocol: "UDP", targetPort: 53 }, |
| 583 | ], |
| 584 | }, |
| 585 | }; |
| 586 | const dnsMask = driftMask(dns); |
| 587 | const dnsBaseline = yield* hashDriftSelection(dns, dns); |
| 588 | expect(yield* hashDriftSelection(dnsMask, dns)).toBe(dnsBaseline); |
| 589 | const udpEdited = { |
| 590 | ...dns, |
| 591 | spec: { |
| 592 | ports: [dns.spec.ports[0], { ...dns.spec.ports[1], targetPort: 5353 }], |
| 593 | }, |
| 594 | }; |
| 595 | expect(yield* hashDriftSelection(dnsMask, udpEdited)).not.toBe(dnsBaseline); |
| 596 | const http = { |
| 597 | apiVersion: "v1", |
| 598 | kind: "Service", |
| 599 | metadata: { name: "http" }, |
| 600 | spec: { ports: [{ port: 80, targetPort: 80 }] }, |
| 601 | }; |
| 602 | expect( |
| 603 | yield* hashDriftSelection(driftMask(http), { |
| 604 | ...http, |
| 605 | spec: { ports: [{ port: 80, protocol: "TCP", targetPort: 80 }] }, |
| 606 | }), |
| 607 | ).toBe(yield* hashDriftSelection(http, http)); |
| 608 | const probed = deployment("probed", { |
| 609 | ...podSpec({ |
| 610 | containers: [ |
| 611 | { |
| 612 | name: "app", |
| 613 | image: "app:1", |
| 614 | ports: [ |
| 615 | { name: "dns-tcp", containerPort: 53, protocol: "TCP", hostPort: 53 }, |
| 616 | { name: "dns-udp", containerPort: 53, protocol: "UDP", hostPort: 53 }, |
| 617 | ], |
| 618 | }, |
| 619 | ], |
| 620 | }), |
| 621 | }); |
| 622 | const probedMask = driftMask(probed); |
| 623 | const probedBaseline = yield* hashDriftSelection(probed, probed); |
| 624 | const probedSpec = probed.spec.template as unknown as { |
| 625 | spec: { containers: Array<{ ports: Array<Record<string, unknown>> }> }; |
| 626 | }; |
| 627 | const probedEdited = { |
| 628 | ...probed, |
| 629 | spec: { |
| 630 | ...probed.spec, |
| 631 | template: { |
| 632 | ...probed.spec.template, |
| 633 | spec: { |
| 634 | containers: [ |
| 635 | { |
| 636 | ...probedSpec.spec.containers[0], |
| 637 | ports: [ |
| 638 | probedSpec.spec.containers[0]!.ports[0], |
| 639 | { ...probedSpec.spec.containers[0]!.ports[1], hostPort: 5353 }, |
| 640 | ], |
| 641 | }, |
| 642 | ], |
| 643 | }, |
| 644 | }, |
| 645 | }, |
| 646 | }; |
| 647 | expect(yield* hashDriftSelection(probedMask, probedEdited)).not.toBe(probedBaseline); |
| 648 | |
| 649 | const namespace = { |
| 650 | apiVersion: "v1", |
| 651 | kind: "Namespace", |
| 652 | metadata: { |
| 653 | name: "team", |
| 654 | finalizers: ["example.com/keep", "example.com/other"], |
| 655 | }, |
| 656 | }; |
| 657 | const namespaceMask = driftMask(namespace); |
| 658 | const namespaceBaseline = yield* hashDriftSelection(namespace, namespace); |
| 659 | expect(yield* hashDriftSelection(namespaceMask, namespace)).toBe(namespaceBaseline); |
| 660 | expect( |
| 661 | yield* hashDriftSelection(namespaceMask, { |
| 662 | ...namespace, |
| 663 | metadata: { |
| 664 | ...namespace.metadata, |
| 665 | finalizers: ["kubernetes", "example.com/other", "example.com/keep"], |
| 666 | }, |
| 667 | }), |
| 668 | ).toBe(namespaceBaseline); |
| 669 | expect( |
| 670 | yield* hashDriftSelection(namespaceMask, { |
| 671 | ...namespace, |
| 672 | metadata: { |
| 673 | ...namespace.metadata, |
| 674 | finalizers: ["kubernetes", "example.com/other"], |
| 675 | }, |
| 676 | }), |
| 677 | ).not.toBe(namespaceBaseline); |
| 678 | |
| 679 | const sized = deployment("sized", { |
| 680 | ...podSpec({ |
| 681 | containers: [ |
| 682 | { |
| 683 | name: "app", |
| 684 | image: "app:1", |
| 685 | resources: { requests: { cpu: "0.1", memory: "1.5Gi" } }, |
| 686 | ports: [{ containerPort: 80, protocol: "tcp" }], |
| 687 | }, |
| 688 | ], |
| 689 | }), |
| 690 | }); |
| 691 | const sizedMask = driftMask(sized); |
| 692 | const sizedLive = { |
| 693 | ...sized, |
| 694 | spec: { |
| 695 | ...sized.spec, |
| 696 | template: { |
| 697 | ...sized.spec.template, |
| 698 | spec: { |
| 699 | containers: [ |
| 700 | { |
| 701 | name: "app", |
| 702 | image: "app:1", |
| 703 | resources: { requests: { cpu: "100m", memory: "1536Mi" } }, |
| 704 | ports: [{ containerPort: 80, protocol: "TCP" }], |
| 705 | }, |
| 706 | ], |
| 707 | }, |
| 708 | }, |
| 709 | }, |
| 710 | }; |
| 711 | expect(yield* hashDriftSelection(sizedMask, sizedLive)).toBe( |
| 712 | yield* hashDriftSelection(sizedMask, sized), |
| 713 | ); |
| 714 | const resizedCpu = { |
| 715 | ...sizedLive, |
| 716 | spec: { |
| 717 | ...sizedLive.spec, |
| 718 | template: { |
| 719 | ...sizedLive.spec.template, |
| 720 | spec: { |
| 721 | containers: [ |
| 722 | { |
| 723 | name: "app", |
| 724 | image: "app:1", |
| 725 | resources: { requests: { cpu: "200m", memory: "1536Mi" } }, |
| 726 | ports: [{ containerPort: 80, protocol: "TCP" }], |
| 727 | }, |
| 728 | ], |
| 729 | }, |
| 730 | }, |
| 731 | }, |
| 732 | }; |
| 733 | expect(yield* hashDriftSelection(sizedMask, resizedCpu)).not.toBe( |
| 734 | yield* hashDriftSelection(sizedMask, sized), |
| 735 | ); |
| 736 | |
| 737 | const claim = { |
| 738 | apiVersion: "v1", |
| 739 | kind: "PersistentVolumeClaim", |
| 740 | metadata: { name: "data" }, |
| 741 | spec: { |
| 742 | accessModes: ["ReadWriteOnce"], |
| 743 | resources: { requests: { storage: "1.5Gi" } }, |
| 744 | }, |
| 745 | }; |
| 746 | const claimLive = { |
| 747 | ...claim, |
| 748 | spec: { |
| 749 | ...claim.spec, |
| 750 | resources: { requests: { storage: "1536Mi" } }, |
| 751 | }, |
| 752 | }; |
| 753 | expect(yield* hashDriftSelection(driftMask(claim), claimLive)).toBe( |
| 754 | yield* hashDriftSelection(driftMask(claim), claim), |
| 755 | ); |
| 756 | |
| 757 | const quota = { |
| 758 | apiVersion: "v1", |
| 759 | kind: "ResourceQuota", |
| 760 | metadata: { name: "team" }, |
| 761 | spec: { |
| 762 | hard: { |
| 763 | "requests.cpu": "0.1", |
| 764 | "limits.memory": "1.5Gi", |
| 765 | "requests.storage": "1.5Gi", |
| 766 | }, |
| 767 | }, |
| 768 | }; |
| 769 | expect( |
| 770 | yield* hashDriftSelection(driftMask(quota), { |
| 771 | ...quota, |
| 772 | spec: { |
| 773 | hard: { |
| 774 | "requests.cpu": "100m", |
| 775 | "limits.memory": "1536Mi", |
| 776 | "requests.storage": "1536Mi", |
| 777 | }, |
| 778 | }, |
| 779 | }), |
| 780 | ).toBe(yield* hashDriftSelection(driftMask(quota), quota)); |
| 781 | |
| 782 | const config = { |
| 783 | apiVersion: "v1", |
| 784 | kind: "ConfigMap", |
| 785 | metadata: { name: "cfg" }, |
| 786 | data: { memory: "1Gi", cpu: "1", protocol: "tcp" }, |
| 787 | }; |
| 788 | expect( |
| 789 | yield* hashDriftSelection(driftMask(config), { |
| 790 | ...config, |
| 791 | data: { memory: "1024Mi", cpu: "1000m", protocol: "TCP" }, |
| 792 | }), |
| 793 | ).not.toBe(yield* hashDriftSelection(driftMask(config), config)); |
| 794 | }), |
| 795 | { tags: ["provider:kubernetes", "local"] }, |
| 796 | ); |
| 797 | |
| 798 | const containers = [ |
| 799 | { name: "a", image: "a:1" }, |
| 800 | { name: "b", image: "b:1" }, |
| 801 | ]; |
| 802 | |
| 803 | const reorderedContainers = [ |
| 804 | { name: "b", image: "b:1" }, |
| 805 | { name: "a", image: "a:1" }, |
| 806 | ]; |
| 807 | |
| 808 | const deploymentList = (fieldsV1?: unknown) => ({ |
| 809 | apiVersion: "apps/v1", |
| 810 | kind: "Deployment", |
| 811 | metadata: |
| 812 | fieldsV1 === undefined |
| 813 | ? { name: "app" } |
| 814 | : { |
| 815 | name: "app", |
| 816 | managedFields: [{ manager: "alchemy", fieldsV1 }], |
| 817 | }, |
| 818 | spec: { template: { spec: { containers } } }, |
| 819 | }); |
| 820 | |
| 821 | const containerFields = (node: Record<string, unknown>) => ({ |
| 822 | "f:spec": { "f:template": { "f:spec": { "f:containers": node } } }, |
| 823 | }); |
| 824 | |
| 825 | test( |
| 826 | "fieldsV1 topology is reused on a later read", |
| 827 | Effect.gen(function* () { |
| 828 | const widget = { |
| 829 | apiVersion: "example.com/v1", |
| 830 | kind: "Widget", |
| 831 | metadata: { name: "w" }, |
| 832 | spec: { containers }, |
| 833 | }; |
| 834 | const widgetWitness = { |
| 835 | ...widget, |
| 836 | metadata: { |
| 837 | name: "w", |
| 838 | managedFields: [ |
| 839 | { |
| 840 | manager: "alchemy", |
| 841 | fieldsV1: { |
| 842 | "f:spec": { |
| 843 | "f:containers": { |
| 844 | ".": {}, |
| 845 | 'k:{"name":"a"}': { ".": {} }, |
| 846 | 'k:{"name":"b"}': { ".": {} }, |
| 847 | }, |
| 848 | }, |
| 849 | }, |
| 850 | }, |
| 851 | ], |
| 852 | }, |
| 853 | }; |
| 854 | const widgetMask = driftMask(widget, widgetWitness); |
| 855 | const widgetReordered = { ...widget, spec: { containers: reorderedContainers } }; |
| 856 | const widgetMissing = { ...widget, spec: { containers: [containers[0]] } }; |
| 857 | const widgetExtra = { |
| 858 | ...widget, |
| 859 | spec: { containers: [...containers, { name: "c", image: "c:1" }] }, |
| 860 | }; |
| 861 | const widgetBaseline = yield* hashDriftSelection(widgetMask, widget); |
| 862 | // The raw CRD list is atomic. The stored apply topology is what ignores order. |
| 863 | expect(yield* hashDriftSelection(widget, widgetReordered)).not.toBe( |
| 864 | yield* hashDriftSelection(widget, widget), |
| 865 | ); |
| 866 | expect(yield* hashDriftSelection(widgetMask, widgetReordered)).toBe(widgetBaseline); |
| 867 | expect(yield* hashDriftSelection(widgetMask, widgetExtra)).toBe(widgetBaseline); |
| 868 | expect(yield* hashDriftSelection(widgetMask, widgetMissing)).not.toBe(widgetBaseline); |
| 869 | |
| 870 | const blank = deploymentList(); |
| 871 | const blankMask = driftMask(blank); |
| 872 | const blankReordered = { |
| 873 | ...blank, |
| 874 | spec: { template: { spec: { containers: reorderedContainers } } }, |
| 875 | }; |
| 876 | const blankMissing = { |
| 877 | ...blank, |
| 878 | spec: { template: { spec: { containers: [containers[0]] } } }, |
| 879 | }; |
| 880 | const blankBaseline = yield* hashDriftSelection(blankMask, blank); |
| 881 | expect(yield* hashDriftSelection(blankMask, blankReordered)).toBe(blankBaseline); |
| 882 | expect(yield* hashDriftSelection(blankMask, blankMissing)).not.toBe(blankBaseline); |
| 883 | |
| 884 | const keyed = deploymentList( |
| 885 | containerFields({ |
| 886 | ".": {}, |
| 887 | 'k:{"name":"a"}': { ".": {} }, |
| 888 | 'k:{"name":"b"}': { ".": {} }, |
| 889 | }), |
| 890 | ); |
| 891 | const keyedMask = driftMask(deploymentList(), keyed); |
| 892 | const keyedBaseline = yield* hashDriftSelection(keyedMask, deploymentList()); |
| 893 | expect( |
| 894 | yield* hashDriftSelection(keyedMask, { |
| 895 | ...deploymentList(), |
| 896 | spec: { template: { spec: { containers: reorderedContainers } } }, |
| 897 | }), |
| 898 | ).toBe(keyedBaseline); |
| 899 | |
| 900 | // An owned list with no k: entry is atomic until a later apply stores one. |
| 901 | const emptyOwned = deploymentList(containerFields({ ".": {} })); |
| 902 | const emptyMask = driftMask(deploymentList(), emptyOwned); |
| 903 | expect(JSON.stringify(emptyMask)).toContain('"spec.template.spec.containers":"atomic"'); |
| 904 | const emptyBaseline = yield* hashDriftSelection(emptyMask, deploymentList()); |
| 905 | expect( |
| 906 | yield* hashDriftSelection(emptyMask, { |
| 907 | ...deploymentList(), |
| 908 | spec: { template: { spec: { containers: reorderedContainers } } }, |
| 909 | }), |
| 910 | ).not.toBe(emptyBaseline); |
| 911 | }), |
| 912 | { tags: ["provider:kubernetes", "local"] }, |
| 913 | ); |
| 914 | |
| 915 | test( |
| 916 | "empty owned finalizers are a set and an appended toleration drifts", |
| 917 | Effect.gen(function* () { |
| 918 | const finalizers: string[] = []; |
| 919 | const namespace = { |
| 920 | apiVersion: "v1", |
| 921 | kind: "Namespace", |
| 922 | metadata: { name: "team", finalizers }, |
| 923 | }; |
| 924 | const finalizerWitness = (fields: ReadonlyArray<Record<string, unknown>>) => ({ |
| 925 | ...namespace, |
| 926 | metadata: { |
| 927 | ...namespace.metadata, |
| 928 | managedFields: fields.map((fieldsV1) => ({ manager: "alchemy", fieldsV1 })), |
| 929 | }, |
| 930 | }); |
| 931 | const dotOnly = { "f:metadata": { "f:finalizers": { ".": {} } } }; |
| 932 | const withMember = { |
| 933 | "f:metadata": { "f:finalizers": { ".": {}, 'v:"kubernetes"': {} } }, |
| 934 | }; |
| 935 | const emptySet = driftMask(namespace, finalizerWitness([dotOnly])); |
| 936 | const dotThenMember = driftMask(namespace, finalizerWitness([dotOnly, withMember])); |
| 937 | const memberThenDot = driftMask(namespace, finalizerWitness([withMember, dotOnly])); |
| 938 | expect(JSON.stringify(emptySet)).toContain('"metadata.finalizers":"set"'); |
| 939 | expect(JSON.stringify(dotThenMember)).toContain('"metadata.finalizers":"set"'); |
| 940 | expect(JSON.stringify(memberThenDot)).toContain('"metadata.finalizers":"set"'); |
| 941 | const addedFinalizer = { |
| 942 | ...namespace, |
| 943 | metadata: { name: "team", finalizers: ["kubernetes"] }, |
| 944 | }; |
| 945 | for (const mask of [emptySet, dotThenMember, memberThenDot]) { |
| 946 | const baseline = yield* hashDriftSelection(mask, namespace); |
| 947 | expect(yield* hashDriftSelection(mask, addedFinalizer)).toBe(baseline); |
| 948 | } |
| 949 | const kept = { |
| 950 | ...namespace, |
| 951 | metadata: { name: "team", finalizers: ["example.com/keep"] }, |
| 952 | }; |
| 953 | const keptMask = driftMask(kept, finalizerWitness([dotOnly])); |
| 954 | expect( |
| 955 | yield* hashDriftSelection(keptMask, { |
| 956 | ...kept, |
| 957 | metadata: { name: "team", finalizers: ["kubernetes"] }, |
| 958 | }), |
| 959 | ).not.toBe(yield* hashDriftSelection(keptMask, kept)); |
| 960 | |
| 961 | const codes = { |
| 962 | apiVersion: "example.com/v1", |
| 963 | kind: "Widget", |
| 964 | metadata: { name: "w" }, |
| 965 | spec: { codes: ["a"] }, |
| 966 | }; |
| 967 | const codeFields = (node: Record<string, unknown>) => ({ |
| 968 | "f:spec": { "f:codes": node }, |
| 969 | }); |
| 970 | const dotThenValue = driftMask(codes, { |
| 971 | ...codes, |
| 972 | metadata: { |
| 973 | name: "w", |
| 974 | managedFields: [ |
| 975 | { manager: "empty", fieldsV1: codeFields({ ".": {} }) }, |
| 976 | { manager: "member", fieldsV1: codeFields({ ".": {}, 'v:"a"': {} }) }, |
| 977 | ], |
| 978 | }, |
| 979 | }); |
| 980 | const valueThenDot = driftMask(codes, { |
| 981 | ...codes, |
| 982 | metadata: { |
| 983 | name: "w", |
| 984 | managedFields: [ |
| 985 | { manager: "member", fieldsV1: codeFields({ ".": {}, 'v:"a"': {} }) }, |
| 986 | { manager: "empty", fieldsV1: codeFields({ ".": {} }) }, |
| 987 | ], |
| 988 | }, |
| 989 | }); |
| 990 | expect(JSON.stringify(dotThenValue)).toContain('"spec.codes":"set"'); |
| 991 | expect(JSON.stringify(valueThenDot)).toContain('"spec.codes":"set"'); |
| 992 | const codesBaseline = yield* hashDriftSelection(dotThenValue, codes); |
| 993 | expect(yield* hashDriftSelection(dotThenValue, { ...codes, spec: { codes: ["b", "a"] } })).toBe( |
| 994 | codesBaseline, |
| 995 | ); |
| 996 | expect(yield* hashDriftSelection(valueThenDot, { ...codes, spec: { codes: ["b"] } })).not.toBe( |
| 997 | yield* hashDriftSelection(valueThenDot, codes), |
| 998 | ); |
| 999 | |
| 1000 | const pod = { |
| 1001 | apiVersion: "v1", |
| 1002 | kind: "Pod", |
| 1003 | metadata: { name: "app" }, |
| 1004 | spec: { |
| 1005 | tolerations: [{ key: "disk", operator: "Equal", value: "ssd", effect: "NoSchedule" }], |
| 1006 | }, |
| 1007 | }; |
| 1008 | const podMask = driftMask(pod, { |
| 1009 | ...pod, |
| 1010 | metadata: { |
| 1011 | name: "app", |
| 1012 | managedFields: [{ manager: "alchemy", fieldsV1: { "f:spec": { "f:tolerations": {} } } }], |
| 1013 | }, |
| 1014 | }); |
| 1015 | expect(JSON.stringify(podMask)).not.toContain('"spec.tolerations"'); |
| 1016 | const podBaseline = yield* hashDriftSelection(podMask, pod); |
| 1017 | expect( |
| 1018 | yield* hashDriftSelection(podMask, { |
| 1019 | ...pod, |
| 1020 | spec: { |
| 1021 | tolerations: [ |
| 1022 | ...pod.spec.tolerations, |
| 1023 | { key: "gpu", operator: "Exists", effect: "NoSchedule" }, |
| 1024 | ], |
| 1025 | }, |
| 1026 | }), |
| 1027 | ).not.toBe(podBaseline); |
| 1028 | }), |
| 1029 | { tags: ["provider:kubernetes", "local"] }, |
| 1030 | ); |
| 1031 | |
| 1032 | test( |
| 1033 | "a redacted set identity stays wrapped in the drift mask", |
| 1034 | Effect.gen(function* () { |
| 1035 | const token = "private-token"; |
| 1036 | const declared = { |
| 1037 | apiVersion: "example.com/v1", |
| 1038 | kind: "Widget", |
| 1039 | metadata: { name: "w" }, |
| 1040 | spec: { tokens: Redacted.make([token]) }, |
| 1041 | }; |
| 1042 | const witness = { |
| 1043 | ...declared, |
| 1044 | metadata: { |
| 1045 | name: "w", |
| 1046 | managedFields: [ |
| 1047 | { |
| 1048 | manager: "alchemy", |
| 1049 | fieldsV1: { "f:spec": { "f:tokens": { ".": {}, 'v:"private-token"': {} } } }, |
| 1050 | }, |
| 1051 | ], |
| 1052 | }, |
| 1053 | spec: { tokens: [token] }, |
| 1054 | }; |
| 1055 | const mask = driftMask(declared, witness); |
| 1056 | const revived = reviveStateRecursive(JSON.parse(JSON.stringify(encodeState(mask)))); |
| 1057 | const shown = JSON.stringify(toYamlDisplayValue(revived)); |
| 1058 | expect(shown).not.toContain(token); |
| 1059 | expect(shown).toContain("(redacted)"); |
| 1060 | expect(JSON.stringify(toYamlDisplayValue(declared))).not.toContain(token); |
| 1061 | |
| 1062 | const present = { |
| 1063 | apiVersion: "example.com/v1", |
| 1064 | kind: "Widget", |
| 1065 | metadata: { name: "w" }, |
| 1066 | spec: { tokens: [token, "other"] }, |
| 1067 | }; |
| 1068 | const baseline = yield* hashDriftSelection(revived, present); |
| 1069 | expect(yield* hashDriftSelection(mask, present)).toBe(baseline); |
| 1070 | expect( |
| 1071 | yield* hashDriftSelection(revived, { ...present, spec: { tokens: ["other"] } }), |
| 1072 | ).not.toBe(baseline); |
| 1073 | |
| 1074 | const named = { |
| 1075 | apiVersion: "apps/v1", |
| 1076 | kind: "Deployment", |
| 1077 | metadata: { name: "app" }, |
| 1078 | spec: { |
| 1079 | template: { spec: { containers: [{ name: Redacted.make(token), image: "app:1" }] } }, |
| 1080 | }, |
| 1081 | }; |
| 1082 | const namedMask = driftMask(named); |
| 1083 | expect(JSON.stringify(toYamlDisplayValue(namedMask))).not.toContain(token); |
| 1084 | const live = { |
| 1085 | ...named, |
| 1086 | spec: { template: { spec: { containers: [{ name: token, image: "app:2" }] } } }, |
| 1087 | }; |
| 1088 | expect(yield* hashDriftSelection(namedMask, live)).toBe( |
| 1089 | yield* hashDriftSelection( |
| 1090 | driftMask({ |
| 1091 | ...named, |
| 1092 | spec: { template: { spec: { containers: [{ name: token, image: "app:1" }] } } }, |
| 1093 | }), |
| 1094 | live, |
| 1095 | ), |
| 1096 | ); |
| 1097 | }), |
| 1098 | { tags: ["provider:kubernetes", "local"] }, |
| 1099 | ); |