Skip to content

Commit

Permalink
imporve(kubernetes): update the module to the format of the new version.
Browse files Browse the repository at this point in the history
  • Loading branch information
lexwong authored and xiaozhu36 committed Sep 24, 2019
1 parent 24cf9cf commit ceba86a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 86 deletions.
100 changes: 58 additions & 42 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,78 +1,94 @@
// Provider specific configs
provider "alicloud" {
access_key = "${var.alicloud_access_key}"
secret_key = "${var.alicloud_secret_key}"
region = "${var.region}"
version = ">=1.56.0"
region = var.region != "" ? var.region : null
configuration_source = "terraform-alicloud-modules/kubernetes"
}

// 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
}

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

// 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}"
count = var.vpc_id == "" ? 1 : 0
cidr_block = var.vpc_cidr
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 = "${element(var.vswitch_cidrs, count.index)}"
availability_zone = "${lookup(data.alicloud_zones.default.zones[count.index%length(data.alicloud_zones.default.zones)], "id")}"
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))}"
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(
"%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),
)
}

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
}

resource "alicloud_eip" "default" {
count = "${var.new_nat_gateway == "true" ? 1 : 0}"
count = var.new_nat_gateway == "true" ? 1 : 0
bandwidth = 10
}

resource "alicloud_eip_association" "default" {
count = "${var.new_nat_gateway == "true" ? 1 : 0}"
allocation_id = "${alicloud_eip.default.id}"
instance_id = "${alicloud_nat_gateway.default.id}"
count = var.new_nat_gateway == "true" ? 1 : 0
allocation_id = alicloud_eip.default[0].id
instance_id = alicloud_nat_gateway.default[0].id
}

resource "alicloud_snat_entry" "default"{
count = "${var.new_nat_gateway == "false" ? 0 : length(var.vswitch_ids) > 0 ? length(var.vswitch_ids) : length(var.vswitch_cidrs)}"
snat_table_id = "${alicloud_nat_gateway.default.snat_table_ids}"
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))))}"
snat_ip = "${alicloud_eip.default.ip_address}"
resource "alicloud_snat_entry" "default" {
count = var.new_nat_gateway == "false" ? 0 : length(var.vswitch_ids) > 0 ? length(var.vswitch_ids) : length(var.vswitch_cidrs)
snat_table_id = alicloud_nat_gateway.default[0].snat_table_ids
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)))]
snat_ip = alicloud_eip.default[0].ip_address
}

resource "alicloud_cs_kubernetes" "k8s" {
count = "${var.k8s_number}"
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))}"
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))))}"
new_nat_gateway = false
master_instance_type = "${var.master_instance_type == "" ? data.alicloud_instance_types.default.instance_types.0.id : var.master_instance_type}"
worker_instance_type = "${var.worker_instance_type == "" ? data.alicloud_instance_types.default.instance_types.0.id : var.worker_instance_type}"
worker_number = "${var.k8s_worker_number}"
master_disk_category = "${var.master_disk_category}"
worker_disk_category = "${var.worker_disk_category}"
master_disk_size = "${var.master_disk_size}"
worker_disk_size = "${var.master_disk_size}"
password = "${var.ecs_password}"
pod_cidr = "${var.k8s_pod_cidr}"
service_cidr = "${var.k8s_service_cidr}"
enable_ssh = true
count = var.k8s_number
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),
)
new_nat_gateway = false
master_disk_category = var.master_disk_category
worker_disk_category = var.worker_disk_category
master_disk_size = var.master_disk_size
worker_disk_size = var.master_disk_size
password = var.ecs_password
pod_cidr = var.k8s_pod_cidr
service_cidr = var.k8s_service_cidr
enable_ssh = true
install_cloud_monitor = true

depends_on = ["alicloud_snat_entry.default"]
depends_on = [alicloud_snat_entry.default]
master_instance_types = var.master_instance_types
worker_instance_types = var.worker_instance_types
worker_numbers = var.k8s_worker_numbers
}

15 changes: 8 additions & 7 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
// Output VPC
output "vpc_id" {
description = "The ID of the VPC."
value = "${alicloud_cs_kubernetes.k8s.0.vpc_id}"
value = alicloud_cs_kubernetes.k8s[0].vpc_id
}

output "vswitch_ids" {
description = "List ID of the VSwitches."
value = ["${alicloud_cs_kubernetes.k8s.*.vswitch_id}"]
value = [alicloud_cs_kubernetes.k8s.*.vswitch_id]
}

output "nat_gateway_id" {
value = "${alicloud_cs_kubernetes.k8s.0.nat_gateway_id}"
value = alicloud_cs_kubernetes.k8s[0].nat_gateway_id
}

// Output kubernetes resource
output "cluster_id" {
description = "ID of the kunernetes cluster."
value = ["${alicloud_cs_kubernetes.k8s.*.id}"]
value = alicloud_cs_kubernetes.k8s.*.id
}

output "security_group_id" {
description = "ID of the Security Group used to deploy kubernetes cluster."
value = "${alicloud_cs_kubernetes.k8s.0.security_group_id}"
value = alicloud_cs_kubernetes.k8s[0].security_group_id
}

output "cluster_nodes" {
description = "List nodes of cluster."
value = ["${alicloud_cs_kubernetes.k8s.*.nodes}"]
}
value = alicloud_cs_kubernetes.k8s.*.nodes
}

79 changes: 42 additions & 37 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,132 +1,137 @@
# common variables
variable "alicloud_access_key" {
description = "The Alicloud Access Key ID to launch resources."
}
variable "alicloud_secret_key" {
description = "The Alicloud Access Secret Key to launch resources."
}


variable "region" {
description = "The region to launch resources."
default = "cn-hangzhou"
description = "The region used to launch this module resources."
default = ""
}

variable "availability_zone" {
description = "The available zone to launch ecs instance and other resources."
default = ""
default = ""
}

variable "number_format" {
description = "The number format used to output."
default = "%02d"
default = "%02d"
}

variable "example_name" {
default = "tf-example-kubernetes"
}

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

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

# VPC variables
variable "vpc_name" {
description = "The vpc name used to create a new vpc when 'vpc_id' is not specified. Default to variable `example_name`"
default = ""
default = ""
}

variable "vpc_id" {
description = "A existing vpc id used to create several vswitches and other resources."
default = ""
default = ""
}

variable "vpc_cidr" {
description = "The cidr block used to launch a new vpc when 'vpc_id' is not specified."
default = "10.1.0.0/21"
default = "10.1.0.0/21"
}

# VSwitch variables
variable "vswitch_name_prefix" {
description = "The vswitch name prefix used to create several new vswitches. Default to variable `example_name`"
default = ""
default = ""
}

variable "vswitch_ids" {
description = "List of existing vswitch id."
type = "list"
default = []
type = list(string)
default = []
}

variable "vswitch_cidrs" {
description = "List of cidr blocks used to create several new vswitches when 'vswitch_ids' is not specified."
type = "list"
default = ["10.1.2.0/24"]
type = list(string)
default = ["10.1.2.0/24"]
}

variable "new_nat_gateway" {
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."
default = "true"
default = "true"
}

# Cluster nodes variables

variable "master_instance_type" {
variable "master_instance_types" {
description = "The ecs instance type used to launch master nodes. Default from instance typs datasource."
default = ""
type = list(string)
default = [""]
}

variable "worker_instance_type" {
variable "worker_instance_types" {
description = "The ecs instance type used to launch worker nodes. Default from instance typs datasource."
default = ""
type = list(string)
default = [""]
}

variable "master_disk_category" {
description = "The system disk category used to launch one or more master nodes."
default = "cloud_efficiency"
default = "cloud_efficiency"
}

variable "worker_disk_category" {
description = "The system disk category used to launch one or more worker nodes."
default = "cloud_efficiency"
default = "cloud_efficiency"
}

variable "master_disk_size" {
description = "The system disk size used to launch one or more master nodes."
default = "40"
default = "40"
}

variable "worker_disk_size" {
description = "The system disk size used to launch one or more worker nodes."
default = "40"
default = "40"
}

variable "ecs_password" {
description = "The password of instance."
default = "Abc12345"
default = "Abc12345"
}

variable "k8s_number" {
description = "The number of kubernetes cluster."
default = 1
default = 1
}

variable "k8s_worker_number" {
variable "k8s_worker_numbers" {
description = "The number of worker nodes in each kubernetes cluster."
default = 3
type = list(number)
default = [3]
}

variable "k8s_name_prefix" {
description = "The name prefix used to create several kubernetes clusters. Default to variable `example_name`"
default = ""
default = ""
}

variable "k8s_pod_cidr" {
description = "The kubernetes pod cidr block. It cannot be equals to vpc's or vswitch's and cannot be in them."
default = "172.20.0.0/16"
default = "172.20.0.0/16"
}

variable "k8s_service_cidr" {
description = "The kubernetes service cidr block. It cannot be equals to vpc's or vswitch's or pod's and cannot be in them."
default = "172.21.0.0/20"
}
default = "172.21.0.0/20"
}

0 comments on commit ceba86a

Please sign in to comment.