Skip to content

improve(kubernetes): updated module kubernetes and added example. #3

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 1 commit 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
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ This example can specify the following arguments to create user-defined kubernte
* alicloud_access_key: The Alicloud Access Key ID
* alicloud_secret_key: The Alicloud Access Secret Key
* region: The ID of region in which launching resources
* k8s_name_prefix: The name prefix of kubernetes cluster
* k8s_number: The number of kubernetes cluster
* k8s_name: The name of kubernetes cluster
* k8s_worker_number: The number of worker nodes in each kubernetes cluster
* 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`,
it had better to `192.168.XX.XX/XX` or `10.XX.XX.XX/XX`
Expand Down Expand Up @@ -49,14 +48,13 @@ Conditional creation
--------------------
This example can support the following creating kubernetes cluster scenario by setting different arguments.

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

You can specify the following user-defined arguments:

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

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

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

Terraform version
-----------------
Terraform version 0.11.0 or newer and Provider version 1.9.0 or newer are required for this example to work.
Terraform version 0.12.0 or newer and Provider version 1.60.0 or newer are required for this example to work.

Authors
-------
Expand Down
4 changes: 4 additions & 0 deletions examples/basic/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module "kubernetes" {
source = "../../"
k8s_name = "CreateByTerraform"
}
23 changes: 23 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
locals {
vswitch_name_regex = var.vswitch_name_regex != "" ? var.vswitch_name_regex : var.filter_with_name_regex
vswitch_tags = length(var.vswitch_tags) > 0 ? var.vswitch_tags : var.filter_with_tags
vswitch_resource_group_id = var.vswitch_resource_group_id != "" ? var.vswitch_resource_group_id : var.filter_with_resource_group_id
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 : []
master_instance_types = length(var.master_instance_types) > 0 ? var.master_instance_types : [data.alicloud_instance_types.this.instance_types[0].id]
worker_instance_types = length(var.worker_instance_types) > 0 ? var.worker_instance_types : [data.alicloud_instance_types.this.instance_types[0].id]
zone_id = data.alicloud_vswitches.this.vswitches.0.zone_id
}


// Instance_types data source for instance_type
data "alicloud_instance_types" "this" {
availability_zone = local.zone_id
cpu_core_count = var.cpu_core_count
memory_size = var.memory_size
}

data "alicloud_vswitches" "this" {
name_regex = local.vswitch_name_regex
tags = local.vswitch_tags
resource_group_id = local.vswitch_resource_group_id
}
94 changes: 14 additions & 80 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,96 +1,30 @@
// Provider specific configs
provider "alicloud" {
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
}

// Zones data source for availability_zone
data "alicloud_zones" "default" {
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
}

// 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(
"%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
}

resource "alicloud_eip" "default" {
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[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[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
version = ">=1.60.0"
profile = var.profile != "" ? var.profile : null
shared_credentials_file = var.shared_credentials_file != "" ? var.shared_credentials_file : null
region = var.region != "" ? var.region : null
skip_region_validation = var.skip_region_validation
configuration_source = "terraform-alicloud-modules/kubernetes"
}

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_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)))]]

new_nat_gateway = false
availability_zone = local.zone_id
name = var.k8s_name
vswitch_ids = local.vswitch_ids
new_nat_gateway = var.new_nat_gateway
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
worker_disk_size = var.worker_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]
master_instance_types = var.master_instance_types
worker_instance_types = var.worker_instance_types
worker_numbers = var.k8s_worker_numbers
master_instance_types = local.master_instance_types
worker_instance_types = local.worker_instance_types
worker_numbers = var.k8s_worker_numbers
}

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

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

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

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

output "security_group_id" {
output "this_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.security_group_id
}

output "cluster_nodes" {
output "this_cluster_nodes" {
description = "List nodes of cluster."
value = alicloud_cs_kubernetes.k8s.*.worker_nodes
value = alicloud_cs_kubernetes.k8s.worker_nodes
}

89 changes: 45 additions & 44 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,65 @@

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

variable "availability_zone" {
description = "The available zone to launch ecs instance and other resources."
variable "profile" {
description = "The profile name as set in the shared credentials file. If not set, it will be sourced from the ALICLOUD_PROFILE environment variable."
default = ""
}
variable "shared_credentials_file" {
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."
default = ""
}

variable "number_format" {
description = "The number format used to output."
default = "%02d"
variable "skip_region_validation" {
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)."
default = false
}

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

# Instance typs variables
variable "cpu_core_count" {
description = "CPU core count is used to fetch instance types."
default = 1
variable "filter_with_tags" {
description = "A default filter applied to retrieve existing vswitches, nat gateway, eip, snat entry and kubernetes clusters by tags."
type = map(string)
default = {}
}

variable "memory_size" {
description = "Memory size used to fetch instance types."
variable "filter_with_resource_group_id" {
description = "A default filter applied to retrieve existing vswitches, nat gateway, eip, snat entry and kubernetes clusters by resource group id."
default = ""
}

# Instancetypes variables
variable "cpu_core_count" {
description = "CPU core count is used to fetch instancetypes."
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 = ""
variable "memory_size" {
description = "Memory size used to fetch instancetypes."
default = 4
}

variable "vpc_id" {
description = "A existing vpc id used to create several vswitches and other resources."
# VSwitch variables

variable "vswitch_name_regex" {
description = "A default filter applied to retrieve existing vswitches by name regex. If not set, `filter_with_name_regex` will be used."
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"
variable "vswitch_tags" {
description = "A default filter applied to retrieve existing vswitches by tags. If not set, `filter_with_tags` will be used."
type = map(string)
default = {}
}

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

Expand All @@ -59,12 +71,6 @@ variable "vswitch_ids" {
default = []
}

variable "vswitch_cidrs" {
description = "List of cidr blocks used to create several new vswitches when 'vswitch_ids' is not specified."
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"
Expand All @@ -73,15 +79,15 @@ variable "new_nat_gateway" {
# Cluster nodes variables

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

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

variable "master_disk_category" {
Expand Down Expand Up @@ -109,20 +115,15 @@ variable "ecs_password" {
default = "Abc12345"
}

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

variable "k8s_worker_numbers" {
description = "The number of worker nodes in each kubernetes cluster."
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 = ""
variable "k8s_name" {
description = "The name used to create kubernetes cluster."
default = "tf-example-kubernetes"
}

variable "k8s_pod_cidr" {
Expand Down