Skip to content

Commit

Permalink
add alicloud_cs_kubernetes_node_pool
Browse files Browse the repository at this point in the history
  • Loading branch information
shanye997 committed Mar 12, 2024
1 parent e438f0c commit ca97c2f
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 34 deletions.
38 changes: 28 additions & 10 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,42 @@ variable "profile" {
default = "default"
}

variable "region" {
default = "cn-hangzhou"
data "alicloud_zones" "default" {
available_resource_creation = "VSwitch"
}

data "alicloud_vpcs" "default" {
is_default = true
resource "alicloud_vpc" "default" {
vpc_name = "tf_module"
cidr_block = "10.4.0.0/16"
}

resource "alicloud_vswitch" "default" {
count = 3
vpc_id = alicloud_vpc.default.id
cidr_block = cidrsubnet(alicloud_vpc.default.cidr_block, 8, count.index)
zone_id = data.alicloud_zones.default.zones[0].id
}

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****"]
vpc_id = alicloud_vpc.default.id
vswitch_ids = alicloud_vswitch.default.*.id
master_instance_types = ["ecs.n1.medium", "ecs.c5.large", "ecs.n1.medium"]
worker_instance_types = ["ecs.n1.medium"]
k8s_pod_cidr = "192.168.5.0/24"
k8s_service_cidr = "192.168.2.0/24"
k8s_pod_cidr = "10.72.0.0/16"
k8s_service_cidr = "172.18.0.0/16"
k8s_worker_number = 2
}

data_disks = [{
category = "cloud_efficiency"
size = 40
}]
}

data "alicloud_cs_cluster_credential" "auth" {
cluster_id = module.k8s.cluster_id[0]
temporary_duration_minutes = 60
output_file = "~/.kube/config"
}
3 changes: 3 additions & 0 deletions local.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
locals {
subscription = var.instance_charge_type == "PostPaid" ? {} : var.subscription
}
61 changes: 48 additions & 13 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ data "alicloud_zones" "default" {

// If there is not specifying vpc_id, the module will launch a new vpc
resource "alicloud_vpc" "vpc" {
count = var.vpc_id == "" ? 1 : 0
count = var.create_vpc ? 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 = data.alicloud_zones.default.zones[count.index % length(data.alicloud_zones.default.zones)]["id"]
vswitch_name = var.vswitch_name_prefix == "" ? format(
"%s-%s",
var.example_name,
format(var.number_format, count.index + 1),
Expand Down Expand Up @@ -71,16 +71,12 @@ resource "alicloud_cs_kubernetes" "k8s" {
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
worker_number = var.k8s_worker_number
node_cidr_mask = var.node_cidr_mask
enable_ssh = var.enable_ssh
install_cloud_monitor = var.install_cloud_monitor
cpu_policy = var.cpu_policy
proxy_mode = var.proxy_mode
password = var.password
password = var.master_password
pod_cidr = var.k8s_pod_cidr
service_cidr = var.k8s_service_cidr
version = var.k8s_version
Expand All @@ -92,4 +88,43 @@ resource "alicloud_cs_kubernetes" "k8s" {
}
}
depends_on = [alicloud_snat_entry.default]
}
}

resource "alicloud_cs_kubernetes_node_pool" "default" {
count = var.k8s_number

name = alicloud_cs_kubernetes.k8s[count.index].name
cluster_id = alicloud_cs_kubernetes.k8s[count.index].id
vswitch_ids = length(var.vswitch_ids) > 0 ? split(",", join(",", var.vswitch_ids)) : length(var.vswitch_cidrs) < 1 ? [] : split(",", join(",", alicloud_vswitch.vswitches.*.id))
password = var.worker_password[count.index]

desired_size = var.k8s_worker_number
install_cloud_monitor = var.install_cloud_monitor
instance_types = var.worker_instance_types

instance_charge_type = var.instance_charge_type
period = lookup(local.subscription, "period", null)
period_unit = lookup(local.subscription, "period_unit", null)
auto_renew = lookup(local.subscription, "auto_renew", null)
auto_renew_period = lookup(local.subscription, "auto_renew_period", null)

cpu_policy = var.cpu_policy
system_disk_category = var.system_disk_category
system_disk_size = var.system_disk_size

dynamic "data_disks" {
for_each = var.data_disks
content {
name = lookup(data_disks.value, "name", null)
size = lookup(data_disks.value, "size", null)
category = lookup(data_disks.value, "category", null)
encrypted = lookup(data_disks.value, "encrypted", null)
performance_level = lookup(data_disks.value, "encperformance_levelrypted", null)
snapshot_id = lookup(data_disks.value, "snapshot_id", null)
device = lookup(data_disks.value, "device", null)
kms_key_id = lookup(data_disks.value, "kms_key_id", null)
auto_snapshot_policy_id = lookup(data_disks.value, "auto_snapshot_policy_id", null)

}
}
}
85 changes: 74 additions & 11 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ variable "vpc_name" {
default = ""
}

variable "create_vpc" {
description = "Boolean. If you have a vpc already, use that one, else make this true and one will be created."
type = bool
default = false
}

variable "vpc_id" {
description = "Existing vpc id used to create several vswitches and other resources."
type = string
Expand Down Expand Up @@ -130,13 +136,7 @@ variable "node_cidr_mask" {
variable "enable_ssh" {
description = "Enable login to the node through SSH."
type = bool
default = true
}

variable "install_cloud_monitor" {
description = "Install cloud monitor agent on ECS."
type = bool
default = true
default = false
}

variable "cpu_policy" {
Expand All @@ -151,8 +151,8 @@ variable "proxy_mode" {
default = "iptables"
}

variable "password" {
description = "The password of ECS instance."
variable "master_password" {
description = "The password of master ECS instance."
type = string
default = "Just4Test"
}
Expand All @@ -177,9 +177,9 @@ 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."
type = string
default = "1.16.6-aliyun.1"
default = ""
}

variable "cluster_addons" {
Expand All @@ -190,3 +190,66 @@ variable "cluster_addons" {
}))
default = []
}

######################
# node pool variables
######################

variable "worker_password" {
description = "The password of worker ECS instance."
type = list(string)
default = ["Just4Test"]
}

variable "install_cloud_monitor" {
description = "Install cloud monitor agent on ECS."
type = bool
default = true
}

variable "instance_charge_type" {
description = "The charge type of instance. Choices are 'PostPaid' and 'PrePaid'."
type = string
default = "PostPaid"
}

variable "subscription" {
description = "A mapping of fields for Prepaid ECS instances created. "
type = map(string)
default = {
period = 1
period_unit = "Month"
auto_renew = false
auto_renew_period = 1
}
}

variable "system_disk_category" {
description = "The system disk category used to launch one or more worker ecs instances."
type = string
default = "cloud_efficiency"
}

variable "system_disk_size" {
description = "The system disk size used to launch one or more worker ecs instances."
type = number
default = 40
}

variable "data_disks" {
description = "Additional data disks to attach to the scaled ECS instance."
type = list(map(string))
default = []
}

variable "disk_category" {
description = "The system disk category used to launch one or more worker ecs instances."
type = string
default = "cloud_efficiency"
}

variable "disk_size" {
description = "The system disk size used to launch one or more worker ecs instances."
type = number
default = 40
}

0 comments on commit ca97c2f

Please sign in to comment.