Skip to content

Commit

Permalink
Merge pull request #19 from redbadger/gcp-infra
Browse files Browse the repository at this point in the history
gcp infra wip
  • Loading branch information
StuartHarris authored Feb 10, 2025
2 parents 6a8e15f + 093b708 commit 2876fb4
Show file tree
Hide file tree
Showing 21 changed files with 416 additions and 7 deletions.
14 changes: 7 additions & 7 deletions java-containers-k8s/tear_down.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ pushd infrastructure
echo "tearing down infrastructure..."

pushd kubernetes
terraform init
terraform destroy -auto-approve
tofu init
tofu destroy
popd

pushd cluster
terraform init
terraform destroy -auto-approve
tofu init
tofu destroy
popd

pushd storage
terraform init
terraform destroy -auto-approve
tofu init
tofu destroy
popd

echo "infrastructure tear down finished!"

popd
popd
1 change: 1 addition & 0 deletions platform-wasmcloud/google/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.terraform
37 changes: 37 additions & 0 deletions platform-wasmcloud/google/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Platform PoC wasmCloud on Google Cloud

## Prerequisites

- [Google Cloud SDK](https://cloud.google.com/sdk/docs/install)
- [OpenTofu](https://opentofu.org/)
- [wash](https://wasmcloud.com/docs/installation/)

## Setup

Login to Google Cloud:

```fish
# for interactive use of gcloud CLI ...
gcloud auth login
# for OpenTofu/Terraform ...
gcloud auth application-default login
```

Install the GKE gcloud auth plugin (so that the provision script can get the cluster credentials):

```fish
gcloud components install gke-gcloud-auth-plugin
```

## Provisioning

```fish
./scripts/provision.fish
```

## Destroying

```fish
./scripts/destroy.fish
```
14 changes: 14 additions & 0 deletions platform-wasmcloud/google/scripts/destroy.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env fish
set SCRIPT_DIR (dirname (realpath (status -f)))

# STAGE 2
pushd $SCRIPT_DIR/../stage_2
tofu init
tofu destroy --var-file=../terraform.tfvars
popd

# STAGE 1
pushd $SCRIPT_DIR/../stage_1
tofu init
tofu destroy --var-file=../terraform.tfvars
popd
19 changes: 19 additions & 0 deletions platform-wasmcloud/google/scripts/provision.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env fish
set SCRIPT_DIR (dirname (realpath (status -f)))

# STAGE 1
pushd $SCRIPT_DIR/../stage_1
tofu init
tofu apply --var-file=../terraform.tfvars
popd

# Authenticate with the cluster
gcloud container clusters get-credentials platform-poc-wasmcloud-cluster \
--project platform-poc-wasmcloud \
--location europe-west2

# STAGE 2
pushd $SCRIPT_DIR/../stage_2
tofu init
tofu apply --var-file=../terraform.tfvars
popd
20 changes: 20 additions & 0 deletions platform-wasmcloud/google/stage_1/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions platform-wasmcloud/google/stage_1/cluster.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
resource "google_container_cluster" "primary" {
name = "${var.project_id}-cluster"
location = var.region
deletion_protection = false

remove_default_node_pool = true
initial_node_count = 1

workload_identity_config {
workload_pool = "${var.project_id}.svc.id.goog"
}
}

resource "google_container_node_pool" "primary_nodes" {
name = "primary-node-pool"
location = var.region
cluster = google_container_cluster.primary.name
node_count = 1

node_config {
machine_type = "e2-standard-2"
service_account = google_service_account.workload-identity-user-sa.email
}
}

resource "google_service_account" "workload-identity-user-sa" {
account_id = "cloud-sql-client-sa"
display_name = "Cloud SQL Client Service Account"
description = "Service account used for Cloud SQL Auth PRoxy"
}

resource "google_project_iam_member" "sql-client-role" {
project = var.project_id
role = "roles/cloudsql.client"
member = "serviceAccount:${google_service_account.workload-identity-user-sa.email}"
}

resource "google_project_iam_member" "datastore-user-role" {
project = var.project_id
role = "roles/datastore.user"
member = "serviceAccount:${google_service_account.workload-identity-user-sa.email}"
}

resource "google_project_iam_member" "artifact-registry-reader-role" {
project = var.project_id
role = "roles/artifactregistry.reader"
member = "serviceAccount:${google_service_account.workload-identity-user-sa.email}"
}

output "node_pool_service_account" {
value = google_service_account.workload-identity-user-sa.email
}
4 changes: 4 additions & 0 deletions platform-wasmcloud/google/stage_1/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
provider "google" {
project = var.project_id
region = var.region
}
44 changes: 44 additions & 0 deletions platform-wasmcloud/google/stage_1/memorystore.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
resource "google_memorystore_instance" "redis" {
instance_id = "${var.project_id}-redis"
shard_count = 1
desired_psc_auto_connections {
network = google_compute_network.producer_net.id
project_id = data.google_project.project.project_id
}
location = var.region
deletion_protection_enabled = false
depends_on = [
google_network_connectivity_service_connection_policy.default
]

lifecycle {
# we don't store any critical data
prevent_destroy = false
}
}

resource "google_network_connectivity_service_connection_policy" "default" {
name = "${var.project_id}-redis-policy"
location = var.region
service_class = "gcp-memorystore"
description = "redis connection policy"
network = google_compute_network.producer_net.id
psc_config {
subnetworks = [google_compute_subnetwork.producer_subnet.id]
}
}

resource "google_compute_subnetwork" "producer_subnet" {
name = "my-subnet"
ip_cidr_range = "10.0.0.248/29"
region = var.region
network = google_compute_network.producer_net.id
}

resource "google_compute_network" "producer_net" {
name = "my-network"
auto_create_subnetworks = false
}

data "google_project" "project" {
}
7 changes: 7 additions & 0 deletions platform-wasmcloud/google/stage_1/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "kubernetes_context" {
value = "gke_${var.project_id}_${var.region}_${google_container_cluster.primary.name}"
}

output "workload-identity-user-sa" {
value = google_service_account.workload-identity-user-sa.email
}
30 changes: 30 additions & 0 deletions platform-wasmcloud/google/stage_1/postgres.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
resource "google_sql_database" "database_orders" {
name = "order-service"
instance = google_sql_database_instance.instance.name
}

resource "google_sql_database" "database_inventory" {
name = "inventory-service"
instance = google_sql_database_instance.instance.name
}

resource "google_sql_user" "user" {
name = var.pg_user
instance = google_sql_database_instance.instance.name
password = var.pg_password
}

resource "google_sql_database_instance" "instance" {
name = "${var.project_id}-pg"
region = var.region
database_version = "POSTGRES_15"
settings {
tier = "db-f1-micro"
database_flags {
name = "max_connections"
value = "50"
}
}

deletion_protection = "false"
}
10 changes: 10 additions & 0 deletions platform-wasmcloud/google/stage_1/registry.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "google_artifact_registry_repository" "registry" {
location = var.region
repository_id = "registry"
description = "OCI registry for wasmcloud services"
format = "DOCKER"

docker_config {
immutable_tags = false
}
}
24 changes: 24 additions & 0 deletions platform-wasmcloud/google/stage_1/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
variable "project_id" {
type = string
description = "The project ID"
}

variable "region" {
type = string
description = "The region"
}

variable "pg_user" {
type = string
description = "Username for Postgres Cloud SQL database"
}

variable "pg_password" {
type = string
description = "password for Postgres Cloud SQL database"
}

variable "pg_database" {
type = string
description = "Postgres Cloud SQL database name"
}
15 changes: 15 additions & 0 deletions platform-wasmcloud/google/stage_1/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "6.19.0"
}
}

backend "gcs" {
bucket = "platform-poc-wasmcloud-tofu-state"
prefix = "dev"
}

required_version = ">= 1.9.0"
}
38 changes: 38 additions & 0 deletions platform-wasmcloud/google/stage_2/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions platform-wasmcloud/google/stage_2/kubernetes.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
resource "google_project_iam_member" "workload_identity-role" {
project = var.project_id
role = "roles/iam.workloadIdentityUser"
member = "serviceAccount:${var.project_id}.svc.id.goog[default/${kubernetes_service_account.ksa.metadata[0].name}]"
}

resource "kubernetes_service_account" "ksa" {
metadata {
name = "kubernetes-service-account"
annotations = {
"iam.gke.io/gcp-service-account" = data.terraform_remote_state.stage_1.outputs.workload-identity-user-sa
}
}
}

resource "kubernetes_secret" "db_secrets" {
metadata {
name = "postgres-db-secrets"
}

data = {
username = var.pg_user
password = var.pg_password
database = var.pg_database
}
}
Loading

0 comments on commit 2876fb4

Please sign in to comment.