Skip to content

Commit

Permalink
feat: Autopilot cluster Terraform quickstart samples (#1024)
Browse files Browse the repository at this point in the history
* Create main.tf

* Create ingress.tf

* Create network.tf

* Create outputs.tf

* Create variables.tf

* Create providers.tf

* Fixed
  • Loading branch information
eballestas authored Dec 12, 2023
1 parent 551b958 commit ddec488
Show file tree
Hide file tree
Showing 6 changed files with 390 additions and 0 deletions.
159 changes: 159 additions & 0 deletions autopilot/quickstart/ingress.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START gke_ap_autopilot_quickstart_ingress_terraform]
resource "google_compute_global_address" "gke_ingress_ipv4" {
name = "external-address-gke-ingress-ipv4"
ip_version = "IPV4"
address_type = "EXTERNAL"
}

resource "google_compute_managed_ssl_certificate" "ingress-certs" {
provider = google-beta
name = "ingress-certs"

managed {
domains = local.certificate_host
]
}
}

resource "kubernetes_ingress_v1" "example_ingress" {
metadata {
name = "example-ingress"
annotations = {
"kubernetes.io/ingress.global-static-ip-name" = "external-address-gke-ingress-ipv4"
"ingress.gcp.kubernetes.io/pre-shared-cert" = "ingress-certs"
}
}

spec {
default_backend {
service {
name = "myapp-1"
port {
number = 8080
}
}
}

rule {
http {
path {
backend {
service {
name = "myapp-1"
port {
number = 8080
}
}
}

path = "/app1/*"
}

path {
backend {
service {
name = "myapp-2"
port {
number = 8080
}
}
}

path = "/app2/*"
}
}
}
}
}

resource "kubernetes_service_v1" "example" {
metadata {
name = "myapp-1"
}
spec {
selector = {
app = kubernetes_pod_v1.example.metadata.0.labels.app
}
session_affinity = "ClientIP"
port {
port = 8080
target_port = 8080
}

type = "NodePort"
}
}

resource "kubernetes_service_v1" "example2" {
metadata {
name = "myapp-2"
}
spec {
selector = {
app = kubernetes_pod_v1.example2.metadata.0.labels.app
}
session_affinity = "ClientIP"
port {
port = 8080
target_port = 8080
}

type = "NodePort"
}
}

resource "kubernetes_pod_v1" "example" {
metadata {
name = "terraform-myapp1"
labels = {
app = "myapp-1"
}
}

spec {
container {
image = "gcr.io/google-samples/hello-app:1.0"
name = "example"

port {
container_port = 8080
}
}
}
}

resource "kubernetes_pod_v1" "example2" {
metadata {
name = "terraform-myapp2"
labels = {
app = "myapp-2"
}
}

spec {
container {
image = "gcr.io/google-samples/hello-app:2.0"
name = "example"

port {
container_port = 8080
}
}
# [END gke_ap_autopilot_quickstart_ingress_terraform]
}
}

39 changes: 39 additions & 0 deletions autopilot/quickstart/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START gke_ap_autopilot_quickstart_main_terraform]
data "google_client_config" "default" {}

provider "kubernetes" {
host = "https://${module.gke.endpoint}"
token = data.google_client_config.default.access_token
cluster_ca_certificate = base64decode(module.gke.ca_certificate)
}

module "gke" {
source = "terraform-google-modules/kubernetes-engine/google//modules/beta-autopilot-public-cluster"
project_id = var.project_id
name = "${local.cluster_type}-cluster"
regional = true
region = var.region
network = module.gcp-network.network_name
subnetwork = local.subnet_names[index(module.gcp-network.subnets_names, local.subnet_name)]
ip_range_pods = local.pods_range_name
ip_range_services = local.svc_range_name
release_channel = "REGULAR"
enable_vertical_pod_autoscaling = true
network_tags = [local.cluster_type]
deletion_protection = false
}
# [END gke_ap_autopilot_quickstart_main_terraform]
49 changes: 49 additions & 0 deletions autopilot/quickstart/network.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START gke_ap_autopilot_quickstart_network_terraform]
module "gcp-network" {
source = "terraform-google-modules/network/google"
version = ">= 7.5"

project_id = var.project_id
network_name = local.network_name

subnets = [
{
subnet_name = local.subnet_name
subnet_ip = "10.0.0.0/17"
subnet_region = var.region
},
{
subnet_name = local.master_auth_subnetwork
subnet_ip = "10.60.0.0/17"
subnet_region = var.region
},
]

secondary_ranges = {
(local.subnet_name) = [
{
range_name = local.pods_range_name
ip_cidr_range = "192.168.0.0/18"
},
{
range_name = local.svc_range_name
ip_cidr_range = "192.168.64.0/18"
},
]
}
}
# [END gke_ap_autopilot_quickstart_network_terraform]
77 changes: 77 additions & 0 deletions autopilot/quickstart/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START gke_ap_autopilot_quickstart_outputs_terraform]
output "kubernetes_endpoint" {
description = "The cluster endpoint"
sensitive = true
value = module.gke.endpoint
}

output "cluster_name" {
description = "Cluster name"
value = module.gke.name
}

output "location" {
value = module.gke.location
}

output "master_kubernetes_version" {
description = "Kubernetes version of the master"
value = module.gke.master_version
}

output "ca_certificate" {
description = "The cluster ca certificate (base64 encoded)"
value = module.gke.ca_certificate
sensitive = true

}

output "service_account" {
description = "The service account to default running nodes as if not overridden in `node_pools`."
value = module.gke.service_account
}

output "network_name" {
description = "The name of the VPC being created"
value = module.gcp-network.network_name
}

output "subnet_names" {
description = "The names of the subnet being created"
value = module.gcp-network.subnets_names
}

output "region" {
description = "The region in which the cluster resides"
value = module.gke.region
}

output "zones" {
description = "List of zones in which the cluster resides"
value = module.gke.zones
}

output "project_id" {
description = "The project ID the cluster is in"
value = var.project_id
}

output "ingress_ip" {
description = "This is the public IP of the ingress"
value = google_compute_global_address.gke_ingress_ipv4.address
}
# [END gke_ap_autopilot_quickstart_outputs_terraform]
29 changes: 29 additions & 0 deletions autopilot/quickstart/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START gke_ap_autopilot_quickstart_providers_terraform]
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "5.6.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = "2.23.0"
}
}
required_version = ">= 0.13"
}
# [END gke_ap_autopilot_quickstart_providers_terraform]
37 changes: 37 additions & 0 deletions autopilot/quickstart/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# [START gke_ap_autopilot_quickstart_variables_terraform]
variable "project_id" {
description = "The project ID to host the cluster in"
}

variable "region" {
description = "The region the cluster in"
default = "us-central1"
}


locals {
cluster_type = "simple-autopilot-public"
network_name = "simple-autopilot-public-network"
subnet_name = "simple-autopilot-public-subnet"
master_auth_subnetwork = "simple-autopilot-public-master-subnet"
pods_range_name = "ip-range-pods-simple-autopilot-public"
svc_range_name = "ip-range-svc-simple-autopilot-public"
subnet_names = [for subnet_self_link in module.gcp-network.subnets_self_links : split("/", subnet_self_link)[length(split("/", subnet_self_link)) - 1]]
ingress_IP = google_compute_global_address.gke_ingress_ipv4.address
certificate_host = ["ingress.quickstart-playground.com", "quickstart-playground.com"]
}
# [END gke_ap_autopilot_quickstart_variables_terraform]

0 comments on commit ddec488

Please sign in to comment.