-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.tf
135 lines (127 loc) · 4.54 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Copyright 2022 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.
*/
locals {
os_type = var.is_windows ? "Windows" : "Linux"
use_exsiting_boot_disk = var.boot_disk_name == "" ? false : true
boot_disk_name = var.boot_disk_name == "" ? "${var.name}-boot-disk" : var.boot_disk_name
use_advanced_compute = var.is_guaranteed && var.dedicated_cpu
compute_base = var.vm_type_name == "" ? {
virtualMachineTypeName = ""
cpu = {
vcpus = var.vcpus
}
memory = {
capacity = var.memory
}
guaranteed = var.is_guaranteed
advancedCompute = local.use_advanced_compute ? {
dedicatedCPUPlacement = var.dedicated_cpu
isolatedEmulatorThread = var.isolated_emulator_thread
hugePageSize = var.hugepage_size
numaGuestMappingPassthrough = var.numa_guest_mapping_passthrough ? {} : null
} : null
} : {
virtualMachineTypeName = var.vm_type_name
cpu = null
memory = null
guaranteed = null
advancedCompute = null
}
compute = { for k, v in local.compute_base : k => v if v != null } # Remove the sections if they are null
firmware = var.boot_loader_type == "" ? null : {
bootloader = {
type = var.boot_loader_type
enableSecureBoot = var.enable_secure_boot
}
}
spec_base = {
osType = local.os_type
compute = local.compute
scheduling = var.scheduling == null ? null : { for k, v in var.scheduling : k => v if v != null }
autoRestartOnConfigurationChange = var.auto_restart_on_config_change
gpu = var.vm_type_name == "" ? var.gpu : null
firmware = local.firmware
guestEnvironment = var.guest_environment == null ? null : {
accessManagement = var.guest_environment.enable_access_management ? {
enable = var.guest_environment.enable_access_management
} : null
}
disks = concat([
{
boot = true
autoDelete = !local.use_exsiting_boot_disk
virtualMachineDiskName = local.boot_disk_name
}
], [for disk in var.extra_disks : {
virtualMachineDiskName = disk["name"]
readOnly = disk["readonly"]
autoDelete = disk["auto_delete"]
}])
interfaces = concat([
{
name = "eth0"
networkName = "pod-network"
default = true
}
], [for intf in var.extra_interfaces : {
name = intf["name"]
networkName = intf["network"]
ipAddresses = intf["ips"]
}])
cloudInit = var.cloudinit_nocloud == null ? null : {
noCloud = { for k, v in var.cloudinit_nocloud : k => v if v != null }
}
startupScripts = var.startup_scripts == null ? null : [for s in var.startup_scripts : { for k, v in s : k => v if v != null }]
}
spec = { for k, v in local.spec_base : k => v if v != null } # Remove the sections if they are null
}
module "boot_disk" {
count = local.use_exsiting_boot_disk ? 0 : 1
source = "./modules/vm-disk"
name = local.boot_disk_name
namespace = var.namespace
disk_size = var.boot_disk_size
storage_class = var.storage_class
gcs_source = var.boot_disk_gcs_source
http_source = var.boot_disk_http_source
registry_source = var.boot_disk_registry_source
}
resource "kubernetes_manifest" "vm_instance" {
manifest = {
apiVersion = "vm.cluster.gke.io/v1"
kind = "VirtualMachine"
metadata = {
name = var.name
namespace = var.namespace
}
spec = local.spec
}
wait {
fields = var.wait_fields
dynamic "condition" {
for_each = var.wait_conditions
content {
type = condition.value["type"]
status = condition.value["status"]
}
}
}
timeouts {
create = var.create_timeout
update = var.update_timeout
delete = var.delete_timeout
}
}