Skip to content

Commit 99c2372

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

File tree

4 files changed

+77
-68
lines changed

4 files changed

+77
-68
lines changed

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+
example_name = "CreateByTerraform"
4+
}

locals.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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_ids = length(var.vswitch_ids) > 0 ? var.vswitch_ids : local.vswitch_name_regex != "" || length(local.vswitch_tags) > 0 ? data.alicloud_vswitches.this.ids : []
5+
zone_id = length(var.vswitch_ids) > 0 ? data.alicloud_vswitches.this.vswitches.0.zone_id : data.alicloud_zones.this.ids.0
6+
}
7+
8+
9+
// Instance_types data source for instance_type
10+
data "alicloud_instance_types" "this" {
11+
availability_zone = local.zone_id
12+
cpu_core_count = var.cpu_core_count
13+
memory_size = var.memory_size
14+
}
15+
16+
// Zones data source for availability_zone
17+
data "alicloud_zones" "this" {
18+
available_resource_creation = "VSwitch"
19+
}
20+
21+
data "alicloud_vswitches" "this" {
22+
ids = var.vswitch_ids
23+
name_regex = local.vswitch_name_regex
24+
tags = local.vswitch_tags
25+
}

main.tf

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,16 @@
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.56.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
12+
availability_zone = var.availability_zone == "" ? data.alicloud_zones.this.zones[0].id : var.availability_zone
13+
count = var.k8s_number
6914
name = var.k8s_name_prefix == "" ? format(
7015
"%s-%s",
7116
var.example_name,
@@ -75,9 +20,8 @@ resource "alicloud_cs_kubernetes" "k8s" {
7520
var.k8s_name_prefix,
7621
format(var.number_format, count.index + 1),
7722
)
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
23+
vswitch_ids = local.vswitch_ids
24+
new_nat_gateway = var.new_nat_gateway
8125
master_disk_category = var.master_disk_category
8226
worker_disk_category = var.worker_disk_category
8327
master_disk_size = var.master_disk_size
@@ -88,9 +32,8 @@ resource "alicloud_cs_kubernetes" "k8s" {
8832
enable_ssh = true
8933
install_cloud_monitor = true
9034

91-
depends_on = [alicloud_snat_entry.default]
9235
master_instance_types = var.master_instance_types
9336
worker_instance_types = var.worker_instance_types
94-
worker_numbers = var.k8s_worker_numbers
37+
worker_numbers = var.k8s_worker_numbers
9538
}
9639

variables.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@ variable "region" {
66
default = "cn-beijing"
77
}
88

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."
15+
default = ""
16+
}
17+
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
21+
}
22+
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 = ""
26+
}
27+
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 = {}
32+
}
33+
934
variable "availability_zone" {
1035
description = "The available zone to launch ecs instance and other resources."
1136
default = ""
@@ -48,6 +73,18 @@ variable "vpc_cidr" {
4873
}
4974

5075
# VSwitch variables
76+
77+
variable "vswitch_name_regex" {
78+
description = "A default filter applied to retrieve existing vswitches by name regex. If not set, `filter_with_name_regex` will be used."
79+
default = ""
80+
}
81+
82+
variable "vswitch_tags" {
83+
description = "A default filter applied to retrieve existing vswitches by tags. If not set, `filter_with_tags` will be used."
84+
type = map(string)
85+
default = {}
86+
}
87+
5188
variable "vswitch_name_prefix" {
5289
description = "The vswitch name prefix used to create several new vswitches. Default to variable `example_name`"
5390
default = ""

0 commit comments

Comments
 (0)