mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-12-05 01:26:07 +09:00
Part of the June 2026 cost-reduction effort (`docs/archive/2026-06-14-gcp-cost-reduction-plan.md`). Targets the post-credit-expiry (2026-09-19) bill. ## Changes - **Item 4 — Artifact Registry cleanup** (~$6/mo + caps growth): keep 10 most-recent versions + delete untagged digests >7d, on all 3 repos (karaoke-repo, karaoke-backend-gpu, audio-separator; 92 GiB, previously no policy). In-place update. - **Item 5 — NAT egress** (~$26/mo): ephemeral GHA runners now always get an external IP, so egress bypasses the Cloud NAT data-processing charge (runners were the NAT's only user; us-central1 IN_USE_ADDRESSES quota is 69, usage 1). - **Item 6 — backup cadence** (~$15/mo): Firestore export stays nightly to GCS (1-day local restore point) but uploads to S3 only on Sundays (1-week off-site RPO) — Firestore is the bulk of the cross-cloud egress. Staging keeps only the latest export so the weekly upload ships exactly one. - **Item 1 — encoding disks** (~$35/mo): pinned the 4 worker hyperdisks to the free baseline (3000 IOPS / 140 MB/s) **live via `gcloud`**, not IaC — setting it in IaC forces a capacity-risky VM replacement. A code comment documents this. ## Deploy status - `pulumi up` (artifact policies + runner function): **applied** locally (prod, update 751). - Backup function source: shipped via `functions/backup_to_aws/deploy.sh` (pulumi doesn't manage the source zip). - Encoding disks: tuned live via gcloud. ## Tests - `backup_to_aws`: 13 pass (added firestore-cleanup + s3-exclude coverage). - `runner_manager/ephemeral`: 32 pass (updated external-IP assertion). Item 7 (fallback disks on-demand) intentionally deferred — reliability tradeoff not worth ~$20/mo. See plan doc. @coderabbitai ignore 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
196 lines
8.0 KiB
Python
196 lines
8.0 KiB
Python
"""
|
|
Encoding Worker VM resources — blue-green deployment pair.
|
|
|
|
Manages two identical VMs (a/b) for zero-downtime deployments.
|
|
Only one is active (primary) at a time; the other is stopped (secondary).
|
|
Both auto-shutdown when idle to minimize cost.
|
|
|
|
See docs/superpowers/specs/2026-03-24-blue-green-encoding-worker-design.md
|
|
"""
|
|
|
|
import pulumi
|
|
from pulumi_gcp import compute, serviceaccount
|
|
|
|
from config import REGION, ENCODING_WORKER_ZONE, PROJECT_ID, MachineTypes, DiskSizes, EncodingWorkerConfig
|
|
from .startup_scripts import read_script
|
|
|
|
|
|
def create_encoding_worker_ips() -> list[compute.Address]:
|
|
"""Create static IPs for both encoding worker VMs."""
|
|
ips = []
|
|
for name in EncodingWorkerConfig.IP_NAMES:
|
|
ip = compute.Address(
|
|
name,
|
|
name=name,
|
|
region=REGION,
|
|
address_type="EXTERNAL",
|
|
description=f"Static external IP for {name}",
|
|
)
|
|
ips.append(ip)
|
|
return ips
|
|
|
|
|
|
def create_encoding_worker_vms(
|
|
ips: list[compute.Address],
|
|
service_account: serviceaccount.Account,
|
|
) -> list[compute.Instance]:
|
|
"""Create the blue-green encoding worker VM pair."""
|
|
startup_script = read_script("encoding_worker.sh")
|
|
custom_image = f"projects/{PROJECT_ID}/global/images/family/encoding-worker"
|
|
|
|
vms = []
|
|
for vm_name, ip in zip(EncodingWorkerConfig.VM_NAMES, ips):
|
|
vm = compute.Instance(
|
|
vm_name,
|
|
name=vm_name,
|
|
machine_type=MachineTypes.ENCODING_WORKER,
|
|
zone=ENCODING_WORKER_ZONE,
|
|
boot_disk=compute.InstanceBootDiskArgs(
|
|
initialize_params=compute.InstanceBootDiskInitializeParamsArgs(
|
|
image=custom_image,
|
|
size=DiskSizes.ENCODING_WORKER,
|
|
type="hyperdisk-balanced",
|
|
# NOTE: Hyperdisk provisioned IOPS/throughput are deliberately
|
|
# NOT set here. Specifying them forces a full VM *replacement*
|
|
# (the provider treats boot-disk init params as immutable), and
|
|
# recreating a primary depends on us-central1-c capacity — the
|
|
# exact shortage the fallback fleet exists to absorb. So the
|
|
# disks are pinned to the Hyperdisk Balanced free baseline
|
|
# (3000 IOPS / 140 MB/s) live via `gcloud compute disks update`
|
|
# instead — non-disruptive, and pulumi does not manage this
|
|
# attribute so there is no drift. They were over-provisioned at
|
|
# 3600 IOPS / 290 MB/s (~$8.70/disk/mo wasted). Re-apply the
|
|
# gcloud tune after any worker-image rebuild that recreates the
|
|
# disks. See docs/archive/2026-06-14-gcp-cost-reduction-plan.md.
|
|
),
|
|
),
|
|
network_interfaces=[compute.InstanceNetworkInterfaceArgs(
|
|
network="default",
|
|
access_configs=[compute.InstanceNetworkInterfaceAccessConfigArgs(
|
|
nat_ip=ip.address,
|
|
)],
|
|
)],
|
|
service_account=compute.InstanceServiceAccountArgs(
|
|
email=service_account.email,
|
|
scopes=["cloud-platform"],
|
|
),
|
|
metadata_startup_script=startup_script,
|
|
tags=["encoding-worker"],
|
|
allow_stopping_for_update=True,
|
|
advanced_machine_features=compute.InstanceAdvancedMachineFeaturesArgs(
|
|
threads_per_core=2,
|
|
),
|
|
)
|
|
vms.append(vm)
|
|
return vms
|
|
|
|
|
|
def create_encoding_worker_fallback_ips() -> list[compute.Address]:
|
|
"""Create static IPs for the multi-zone capacity-fallback VMs.
|
|
|
|
These IPs live in alternate zones (e.g. us-central1-a, -f) to provide
|
|
zone-level resilience when the primary zone is out of c4d-highcpu-32
|
|
capacity (ZONE_RESOURCE_POOL_EXHAUSTED).
|
|
"""
|
|
ips = []
|
|
for ip_name, zone_suffix in zip(
|
|
EncodingWorkerConfig.FALLBACK_IP_NAMES,
|
|
EncodingWorkerConfig.FALLBACK_ZONE_SUFFIXES,
|
|
):
|
|
ip = compute.Address(
|
|
ip_name,
|
|
name=ip_name,
|
|
region=REGION,
|
|
address_type="EXTERNAL",
|
|
description=f"Static external IP for {ip_name} (zone-fallback)",
|
|
)
|
|
ips.append(ip)
|
|
return ips
|
|
|
|
|
|
def create_encoding_worker_fallback_vms(
|
|
ips: list[compute.Address],
|
|
service_account: serviceaccount.Account,
|
|
) -> list[compute.Instance]:
|
|
"""Create capacity-fallback encoding worker VMs in alternate zones.
|
|
|
|
Provisioned stopped — they're only started by the application when
|
|
the primary zone (us-central1-c) rejects starts with
|
|
ZONE_RESOURCE_POOL_EXHAUSTED. Cost when stopped is just the boot disk
|
|
(~$10/mo for 100GB hyperdisk-balanced).
|
|
"""
|
|
startup_script = read_script("encoding_worker.sh")
|
|
custom_image = f"projects/{PROJECT_ID}/global/images/family/encoding-worker"
|
|
|
|
vms = []
|
|
for vm_name, ip, zone_suffix in zip(
|
|
EncodingWorkerConfig.FALLBACK_VM_NAMES,
|
|
ips,
|
|
EncodingWorkerConfig.FALLBACK_ZONE_SUFFIXES,
|
|
):
|
|
zone = f"{REGION}-{zone_suffix}"
|
|
vm = compute.Instance(
|
|
vm_name,
|
|
name=vm_name,
|
|
machine_type=MachineTypes.ENCODING_WORKER,
|
|
zone=zone,
|
|
boot_disk=compute.InstanceBootDiskArgs(
|
|
initialize_params=compute.InstanceBootDiskInitializeParamsArgs(
|
|
image=custom_image,
|
|
size=DiskSizes.ENCODING_WORKER,
|
|
type="hyperdisk-balanced",
|
|
# NOTE: Hyperdisk provisioned IOPS/throughput are deliberately
|
|
# NOT set here. Specifying them forces a full VM *replacement*
|
|
# (the provider treats boot-disk init params as immutable), and
|
|
# recreating a primary depends on us-central1-c capacity — the
|
|
# exact shortage the fallback fleet exists to absorb. So the
|
|
# disks are pinned to the Hyperdisk Balanced free baseline
|
|
# (3000 IOPS / 140 MB/s) live via `gcloud compute disks update`
|
|
# instead — non-disruptive, and pulumi does not manage this
|
|
# attribute so there is no drift. They were over-provisioned at
|
|
# 3600 IOPS / 290 MB/s (~$8.70/disk/mo wasted). Re-apply the
|
|
# gcloud tune after any worker-image rebuild that recreates the
|
|
# disks. See docs/archive/2026-06-14-gcp-cost-reduction-plan.md.
|
|
),
|
|
),
|
|
network_interfaces=[compute.InstanceNetworkInterfaceArgs(
|
|
network="default",
|
|
access_configs=[compute.InstanceNetworkInterfaceAccessConfigArgs(
|
|
nat_ip=ip.address,
|
|
)],
|
|
)],
|
|
service_account=compute.InstanceServiceAccountArgs(
|
|
email=service_account.email,
|
|
scopes=["cloud-platform"],
|
|
),
|
|
metadata_startup_script=startup_script,
|
|
tags=["encoding-worker"],
|
|
allow_stopping_for_update=True,
|
|
advanced_machine_features=compute.InstanceAdvancedMachineFeaturesArgs(
|
|
threads_per_core=2,
|
|
),
|
|
# Pulumi default is "running" — we want fallbacks stopped at create.
|
|
desired_status="TERMINATED",
|
|
)
|
|
vms.append(vm)
|
|
return vms
|
|
|
|
|
|
def create_encoding_worker_firewall() -> compute.Firewall:
|
|
"""Create firewall rules for encoding worker VMs.
|
|
|
|
Opens port 8080 for the HTTP API. Both VMs share the same
|
|
firewall rule via the 'encoding-worker' network tag.
|
|
"""
|
|
return compute.Firewall(
|
|
"encoding-worker-firewall",
|
|
name="encoding-worker-allow-http",
|
|
network="default",
|
|
allows=[
|
|
compute.FirewallAllowArgs(protocol="tcp", ports=["8080"]),
|
|
],
|
|
source_ranges=["0.0.0.0/0"],
|
|
target_tags=["encoding-worker"],
|
|
description="Allow HTTP access to encoding workers (auth required)",
|
|
)
|