|
| 1 | +/** |
| 2 | + * Copyright 2018-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 | +locals { |
| 18 | + tmp_credentials_path = "${path.module}/terraform-google-credentials.json" |
| 19 | + cache_path = "${path.module}/.cache/${random_id.cache.hex}" |
| 20 | + gcloud_tar_path = "${local.cache_path}/google-cloud-sdk.tar.gz" |
| 21 | + gcloud_bin_path = "${local.cache_path}/google-cloud-sdk/bin" |
| 22 | + gcloud_bin_abs_path = abspath(local.gcloud_bin_path) |
| 23 | + |
| 24 | + gcloud = "${local.gcloud_bin_path}/gcloud" |
| 25 | + gcloud_download_url = var.gcloud_download_url != null ? var.gcloud_download_url : "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-${var.gcloud_sdk_version}-${var.platform}-x86_64.tar.gz" |
| 26 | + jq_platform = var.platform == "darwin" ? "osx-amd" : var.platform |
| 27 | + jq_download_url = var.jq_download_url != null ? var.jq_download_url : "https://github.com/stedolan/jq/releases/download/jq-${var.jq_version}/jq-${local.jq_platform}64" |
| 28 | + asmcli_download_url = var.asmcli_download_url != null ? var.asmcli_download_url : "https://storage.googleapis.com/csm-artifacts/asm/asmcli_${var.asmcli_version}" |
| 29 | + |
| 30 | + cmd_entrypoint = "${local.gcloud_bin_path}/asmcli" |
| 31 | + create_cmd_body = "install --kubeconfig ${var.kubeconfig} --context ${var.context} --fleet_id ${var.fleet_id} --platform multicloud --enable_cluster_labels --enable_namespace_creation --enable_gcp_components --enable_cluster_roles --ca mesh_ca --option attached-cluster" |
| 32 | + |
| 33 | + wait = length(null_resource.additional_components[*].triggers) + length( |
| 34 | + null_resource.gcloud_auth_service_account_key_file[*].triggers, |
| 35 | + ) + length(null_resource.gcloud_auth_google_credentials[*].triggers, |
| 36 | + ) + length(null_resource.run_command[*].triggers) |
| 37 | + |
| 38 | + prepare_cache_command = "mkdir -p ${local.cache_path}" |
| 39 | + download_gcloud_command = "curl -sL -o ${local.cache_path}/google-cloud-sdk.tar.gz ${local.gcloud_download_url}" |
| 40 | + download_jq_command = "curl -sL -o ${local.cache_path}/jq ${local.jq_download_url} && chmod +x ${local.cache_path}/jq" |
| 41 | + download_asmcli_command = "curl -sL -o ${local.cache_path}/asmcli ${local.asmcli_download_url} && chmod +x ${local.cache_path}/asmcli" |
| 42 | + decompress_command = "tar -xzf ${local.gcloud_tar_path} -C ${local.cache_path} && cp ${local.cache_path}/jq ${local.cache_path}/google-cloud-sdk/bin/ && cp ${local.cache_path}/asmcli ${local.cache_path}/google-cloud-sdk/bin/" |
| 43 | + additional_components_command = "${path.module}/scripts/check_components.sh ${local.gcloud} kubectl" |
| 44 | + gcloud_auth_service_account_key_file_command = "${local.gcloud} auth activate-service-account --key-file ${var.service_account_key_file}" |
| 45 | + activate_service_account = var.activate_service_account ? "${local.gcloud} auth activate-service-account --key-file ${local.tmp_credentials_path}" : "true" |
| 46 | + gcloud_auth_google_credentials_command = <<-EOT |
| 47 | + printf "%s" "$GOOGLE_CREDENTIALS" > ${local.tmp_credentials_path} && \ |
| 48 | + ${local.activate_service_account} |
| 49 | + EOT |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +resource "random_id" "cache" { |
| 54 | + byte_length = 4 |
| 55 | +} |
| 56 | + |
| 57 | +resource "null_resource" "prepare_cache" { |
| 58 | + triggers = { |
| 59 | + arguments = md5(local.create_cmd_body) |
| 60 | + prepare_cache_command = local.prepare_cache_command |
| 61 | + } |
| 62 | + |
| 63 | + provisioner "local-exec" { |
| 64 | + when = create |
| 65 | + command = self.triggers.prepare_cache_command |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +resource "null_resource" "download_gcloud" { |
| 70 | + triggers = { |
| 71 | + arguments = md5(local.create_cmd_body) |
| 72 | + download_gcloud_command = local.download_gcloud_command |
| 73 | + version = var.gcloud_sdk_version |
| 74 | + } |
| 75 | + |
| 76 | + provisioner "local-exec" { |
| 77 | + when = create |
| 78 | + command = self.triggers.download_gcloud_command |
| 79 | + } |
| 80 | + |
| 81 | + depends_on = [null_resource.prepare_cache] |
| 82 | +} |
| 83 | + |
| 84 | +resource "null_resource" "download_jq" { |
| 85 | + triggers = { |
| 86 | + arguments = md5(local.create_cmd_body) |
| 87 | + download_jq_command = local.download_jq_command |
| 88 | + version = var.jq_version |
| 89 | + } |
| 90 | + |
| 91 | + provisioner "local-exec" { |
| 92 | + when = create |
| 93 | + command = self.triggers.download_jq_command |
| 94 | + } |
| 95 | + |
| 96 | + depends_on = [null_resource.prepare_cache] |
| 97 | +} |
| 98 | + |
| 99 | +resource "null_resource" "download_asmcli" { |
| 100 | + triggers = { |
| 101 | + arguments = md5(local.create_cmd_body) |
| 102 | + download_asmcli_command = local.download_asmcli_command |
| 103 | + version = var.asmcli_version |
| 104 | + } |
| 105 | + |
| 106 | + provisioner "local-exec" { |
| 107 | + when = create |
| 108 | + command = self.triggers.download_asmcli_command |
| 109 | + } |
| 110 | + |
| 111 | + depends_on = [null_resource.prepare_cache] |
| 112 | +} |
| 113 | + |
| 114 | +resource "null_resource" "decompress" { |
| 115 | + triggers = { |
| 116 | + arguments = md5(local.create_cmd_body) |
| 117 | + decompress_command = local.decompress_command |
| 118 | + download_gcloud_command = local.download_gcloud_command |
| 119 | + download_jq_command = local.download_jq_command |
| 120 | + download_asmcli_command = local.download_asmcli_command |
| 121 | + } |
| 122 | + |
| 123 | + provisioner "local-exec" { |
| 124 | + when = create |
| 125 | + command = self.triggers.decompress_command |
| 126 | + } |
| 127 | + |
| 128 | + depends_on = [null_resource.download_gcloud, null_resource.download_jq, null_resource.download_asmcli] |
| 129 | +} |
| 130 | + |
| 131 | +resource "null_resource" "additional_components" { |
| 132 | + depends_on = [null_resource.decompress] |
| 133 | + |
| 134 | + triggers = { |
| 135 | + arguments = md5(local.create_cmd_body) |
| 136 | + additional_components_command = local.additional_components_command |
| 137 | + } |
| 138 | + |
| 139 | + provisioner "local-exec" { |
| 140 | + when = create |
| 141 | + command = self.triggers.additional_components_command |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +resource "null_resource" "gcloud_auth_service_account_key_file" { |
| 146 | + count = length(var.service_account_key_file) > 0 ? 1 : 0 |
| 147 | + depends_on = [null_resource.decompress] |
| 148 | + |
| 149 | + triggers = { |
| 150 | + arguments = md5(local.create_cmd_body) |
| 151 | + gcloud_auth_service_account_key_file_command = local.gcloud_auth_service_account_key_file_command |
| 152 | + } |
| 153 | + |
| 154 | + provisioner "local-exec" { |
| 155 | + when = create |
| 156 | + command = self.triggers.gcloud_auth_service_account_key_file_command |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +resource "null_resource" "gcloud_auth_google_credentials" { |
| 161 | + count = var.use_tf_google_credentials_env_var ? 1 : 0 |
| 162 | + depends_on = [null_resource.decompress] |
| 163 | + |
| 164 | + triggers = { |
| 165 | + arguments = md5(local.create_cmd_body) |
| 166 | + gcloud_auth_google_credentials_command = local.gcloud_auth_google_credentials_command |
| 167 | + } |
| 168 | + |
| 169 | + provisioner "local-exec" { |
| 170 | + when = create |
| 171 | + command = self.triggers.gcloud_auth_google_credentials_command |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +resource "null_resource" "run_command" { |
| 176 | + depends_on = [ |
| 177 | + null_resource.decompress, |
| 178 | + null_resource.additional_components, |
| 179 | + null_resource.gcloud_auth_google_credentials, |
| 180 | + null_resource.gcloud_auth_service_account_key_file |
| 181 | + ] |
| 182 | + |
| 183 | + triggers = { |
| 184 | + arguments = md5(local.create_cmd_body) |
| 185 | + cmd_entrypoint = local.cmd_entrypoint |
| 186 | + create_cmd_body = local.create_cmd_body |
| 187 | + gcloud_bin_abs_path = local.gcloud_bin_abs_path |
| 188 | + } |
| 189 | + |
| 190 | + provisioner "local-exec" { |
| 191 | + when = create |
| 192 | + command = <<-EOT |
| 193 | + PATH=${self.triggers.gcloud_bin_abs_path}:$PATH |
| 194 | + ${self.triggers.cmd_entrypoint} ${self.triggers.create_cmd_body} |
| 195 | + EOT |
| 196 | + environment = { |
| 197 | + PROJECT_ID = "" |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | +} |
| 202 | + |
| 203 | +resource "null_resource" "gcloud_auth_google_credentials_destroy" { |
| 204 | + count = var.use_tf_google_credentials_env_var ? 1 : 0 |
| 205 | + triggers = { |
| 206 | + gcloud_auth_google_credentials_command = local.gcloud_auth_google_credentials_command |
| 207 | + } |
| 208 | + provisioner "local-exec" { |
| 209 | + when = destroy |
| 210 | + command = self.triggers.gcloud_auth_google_credentials_command |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +resource "null_resource" "gcloud_auth_service_account_key_file_destroy" { |
| 215 | + count = length(var.service_account_key_file) > 0 ? 1 : 0 |
| 216 | + triggers = { |
| 217 | + gcloud_auth_service_account_key_file_command = local.gcloud_auth_service_account_key_file_command |
| 218 | + } |
| 219 | + |
| 220 | + provisioner "local-exec" { |
| 221 | + when = destroy |
| 222 | + command = self.triggers.gcloud_auth_service_account_key_file_command |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +resource "null_resource" "additional_components_destroy" { |
| 227 | + triggers = { |
| 228 | + additional_components_command = local.additional_components_command |
| 229 | + } |
| 230 | + |
| 231 | + provisioner "local-exec" { |
| 232 | + when = destroy |
| 233 | + command = self.triggers.additional_components_command |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +resource "null_resource" "decompress_destroy" { |
| 238 | + triggers = { |
| 239 | + decompress_command = local.decompress_command |
| 240 | + } |
| 241 | + |
| 242 | + provisioner "local-exec" { |
| 243 | + when = destroy |
| 244 | + command = self.triggers.decompress_command |
| 245 | + } |
| 246 | +} |
0 commit comments