diff --git a/.gitignore b/.gitignore index 077902e..86aa4d2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # used for testing *.tfvars +**.hcl # Compiled files *.tfstate diff --git a/examples/complete/README.md b/examples/complete/README.md index 38ca351..e53bd60 100644 --- a/examples/complete/README.md +++ b/examples/complete/README.md @@ -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: @@ -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. diff --git a/examples/complete/main.tf b/examples/complete/main.tf index cc3ba6a..63e5d45 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -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" } \ No newline at end of file diff --git a/main.tf b/main.tf index 209b39a..ae30dbe 100644 --- a/main.tf +++ b/main.tf @@ -1,7 +1,8 @@ // 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 @@ -9,24 +10,61 @@ 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), @@ -34,9 +72,11 @@ resource "alicloud_vswitch" "vswitches" { } 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" { @@ -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 diff --git a/variables.tf b/variables.tf index 7dc855e..741cd8a 100644 --- a/variables.tf +++ b/variables.tf @@ -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" { @@ -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]." @@ -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 }))