Skip to content

Make module more robust with default parameter #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# used for testing
*.tfvars
**.hcl

# Compiled files
*.tfstate
Expand Down
32 changes: 31 additions & 1 deletion examples/complete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,35 @@ Terraform module for creating Kubernetes Cluster on Alibaba Cloud.
terraform-alicloud-kubernetes
=====================================================================

## Note

1. specifications in `master_instance_types` and `worker_instance_types` parameter
1. can't be sharable instance type (共享型实例).
2. if specify some instance type, check if it supports the `disk_category` parameter, which is `cloud_ssd`(SSD云盘) by default. Or you should set the `disk_catagory` parameter.
2. to specify region where VPC is created, use provider.

```hcl
# default provider configuration
provider "alicloud" {
public_key = "your_public_key"
private_key = "your_private_key"
project_id = "your_project_id"
region = "cn-beijing"
}

# new configuration
provider "alicloud" {
alias = "hz" # alias
region = "cn-hangzhou"
}

resource "alicloud_vpc" "default" {
provider = "alicloud.hz"
cidr_block = "172.16.0.0/12"
name = var.name
}
```

## Usage

To run this example you need to execute:
Expand All @@ -12,5 +41,6 @@ $ terraform plan
$ terraform apply
```

Note that this example will create resources which cost money. Run `terraform destroy` when you don't need these resources.
Note that this example will create resources which cost money. Run `terraform destroy` when you don't need these
resources.

20 changes: 4 additions & 16 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
variable "profile" {
default = "default"
}

variable "region" {
default = "cn-hangzhou"
}

data "alicloud_vpcs" "default" {
is_default = true
provider "alicloud" {
region = "cn-hangzhou"
}

module "k8s" {
source = "../.."
region = var.region

new_nat_gateway = false
vpc_id = data.alicloud_vpcs.default.vpcs.0.id
vswitch_ids = ["vsw-bp1pog8voc3f42arr****", "vsw-bp1jxetj1386gqssg****", "vsw-bp1s1835sq5tjss9s****"]
master_instance_types = ["ecs.n1.medium", "ecs.c5.large", "ecs.n1.medium"]
worker_instance_types = ["ecs.n1.medium"]
new_nat_gateway = true
k8s_pod_cidr = "192.168.5.0/24"
k8s_service_cidr = "192.168.2.0/24"
k8s_worker_number = 2
k8s_version = "1.24.6-aliyun.1"
}
71 changes: 56 additions & 15 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,42 +1,82 @@
// Instance_types data source for instance_type
data "alicloud_instance_types" "default" {
cpu_core_count = var.cpu_core_count
memory_size = var.memory_size
cpu_core_count = var.cpu_core_count
memory_size = var.memory_size
system_disk_category = var.disk_category
}

// Zones data source for availability_zone
data "alicloud_zones" "default" {
available_instance_type = data.alicloud_instance_types.default.instance_types[0].id
}

// Available types in the zone. This is a subset of alicloud_instance_types.default
data "alicloud_instance_types" "available" {
cpu_core_count = var.cpu_core_count
memory_size = var.memory_size
system_disk_category = var.disk_category
availability_zone = local.used_zone
}

locals {
# Find the zone which have most types

# {ecs.n1.large: [z1,z2,z3]}
type_zone_map = {
for type in data.alicloud_instance_types.default.instance_types : type.id => type.availability_zones
}

# {zone1: [e1,e2,e2]}
zone_type_map = transpose(local.type_zone_map)
# [{id: "zone1", count: 3},...]
zone_type_count = [
for zone, types in local.zone_type_map : tomap({ id : zone, count : length(types) })
]

sorted_values = distinct(sort(local.zone_type_count[*].count))

sorted_list = flatten(
[
for value in local.sorted_values :
[for elem in local.zone_type_count : elem if value == elem.count]
])

used_zone = local.sorted_list[length(local.sorted_list) - 1].id

# Filter the type, avoid burst type
available_instance_types = [for instance_type in data.alicloud_instance_types.available.instance_types : instance_type.id if instance_type.family!="ecs.t5" && instance_type.id!="ecs.t6"]
}

// If there is not specifying vpc_id, the module will launch a new vpc
resource "alicloud_vpc" "vpc" {
count = var.vpc_id == "" ? 1 : 0
cidr_block = var.vpc_cidr
name = var.vpc_name == "" ? var.example_name : var.vpc_name
vpc_name = var.vpc_name == "" ? var.example_name : var.vpc_name
}

// According to the vswitch cidr blocks to launch several vswitches
resource "alicloud_vswitch" "vswitches" {
count = length(var.vswitch_ids) > 0 ? 0 : length(var.vswitch_cidrs)
vpc_id = var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id
cidr_block = var.vswitch_cidrs[count.index]
availability_zone = data.alicloud_zones.default.zones[count.index % length(data.alicloud_zones.default.zones)]["id"]
name = var.vswitch_name_prefix == "" ? format(
count = length(var.vswitch_ids) > 0 ? 0 : length(var.vswitch_cidrs)
vpc_id = var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id
cidr_block = var.vswitch_cidrs[count.index]
zone_id = var.zone_id==""?local.used_zone : var.zone_id
vswitch_name = var.vswitch_name_prefix == "" ? format(
"%s-%s",
var.example_name,
format(var.number_format, count.index + 1),
) : format(
) : format(
"%s-%s",
var.vswitch_name_prefix,
format(var.number_format, count.index + 1),
)
}

resource "alicloud_nat_gateway" "default" {
count = var.new_nat_gateway == true ? 1 : 0
vpc_id = var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id
name = var.example_name
count = var.new_nat_gateway == true ? 1 : 0
vpc_id = var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id
name = var.example_name
nat_type = "Enhanced"
vswitch_id = alicloud_vswitch.vswitches[0].id
}

resource "alicloud_eip" "default" {
Expand Down Expand Up @@ -65,15 +105,16 @@ resource "alicloud_cs_kubernetes" "k8s" {
"%s-%s",
var.example_name,
format(var.number_format, count.index + 1),
) : format(
) : format(
"%s-%s",
var.k8s_name_prefix,
format(var.number_format, count.index + 1),
)
master_vswitch_ids = length(var.vswitch_ids) > 0 ? split(",", join(",", var.vswitch_ids)) : length(var.vswitch_cidrs) < 1 ? [] : split(",", join(",", alicloud_vswitch.vswitches.*.id))
worker_vswitch_ids = length(var.vswitch_ids) > 0 ? split(",", join(",", var.vswitch_ids)) : length(var.vswitch_cidrs) < 1 ? [] : split(",", join(",", alicloud_vswitch.vswitches.*.id))
master_instance_types = var.master_instance_types
worker_instance_types = var.worker_instance_types
master_instance_types = length(var.master_instance_types)!=0 ? var.master_instance_types : slice(local.available_instance_types, 0, 3)
worker_instance_types = length(var.worker_instance_types)!=0 ? var.worker_instance_types : slice(local.available_instance_types, 0, 3)
master_disk_category = var.disk_category
worker_number = var.k8s_worker_number
node_cidr_mask = var.node_cidr_mask
enable_ssh = var.enable_ssh
Expand Down
26 changes: 21 additions & 5 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,29 @@ variable "skip_region_validation" {
default = false
}

######################
# Zone
######################

variable "zone_id" {
description = "The Zone to launch the instance."
type = string
default = ""
}

######################
# Instance typs variables
######################
variable "cpu_core_count" {
description = "CPU core count is used to fetch instance types."
type = number
default = 1
default = 4
}

variable "memory_size" {
description = "Memory size used to fetch instance types."
type = number
default = 2
default = 8
}

variable "k8s_number" {
Expand Down Expand Up @@ -121,6 +131,12 @@ variable "worker_instance_types" {
default = []
}

variable "disk_category" {
description = "The disk category used to launch master and worker nodes. default 'cloud_ssd'"
type = string
default = "cloud_ssd"
}

variable "node_cidr_mask" {
type = number
description = "The node cidr block to specific how many pods can run on single node. Valid values: [24-28]."
Expand Down Expand Up @@ -177,14 +193,14 @@ variable "k8s_service_cidr" {
}

variable "k8s_version" {
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'."
description = "The version of the kubernetes version. Valid values: '1.24.6-aliyun.1','1.22.15-aliyun.1'. Default to '1.24.6-aliyun.1'."
type = string
default = "1.16.6-aliyun.1"
default = "1.24.6-aliyun.1"
}

variable "cluster_addons" {
description = "Addon components in kubernetes cluster"
type = list(object({
type = list(object({
name = string
config = string
}))
Expand Down