Skip to content

Commit 1beb22e

Browse files
author
Florencia Comuzzi
committed
fmt and only vpc
1 parent b49d6ed commit 1beb22e

File tree

6 files changed

+213
-61
lines changed

6 files changed

+213
-61
lines changed

main.tf

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,39 @@
1-
module "gke_public" {
2-
source = "terraform-google-modules/kubernetes-engine/google"
3-
project_id = var.project_id
4-
name = var.cluster_name
5-
region = var.region
6-
network = var.network_name
7-
subnetwork = var.subnet_name
8-
9-
ip_range_pods = "gke-pods"
10-
ip_range_services = "gke-services"
11-
12-
# enable_private_nodes = false # Public cluster
13-
# enable_private_endpoint = false # Public API endpoint
14-
# enable_ip_masq_agent = true
15-
remove_default_node_pool = true
16-
17-
node_pools = [
18-
{
19-
name = "default-pool"
20-
machine_type = "e2-medium"
21-
min_count = 1
22-
max_count = 3
23-
disk_size_gb = 50
24-
autoscaling = true
25-
}
26-
]
27-
28-
node_pools_oauth_scopes = {
29-
all = [
30-
"https://www.googleapis.com/auth/cloud-platform"
31-
]
32-
}
33-
}
34-
351
module "vpc" {
36-
source = "terraform-google-modules/network/google"
37-
project_id = var.project_id
38-
network_name = var.network_name
39-
subnets = [
40-
{
41-
subnet_name = var.subnet_name
42-
subnet_ip = var.subnet_cidr
43-
subnet_region = var.region
44-
}
45-
]
46-
secondary_ranges = {
47-
subnet-01 = [
48-
{
49-
range_name = "services"
50-
ip_cidr_range = "192.168.0.0/24"
51-
},
52-
{
53-
range_name = "pods"
54-
ip_cidr_range = "192.168.64.0/24"
55-
},
56-
]
57-
subnet-02 = []
58-
}
2+
source = "modules/vpc"
3+
network_name = var.network_name
4+
subnet_cidr = var.subnet_cidr
5+
subnet_name = var.subnet_name
6+
cluster_secondary_range_name = var.cluster_secondary_range_name
7+
cluster_secondary_range_cidr = var.cluster_secondary_range_cidr
8+
services_secondary_range_name = var.services_secondary_range_name
9+
services_secondary_range_cidr = var.services_secondary_range_cidr
5910
}
11+
12+
13+
# Optional: Create a GKE Cluster
14+
# resource "google_container_cluster" "gke_cluster" {
15+
# name = "gke-cluster"
16+
# location = "us-central1"
17+
# network = var.network_name
18+
# subnetwork = var.subnet_name
19+
# remove_default_node_pool = true
20+
#
21+
# ip_allocation_policy {
22+
# cluster_secondary_range_name = var.cluster_secondary_range_name
23+
# services_secondary_range_name = var.services_secondary_range_name
24+
# }
25+
# }
26+
#
27+
# # Node pool for GKE Cluster
28+
# resource "google_container_node_pool" "gke_nodes" {
29+
# name = "gke-node-pool"
30+
# location = "us-central1"
31+
# cluster = google_container_cluster.gke_cluster.name
32+
# node_count = 3
33+
#
34+
# node_config {
35+
# machine_type = "e2-standard-4"
36+
# disk_size_gb = 100
37+
# oauth_scopes = ["https://www.googleapis.com/auth/cloud-platform"]
38+
# }
39+
# }

modules/vpc/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
## Requirements
3+
4+
No requirements.
5+
6+
## Providers
7+
8+
| Name | Version |
9+
|------|---------|
10+
| <a name="provider_google"></a> [google](#provider\_google) | n/a |
11+
12+
## Modules
13+
14+
No modules.
15+
16+
## Resources
17+
18+
| Name | Type |
19+
|------|------|
20+
| [google_compute_firewall.allow-health-checks](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_firewall) | resource |
21+
| [google_compute_firewall.allow-internal](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_firewall) | resource |
22+
| [google_compute_network.vpc](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_network) | resource |
23+
| [google_compute_subnetwork.gke_subnet](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_subnetwork) | resource |
24+
25+
## Inputs
26+
27+
| Name | Description | Type | Default | Required |
28+
|------|-------------|------|---------|:--------:|
29+
| <a name="input_network_name"></a> [network\_name](#input\_network\_name) | The name of the VPC network | `string` | n/a | yes |
30+
| <a name="input_project_id"></a> [project\_id](#input\_project\_id) | GCP project id | `string` | n/a | yes |
31+
| <a name="input_region"></a> [region](#input\_region) | GCP region | `string` | `"us-east1"` | no |
32+
| <a name="input_subnet_cidr"></a> [subnet\_cidr](#input\_subnet\_cidr) | The CIDR block for the subnet | `string` | n/a | yes |
33+
| <a name="input_subnet_name"></a> [subnet\_name](#input\_subnet\_name) | The name of the subnet | `string` | n/a | yes |
34+
35+
## Outputs
36+
37+
No outputs.
38+
<!-- END_TF_DOCS -->

modules/vpc/main.tf

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Create a VPC Network
2+
resource "google_compute_network" "vpc" {
3+
name = var.network_name
4+
auto_create_subnetworks = false
5+
}
6+
7+
# Create a Subnet for GKE with Secondary Ranges
8+
resource "google_compute_subnetwork" "gke_subnet" {
9+
name = var.subnet_name
10+
network = google_compute_network.vpc.id
11+
ip_cidr_range = var.subnet_cidr
12+
region = var.region
13+
14+
secondary_ip_range {
15+
range_name = var.cluster_secondary_range_name
16+
ip_cidr_range = var.cluster_secondary_range_cidr
17+
}
18+
19+
secondary_ip_range {
20+
range_name = var.services_secondary_range_name
21+
ip_cidr_range = var.services_secondary_range_cidr
22+
}
23+
}
24+
25+
# Firewall rules for GKE communication
26+
resource "google_compute_firewall" "allow-internal" {
27+
name = "gke-allow-internal"
28+
network = google_compute_network.vpc.name
29+
30+
allow {
31+
protocol = "tcp"
32+
ports = ["0-65535"]
33+
}
34+
35+
allow {
36+
protocol = "udp"
37+
ports = ["0-65535"]
38+
}
39+
40+
allow {
41+
protocol = "icmp"
42+
}
43+
44+
source_ranges = [var.subnet_cidr]
45+
}
46+
47+
# Firewall rule to allow health checks
48+
resource "google_compute_firewall" "allow-health-checks" {
49+
name = "gke-allow-health-checks"
50+
network = google_compute_network.vpc.name
51+
52+
allow {
53+
protocol = "tcp"
54+
ports = ["10256", "15017"] # Common ports for health checks
55+
}
56+
57+
source_ranges = ["130.211.0.0/22", "35.191.0.0/16"] # Google Cloud health check IPs
58+
}

modules/vpc/variables.tf

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
variable "project_id" {
2+
type = string
3+
description = "GCP project id"
4+
}
5+
6+
variable "region" {
7+
type = string
8+
default = "us-east1"
9+
description = "GCP region"
10+
}
11+
12+
variable "network_name" {
13+
description = "The name of the VPC network"
14+
type = string
15+
}
16+
17+
variable "subnet_name" {
18+
description = "The name of the subnet"
19+
type = string
20+
}
21+
22+
variable "subnet_cidr" {
23+
description = "The CIDR block for the subnet"
24+
type = string
25+
}
26+
27+
variable "cluster_secondary_range_name" {
28+
type = string
29+
default = "gke-pods"
30+
description = "The name of the secondary range to use for pods"
31+
}
32+
33+
variable "services_secondary_range_name" {
34+
type = string
35+
default = "gke-services"
36+
description = "The name of the secondary range to use for services"
37+
}
38+
39+
variable "cluster_secondary_range_cidr" {
40+
type = string
41+
default = "10.20.0.0/16"
42+
description = "The secondary range to use for pods"
43+
}
44+
45+
variable "services_secondary_range_cidr" {
46+
type = string
47+
default = "10.30.0.0/16"
48+
description = "The name of the secondary range to use for services"
49+
}

variables.tf

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,30 @@ variable "subnet_cidr" {
2525
type = string
2626
}
2727

28-
variable "cluster_name" {
29-
description = "The name of the GKE cluster"
28+
# variable "cluster_name" {
29+
# description = "The name of the GKE cluster"
30+
# type = string
31+
# }
32+
33+
variable "cluster_secondary_range_name" {
34+
type = string
35+
default = "gke-pods"
36+
description = "The name of the secondary range to use for pods"
37+
}
38+
39+
variable "services_secondary_range_name" {
40+
type = string
41+
default = "gke-services"
42+
description = "The name of the secondary range to use for services"
43+
}
44+
45+
variable "cluster_secondary_range_cidr" {
46+
type = string
47+
description = "The secondary range to use for pods"
48+
}
49+
50+
variable "services_secondary_range_cidr" {
3051
type = string
52+
default = "10.30.0.0/16"
53+
description = "The name of the secondary range to use for services"
3154
}

variables/prod.auto.tfvars

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ project_id = "florenciacomuzzi"
22
region = "us-east1"
33
network_name = "florenciacomuzzi-vpc-prod"
44
cluster_name = "florenciacomuzzi-cluster-prod"
5-
subnet_cidr = "10.0.0.0/12"
6-
subnet_name = "my-subnet"
5+
subnet_cidr = "10.10.0.0/16"
6+
subnet_name = "my-subnet"
7+
cluster_secondary_range_name="gke-pods"
8+
cluster_secondary_range_cidr="10.20.0.0/16"
9+
services_secondary_range_name="gke-services"
10+
services_secondary_range_cidr="10.30.0.0/16"

0 commit comments

Comments
 (0)