/
main.tf
84 lines (68 loc) · 2.04 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
data "external" "rancher_cluster" {
program = ["bash", "${path.module}/files/rancher_cluster_import.sh"]
query = {
rancher_api_url = var.rancher_api_url
rancher_access_key = var.rancher_access_key
rancher_secret_key = var.rancher_secret_key
name = var.name
}
}
provider "azurerm" {
version = "=2.0.0"
subscription_id = var.azure_subscription_id
client_id = var.azure_client_id
client_secret = var.azure_client_secret
tenant_id = var.azure_tenant_id
environment = var.azure_environment
}
resource "azurerm_resource_group" "resource_group" {
name = "${var.name}-resource_group"
location = var.azure_location
}
resource "azurerm_kubernetes_cluster" "primary" {
name = var.name
location = azurerm_resource_group.resource_group.location
resource_group_name = azurerm_resource_group.resource_group.name
dns_prefix = var.name
kubernetes_version = var.k8s_version
linux_profile {
admin_username = var.azure_ssh_user
ssh_key {
key_data = file(var.azure_public_key_path)
}
}
default_node_pool {
name = "default"
node_count = var.node_count
vm_size = var.azure_size
}
service_principal {
client_id = var.azure_client_id
client_secret = var.azure_client_secret
}
}
locals {
kube_config_path = "./${var.name}_config"
}
# Bootstrap rancher in aks environment
resource "null_resource" "import_rancher" {
triggers = {
cluster = azurerm_kubernetes_cluster.primary.id
}
provisioner "local-exec" {
command = format(
"cat << EOF > %s \n%s\nEOF",
local.kube_config_path,
azurerm_kubernetes_cluster.primary.kube_config_raw,
)
}
provisioner "local-exec" {
command = "curl --insecure -sfL ${var.rancher_api_url}/v3/import/${data.external.rancher_cluster.result.registration_token}.yaml | kubectl apply -f -"
environment = {
KUBECONFIG = local.kube_config_path
}
}
provisioner "local-exec" {
command = "rm ${local.kube_config_path}"
}
}