Skip to content

Commit ca97c2f

Browse files
committed
add alicloud_cs_kubernetes_node_pool
1 parent e438f0c commit ca97c2f

File tree

4 files changed

+153
-34
lines changed

4 files changed

+153
-34
lines changed

examples/complete/main.tf

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,42 @@ variable "profile" {
22
default = "default"
33
}
44

5-
variable "region" {
6-
default = "cn-hangzhou"
5+
data "alicloud_zones" "default" {
6+
available_resource_creation = "VSwitch"
77
}
88

9-
data "alicloud_vpcs" "default" {
10-
is_default = true
9+
resource "alicloud_vpc" "default" {
10+
vpc_name = "tf_module"
11+
cidr_block = "10.4.0.0/16"
12+
}
13+
14+
resource "alicloud_vswitch" "default" {
15+
count = 3
16+
vpc_id = alicloud_vpc.default.id
17+
cidr_block = cidrsubnet(alicloud_vpc.default.cidr_block, 8, count.index)
18+
zone_id = data.alicloud_zones.default.zones[0].id
1119
}
1220

1321
module "k8s" {
1422
source = "../.."
15-
region = var.region
1623

1724
new_nat_gateway = false
18-
vpc_id = data.alicloud_vpcs.default.vpcs.0.id
19-
vswitch_ids = ["vsw-bp1pog8voc3f42arr****", "vsw-bp1jxetj1386gqssg****", "vsw-bp1s1835sq5tjss9s****"]
25+
vpc_id = alicloud_vpc.default.id
26+
vswitch_ids = alicloud_vswitch.default.*.id
2027
master_instance_types = ["ecs.n1.medium", "ecs.c5.large", "ecs.n1.medium"]
2128
worker_instance_types = ["ecs.n1.medium"]
22-
k8s_pod_cidr = "192.168.5.0/24"
23-
k8s_service_cidr = "192.168.2.0/24"
29+
k8s_pod_cidr = "10.72.0.0/16"
30+
k8s_service_cidr = "172.18.0.0/16"
2431
k8s_worker_number = 2
25-
}
32+
33+
data_disks = [{
34+
category = "cloud_efficiency"
35+
size = 40
36+
}]
37+
}
38+
39+
data "alicloud_cs_cluster_credential" "auth" {
40+
cluster_id = module.k8s.cluster_id[0]
41+
temporary_duration_minutes = 60
42+
output_file = "~/.kube/config"
43+
}

local.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
locals {
2+
subscription = var.instance_charge_type == "PostPaid" ? {} : var.subscription
3+
}

main.tf

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ data "alicloud_zones" "default" {
1111

1212
// If there is not specifying vpc_id, the module will launch a new vpc
1313
resource "alicloud_vpc" "vpc" {
14-
count = var.vpc_id == "" ? 1 : 0
14+
count = var.create_vpc ? 1 : 0
1515
cidr_block = var.vpc_cidr
16-
name = var.vpc_name == "" ? var.example_name : var.vpc_name
16+
vpc_name = var.vpc_name == "" ? var.example_name : var.vpc_name
1717
}
1818

1919
// According to the vswitch cidr blocks to launch several vswitches
2020
resource "alicloud_vswitch" "vswitches" {
21-
count = length(var.vswitch_ids) > 0 ? 0 : length(var.vswitch_cidrs)
22-
vpc_id = var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id
23-
cidr_block = var.vswitch_cidrs[count.index]
24-
availability_zone = data.alicloud_zones.default.zones[count.index % length(data.alicloud_zones.default.zones)]["id"]
25-
name = var.vswitch_name_prefix == "" ? format(
21+
count = length(var.vswitch_ids) > 0 ? 0 : length(var.vswitch_cidrs)
22+
vpc_id = var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id
23+
cidr_block = var.vswitch_cidrs[count.index]
24+
zone_id = data.alicloud_zones.default.zones[count.index % length(data.alicloud_zones.default.zones)]["id"]
25+
vswitch_name = var.vswitch_name_prefix == "" ? format(
2626
"%s-%s",
2727
var.example_name,
2828
format(var.number_format, count.index + 1),
@@ -71,16 +71,12 @@ resource "alicloud_cs_kubernetes" "k8s" {
7171
format(var.number_format, count.index + 1),
7272
)
7373
master_vswitch_ids = length(var.vswitch_ids) > 0 ? split(",", join(",", var.vswitch_ids)) : length(var.vswitch_cidrs) < 1 ? [] : split(",", join(",", alicloud_vswitch.vswitches.*.id))
74-
worker_vswitch_ids = length(var.vswitch_ids) > 0 ? split(",", join(",", var.vswitch_ids)) : length(var.vswitch_cidrs) < 1 ? [] : split(",", join(",", alicloud_vswitch.vswitches.*.id))
7574
master_instance_types = var.master_instance_types
76-
worker_instance_types = var.worker_instance_types
77-
worker_number = var.k8s_worker_number
7875
node_cidr_mask = var.node_cidr_mask
7976
enable_ssh = var.enable_ssh
8077
install_cloud_monitor = var.install_cloud_monitor
81-
cpu_policy = var.cpu_policy
8278
proxy_mode = var.proxy_mode
83-
password = var.password
79+
password = var.master_password
8480
pod_cidr = var.k8s_pod_cidr
8581
service_cidr = var.k8s_service_cidr
8682
version = var.k8s_version
@@ -92,4 +88,43 @@ resource "alicloud_cs_kubernetes" "k8s" {
9288
}
9389
}
9490
depends_on = [alicloud_snat_entry.default]
95-
}
91+
}
92+
93+
resource "alicloud_cs_kubernetes_node_pool" "default" {
94+
count = var.k8s_number
95+
96+
name = alicloud_cs_kubernetes.k8s[count.index].name
97+
cluster_id = alicloud_cs_kubernetes.k8s[count.index].id
98+
vswitch_ids = length(var.vswitch_ids) > 0 ? split(",", join(",", var.vswitch_ids)) : length(var.vswitch_cidrs) < 1 ? [] : split(",", join(",", alicloud_vswitch.vswitches.*.id))
99+
password = var.worker_password[count.index]
100+
101+
desired_size = var.k8s_worker_number
102+
install_cloud_monitor = var.install_cloud_monitor
103+
instance_types = var.worker_instance_types
104+
105+
instance_charge_type = var.instance_charge_type
106+
period = lookup(local.subscription, "period", null)
107+
period_unit = lookup(local.subscription, "period_unit", null)
108+
auto_renew = lookup(local.subscription, "auto_renew", null)
109+
auto_renew_period = lookup(local.subscription, "auto_renew_period", null)
110+
111+
cpu_policy = var.cpu_policy
112+
system_disk_category = var.system_disk_category
113+
system_disk_size = var.system_disk_size
114+
115+
dynamic "data_disks" {
116+
for_each = var.data_disks
117+
content {
118+
name = lookup(data_disks.value, "name", null)
119+
size = lookup(data_disks.value, "size", null)
120+
category = lookup(data_disks.value, "category", null)
121+
encrypted = lookup(data_disks.value, "encrypted", null)
122+
performance_level = lookup(data_disks.value, "encperformance_levelrypted", null)
123+
snapshot_id = lookup(data_disks.value, "snapshot_id", null)
124+
device = lookup(data_disks.value, "device", null)
125+
kms_key_id = lookup(data_disks.value, "kms_key_id", null)
126+
auto_snapshot_policy_id = lookup(data_disks.value, "auto_snapshot_policy_id", null)
127+
128+
}
129+
}
130+
}

variables.tf

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ variable "vpc_name" {
5252
default = ""
5353
}
5454

55+
variable "create_vpc" {
56+
description = "Boolean. If you have a vpc already, use that one, else make this true and one will be created."
57+
type = bool
58+
default = false
59+
}
60+
5561
variable "vpc_id" {
5662
description = "Existing vpc id used to create several vswitches and other resources."
5763
type = string
@@ -130,13 +136,7 @@ variable "node_cidr_mask" {
130136
variable "enable_ssh" {
131137
description = "Enable login to the node through SSH."
132138
type = bool
133-
default = true
134-
}
135-
136-
variable "install_cloud_monitor" {
137-
description = "Install cloud monitor agent on ECS."
138-
type = bool
139-
default = true
139+
default = false
140140
}
141141

142142
variable "cpu_policy" {
@@ -151,8 +151,8 @@ variable "proxy_mode" {
151151
default = "iptables"
152152
}
153153

154-
variable "password" {
155-
description = "The password of ECS instance."
154+
variable "master_password" {
155+
description = "The password of master ECS instance."
156156
type = string
157157
default = "Just4Test"
158158
}
@@ -177,9 +177,9 @@ variable "k8s_service_cidr" {
177177
}
178178

179179
variable "k8s_version" {
180-
description = "The version of the kubernetes version. Valid values: '1.16.6-aliyun.1','1.14.8-aliyun.1'. Default to '1.16.6-aliyun.1'."
180+
description = "The version of the kubernetes version."
181181
type = string
182-
default = "1.16.6-aliyun.1"
182+
default = ""
183183
}
184184

185185
variable "cluster_addons" {
@@ -190,3 +190,66 @@ variable "cluster_addons" {
190190
}))
191191
default = []
192192
}
193+
194+
######################
195+
# node pool variables
196+
######################
197+
198+
variable "worker_password" {
199+
description = "The password of worker ECS instance."
200+
type = list(string)
201+
default = ["Just4Test"]
202+
}
203+
204+
variable "install_cloud_monitor" {
205+
description = "Install cloud monitor agent on ECS."
206+
type = bool
207+
default = true
208+
}
209+
210+
variable "instance_charge_type" {
211+
description = "The charge type of instance. Choices are 'PostPaid' and 'PrePaid'."
212+
type = string
213+
default = "PostPaid"
214+
}
215+
216+
variable "subscription" {
217+
description = "A mapping of fields for Prepaid ECS instances created. "
218+
type = map(string)
219+
default = {
220+
period = 1
221+
period_unit = "Month"
222+
auto_renew = false
223+
auto_renew_period = 1
224+
}
225+
}
226+
227+
variable "system_disk_category" {
228+
description = "The system disk category used to launch one or more worker ecs instances."
229+
type = string
230+
default = "cloud_efficiency"
231+
}
232+
233+
variable "system_disk_size" {
234+
description = "The system disk size used to launch one or more worker ecs instances."
235+
type = number
236+
default = 40
237+
}
238+
239+
variable "data_disks" {
240+
description = "Additional data disks to attach to the scaled ECS instance."
241+
type = list(map(string))
242+
default = []
243+
}
244+
245+
variable "disk_category" {
246+
description = "The system disk category used to launch one or more worker ecs instances."
247+
type = string
248+
default = "cloud_efficiency"
249+
}
250+
251+
variable "disk_size" {
252+
description = "The system disk size used to launch one or more worker ecs instances."
253+
type = number
254+
default = 40
255+
}

0 commit comments

Comments
 (0)