import * as compute from "@distilled.cloud/gcp/compute_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 * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";
import * as Stream from "effect/Stream";
import { GcpEnvironment } from "../Environment.ts";
import { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
import type { Providers } from "../Providers.ts";
import { waitGlobalOperation } from "./operations.ts";
const DEFAULT_CHECK_INTERVAL = 5;
const DEFAULT_TIMEOUT = 5;
const DEFAULT_HEALTHY = 2;
const DEFAULT_UNHEALTHY = 2;
const DEFAULT_PORT = 80;
const DEFAULT_REQUEST_PATH = "/";
export type HttpHealthCheckProps = {
/**
* Health check name (RFC1035, 1-63 chars). If omitted, a unique name is
* generated from the stack, stage, and logical id. Changing it replaces
* the resource.
*/
httpHealthCheckName?: string;
/**
* Optional description. Alchemy ownership is stored in a `[alchemy …]`
* prefix so `list` / nuke can find resources (legacy HttpHealthCheck has
* no labels field).
*/
description?: string;
/**
* How often to probe, in seconds.
* @default 5
*/
checkIntervalSec?: number;
/**
* Probe timeout in seconds. Must be ≤ `checkIntervalSec`.
* @default 5
*/
timeoutSec?: number;
/**
* Consecutive successes before an instance is healthy.
* @default 2
*/
healthyThreshold?: number;
/**
* Consecutive failures before an instance is unhealthy.
* @default 2
*/
unhealthyThreshold?: number;
/**
* TCP port for the HTTP probe.
* @default 80
*/
port?: number;
/**
* Request path. Query parameters are not supported.
* @default "/"
*/
requestPath?: string;
/**
* Host header. Empty uses the destination IP.
*/
host?: string;
};
export type HttpHealthCheck = Resource<
"GCP.Compute.HttpHealthCheck",
HttpHealthCheckProps,
{
/** Health check name. */
httpHealthCheckName: string;
/** Project id. */
project: string;
/** User description with the Alchemy ownership prefix stripped. */
description: string | undefined;
/** Probe interval in seconds. */
checkIntervalSec: number;
/** Probe timeout in seconds. */
timeoutSec: number;
/** Consecutive successes before healthy. */
healthyThreshold: number;
/** Consecutive failures before unhealthy. */
unhealthyThreshold: number;
/** TCP port. */
port: number;
/** Request path. */
requestPath: string;
/** Host header, if set. */
host: string | undefined;
/** Server-defined URL. */
selfLink: string | undefined;
/** Server-assigned numeric id. */
httpHealthCheckId: string | undefined;
/** RFC3339 creation timestamp. */
creationTimestamp: string | undefined;
/** Resource kind. */
kind: string | undefined;
},
never,
Providers
>;
/**
* A legacy global Compute Engine HTTP health check.
*
* Legacy HTTP health checks are required by target-pool network load
* balancers. Other load balancers and MIG autohealing should use
* `HealthCheck`. Compute HttpHealthCheck has no labels field — Alchemy
* ownership is stored in the description so nuke can find leaked checks.
*
* ### Creating an HTTP Health Check
* **Example:** Generated name
* ```typescript
* const check = yield* GCP.Compute.HttpHealthCheck("api", {});
* ```
*
* **Example:** Path, port, and interval
* ```typescript
* const check = yield* GCP.Compute.HttpHealthCheck("api", {
* description: "frontend /health",
* port: 80,
* requestPath: "/health",
* checkIntervalSec: 10,
* timeoutSec: 5,
* });
* ```
*
* ### Target Pools
* **Example:** Attach to a target pool
* ```typescript
* const check = yield* GCP.Compute.HttpHealthCheck("api", {
* requestPath: "/health",
* });
* const pool = yield* GCP.Compute.TargetPool("backends", {
* healthChecks: [check.httpHealthCheckName],
* });
* ```
*
* @resource
* @category Compute
*/
export const HttpHealthCheck = Resource<HttpHealthCheck>("GCP.Compute.HttpHealthCheck");
export class HttpHealthCheckNotResolved extends Data.TaggedError(
"GCP.Compute.HttpHealthCheckNotResolved",
)<{
httpHealthCheckName: string;
}> {}
const rfc1035 = (name: string): string => {
let next = name
.toLowerCase()
.replace(/[^a-z0-9-]/g, "-")
.replace(/-+/g, "-")
.replace(/^-+/, "")
.replace(/-+$/, "");
if (!/^[a-z]/.test(next)) {
next = `h${next}`;
}
next = next.slice(0, 63).replace(/-+$/, "");
return next.length > 0 ? next : "httphealthcheck";
};
const toName = (id: string, name: string | undefined, existing?: string) =>
Effect.gen(function* () {
if (name !== undefined) return name;
if (existing !== undefined) return existing;
return rfc1035(
yield* createPhysicalName({
id,
maxLength: 63,
lowercase: true,
}),
);
});
const encodeDescription = (
labels: Record<string, string>,
description: string | undefined,
): string => {
const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
return description ? `${marker}\n${description}` : marker;
};
const parseDescription = (
description: string | undefined,
): {
labels: Record<string, string>;
description: string | undefined;
} => {
if (!description?.startsWith("[alchemy ")) {
return { labels: {}, description };
}
const end = description.indexOf("]");
if (end < 0) return { labels: {}, description };
const labels: Record<string, string> = {};
for (const part of description.slice("[alchemy ".length, end).split(/\s+/)) {
const eq = part.indexOf("=");
if (eq > 0) {
labels[part.slice(0, eq)] = part.slice(eq + 1);
}
}
const rest = description.slice(end + 1).replace(/^\n/, "");
return { labels, description: rest.length > 0 ? rest : undefined };
};
const toBody = (
httpHealthCheckName: string,
props: HttpHealthCheckProps,
ownership: Record<string, string>,
): compute.HttpHealthCheck => ({
name: httpHealthCheckName,
description: encodeDescription(ownership, props.description),
checkIntervalSec: props.checkIntervalSec ?? DEFAULT_CHECK_INTERVAL,
timeoutSec: props.timeoutSec ?? DEFAULT_TIMEOUT,
healthyThreshold: props.healthyThreshold ?? DEFAULT_HEALTHY,
unhealthyThreshold: props.unhealthyThreshold ?? DEFAULT_UNHEALTHY,
port: props.port ?? DEFAULT_PORT,
requestPath: props.requestPath ?? DEFAULT_REQUEST_PATH,
host: props.host,
});
const toAttrs = (
check: compute.HttpHealthCheck,
project: string,
): HttpHealthCheck["Attributes"] => {
const parsed = parseDescription(check.description);
const host = check.host && check.host.length > 0 ? check.host : undefined;
return {
httpHealthCheckName: check.name ?? check.id ?? "",
project,
description: parsed.description,
checkIntervalSec: check.checkIntervalSec ?? DEFAULT_CHECK_INTERVAL,
timeoutSec: check.timeoutSec ?? DEFAULT_TIMEOUT,
healthyThreshold: check.healthyThreshold ?? DEFAULT_HEALTHY,
unhealthyThreshold: check.unhealthyThreshold ?? DEFAULT_UNHEALTHY,
port: check.port ?? DEFAULT_PORT,
requestPath: check.requestPath ?? DEFAULT_REQUEST_PATH,
host,
selfLink: check.selfLink,
httpHealthCheckId: check.id,
creationTimestamp: check.creationTimestamp,
kind: check.kind,
};
};
const needsUpdate = (current: compute.HttpHealthCheck, desired: compute.HttpHealthCheck) => {
if ((current.description ?? "") !== (desired.description ?? "")) return true;
if (
(current.checkIntervalSec ?? DEFAULT_CHECK_INTERVAL) !==
(desired.checkIntervalSec ?? DEFAULT_CHECK_INTERVAL)
) {
return true;
}
if ((current.timeoutSec ?? DEFAULT_TIMEOUT) !== (desired.timeoutSec ?? DEFAULT_TIMEOUT)) {
return true;
}
if (
(current.healthyThreshold ?? DEFAULT_HEALTHY) !== (desired.healthyThreshold ?? DEFAULT_HEALTHY)
) {
return true;
}
if (
(current.unhealthyThreshold ?? DEFAULT_UNHEALTHY) !==
(desired.unhealthyThreshold ?? DEFAULT_UNHEALTHY)
) {
return true;
}
if ((current.port ?? DEFAULT_PORT) !== (desired.port ?? DEFAULT_PORT)) {
return true;
}
if (
(current.requestPath ?? DEFAULT_REQUEST_PATH) !== (desired.requestPath ?? DEFAULT_REQUEST_PATH)
) {
return true;
}
return (current.host ?? "") !== (desired.host ?? "");
};
const getByName = (project: string, httpHealthCheck: string) =>
compute
.getHttpHealthChecks({ project, httpHealthCheck })
.pipe(Effect.catchTag("NotFound", () => Effect.undefined));
const awaitResource = (project: string, httpHealthCheckName: string) =>
getByName(project, httpHealthCheckName).pipe(
Effect.repeat({
schedule: Schedule.spaced("1 second"),
until: (check) => check !== undefined,
times: 8,
}),
);
export const HttpHealthCheckProvider = () =>
Provider.succeed(HttpHealthCheck, {
stables: [
"httpHealthCheckName",
"project",
"httpHealthCheckId",
"selfLink",
"creationTimestamp",
],
diff: Effect.fn(function* ({ news, olds, output }) {
if (!isResolved(news)) return undefined;
const previousName = olds?.httpHealthCheckName ?? output?.httpHealthCheckName;
const nextName = news.httpHealthCheckName;
if (previousName !== undefined && nextName !== undefined && previousName !== nextName) {
return { action: "replace" as const };
}
return undefined;
}),
read: Effect.fn(function* ({ id, olds, output }) {
const env = yield* GcpEnvironment.current;
const httpHealthCheckName = yield* toName(
id,
olds?.httpHealthCheckName,
output?.httpHealthCheckName,
);
const existing = yield* getByName(env.project, httpHealthCheckName);
if (existing === undefined) return undefined;
const attrs = toAttrs(existing, env.project);
const { labels } = parseDescription(existing.description);
return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs);
}),
list: () =>
Effect.gen(function* () {
const env = yield* GcpEnvironment.current;
return yield* compute.listHttpHealthChecks
.items({ project: env.project, maxResults: 500 })
.pipe(
Stream.filter((check) => {
const { labels } = parseDescription(check.description);
return Object.keys(labels).some((key) => key.startsWith("alchemy-"));
}),
Stream.map((check) => toAttrs(check, env.project)),
Stream.runCollect,
Effect.map((chunk) => Array.from(chunk)),
);
}),
reconcile: Effect.fn(function* ({ id, news, output }) {
const env = yield* GcpEnvironment.current;
const httpHealthCheckName = yield* toName(
id,
news.httpHealthCheckName,
output?.httpHealthCheckName,
);
const ownership = yield* createInternalLabels(id);
const desired = toBody(httpHealthCheckName, news, ownership);
let current = yield* getByName(env.project, httpHealthCheckName);
if (current === undefined) {
yield* compute
.insertHttpHealthChecks({
project: env.project,
body: desired,
})
.pipe(
Effect.flatMap((operation) => waitGlobalOperation(env.project, operation)),
Effect.catchTag("Conflict", () => Effect.undefined),
);
current = yield* awaitResource(env.project, httpHealthCheckName);
}
if (current === undefined) {
return yield* new HttpHealthCheckNotResolved({ httpHealthCheckName });
}
if (needsUpdate(current, desired)) {
yield* compute
.patchHttpHealthChecks({
project: env.project,
httpHealthCheck: httpHealthCheckName,
body: desired,
})
.pipe(Effect.flatMap((operation) => waitGlobalOperation(env.project, operation)));
current = yield* getByName(env.project, httpHealthCheckName);
if (current === undefined) {
return yield* new HttpHealthCheckNotResolved({
httpHealthCheckName,
});
}
}
return toAttrs(current, env.project);
}),
delete: Effect.fn(function* ({ output }) {
const env = yield* GcpEnvironment.current;
const operation = yield* compute
.deleteHttpHealthChecks({
project: env.project,
httpHealthCheck: output.httpHealthCheckName,
})
.pipe(
Effect.retry({
while: (error) => error._tag === "Conflict",
schedule: Schedule.spaced("2 seconds"),
times: 8,
}),
Effect.catchTag("NotFound", () => Effect.undefined),
);
if (operation !== undefined) {
yield* waitGlobalOperation(env.project, operation).pipe(
Effect.catchTag("NotFound", () => Effect.void),
);
}
}),
});