sailfork / t3code-github / .repos/ alchemy-effect/ packages/ gcp/ src/ Container/ NodePool.ts

import * as container from "@distilled.cloud/gcp/container_v1";
import { Unowned } from "alchemy/AdoptPolicy";
import { isResolved } from "alchemy/Diff";
import { createPhysicalName } from "alchemy/PhysicalName";
import * as Provider from "alchemy/Provider";
import { Resource } from "alchemy/Resource";
import { tagRecord } from "alchemy/Tags";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";
import { GcpEnvironment } from "../Environment.ts";
import {
  createInternalLabels,
  diffLabels,
  hasAlchemyLabels,
  stripInternalLabels,
  toLabels,
} from "../Labels.ts";
import { waitForOperation as waitForGcpOperation } from "../Operation.ts";
import { matchesDesired } from "../Proto.ts";
import type { Providers } from "../Providers.ts";

// A zone, not a region: `GCP.Region` has no zone and a regional default would triple node count.
const DEFAULT_LOCATION = "us-central1-a";
const DEFAULT_MACHINE_TYPE = "e2-medium";
const DEFAULT_DISK_TYPE = "pd-standard";
const DEFAULT_DISK_SIZE_GB = 20;
const DEFAULT_NODE_COUNT = 1;
const MAX_NAME_LENGTH = 40;

export type NodePoolAutoscaling = {
  /** Enable the cluster autoscaler for this pool. */
  enabled?: boolean;
  /** Minimum nodes per zone. Mutually exclusive with `totalMinNodeCount`. */
  minNodeCount?: number;
  /** Maximum nodes per zone. Mutually exclusive with `totalMaxNodeCount`. */
  maxNodeCount?: number;
  /** Minimum nodes across all zones. */
  totalMinNodeCount?: number;
  /** Maximum nodes across all zones. */
  totalMaxNodeCount?: number;
  /** Scale-up location policy (`BALANCED`, `ANY`). */
  locationPolicy?: container.NodePoolAutoscalingLocationPolicyEnum | (string & {});
  /** Allow NAP to delete this pool. */
  autoprovisioned?: boolean;
};

export type NodePoolManagement = {
  /** Automatically repair unhealthy nodes. */
  autoRepair?: boolean;
  /** Automatically upgrade nodes. */
  autoUpgrade?: boolean;
};

export type NodePoolUpgradeSettings = {
  /** Extra nodes created during an upgrade. */
  maxSurge?: number;
  /** Nodes allowed to be unavailable during an upgrade. */
  maxUnavailable?: number;
  /** Upgrade strategy (`SURGE`, `BLUE_GREEN`). */
  strategy?: container.UpgradeSettingsStrategyEnum | (string & {});
};

export type NodePoolTaint = {
  /** Taint key. */
  key?: string;
  /** Taint value. */
  value?: string;
  /** Taint effect (`NO_SCHEDULE`, `PREFER_NO_SCHEDULE`, `NO_EXECUTE`). */
  effect?: container.NodeTaintEffectEnum | (string & {});
};

export type NodePoolProps = {
  /**
   * Parent cluster id or full resource name
   * (`projects/{project}/locations/{location}/clusters/{cluster}`).
   * Immutable — changing it replaces the node pool.
   */
  cluster: string;
  /**
   * Zone or region of the parent cluster (`us-central1-a`, `us-central1`,
   * …). Ignored when `cluster` is a full resource name. Immutable —
   * changing it replaces the node pool.
   * @default "us-central1-a"
   */
  location?: string;
  /**
   * Node pool id (the `{nodePool}` segment of
   * `.../clusters/{cluster}/nodePools/{nodePool}`). If omitted, a unique
   * RFC1035 name is generated. Must be 1-40 characters and match
   * `[a-z]([a-z0-9-]*[a-z0-9])?`. Immutable — changing it replaces the
   * pool.
   */
  nodePoolId?: string;
  /**
   * Desired node count (per zone for zonal/multi-zonal pools). Create
   * uses `initialNodeCount`; later changes call `setSize`. Ignored while
   * autoscaling is enabled.
   * @default 1
   */
  nodeCount?: number;
  /**
   * User labels stored as GKE `config.resourceLabels` on the pool's GCE
   * VMs. Alchemy ownership labels are merged in automatically.
   */
  labels?: Record<string, string>;
  /**
   * Machine type (`e2-medium`, `e2-small`, …).
   * @default "e2-medium"
   */
  machineType?: string;
  /**
   * Boot disk size in GB. Smallest allowed is 10.
   * @default 20
   */
  diskSizeGb?: number;
  /**
   * Boot disk type (`pd-standard`, `pd-balanced`, `pd-ssd`).
   * @default "pd-standard"
   */
  diskType?: string;
  /**
   * Node image type (`COS_CONTAINERD`, `UBUNTU_CONTAINERD`, …).
   */
  imageType?: string;
  /**
   * Kubernetes version for nodes (or alias such as `"-"` for the
   * control-plane version).
   */
  version?: string;
  /**
   * Use Spot VMs. Immutable — changing it replaces the pool.
   * @default false
   */
  spot?: boolean;
  /**
   * Use preemptible VMs. Immutable — changing it replaces the pool.
   * @default false
   */
  preemptible?: boolean;
  /**
   * Zones in which this pool's nodes should run.
   */
  nodeLocations?: string[];
  /**
   * Kubernetes labels applied to each node.
   */
  nodeLabels?: Record<string, string>;
  /**
   * Compute Engine instance metadata applied to each node. Changing it
   * replaces the pool.
   */
  metadata?: Record<string, string>;
  /**
   * Workload Identity metadata exposure mode for each node.
   * @default `{ mode: "GKE_METADATA" }` when the cluster has Workload Identity
   */
  workloadMetadataConfig?: container.WorkloadMetadataConfig;
  /**
   * Shielded VM settings applied to each node. Changing them replaces the
   * pool.
   */
  shieldedInstanceConfig?: container.ShieldedInstanceConfig;
  /**
   * Advanced Compute Engine VM features, such as nested virtualization.
   * Changing them replaces the pool (GKE cannot update them in place).
   */
  advancedMachineFeatures?: container.AdvancedMachineFeatures;
  /**
   * Network tags applied to each node.
   */
  tags?: string[];
  /**
   * Kubernetes taints applied to each node.
   */
  taints?: NodePoolTaint[];
  /**
   * OAuth scopes for the node service account. Immutable — changing them
   * replaces the pool.
   */
  oauthScopes?: string[];
  /**
   * Service account email for node VMs. Immutable — changing it replaces
   * the pool.
   */
  serviceAccount?: string;
  /**
   * Local SSD count. Immutable — changing it replaces the pool.
   */
  localSsdCount?: number;
  /**
   * Customer-managed KMS key for boot disks. Immutable — changing it
   * replaces the pool.
   */
  bootDiskKmsKey?: string;
  /**
   * Max pods per node. Immutable — changing it replaces the pool.
   */
  maxPodsPerNode?: number;
  /**
   * Cluster autoscaler settings for this pool.
   */
  autoscaling?: NodePoolAutoscaling;
  /**
   * Auto-repair / auto-upgrade settings.
   */
  management?: NodePoolManagement;
  /**
   * Upgrade surge settings.
   */
  upgradeSettings?: NodePoolUpgradeSettings;
};

export type NodePool = Resource<
  "GCP.Container.NodePool",
  NodePoolProps,
  {
    /** Full resource name `projects/{project}/locations/{location}/clusters/{cluster}/nodePools/{nodePool}`. */
    name: string;
    /** Node pool id (last path segment). */
    nodePoolId: string;
    /** Parent cluster id. */
    clusterId: string;
    /** Parent cluster resource name. */
    clusterName: string;
    /** Project id. */
    project: string;
    /** Zone or region id. */
    location: string;
    /** Server-reported status (`RUNNING`, `PROVISIONING`, …). */
    status: string | undefined;
    /** Kubernetes version currently on the nodes. */
    version: string | undefined;
    /** Desired / initial node count. */
    nodeCount: number;
    /** Machine type. */
    machineType: string | undefined;
    /** Boot disk size in GB. */
    diskSizeGb: number | undefined;
    /** Boot disk type. */
    diskType: string | undefined;
    /** Node image type. */
    imageType: string | undefined;
    /** Whether nodes are Spot VMs. */
    spot: boolean;
    /** Whether nodes are preemptible VMs. */
    preemptible: boolean;
    /** User GCP labels (Alchemy ownership labels stripped). */
    labels: Record<string, string>;
    /** Kubernetes node labels. */
    nodeLabels: Record<string, string>;
    /** Compute Engine instance metadata (including GKE-injected keys). */
    metadata: Record<string, string>;
    /** Workload Identity metadata exposure mode. */
    workloadMetadataConfig: container.WorkloadMetadataConfig | undefined;
    /** Shielded VM settings. */
    shieldedInstanceConfig: container.ShieldedInstanceConfig | undefined;
    /** Advanced Compute Engine VM features. */
    advancedMachineFeatures: container.AdvancedMachineFeatures | undefined;
    /** Network tags. */
    tags: string[];
    /** Node locations. */
    nodeLocations: string[];
    /** Autoscaler settings currently applied. */
    autoscaling: NodePoolAutoscaling | undefined;
    /** Management settings currently applied. */
    management: NodePoolManagement | undefined;
    /** Managed instance group URLs. */
    instanceGroupUrls: string[];
    /** Server-defined URL. */
    selfLink: string | undefined;
  },
  never,
  Providers
>;

/**
 * A Google Kubernetes Engine node pool.
 *
 * Node pools belong to a {@link Cluster}. Changing `cluster`, `location`,
 * `nodePoolId`, `spot`, `preemptible`, `serviceAccount`, `oauthScopes`,
 * `localSsdCount`, `bootDiskKmsKey`, `maxPodsPerNode`, `metadata`,
 * `shieldedInstanceConfig`, or `advancedMachineFeatures` replaces the pool.
 * Size, labels, machine type, disk, image, version, workload metadata,
 * management, autoscaling, and upgrade settings update in place.
 *
 * Provisioning typically takes several minutes.
 *
 * ### Creating a Node Pool
 * **Example:** Generated name on an existing cluster
 * ```typescript
 * const cluster = yield* GCP.Container.Cluster("App", {});
 * const pool = yield* GCP.Container.NodePool("Workers", {
 *   cluster: cluster.clusterId,
 *   location: cluster.location,
 * });
 * ```
 *
 * **Example:** Explicit id, Spot nodes, and labels
 * ```typescript
 * const pool = yield* GCP.Container.NodePool("Workers", {
 *   cluster: cluster.name,
 *   nodePoolId: "app-workers",
 *   nodeCount: 1,
 *   machineType: "e2-small",
 *   diskSizeGb: 20,
 *   spot: true,
 *   labels: { env: "prod" },
 * });
 * ```
 *
 * ### Autoscaling
 * **Example:** Per-zone autoscaling
 * ```typescript
 * const pool = yield* GCP.Container.NodePool("Workers", {
 *   cluster: cluster.clusterId,
 *   location: cluster.location,
 *   autoscaling: { enabled: true, minNodeCount: 1, maxNodeCount: 3 },
 * });
 * ```
 *
 * ### Reading a Node Pool
 * **Example:** Get the bound node pool
 * ```typescript
 * const getNodePool = yield* GCP.Container.GetNodePool(pool);
 * const live = yield* getNodePool();
 * ```
 *
 * @resource
 * @category Container
 */
export const NodePool = Resource<NodePool>("GCP.Container.NodePool");

export class NodePoolNotResolved extends Data.TaggedError("GCP.Container.NodePoolNotResolved")<{
  resourceName: string;
}> {}

export class NodePoolClusterMissing extends Data.TaggedError(
  "GCP.Container.NodePoolClusterMissing",
)<{
  message: string;
}> {}

export class NodePoolNotReady extends Data.TaggedError("GCP.Container.NodePoolNotReady")<{
  resourceName: string;
  status: string;
}> {}

export class NodePoolOperationFailed extends Data.TaggedError(
  "GCP.Container.NodePoolOperationFailed",
)<{
  operation: string;
  message: string;
}> {}

export class NodePoolStillExists extends Data.TaggedError("GCP.Container.NodePoolStillExists")<{
  resourceName: string;
}> {}

const lastSegment = (value: string) => {
  const trimmed = value.replace(/\/+$/, "");
  const parts = trimmed.split("/");
  return parts[parts.length - 1] || trimmed;
};

const normalizeLocation = (location: string | undefined) =>
  lastSegment(location ?? DEFAULT_LOCATION).toLowerCase();

const rfc1035 = (name: string): string => {
  let next = name
    .toLowerCase()
    .replace(/[^a-z0-9-]/g, "-")
    .replace(/-+/g, "-")
    .replace(/^-+|-+$/g, "");
  if (!/^[a-z]/.test(next)) next = `n${next}`;
  next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/g, "");
  if (next.length === 0) return "nodepool";
  if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, MAX_NAME_LENGTH - 1)}0`;
  return next.slice(0, MAX_NAME_LENGTH);
};

const clusterNameOf = (project: string, location: string, clusterId: string) =>
  `projects/${project}/locations/${location}/clusters/${clusterId}`;

const resourceName = (project: string, location: string, clusterId: string, nodePoolId: string) =>
  `${clusterNameOf(project, location, clusterId)}/nodePools/${nodePoolId}`;

const parseName = (name: string) => {
  const idx = name.indexOf("projects/");
  const path = idx >= 0 ? name.slice(idx) : name;
  const parts = path.split("/").filter((part) => part.length > 0);
  const poolsAt = parts.lastIndexOf("nodePools");
  const clustersAt = parts.lastIndexOf("clusters");
  // `selfLink` spells the location as `zones/{zone}` (zonal clusters) or
  // `regions/{region}`; resource names use `locations/{location}`.
  const locationsAt = Math.max(
    parts.lastIndexOf("locations"),
    parts.lastIndexOf("zones"),
    parts.lastIndexOf("regions"),
  );
  const projectsAt = parts.lastIndexOf("projects");
  return {
    project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
    location: locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : "",
    clusterId: clustersAt >= 0 && parts[clustersAt + 1] ? parts[clustersAt + 1]! : "",
    nodePoolId: poolsAt >= 0 && parts[poolsAt + 1] ? parts[poolsAt + 1]! : lastSegment(name),
  };
};

const parseClusterRef = (
  cluster: string,
  fallbackProject: string,
  fallbackLocation: string | undefined,
) => {
  const trimmed = cluster.trim();
  if (trimmed.includes("/clusters/") || trimmed.includes("projects/")) {
    const parsed = parseName(trimmed.includes("/nodePools/") ? trimmed : `${trimmed}/nodePools/_`);
    return {
      project: parsed.project || fallbackProject,
      location: normalizeLocation(parsed.location || fallbackLocation),
      clusterId: parsed.clusterId,
    };
  }
  return {
    project: fallbackProject,
    location: normalizeLocation(fallbackLocation),
    clusterId: lastSegment(trimmed),
  };
};

const userLabels = (
  labels: Record<string, string | undefined> | null | undefined,
): Record<string, string> => stripInternalLabels(tagRecord(labels));

const stringMap = (
  labels: Record<string, string | undefined> | null | undefined,
): Record<string, string> => tagRecord(labels);

const sameStrings = (left: readonly string[], right: readonly string[]) => {
  if (left.length !== right.length) return false;
  const a = [...left].map((item) => item.toLowerCase()).sort();
  const b = [...right].map((item) => item.toLowerCase()).sort();
  return a.every((value, index) => value === b[index]);
};

const sameStringMap = (
  left: Record<string, string> | undefined,
  right: Record<string, string> | undefined,
) => {
  const a = stringMap(left);
  const b = stringMap(right);
  const keys = Object.keys(a);
  return keys.length === Object.keys(b).length && keys.every((key) => a[key] === b[key]);
};

const taintKey = (taint: NodePoolTaint) =>
  `${taint.key ?? ""}\0${taint.value ?? ""}\0${(taint.effect ?? "").toUpperCase()}`;

const sameTaints = (
  left: readonly NodePoolTaint[] | undefined,
  right: readonly NodePoolTaint[] | undefined,
) => {
  const a = [...(left ?? [])].map(taintKey).sort();
  const b = [...(right ?? [])].map(taintKey).sort();
  return a.length === b.length && a.every((value, index) => value === b[index]);
};

const autoscalingKey = (value: NodePoolAutoscaling | undefined) =>
  JSON.stringify({
    enabled: value?.enabled === true,
    minNodeCount: value?.minNodeCount ?? 0,
    maxNodeCount: value?.maxNodeCount ?? 0,
    totalMinNodeCount: value?.totalMinNodeCount ?? 0,
    totalMaxNodeCount: value?.totalMaxNodeCount ?? 0,
    locationPolicy: (value?.locationPolicy ?? "").toUpperCase(),
    autoprovisioned: value?.autoprovisioned === true,
  });

const managementKey = (value: NodePoolManagement | undefined) =>
  JSON.stringify({
    autoRepair: value?.autoRepair === true,
    autoUpgrade: value?.autoUpgrade === true,
  });

const upgradeKey = (value: NodePoolUpgradeSettings | undefined) =>
  JSON.stringify({
    maxSurge: value?.maxSurge ?? 0,
    maxUnavailable: value?.maxUnavailable ?? 0,
    strategy: (value?.strategy ?? "").toUpperCase(),
  });

const toId = (id: string, nodePoolId: string | undefined, existing?: string) =>
  Effect.gen(function* () {
    return (
      nodePoolId ??
      existing ??
      rfc1035(
        yield* createPhysicalName({
          id,
          maxLength: MAX_NAME_LENGTH,
          lowercase: true,
        }),
      )
    );
  });

const toAttrs = (
  pool: container.NodePool,
  project: string,
  location: string,
  clusterId: string,
) => {
  const selfLink = pool.selfLink ?? "";
  const parsed = parseName(
    selfLink.length > 0 ? selfLink : resourceName(project, location, clusterId, pool.name ?? ""),
  );
  const nodePoolId = pool.name || parsed.nodePoolId;
  const resolvedProject = parsed.project || project;
  const resolvedLocation = normalizeLocation(parsed.location || location);
  const resolvedCluster = parsed.clusterId || clusterId;
  const config = pool.config;
  return {
    name: resourceName(resolvedProject, resolvedLocation, resolvedCluster, nodePoolId),
    nodePoolId,
    clusterId: resolvedCluster,
    clusterName: clusterNameOf(resolvedProject, resolvedLocation, resolvedCluster),
    project: resolvedProject,
    location: resolvedLocation,
    status: pool.status,
    version: pool.version,
    // The API omits a zero count.
    nodeCount: pool.initialNodeCount ?? 0,
    machineType: config?.machineType,
    diskSizeGb: config?.diskSizeGb,
    diskType: config?.diskType,
    imageType: config?.imageType,
    spot: config?.spot === true,
    preemptible: config?.preemptible === true,
    labels: userLabels(config?.resourceLabels),
    nodeLabels: stringMap(config?.labels),
    metadata: stringMap(config?.metadata),
    workloadMetadataConfig: config?.workloadMetadataConfig,
    shieldedInstanceConfig: config?.shieldedInstanceConfig,
    advancedMachineFeatures: config?.advancedMachineFeatures,
    tags: [...(config?.tags ?? [])],
    nodeLocations: [...(pool.locations ?? [])],
    autoscaling: pool.autoscaling,
    management: pool.management
      ? {
          autoRepair: pool.management.autoRepair,
          autoUpgrade: pool.management.autoUpgrade,
        }
      : undefined,
    instanceGroupUrls: [...(pool.instanceGroupUrls ?? [])],
    selfLink: pool.selfLink,
  };
};

const getByName = (name: string) =>
  container
    .getProjectsLocationsClustersNodePools({ name })
    .pipe(Effect.catchTag("NotFound", () => Effect.undefined));

const operationResourceName = (
  project: string,
  location: string,
  operation: container.Operation,
) => {
  const raw = operation.name ?? "";
  if (raw.includes("/operations/")) {
    const idx = raw.indexOf("projects/");
    return idx >= 0 ? raw.slice(idx) : raw;
  }
  const fromLink = operation.selfLink ?? "";
  if (fromLink.includes("/operations/")) {
    const idx = fromLink.indexOf("projects/");
    return idx >= 0 ? fromLink.slice(idx) : fromLink;
  }
  const loc = lastSegment(operation.location ?? operation.zone ?? location);
  return `projects/${project}/locations/${loc}/operations/${lastSegment(raw)}`;
};

/**
 * Wait for a GKE node-pool operation (create/delete takes several
 * minutes). ALREADY_EXISTS (code 6) counts as success (create race); with
 * `notFoundOk`, so does NOT_FOUND (code 5, delete race) and an operation
 * that is already gone.
 */
const waitForOperation = (
  project: string,
  location: string,
  operation: container.Operation,
  options?: { notFoundOk?: boolean },
) =>
  waitForGcpOperation(
    {
      ...operation,
      name: operationResourceName(project, location, operation),
    },
    (name) => container.getProjectsLocationsOperations({ name }),
    { budget: "20 minutes", interval: "10 seconds" },
  ).pipe(
    Effect.catchIf(
      (error) =>
        (error._tag === "GCP.OperationFailed" &&
          (error.code === 6 || (options?.notFoundOk === true && error.code === 5))) ||
        (options?.notFoundOk === true && error._tag === "NotFound"),
      () => Effect.succeed(operation),
    ),
  );

const waitUntilExists = (name: string) =>
  getByName(name).pipe(
    Effect.filterOrFail(
      (pool): pool is container.NodePool => pool !== undefined,
      () => new NodePoolNotResolved({ resourceName: name }),
    ),
    Effect.retry({
      while: (error) => error._tag === "GCP.Container.NodePoolNotResolved",
      times: 8,
      schedule: Schedule.spaced("1 second"),
    }),
  );

const waitUntilReady = (name: string) =>
  getByName(name).pipe(
    Effect.filterOrFail(
      (pool): pool is container.NodePool => pool !== undefined,
      () => new NodePoolNotResolved({ resourceName: name }),
    ),
    Effect.filterOrFail(
      (pool) => {
        const status = pool.status ?? "STATUS_UNSPECIFIED";
        return status !== "ERROR" && status !== "STOPPING";
      },
      (pool) =>
        new NodePoolOperationFailed({
          operation: name,
          message: pool.statusMessage ?? `node pool is in ${pool.status ?? "STATUS_UNSPECIFIED"}`,
        }),
    ),
    Effect.filterOrFail(
      (pool) => {
        const status = pool.status ?? "STATUS_UNSPECIFIED";
        return status === "RUNNING" || status === "RUNNING_WITH_ERROR";
      },
      (pool) =>
        new NodePoolNotReady({
          resourceName: name,
          status: pool.status ?? "STATUS_UNSPECIFIED",
        }),
    ),
    Effect.retry({
      while: (error) =>
        error._tag === "GCP.Container.NodePoolNotReady" ||
        error._tag === "GCP.Container.NodePoolNotResolved",
      // Node pool create/delete takes several minutes.
      times: 60,
      schedule: Schedule.spaced("10 seconds"),
    }),
  );

const waitUntilGone = (name: string) =>
  getByName(name).pipe(
    Effect.flatMap((pool) =>
      pool === undefined
        ? Effect.void
        : Effect.fail(new NodePoolStillExists({ resourceName: name })),
    ),
    Effect.retry({
      while: (error) => error._tag === "GCP.Container.NodePoolStillExists",
      // Node pool create/delete takes several minutes.
      times: 60,
      schedule: Schedule.spaced("10 seconds"),
    }),
  );

const retryConflict = <A, E extends { readonly _tag: string }, R>(effect: Effect.Effect<A, E, R>) =>
  effect.pipe(
    Effect.retry({
      while: (error) => error._tag === "Conflict",
      times: 8,
      schedule: Schedule.spaced("5 seconds"),
    }),
  );

const toCreatePool = (
  news: NodePoolProps,
  nodePoolId: string,
  desiredLabels: Record<string, string>,
  workloadIdentity: boolean,
): container.NodePool => ({
  name: nodePoolId,
  initialNodeCount: news.nodeCount ?? DEFAULT_NODE_COUNT,
  config: {
    machineType: news.machineType ?? DEFAULT_MACHINE_TYPE,
    diskSizeGb: news.diskSizeGb ?? DEFAULT_DISK_SIZE_GB,
    diskType: news.diskType ?? DEFAULT_DISK_TYPE,
    imageType: news.imageType,
    spot: news.spot === true,
    preemptible: news.preemptible === true,
    resourceLabels: desiredLabels,
    labels: news.nodeLabels,
    metadata: news.metadata,
    tags: news.tags,
    taints: news.taints,
    oauthScopes: news.oauthScopes,
    serviceAccount: news.serviceAccount,
    localSsdCount: news.localSsdCount,
    bootDiskKmsKey: news.bootDiskKmsKey,
    // GKE rejects GKE_METADATA on clusters without Workload Identity.
    workloadMetadataConfig:
      news.workloadMetadataConfig ?? (workloadIdentity ? { mode: "GKE_METADATA" } : undefined),
    shieldedInstanceConfig: news.shieldedInstanceConfig,
    advancedMachineFeatures: news.advancedMachineFeatures,
  },
  autoscaling: news.autoscaling,
  management: news.management,
  version: news.version,
  locations: news.nodeLocations,
  maxPodsConstraint:
    news.maxPodsPerNode !== undefined ? { maxPodsPerNode: String(news.maxPodsPerNode) } : undefined,
  upgradeSettings: news.upgradeSettings,
});

const poolLabels = (pool: container.NodePool) => tagRecord(pool.config?.resourceLabels);

const systemLabels = (labels: Record<string, string>) =>
  Object.fromEntries(Object.entries(labels).filter(([key]) => key.startsWith("goog-")));

const shortName = (value: string | undefined, fallback: string) => lastSegment(value ?? fallback);

const desiredManagement = (
  news: NodePoolManagement,
  observed: NodePoolManagement | undefined,
): NodePoolManagement => ({
  autoRepair: news.autoRepair ?? observed?.autoRepair,
  autoUpgrade: news.autoUpgrade ?? observed?.autoUpgrade,
});

export const NodePoolProvider = () =>
  Provider.succeed(NodePool, {
    stables: ["name", "nodePoolId", "clusterId", "clusterName", "project", "location", "selfLink"],

    diff: Effect.fn(function* ({ news, olds, output }) {
      if (!isResolved(news)) return undefined;
      if (news.cluster === undefined || news.cluster.length === 0) {
        return undefined;
      }

      const previousId = olds?.nodePoolId ?? output?.nodePoolId;
      const nextId = news.nodePoolId ?? previousId;
      const previousCluster = lastSegment(olds?.cluster ?? output?.clusterId ?? "");
      const nextCluster = lastSegment(
        parseClusterRef(news.cluster, "", news.location ?? output?.location).clusterId,
      );
      const previousLocation = normalizeLocation(olds?.location ?? output?.location);
      const nextLocation = normalizeLocation(
        parseClusterRef(news.cluster, "", news.location ?? output?.location).location,
      );
      const previousSpot = olds?.spot === true || output?.spot === true;
      const nextSpot = news.spot === true;
      const previousPreemptible = olds?.preemptible === true || output?.preemptible === true;
      const nextPreemptible = news.preemptible === true;
      const previousSa = olds?.serviceAccount ?? "";
      const nextSa = news.serviceAccount ?? previousSa;
      const previousKms = olds?.bootDiskKmsKey ?? "";
      const nextKms = news.bootDiskKmsKey ?? previousKms;
      const previousSsds = olds?.localSsdCount ?? 0;
      const nextSsds = news.localSsdCount ?? previousSsds;
      const previousPods = olds?.maxPodsPerNode;
      const nextPods = news.maxPodsPerNode ?? previousPods;
      const previousScopes = olds?.oauthScopes ?? [];
      const nextScopes = news.oauthScopes ?? previousScopes;
      // Observed node config is the baseline: GKE injects metadata keys and
      // Shielded VM defaults the user never declared, so a redeploy that
      // spells them out must not replace the pool.
      const previousMetadata = output?.metadata ?? olds?.metadata ?? {};
      const previousShielded = output?.shieldedInstanceConfig ?? olds?.shieldedInstanceConfig;
      const previousAdvanced = output?.advancedMachineFeatures ?? olds?.advancedMachineFeatures;

      const replace =
        (previousId !== undefined && nextId !== undefined && nextId !== previousId) ||
        (previousCluster.length > 0 && nextCluster.length > 0 && previousCluster !== nextCluster) ||
        previousLocation !== nextLocation ||
        previousSpot !== nextSpot ||
        previousPreemptible !== nextPreemptible ||
        previousSa !== nextSa ||
        previousKms !== nextKms ||
        previousSsds !== nextSsds ||
        (previousPods !== undefined && nextPods !== undefined && previousPods !== nextPods) ||
        (news.oauthScopes !== undefined && !sameStrings(previousScopes, nextScopes)) ||
        (news.metadata !== undefined && !matchesDesired(previousMetadata, news.metadata)) ||
        (news.shieldedInstanceConfig !== undefined &&
          !matchesDesired(previousShielded, news.shieldedInstanceConfig)) ||
        (news.advancedMachineFeatures !== undefined &&
          !matchesDesired(previousAdvanced, news.advancedMachineFeatures));

      if (!replace) return undefined;
      return {
        action: "replace" as const,
        deleteFirst:
          previousLocation === nextLocation &&
          previousCluster === nextCluster &&
          previousId !== undefined &&
          nextId === previousId,
      };
    }),

    read: Effect.fn(function* ({ id, olds, output }) {
      const env = yield* GcpEnvironment.current;
      const nodePoolId = yield* toId(id, olds?.nodePoolId, output?.nodePoolId);
      const clusterRef =
        (typeof olds?.cluster === "string" ? olds.cluster : undefined) ??
        output?.clusterName ??
        output?.clusterId ??
        "";
      // A pool whose cluster never resolved (e.g. the cluster create failed)
      // was never created and cannot be looked up.
      if (output?.name === undefined && clusterRef.trim().length === 0) {
        return undefined;
      }
      const ref = parseClusterRef(clusterRef, env.project, olds?.location ?? output?.location);
      const name =
        output?.name ?? resourceName(ref.project, ref.location, ref.clusterId, nodePoolId);
      const existing = yield* getByName(name);
      if (existing === undefined) return undefined;
      const attrs = toAttrs(existing, ref.project, ref.location, ref.clusterId);
      return (yield* hasAlchemyLabels(id, poolLabels(existing))) ? attrs : Unowned(attrs);
    }),

    list: () =>
      Effect.gen(function* () {
        const env = yield* GcpEnvironment.current;
        const page = yield* container
          .listProjectsLocationsClusters({
            parent: `projects/${env.project}/locations/-`,
          })
          .pipe(
            Effect.catchTag("NotFound", () =>
              Effect.succeed({
                clusters: [],
              } satisfies container.ListClustersResponse),
            ),
          );

        const found: ReturnType<typeof toAttrs>[] = [];
        for (const cluster of page.clusters ?? []) {
          const parsed = parseName(
            cluster.selfLink ??
              resourceName(
                env.project,
                cluster.location ?? DEFAULT_LOCATION,
                cluster.name ?? "",
                "_",
              ),
          );
          const project = parsed.project || env.project;
          const location = normalizeLocation(cluster.location ?? parsed.location);
          const clusterId = cluster.name || parsed.clusterId;
          const nested = cluster.nodePools;
          const pools =
            nested !== undefined
              ? nested
              : yield* container
                  .listProjectsLocationsClustersNodePools({
                    parent: clusterNameOf(project, location, clusterId),
                  })
                  .pipe(
                    Effect.map((response) => response.nodePools ?? []),
                    Effect.catchTag("NotFound", () => Effect.succeed([] as container.NodePool[])),
                  );
          for (const pool of pools) {
            if (
              Object.keys(pool.config?.resourceLabels ?? {}).some((key) =>
                key.startsWith("alchemy-"),
              )
            ) {
              found.push(toAttrs(pool, project, location, clusterId));
            }
          }
        }
        return found;
      }),

    reconcile: Effect.fn(function* ({ id, news, output }) {
      const env = yield* GcpEnvironment.current;
      if (news.cluster === undefined || news.cluster.length === 0) {
        return yield* new NodePoolClusterMissing({
          message: "GCP.Container.NodePool requires `cluster` (cluster id or full resource name).",
        });
      }
      const ref = parseClusterRef(news.cluster, env.project, news.location ?? output?.location);
      const nodePoolId = yield* toId(id, news.nodePoolId, output?.nodePoolId);
      const name = resourceName(ref.project, ref.location, ref.clusterId, nodePoolId);
      const parent = clusterNameOf(ref.project, ref.location, ref.clusterId);
      const desiredLabels = {
        ...toLabels(news.labels),
        ...(yield* createInternalLabels(id)),
      };
      const nodeCount = news.nodeCount ?? DEFAULT_NODE_COUNT;
      const machineType = news.machineType ?? DEFAULT_MACHINE_TYPE;
      const diskSizeGb = news.diskSizeGb ?? DEFAULT_DISK_SIZE_GB;
      const diskType = news.diskType ?? DEFAULT_DISK_TYPE;

      let current = yield* getByName(output?.name ?? name);

      if (current === undefined) {
        const cluster = yield* container
          .getProjectsLocationsClusters({ name: parent })
          .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
        const workloadIdentity = (cluster?.workloadIdentityConfig?.workloadPool ?? "").length > 0;
        const created = yield* retryConflict(
          container
            .createProjectsLocationsClustersNodePools({
              parent,
              body: {
                nodePool: toCreatePool(news, nodePoolId, desiredLabels, workloadIdentity),
              },
            })
            .pipe(Effect.catchTag("Conflict", () => Effect.undefined)),
        );
        if (created !== undefined) {
          yield* waitForOperation(ref.project, ref.location, created);
        }
        current = yield* waitUntilExists(name);
      }

      if (current === undefined) {
        return yield* new NodePoolNotResolved({ resourceName: name });
      }

      let live = current;
      const status = live.status ?? "STATUS_UNSPECIFIED";
      if (status !== "RUNNING" && status !== "RUNNING_WITH_ERROR") {
        live = yield* waitUntilReady(name);
      }

      const apply = (operation: container.Operation) =>
        Effect.gen(function* () {
          yield* waitForOperation(ref.project, ref.location, operation);
          return yield* waitUntilReady(name);
        });

      const observedLabels = poolLabels(live);
      const nextLabels = { ...systemLabels(observedLabels), ...desiredLabels };
      const { upsert, removed } = diffLabels(observedLabels, nextLabels);
      const labelsChanged = upsert.length > 0 || removed.length > 0;
      const observedMachine = shortName(live.config?.machineType, DEFAULT_MACHINE_TYPE);
      const machineChanged = observedMachine.toLowerCase() !== machineType.toLowerCase();
      const observedDiskSize = live.config?.diskSizeGb ?? DEFAULT_DISK_SIZE_GB;
      const diskSizeChanged = observedDiskSize !== diskSizeGb;
      const observedDiskType = shortName(live.config?.diskType, DEFAULT_DISK_TYPE).toLowerCase();
      const diskTypeChanged = observedDiskType !== diskType.toLowerCase();
      const imageChanged =
        news.imageType !== undefined && (live.config?.imageType ?? "") !== news.imageType;
      const versionChanged = news.version !== undefined && (live.version ?? "") !== news.version;
      const nodeLabelsChanged =
        news.nodeLabels !== undefined &&
        !sameStringMap(stringMap(live.config?.labels), news.nodeLabels);
      const tagsChanged =
        news.tags !== undefined && !sameStrings(live.config?.tags ?? [], news.tags);
      const taintsChanged =
        news.taints !== undefined && !sameTaints(live.config?.taints, news.taints);
      const workloadMetadataChanged =
        news.workloadMetadataConfig !== undefined &&
        !matchesDesired(live.config?.workloadMetadataConfig, news.workloadMetadataConfig);
      const upgradeChanged =
        news.upgradeSettings !== undefined &&
        upgradeKey(live.upgradeSettings) !== upgradeKey(news.upgradeSettings);
      const locationsChanged =
        news.nodeLocations !== undefined && !sameStrings(live.locations ?? [], news.nodeLocations);

      const configChanged =
        labelsChanged ||
        machineChanged ||
        diskSizeChanged ||
        diskTypeChanged ||
        imageChanged ||
        versionChanged ||
        nodeLabelsChanged ||
        tagsChanged ||
        taintsChanged ||
        workloadMetadataChanged ||
        upgradeChanged;

      if (configChanged) {
        const imageType = news.imageType ?? live.config?.imageType;
        const nodeVersion = news.version ?? live.version;
        const body: container.UpdateNodePoolRequest = {};
        if (imageType !== undefined) body.imageType = imageType;
        if (nodeVersion !== undefined) body.nodeVersion = nodeVersion;
        if (labelsChanged) {
          body.resourceLabels = { labels: nextLabels };
        }
        if (machineChanged) body.machineType = machineType;
        if (diskSizeChanged) body.diskSizeGb = String(diskSizeGb);
        if (diskTypeChanged) body.diskType = diskType;
        if (nodeLabelsChanged) {
          body.labels = { labels: news.nodeLabels };
        }
        if (tagsChanged) body.tags = { tags: news.tags };
        if (taintsChanged) body.taints = { taints: news.taints };
        if (workloadMetadataChanged) {
          body.workloadMetadataConfig = news.workloadMetadataConfig;
        }
        if (upgradeChanged) body.upgradeSettings = news.upgradeSettings;
        const updated = yield* retryConflict(
          container.updateProjectsLocationsClustersNodePools({
            name,
            body,
          }),
        );
        live = yield* apply(updated);
      }

      if (locationsChanged) {
        const imageType = news.imageType ?? live.config?.imageType;
        const nodeVersion = news.version ?? live.version;
        const relocated = yield* retryConflict(
          container.updateProjectsLocationsClustersNodePools({
            name,
            body: {
              locations: news.nodeLocations,
              ...(imageType !== undefined ? { imageType } : {}),
              ...(nodeVersion !== undefined ? { nodeVersion } : {}),
            },
          }),
        );
        live = yield* apply(relocated);
      }

      if (
        news.autoscaling !== undefined &&
        autoscalingKey(live.autoscaling) !== autoscalingKey(news.autoscaling)
      ) {
        const scaled = yield* retryConflict(
          container.setAutoscalingProjectsLocationsClustersNodePools({
            name,
            body: { autoscaling: news.autoscaling },
          }),
        );
        live = yield* apply(scaled);
      }

      if (news.management !== undefined) {
        const nextManagement = desiredManagement(news.management, live.management);
        if (managementKey(live.management) !== managementKey(nextManagement)) {
          const managed = yield* retryConflict(
            container.setManagementProjectsLocationsClustersNodePools({
              name,
              body: { management: nextManagement },
            }),
          );
          live = yield* apply(managed);
        }
      }

      const autoscalingOn =
        news.autoscaling?.enabled === true || live.autoscaling?.enabled === true;
      if (!autoscalingOn && (live.initialNodeCount ?? 0) !== nodeCount) {
        const resized = yield* retryConflict(
          container.setSizeProjectsLocationsClustersNodePools({
            name,
            body: { nodeCount },
          }),
        );
        live = yield* apply(resized);
      }

      return toAttrs(live, ref.project, ref.location, ref.clusterId);
    }),

    delete: Effect.fn(function* ({ output }) {
      const env = yield* GcpEnvironment.current;
      const location = normalizeLocation(output.location);
      const operation = yield* container
        .deleteProjectsLocationsClustersNodePools({ name: output.name })
        .pipe(
          Effect.catchTag("NotFound", () => Effect.undefined),
          Effect.retry({
            while: (error) => error._tag === "Conflict",
            times: 8,
            schedule: Schedule.spaced("5 seconds"),
          }),
        );
      if (operation !== undefined) {
        yield* waitForOperation(env.project, location, operation, {
          notFoundOk: true,
        });
      }
      yield* waitUntilGone(output.name);
    }),
  });