|
| 1 | +provider "helm" { |
| 2 | + version = ">= 1.1.1" |
| 3 | + |
| 4 | + kubernetes { |
| 5 | + config_path = local.cluster_config |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +locals { |
| 10 | + gitops_dir = var.gitops_dir != "" ? var.gitops_dir : "${path.cwd}/gitops" |
| 11 | + chart_name = "cloud-setup" |
| 12 | + chart_dir = "${local.gitops_dir}/${local.chart_name}" |
| 13 | + global_config = { |
| 14 | + clusterType = local.cluster_type_code |
| 15 | + ingressSubdomain = local.ingress_hostname |
| 16 | + tlsSecretName = local.tls_secret |
| 17 | + } |
| 18 | + ibmcloud_config = { |
| 19 | + apikey = var.ibmcloud_api_key |
| 20 | + resource_group = var.resource_group_name |
| 21 | + server_url = local.server_url |
| 22 | + cluster_type = local.cluster_type |
| 23 | + cluster_name = local.cluster_name |
| 24 | + tls_secret_name = local.tls_secret |
| 25 | + ingress_subdomain = local.ingress_hostname |
| 26 | + region = var.region |
| 27 | + cluster_version = local.cluster_version |
| 28 | + } |
| 29 | + cntk_dev_guide_config = { |
| 30 | + name = "cntk-dev-guide" |
| 31 | + displayName = "Cloud-Native Toolkit" |
| 32 | + url = "https://cloudnativetoolkit.dev" |
| 33 | + } |
| 34 | + first_app_config = { |
| 35 | + name = "first-app" |
| 36 | + displayName = "Deploy first app" |
| 37 | + url = "https://cloudnativetoolkit.dev/getting-started-day-1/deploy-app/" |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +resource "null_resource" "list_tmp" { |
| 42 | + depends_on = [null_resource.create_dirs] |
| 43 | + |
| 44 | + triggers = { |
| 45 | + always_run = timestamp() |
| 46 | + } |
| 47 | + |
| 48 | + provisioner "local-exec" { |
| 49 | + command = "ls ${local.tmp_dir}" |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +data ibm_container_cluster_config cluster_admin { |
| 54 | + depends_on = [ibm_container_vpc_cluster.cluster, null_resource.list_tmp] |
| 55 | + |
| 56 | + cluster_name_id = local.cluster_name |
| 57 | + admin = true |
| 58 | + resource_group_id = data.ibm_resource_group.resource_group.id |
| 59 | + config_dir = local.cluster_config_dir |
| 60 | +} |
| 61 | + |
| 62 | +data ibm_container_cluster_config cluster { |
| 63 | + depends_on = [ |
| 64 | + ibm_container_vpc_cluster.cluster, |
| 65 | + null_resource.list_tmp, |
| 66 | + data.ibm_container_cluster_config.cluster_admin |
| 67 | + ] |
| 68 | + |
| 69 | + cluster_name_id = local.cluster_name |
| 70 | + resource_group_id = data.ibm_resource_group.resource_group.id |
| 71 | + config_dir = local.cluster_config_dir |
| 72 | +} |
| 73 | + |
| 74 | +resource null_resource setup_kube_config { |
| 75 | + depends_on = [null_resource.create_dirs] |
| 76 | + |
| 77 | + provisioner "local-exec" { |
| 78 | + command = "rm -f ${local.cluster_config} && ln -s ${data.ibm_container_cluster_config.cluster.config_file_path} ${local.cluster_config}" |
| 79 | + } |
| 80 | + |
| 81 | + provisioner "local-exec" { |
| 82 | + command = "cp ${regex("(.*)/config.yml", data.ibm_container_cluster_config.cluster.config_file_path)[0]}/* ${local.cluster_config_dir}" |
| 83 | + } |
| 84 | + |
| 85 | + provisioner "local-exec" { |
| 86 | + command = "echo 'Waiting for 5 minutes for permissions to be established...' && sleep 300" |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +resource null_resource setup-chart { |
| 91 | + provisioner "local-exec" { |
| 92 | + command = "mkdir -p ${local.chart_dir} && cp -R ${path.module}/chart/${local.chart_name}/* ${local.chart_dir}" |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +resource null_resource delete-helm-cloud-config { |
| 97 | + depends_on = [null_resource.setup_kube_config] |
| 98 | + |
| 99 | + provisioner "local-exec" { |
| 100 | + command = "kubectl delete secret -n ${local.config_namespace} -l name=${local.ibmcloud_release_name} --ignore-not-found" |
| 101 | + |
| 102 | + environment = { |
| 103 | + KUBECONFIG = local.cluster_config |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + provisioner "local-exec" { |
| 108 | + command = "kubectl delete secret -n ${local.config_namespace} -l name=cloud-setup --ignore-not-found" |
| 109 | + |
| 110 | + environment = { |
| 111 | + KUBECONFIG = local.cluster_config |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + provisioner "local-exec" { |
| 116 | + command = "kubectl delete secret -n ${local.config_namespace} ibmcloud-apikey --ignore-not-found" |
| 117 | + |
| 118 | + environment = { |
| 119 | + KUBECONFIG = local.cluster_config |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + provisioner "local-exec" { |
| 124 | + command = "kubectl delete configmap -n ${local.config_namespace} ibmcloud-config --ignore-not-found" |
| 125 | + |
| 126 | + environment = { |
| 127 | + KUBECONFIG = local.cluster_config |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + provisioner "local-exec" { |
| 132 | + command = "kubectl delete secret -n ${local.config_namespace} cloud-access --ignore-not-found" |
| 133 | + |
| 134 | + environment = { |
| 135 | + KUBECONFIG = local.cluster_config |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + provisioner "local-exec" { |
| 140 | + command = "kubectl delete configmap -n ${local.config_namespace} cloud-config --ignore-not-found" |
| 141 | + |
| 142 | + environment = { |
| 143 | + KUBECONFIG = local.cluster_config |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +resource "null_resource" "delete-consolelink" { |
| 149 | + depends_on = [null_resource.setup_kube_config] |
| 150 | + count = local.cluster_type_code == "ocp4" ? 1 : 0 |
| 151 | + |
| 152 | + provisioner "local-exec" { |
| 153 | + command = "kubectl delete consolelink toolkit-github --ignore-not-found" |
| 154 | + |
| 155 | + environment = { |
| 156 | + KUBECONFIG = local.cluster_config |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + provisioner "local-exec" { |
| 161 | + command = "kubectl delete consolelink toolkit-registry --ignore-not-found" |
| 162 | + |
| 163 | + environment = { |
| 164 | + KUBECONFIG = local.cluster_config |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +resource "local_file" "cloud-values" { |
| 170 | + depends_on = [null_resource.setup-chart] |
| 171 | + |
| 172 | + content = yamlencode({ |
| 173 | + global = local.global_config |
| 174 | + cloud-setup = { |
| 175 | + ibmcloud = local.ibmcloud_config |
| 176 | + cntk-dev-guide = local.cntk_dev_guide_config |
| 177 | + first-app = local.first_app_config |
| 178 | + } |
| 179 | + }) |
| 180 | + filename = "${local.chart_dir}/values.yaml" |
| 181 | +} |
| 182 | + |
| 183 | +resource "null_resource" "print-values" { |
| 184 | + provisioner "local-exec" { |
| 185 | + command = "cat ${local_file.cloud-values.filename}" |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +resource "helm_release" "cloud_setup" { |
| 190 | + depends_on = [null_resource.setup_kube_config, null_resource.delete-helm-cloud-config, null_resource.delete-consolelink, local_file.cloud-values] |
| 191 | + |
| 192 | + name = "cloud-setup" |
| 193 | + chart = local.chart_dir |
| 194 | + version = "0.1.0" |
| 195 | + namespace = local.config_namespace |
| 196 | + timeout = 1200 |
| 197 | + dependency_update = true |
| 198 | + force_update = true |
| 199 | + replace = true |
| 200 | + |
| 201 | + disable_openapi_validation = true |
| 202 | +} |
0 commit comments