Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions deploy/cluster/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
resource "google_compute_network" "this" {
name = "${var.name}-vpc"
auto_create_subnetworks = false
routing_mode = "REGIONAL"
}

resource "google_compute_subnetwork" "this" {
name = var.name
network = google_compute_network.this.id
}

resource "google_container_cluster" "this" {
name = "${var.name}-cluster"
location = var.gcp_zone # zonal cluster avoids data transfer costs between zones
remove_default_node_pool = true
initial_node_count = 1
network = google_compute_network.this.self_link
subnetwork = google_compute_subnetwork.this.self_link
networking_mode = "VPC_NATIVE"
deletion_protection = true

addons_config {
http_load_balancing {
disabled = false
}
horizontal_pod_autoscaling {
disabled = false
}
}

release_channel {
channel = "STABLE"
}

workload_identity_config {
workload_pool = "${}"
}

ip_allocation_policy {
cluster_secondary_range_name = "k8s-pods"
services_secondary_range_name = "k8s-services"
}

private_cluster_config {
enable_private_nodes = true
enable_private_endpoint = false
master_ipv4_cidr_block = "192.168.0.0/28"
}
}
Empty file added deploy/cluster/output.tf
Empty file.
9 changes: 9 additions & 0 deletions deploy/cluster/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "name" {
description = "Name of this Kubernetes cluster"
type = string
}

variable "gcp_zone" {
description = "Google Cloud zone where all nodes are located"
type = string
}
9 changes: 8 additions & 1 deletion deploy/saturn/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,17 @@ resource "google_compute_instance_template" "this" {
}

metadata = {
gce-container-declaration = module.container.metadata_value
google-logging-enabled = true
google-monitoring-enabled = true
shutdown-script = file("${path.module}/shutdown-script.sh")
startup-script = templatefile("${path.module}/startup-script.sh", {
image = jsonencode(var.image)
registry = jsonencode(split("/", var.image)[0])
project = jsonencode(var.gcp_project)
secret = jsonencode(var.secret_id)
subscription = jsonencode(google_pubsub_subscription.queue.name)
parallelism = jsonencode(var.parallelism)
})
}

lifecycle {
Expand Down
50 changes: 50 additions & 0 deletions deploy/saturn/startup-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
set -euo pipefail

IMAGE=${image}
REGISTRY=${registry}
PROJECT=${project}
SECRET=${secret}
SUBSCRIPTION=${subscription}
PARALLELISM=${parallelism}
CONTAINER_NAME=saturn

exec >>/var/log/saturn-startup.log 2>&1

echo "Starting Saturn container at $(date -u +%Y-%m-%dT%H:%M:%SZ)"

until docker info >/dev/null 2>&1; do
echo "Waiting for Docker..."
sleep 2
done

until TOKEN_RESPONSE="$(curl --fail --silent --show-error \
-H 'Metadata-Flavor: Google' \
'http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token')"; do
echo "Waiting for the metadata server..."
sleep 2
done

ACCESS_TOKEN="$(printf '%s' "$TOKEN_RESPONSE" | sed -E 's/.*"access_token":"([^"]+)".*/\1/')"
if [ -z "$ACCESS_TOKEN" ] || [ "$ACCESS_TOKEN" = "$TOKEN_RESPONSE" ]; then
echo "Unable to obtain an access token from the metadata server" >&2
exit 1
fi

printf '%s' "$ACCESS_TOKEN" | docker login \
--username oauth2accesstoken \
--password-stdin "https://$REGISTRY"

docker rm --force "$CONTAINER_NAME" >/dev/null 2>&1 || true
docker pull "$IMAGE"
docker run \
--detach \
--name "$CONTAINER_NAME" \
--restart on-failure \
"$IMAGE" \
"-project=$PROJECT" \
"-secret=$SECRET" \
"-subscription=$SUBSCRIPTION" \
"-parallel=$PARALLELISM"

echo "Saturn container started at $(date -u +%Y-%m-%dT%H:%M:%SZ)"
Loading