|
| 1 | +/** |
| 2 | +* Copyright 2024 Google LLC |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +data "google_client_config" "default" {} |
| 18 | + |
| 19 | +resource "google_container_cluster" "default" { |
| 20 | + name = "gke-autopilot-cluster" |
| 21 | + location = "us-central1" |
| 22 | + |
| 23 | + enable_autopilot = true |
| 24 | + |
| 25 | + # Set `deletion_protection` to `true` will ensure that one cannot |
| 26 | + # accidentally delete this instance by use of Terraform. |
| 27 | + deletion_protection = false |
| 28 | +} |
| 29 | + |
| 30 | +# [START gke_autopilot_reservation_specific_reservation] |
| 31 | +resource "google_compute_reservation" "specific_pod" { |
| 32 | + name = "specific-reservation-pod" |
| 33 | + zone = "us-central1-a" |
| 34 | + |
| 35 | + specific_reservation { |
| 36 | + count = 1 |
| 37 | + |
| 38 | + instance_properties { |
| 39 | + machine_type = "c3-standard-4-lssd" |
| 40 | + |
| 41 | + local_ssds { |
| 42 | + disk_size_gb = 375 |
| 43 | + interface = "NVME" |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + specific_reservation_required = true |
| 49 | +} |
| 50 | +# [END gke_autopilot_reservation_specific_reservation] |
| 51 | + |
| 52 | +# [START gke_autopilot_reservation_specific_cluster] |
| 53 | +resource "google_compute_reservation" "specific_accelerator" { |
| 54 | + name = "specific-reservation-accelerator" |
| 55 | + zone = "us-central1-a" |
| 56 | + |
| 57 | + specific_reservation { |
| 58 | + count = 1 |
| 59 | + |
| 60 | + instance_properties { |
| 61 | + #min_cpu_platform = "Intel Cascade Lake" |
| 62 | + machine_type = "g2-standard-4" |
| 63 | + |
| 64 | + guest_accelerators { |
| 65 | + accelerator_count = 1 |
| 66 | + accelerator_type = "nvidia-l4" |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + specific_reservation_required = true |
| 72 | +} |
| 73 | +# [END gke_autopilot_reservation_specific_cluster] |
| 74 | + |
| 75 | +provider "kubernetes" { |
| 76 | + host = "https://${google_container_cluster.default.endpoint}" |
| 77 | + token = data.google_client_config.default.access_token |
| 78 | + cluster_ca_certificate = base64decode(google_container_cluster.default.master_auth[0].cluster_ca_certificate) |
| 79 | + |
| 80 | + ignore_annotations = [ |
| 81 | + "^autopilot\\.gke\\.io\\/.*", |
| 82 | + "^cloud\\.google\\.com\\/cluster_autoscaler_.*" |
| 83 | + ] |
| 84 | +} |
| 85 | + |
| 86 | +# [START gke_autopilot_reservation_specific_pod] |
| 87 | +resource "kubernetes_pod_v1" "default_pod" { |
| 88 | + metadata { |
| 89 | + name = "specific-same-project-pod" |
| 90 | + } |
| 91 | + |
| 92 | + spec { |
| 93 | + node_selector = { |
| 94 | + "cloud.google.com/compute-class" = "Performance" |
| 95 | + "cloud.google.com/machine-family" = "c3" |
| 96 | + "cloud.google.com/reservation-name" = google_compute_reservation.specific_pod.name |
| 97 | + "cloud.google.com/reservation-affinity" = "specific" |
| 98 | + } |
| 99 | + |
| 100 | + container { |
| 101 | + name = "my-container" |
| 102 | + image = "k8s.gcr.io/pause" |
| 103 | + |
| 104 | + resources { |
| 105 | + requests = { |
| 106 | + cpu = 2 |
| 107 | + memory = "8Gi" |
| 108 | + ephemeral-storage = "1Gi" |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + security_context { |
| 113 | + allow_privilege_escalation = false |
| 114 | + run_as_non_root = false |
| 115 | + |
| 116 | + capabilities { |
| 117 | + add = [] |
| 118 | + drop = ["NET_RAW"] |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + security_context { |
| 124 | + run_as_non_root = false |
| 125 | + supplemental_groups = [] |
| 126 | + |
| 127 | + seccomp_profile { |
| 128 | + type = "RuntimeDefault" |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + depends_on = [ |
| 134 | + google_compute_reservation.specific_pod |
| 135 | + ] |
| 136 | +} |
| 137 | +# [END gke_autopilot_reservation_specific_pod] |
| 138 | + |
| 139 | +# [START gke_autopilot_reservation_specific_accelerator] |
| 140 | +resource "kubernetes_pod_v1" "default_accelerator" { |
| 141 | + metadata { |
| 142 | + name = "specific-same-project-accelerator" |
| 143 | + } |
| 144 | + |
| 145 | + spec { |
| 146 | + node_selector = { |
| 147 | + "cloud.google.com/compute-class" = "Accelerator" |
| 148 | + "cloud.google.com/gke-accelerator" = "nvidia-l4" |
| 149 | + "cloud.google.com/reservation-name" = google_compute_reservation.specific_accelerator.name |
| 150 | + "cloud.google.com/reservation-affinity" = "specific" |
| 151 | + } |
| 152 | + |
| 153 | + container { |
| 154 | + name = "my-container" |
| 155 | + image = "k8s.gcr.io/pause" |
| 156 | + |
| 157 | + resources { |
| 158 | + requests = { |
| 159 | + cpu = 2 |
| 160 | + memory = "7Gi" |
| 161 | + ephemeral-storage = "1Gi" |
| 162 | + "nvidia.com/gpu" = 1 |
| 163 | + |
| 164 | + } |
| 165 | + limits = { |
| 166 | + "nvidia.com/gpu" = 1 |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + security_context { |
| 171 | + allow_privilege_escalation = false |
| 172 | + run_as_non_root = false |
| 173 | + |
| 174 | + capabilities { |
| 175 | + add = [] |
| 176 | + drop = ["NET_RAW"] |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + security_context { |
| 182 | + run_as_non_root = false |
| 183 | + supplemental_groups = [] |
| 184 | + |
| 185 | + seccomp_profile { |
| 186 | + type = "RuntimeDefault" |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + depends_on = [ |
| 192 | + google_compute_reservation.specific_accelerator |
| 193 | + ] |
| 194 | +} |
| 195 | +# [END gke_autopilot_reservation_specific_accelerator] |
0 commit comments