Skip to content

Commit 61a53a5

Browse files
committed
improve(kubernetes): updated module kubernetes and added example.
1 parent d5bb291 commit 61a53a5

File tree

6 files changed

+104
-144
lines changed

6 files changed

+104
-144
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ This example can specify the following arguments to create user-defined kubernte
2020
* alicloud_access_key: The Alicloud Access Key ID
2121
* alicloud_secret_key: The Alicloud Access Secret Key
2222
* region: The ID of region in which launching resources
23-
* k8s_name_prefix: The name prefix of kubernetes cluster
24-
* k8s_number: The number of kubernetes cluster
23+
* k8s_name: The name of kubernetes cluster
2524
* k8s_worker_number: The number of worker nodes in each kubernetes cluster
2625
* k8s_pod_cidr: The kubernetes pod cidr block. It cannot be equals to vpc's or vswitch's and cannot be in them. If vpc's cidr block is `172.16.XX.XX/XX`,
2726
it had better to `192.168.XX.XX/XX` or `10.XX.XX.XX/XX`
@@ -49,14 +48,13 @@ Conditional creation
4948
--------------------
5049
This example can support the following creating kubernetes cluster scenario by setting different arguments.
5150

52-
### 1. Create a new vpc, vswitches and nat gateway for the cluster.
51+
### 1. Retrieve existing vswitch by tags, name regex and resource group id.
5352

5453
You can specify the following user-defined arguments:
5554

56-
* vpc_name: A new vpc name
57-
* vpc_cidr: A new vpc cidr block
58-
* vswitch_name_prefix: The name prefix of several vswitches
59-
* vswitch_cidrs: List of cidr blocks for several new vswitches
55+
* vswitch_name_regex: A default filter applied to retrieve existing vswitches by name regex.
56+
* vswitch_tags: A default filter applied to retrieve existing vswitches by tags.
57+
* vswitch_resource_group_id: A default filter applied to retrieve existing vswitches by resource group id.
6058

6159
### 2. Using existing vpc and vswitches for the cluster.
6260

@@ -77,7 +75,7 @@ In other words, you must set snat entry for each vswitch before running the exam
7775

7876
Terraform version
7977
-----------------
80-
Terraform version 0.11.0 or newer and Provider version 1.9.0 or newer are required for this example to work.
78+
Terraform version 0.12.0 or newer and Provider version 1.60.0 or newer are required for this example to work.
8179

8280
Authors
8381
-------

examples/basic/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module "kubernetes" {
2+
source = "../../"
3+
k8s_name = "CreateByTerraform"
4+
}

locals.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
locals {
2+
vswitch_name_regex = var.vswitch_name_regex != "" ? var.vswitch_name_regex : var.filter_with_name_regex
3+
vswitch_tags = length(var.vswitch_tags) > 0 ? var.vswitch_tags : var.filter_with_tags
4+
vswitch_resource_group_id = var.vswitch_resource_group_id != "" ? var.vswitch_resource_group_id : var.filter_with_resource_group_id
5+
vswitch_ids = length(var.vswitch_ids) > 0 ? var.vswitch_ids : local.vswitch_name_regex != "" || length(local.vswitch_tags) > 0 || local.vswitch_resource_group_id !="" ? data.alicloud_vswitches.this.ids : []
6+
master_instance_types = length(var.master_instance_types) > 0 ? var.master_instance_types : [data.alicloud_instance_types.this.instance_types[0].id]
7+
worker_instance_types = length(var.worker_instance_types) > 0 ? var.worker_instance_types : [data.alicloud_instance_types.this.instance_types[0].id]
8+
zone_id = data.alicloud_vswitches.this.vswitches.0.zone_id
9+
}
10+
11+
12+
// Instance_types data source for instance_type
13+
data "alicloud_instance_types" "this" {
14+
availability_zone = local.zone_id
15+
cpu_core_count = var.cpu_core_count
16+
memory_size = var.memory_size
17+
}
18+
19+
data "alicloud_vswitches" "this" {
20+
name_regex = local.vswitch_name_regex
21+
tags = local.vswitch_tags
22+
resource_group_id = local.vswitch_resource_group_id
23+
}

main.tf

Lines changed: 14 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,30 @@
11
// Provider specific configs
22
provider "alicloud" {
3-
version = ">=1.56.0"
4-
region = var.region != "" ? var.region : null
5-
configuration_source = "terraform-alicloud-modules/kubernetes"
6-
}
7-
8-
// Instance_types data source for instance_type
9-
data "alicloud_instance_types" "default" {
10-
cpu_core_count = var.cpu_core_count
11-
memory_size = var.memory_size
12-
}
13-
14-
// Zones data source for availability_zone
15-
data "alicloud_zones" "default" {
16-
available_instance_type = data.alicloud_instance_types.default.instance_types[0].id
17-
}
18-
19-
// If there is not specifying vpc_id, the module will launch a new vpc
20-
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
24-
}
25-
26-
// According to the vswitch cidr blocks to launch several vswitches
27-
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 = 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-
)
41-
}
42-
43-
resource "alicloud_nat_gateway" "default" {
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
47-
}
48-
49-
resource "alicloud_eip" "default" {
50-
count = var.new_nat_gateway == "true" ? 1 : 0
51-
bandwidth = 10
52-
}
53-
54-
resource "alicloud_eip_association" "default" {
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
58-
}
59-
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
3+
version = ">=1.60.0"
4+
profile = var.profile != "" ? var.profile : null
5+
shared_credentials_file = var.shared_credentials_file != "" ? var.shared_credentials_file : null
6+
region = var.region != "" ? var.region : null
7+
skip_region_validation = var.skip_region_validation
8+
configuration_source = "terraform-alicloud-modules/kubernetes"
659
}
6610

6711
resource "alicloud_cs_kubernetes" "k8s" {
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-
vswitch_ids = [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)))]]
79-
80-
new_nat_gateway = false
12+
availability_zone = local.zone_id
13+
name = var.k8s_name
14+
vswitch_ids = local.vswitch_ids
15+
new_nat_gateway = var.new_nat_gateway
8116
master_disk_category = var.master_disk_category
8217
worker_disk_category = var.worker_disk_category
8318
master_disk_size = var.master_disk_size
84-
worker_disk_size = var.master_disk_size
19+
worker_disk_size = var.worker_disk_size
8520
password = var.ecs_password
8621
pod_cidr = var.k8s_pod_cidr
8722
service_cidr = var.k8s_service_cidr
8823
enable_ssh = true
8924
install_cloud_monitor = true
9025

91-
depends_on = [alicloud_snat_entry.default]
92-
master_instance_types = var.master_instance_types
93-
worker_instance_types = var.worker_instance_types
94-
worker_numbers = var.k8s_worker_numbers
26+
master_instance_types = local.master_instance_types
27+
worker_instance_types = local.worker_instance_types
28+
worker_numbers = var.k8s_worker_numbers
9529
}
9630

outputs.tf

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

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

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

1616
// Output kubernetes resource
17-
output "cluster_id" {
17+
output "this_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

22-
output "security_group_id" {
22+
output "this_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.security_group_id
2525
}
2626

27-
output "cluster_nodes" {
27+
output "this_cluster_nodes" {
2828
description = "List nodes of cluster."
29-
value = alicloud_cs_kubernetes.k8s.*.worker_nodes
29+
value = alicloud_cs_kubernetes.k8s.worker_nodes
3030
}
3131

variables.tf

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,65 @@
33

44
variable "region" {
55
description = "The region used to launch this module resources."
6-
default = "cn-beijing"
6+
default = ""
77
}
88

9-
variable "availability_zone" {
10-
description = "The available zone to launch ecs instance and other resources."
9+
variable "profile" {
10+
description = "The profile name as set in the shared credentials file. If not set, it will be sourced from the ALICLOUD_PROFILE environment variable."
11+
default = ""
12+
}
13+
variable "shared_credentials_file" {
14+
description = "This is the path to the shared credentials file. If this is not set and a profile is specified, $HOME/.aliyun/config.json will be used."
1115
default = ""
1216
}
1317

14-
variable "number_format" {
15-
description = "The number format used to output."
16-
default = "%02d"
18+
variable "skip_region_validation" {
19+
description = "Skip static validation of region ID. Used by users of alternative AlibabaCloud-like APIs or users w/ access to regions that are not public (yet)."
20+
default = false
1721
}
1822

19-
variable "example_name" {
20-
default = "tf-example-kubernetes"
23+
variable "filter_with_name_regex" {
24+
description = "A default filter applied to retrieve existing vswitches, nat gateway, eip, snat entry and kubernetes clusters by name regex."
25+
default = ""
2126
}
2227

23-
# Instance typs variables
24-
variable "cpu_core_count" {
25-
description = "CPU core count is used to fetch instance types."
26-
default = 1
28+
variable "filter_with_tags" {
29+
description = "A default filter applied to retrieve existing vswitches, nat gateway, eip, snat entry and kubernetes clusters by tags."
30+
type = map(string)
31+
default = {}
2732
}
2833

29-
variable "memory_size" {
30-
description = "Memory size used to fetch instance types."
34+
variable "filter_with_resource_group_id" {
35+
description = "A default filter applied to retrieve existing vswitches, nat gateway, eip, snat entry and kubernetes clusters by resource group id."
36+
default = ""
37+
}
38+
39+
# Instancetypes variables
40+
variable "cpu_core_count" {
41+
description = "CPU core count is used to fetch instancetypes."
3142
default = 2
3243
}
3344

34-
# VPC variables
35-
variable "vpc_name" {
36-
description = "The vpc name used to create a new vpc when 'vpc_id' is not specified. Default to variable `example_name`"
37-
default = ""
45+
variable "memory_size" {
46+
description = "Memory size used to fetch instancetypes."
47+
default = 4
3848
}
3949

40-
variable "vpc_id" {
41-
description = "A existing vpc id used to create several vswitches and other resources."
50+
# VSwitch variables
51+
52+
variable "vswitch_name_regex" {
53+
description = "A default filter applied to retrieve existing vswitches by name regex. If not set, `filter_with_name_regex` will be used."
4254
default = ""
4355
}
4456

45-
variable "vpc_cidr" {
46-
description = "The cidr block used to launch a new vpc when 'vpc_id' is not specified."
47-
default = "10.1.0.0/21"
57+
variable "vswitch_tags" {
58+
description = "A default filter applied to retrieve existing vswitches by tags. If not set, `filter_with_tags` will be used."
59+
type = map(string)
60+
default = {}
4861
}
4962

50-
# VSwitch variables
51-
variable "vswitch_name_prefix" {
52-
description = "The vswitch name prefix used to create several new vswitches. Default to variable `example_name`"
63+
variable "vswitch_resource_group_id" {
64+
description = "A default filter applied to retrieve existing vswitches by resource group id. If not set, `filter_with_resource_group_id` will be used."
5365
default = ""
5466
}
5567

@@ -59,12 +71,6 @@ variable "vswitch_ids" {
5971
default = []
6072
}
6173

62-
variable "vswitch_cidrs" {
63-
description = "List of cidr blocks used to create several new vswitches when 'vswitch_ids' is not specified."
64-
type = list(string)
65-
default = ["10.1.2.0/24"]
66-
}
67-
6874
variable "new_nat_gateway" {
6975
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."
7076
default = "true"
@@ -73,15 +79,15 @@ variable "new_nat_gateway" {
7379
# Cluster nodes variables
7480

7581
variable "master_instance_types" {
76-
description = "The ecs instance type used to launch master nodes. Default from instance typs datasource."
82+
description = "The ecs instance type used to launch master nodes. Default from instance types datasource."
7783
type = list(string)
78-
default = ["ecs.n4.xlarge"]
84+
default = []
7985
}
8086

8187
variable "worker_instance_types" {
82-
description = "The ecs instance type used to launch worker nodes. Default from instance typs datasource."
88+
description = "The ecs instance type used to launch worker nodes. Default from instance types datasource."
8389
type = list(string)
84-
default = ["ecs.n4.xlarge"]
90+
default = []
8591
}
8692

8793
variable "master_disk_category" {
@@ -109,20 +115,15 @@ variable "ecs_password" {
109115
default = "Abc12345"
110116
}
111117

112-
variable "k8s_number" {
113-
description = "The number of kubernetes cluster."
114-
default = 1
115-
}
116-
117118
variable "k8s_worker_numbers" {
118119
description = "The number of worker nodes in each kubernetes cluster."
119120
type = list(number)
120121
default = [3]
121122
}
122123

123-
variable "k8s_name_prefix" {
124-
description = "The name prefix used to create several kubernetes clusters. Default to variable `example_name`"
125-
default = ""
124+
variable "k8s_name" {
125+
description = "The name used to create kubernetes cluster."
126+
default = "tf-example-kubernetes"
126127
}
127128

128129
variable "k8s_pod_cidr" {

0 commit comments

Comments
 (0)