Skip to content

Commit ceba86a

Browse files
lexwongxiaozhu36
authored andcommitted
imporve(kubernetes): update the module to the format of the new version.
1 parent 24cf9cf commit ceba86a

File tree

3 files changed

+108
-86
lines changed

3 files changed

+108
-86
lines changed

main.tf

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,94 @@
11
// Provider specific configs
22
provider "alicloud" {
3-
access_key = "${var.alicloud_access_key}"
4-
secret_key = "${var.alicloud_secret_key}"
5-
region = "${var.region}"
3+
version = ">=1.56.0"
4+
region = var.region != "" ? var.region : null
5+
configuration_source = "terraform-alicloud-modules/kubernetes"
66
}
77

88
// Instance_types data source for instance_type
99
data "alicloud_instance_types" "default" {
10-
cpu_core_count = "${var.cpu_core_count}"
11-
memory_size = "${var.memory_size}"
10+
cpu_core_count = var.cpu_core_count
11+
memory_size = var.memory_size
1212
}
1313

1414
// Zones data source for availability_zone
1515
data "alicloud_zones" "default" {
16-
available_instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}"
16+
available_instance_type = data.alicloud_instance_types.default.instance_types[0].id
1717
}
1818

1919
// If there is not specifying vpc_id, the module will launch a new vpc
2020
resource "alicloud_vpc" "vpc" {
21-
count = "${var.vpc_id == "" ? 1 : 0}"
22-
cidr_block = "${var.vpc_cidr}"
23-
name = "${var.vpc_name == "" ? var.example_name : var.vpc_name}"
21+
count = var.vpc_id == "" ? 1 : 0
22+
cidr_block = var.vpc_cidr
23+
name = var.vpc_name == "" ? var.example_name : var.vpc_name
2424
}
2525

2626
// According to the vswitch cidr blocks to launch several vswitches
2727
resource "alicloud_vswitch" "vswitches" {
28-
count = "${length(var.vswitch_ids) > 0 ? 0 : length(var.vswitch_cidrs)}"
29-
vpc_id = "${var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id}"
30-
cidr_block = "${element(var.vswitch_cidrs, count.index)}"
31-
availability_zone = "${lookup(data.alicloud_zones.default.zones[count.index%length(data.alicloud_zones.default.zones)], "id")}"
32-
name = "${var.vswitch_name_prefix == "" ? format("%s-%s", var.example_name, format(var.number_format, count.index+1)) : format("%s-%s", var.vswitch_name_prefix, format(var.number_format, count.index+1))}"
28+
count = length(var.vswitch_ids) > 0 ? 0 : length(var.vswitch_cidrs)
29+
vpc_id = var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id
30+
cidr_block = var.vswitch_cidrs[count.index]
31+
availability_zone = data.alicloud_zones.default.zones[count.index % length(data.alicloud_zones.default.zones)]["id"]
32+
name = var.vswitch_name_prefix == "" ? format(
33+
"%s-%s",
34+
var.example_name,
35+
format(var.number_format, count.index + 1),
36+
) : format(
37+
"%s-%s",
38+
var.vswitch_name_prefix,
39+
format(var.number_format, count.index + 1),
40+
)
3341
}
3442

3543
resource "alicloud_nat_gateway" "default" {
36-
count = "${var.new_nat_gateway == true ? 1 : 0}"
37-
vpc_id = "${var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id}"
38-
name = "${var.example_name}"
44+
count = var.new_nat_gateway == true ? 1 : 0
45+
vpc_id = var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id
46+
name = var.example_name
3947
}
4048

4149
resource "alicloud_eip" "default" {
42-
count = "${var.new_nat_gateway == "true" ? 1 : 0}"
50+
count = var.new_nat_gateway == "true" ? 1 : 0
4351
bandwidth = 10
4452
}
4553

4654
resource "alicloud_eip_association" "default" {
47-
count = "${var.new_nat_gateway == "true" ? 1 : 0}"
48-
allocation_id = "${alicloud_eip.default.id}"
49-
instance_id = "${alicloud_nat_gateway.default.id}"
55+
count = var.new_nat_gateway == "true" ? 1 : 0
56+
allocation_id = alicloud_eip.default[0].id
57+
instance_id = alicloud_nat_gateway.default[0].id
5058
}
5159

52-
resource "alicloud_snat_entry" "default"{
53-
count = "${var.new_nat_gateway == "false" ? 0 : length(var.vswitch_ids) > 0 ? length(var.vswitch_ids) : length(var.vswitch_cidrs)}"
54-
snat_table_id = "${alicloud_nat_gateway.default.snat_table_ids}"
55-
source_vswitch_id = "${length(var.vswitch_ids) > 0 ? element(split(",", join(",", var.vswitch_ids)), count.index%length(split(",", join(",", var.vswitch_ids)))) : length(var.vswitch_cidrs) < 1 ? "" : element(split(",", join(",", alicloud_vswitch.vswitches.*.id)), count.index%length(split(",", join(",", alicloud_vswitch.vswitches.*.id))))}"
56-
snat_ip = "${alicloud_eip.default.ip_address}"
60+
resource "alicloud_snat_entry" "default" {
61+
count = var.new_nat_gateway == "false" ? 0 : length(var.vswitch_ids) > 0 ? length(var.vswitch_ids) : length(var.vswitch_cidrs)
62+
snat_table_id = alicloud_nat_gateway.default[0].snat_table_ids
63+
source_vswitch_id = length(var.vswitch_ids) > 0 ? split(",", join(",", var.vswitch_ids))[count.index % length(split(",", join(",", var.vswitch_ids)))] : length(var.vswitch_cidrs) < 1 ? "" : split(",", join(",", alicloud_vswitch.vswitches.*.id))[count.index % length(split(",", join(",", alicloud_vswitch.vswitches.*.id)))]
64+
snat_ip = alicloud_eip.default[0].ip_address
5765
}
5866

5967
resource "alicloud_cs_kubernetes" "k8s" {
60-
count = "${var.k8s_number}"
61-
name = "${var.k8s_name_prefix == "" ? format("%s-%s", var.example_name, format(var.number_format, count.index+1)) : format("%s-%s", var.k8s_name_prefix, format(var.number_format, count.index+1))}"
62-
vswitch_id = "${length(var.vswitch_ids) > 0 ? element(split(",", join(",", var.vswitch_ids)), count.index%length(split(",", join(",", var.vswitch_ids)))) : length(var.vswitch_cidrs) < 1 ? "" : element(split(",", join(",", alicloud_vswitch.vswitches.*.id)), count.index%length(split(",", join(",", alicloud_vswitch.vswitches.*.id))))}"
63-
new_nat_gateway = false
64-
master_instance_type = "${var.master_instance_type == "" ? data.alicloud_instance_types.default.instance_types.0.id : var.master_instance_type}"
65-
worker_instance_type = "${var.worker_instance_type == "" ? data.alicloud_instance_types.default.instance_types.0.id : var.worker_instance_type}"
66-
worker_number = "${var.k8s_worker_number}"
67-
master_disk_category = "${var.master_disk_category}"
68-
worker_disk_category = "${var.worker_disk_category}"
69-
master_disk_size = "${var.master_disk_size}"
70-
worker_disk_size = "${var.master_disk_size}"
71-
password = "${var.ecs_password}"
72-
pod_cidr = "${var.k8s_pod_cidr}"
73-
service_cidr = "${var.k8s_service_cidr}"
74-
enable_ssh = true
68+
count = var.k8s_number
69+
name = var.k8s_name_prefix == "" ? format(
70+
"%s-%s",
71+
var.example_name,
72+
format(var.number_format, count.index + 1),
73+
) : format(
74+
"%s-%s",
75+
var.k8s_name_prefix,
76+
format(var.number_format, count.index + 1),
77+
)
78+
new_nat_gateway = false
79+
master_disk_category = var.master_disk_category
80+
worker_disk_category = var.worker_disk_category
81+
master_disk_size = var.master_disk_size
82+
worker_disk_size = var.master_disk_size
83+
password = var.ecs_password
84+
pod_cidr = var.k8s_pod_cidr
85+
service_cidr = var.k8s_service_cidr
86+
enable_ssh = true
7587
install_cloud_monitor = true
7688

77-
depends_on = ["alicloud_snat_entry.default"]
89+
depends_on = [alicloud_snat_entry.default]
90+
master_instance_types = var.master_instance_types
91+
worker_instance_types = var.worker_instance_types
92+
worker_numbers = var.k8s_worker_numbers
7893
}
94+

outputs.tf

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
// Output VPC
22
output "vpc_id" {
33
description = "The ID of the VPC."
4-
value = "${alicloud_cs_kubernetes.k8s.0.vpc_id}"
4+
value = alicloud_cs_kubernetes.k8s[0].vpc_id
55
}
66

77
output "vswitch_ids" {
88
description = "List ID of the VSwitches."
9-
value = ["${alicloud_cs_kubernetes.k8s.*.vswitch_id}"]
9+
value = [alicloud_cs_kubernetes.k8s.*.vswitch_id]
1010
}
1111

1212
output "nat_gateway_id" {
13-
value = "${alicloud_cs_kubernetes.k8s.0.nat_gateway_id}"
13+
value = alicloud_cs_kubernetes.k8s[0].nat_gateway_id
1414
}
1515

1616
// Output kubernetes resource
1717
output "cluster_id" {
1818
description = "ID of the kunernetes cluster."
19-
value = ["${alicloud_cs_kubernetes.k8s.*.id}"]
19+
value = alicloud_cs_kubernetes.k8s.*.id
2020
}
2121

2222
output "security_group_id" {
2323
description = "ID of the Security Group used to deploy kubernetes cluster."
24-
value = "${alicloud_cs_kubernetes.k8s.0.security_group_id}"
24+
value = alicloud_cs_kubernetes.k8s[0].security_group_id
2525
}
2626

2727
output "cluster_nodes" {
2828
description = "List nodes of cluster."
29-
value = ["${alicloud_cs_kubernetes.k8s.*.nodes}"]
30-
}
29+
value = alicloud_cs_kubernetes.k8s.*.nodes
30+
}
31+

variables.tf

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,137 @@
11
# common variables
2-
variable "alicloud_access_key" {
3-
description = "The Alicloud Access Key ID to launch resources."
4-
}
5-
variable "alicloud_secret_key" {
6-
description = "The Alicloud Access Secret Key to launch resources."
7-
}
2+
3+
84
variable "region" {
9-
description = "The region to launch resources."
10-
default = "cn-hangzhou"
5+
description = "The region used to launch this module resources."
6+
default = ""
117
}
8+
129
variable "availability_zone" {
1310
description = "The available zone to launch ecs instance and other resources."
14-
default = ""
11+
default = ""
1512
}
13+
1614
variable "number_format" {
1715
description = "The number format used to output."
18-
default = "%02d"
16+
default = "%02d"
1917
}
18+
2019
variable "example_name" {
2120
default = "tf-example-kubernetes"
2221
}
2322

2423
# Instance typs variables
2524
variable "cpu_core_count" {
2625
description = "CPU core count is used to fetch instance types."
27-
default = 1
26+
default = 1
2827
}
2928

3029
variable "memory_size" {
3130
description = "Memory size used to fetch instance types."
32-
default = 2
31+
default = 2
3332
}
3433

3534
# VPC variables
3635
variable "vpc_name" {
3736
description = "The vpc name used to create a new vpc when 'vpc_id' is not specified. Default to variable `example_name`"
38-
default = ""
37+
default = ""
3938
}
39+
4040
variable "vpc_id" {
4141
description = "A existing vpc id used to create several vswitches and other resources."
42-
default = ""
42+
default = ""
4343
}
44+
4445
variable "vpc_cidr" {
4546
description = "The cidr block used to launch a new vpc when 'vpc_id' is not specified."
46-
default = "10.1.0.0/21"
47+
default = "10.1.0.0/21"
4748
}
4849

4950
# VSwitch variables
5051
variable "vswitch_name_prefix" {
5152
description = "The vswitch name prefix used to create several new vswitches. Default to variable `example_name`"
52-
default = ""
53+
default = ""
5354
}
5455

5556
variable "vswitch_ids" {
5657
description = "List of existing vswitch id."
57-
type = "list"
58-
default = []
58+
type = list(string)
59+
default = []
5960
}
6061

6162
variable "vswitch_cidrs" {
6263
description = "List of cidr blocks used to create several new vswitches when 'vswitch_ids' is not specified."
63-
type = "list"
64-
default = ["10.1.2.0/24"]
64+
type = list(string)
65+
default = ["10.1.2.0/24"]
6566
}
6667

6768
variable "new_nat_gateway" {
6869
description = "Whether to create a new nat gateway. In this template, a new nat gateway will create a nat gateway, eip and server snat entries."
69-
default = "true"
70+
default = "true"
7071
}
7172

7273
# Cluster nodes variables
7374

74-
variable "master_instance_type" {
75+
variable "master_instance_types" {
7576
description = "The ecs instance type used to launch master nodes. Default from instance typs datasource."
76-
default = ""
77+
type = list(string)
78+
default = [""]
7779
}
7880

79-
variable "worker_instance_type" {
81+
variable "worker_instance_types" {
8082
description = "The ecs instance type used to launch worker nodes. Default from instance typs datasource."
81-
default = ""
83+
type = list(string)
84+
default = [""]
8285
}
8386

8487
variable "master_disk_category" {
8588
description = "The system disk category used to launch one or more master nodes."
86-
default = "cloud_efficiency"
89+
default = "cloud_efficiency"
8790
}
8891

8992
variable "worker_disk_category" {
9093
description = "The system disk category used to launch one or more worker nodes."
91-
default = "cloud_efficiency"
94+
default = "cloud_efficiency"
9295
}
9396

9497
variable "master_disk_size" {
9598
description = "The system disk size used to launch one or more master nodes."
96-
default = "40"
99+
default = "40"
97100
}
98101

99102
variable "worker_disk_size" {
100103
description = "The system disk size used to launch one or more worker nodes."
101-
default = "40"
104+
default = "40"
102105
}
103106

104107
variable "ecs_password" {
105108
description = "The password of instance."
106-
default = "Abc12345"
109+
default = "Abc12345"
107110
}
108111

109112
variable "k8s_number" {
110113
description = "The number of kubernetes cluster."
111-
default = 1
114+
default = 1
112115
}
113116

114-
variable "k8s_worker_number" {
117+
variable "k8s_worker_numbers" {
115118
description = "The number of worker nodes in each kubernetes cluster."
116-
default = 3
119+
type = list(number)
120+
default = [3]
117121
}
118122

119123
variable "k8s_name_prefix" {
120124
description = "The name prefix used to create several kubernetes clusters. Default to variable `example_name`"
121-
default = ""
125+
default = ""
122126
}
123127

124128
variable "k8s_pod_cidr" {
125129
description = "The kubernetes pod cidr block. It cannot be equals to vpc's or vswitch's and cannot be in them."
126-
default = "172.20.0.0/16"
130+
default = "172.20.0.0/16"
127131
}
128132

129133
variable "k8s_service_cidr" {
130134
description = "The kubernetes service cidr block. It cannot be equals to vpc's or vswitch's or pod's and cannot be in them."
131-
default = "172.21.0.0/20"
132-
}
135+
default = "172.21.0.0/20"
136+
}
137+

0 commit comments

Comments
 (0)