From dfd2899d8d3ec901c74a776aeb02252b50ef26b6 Mon Sep 17 00:00:00 2001 From: Aleksandr Titov Date: Mon, 22 Jan 2024 17:49:30 +0300 Subject: [PATCH] feat: Add module CCE Addon Autoscaler --- README.md | 21 ++++- main.tf | 81 +++++++++++++++++++ variables.tf | 219 +++++++++++++++++++++++++++++++++++++++++++++++++++ versions.tf | 7 ++ 4 files changed, 327 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7234d11..599baae 100644 --- a/README.md +++ b/README.md @@ -1 +1,20 @@ -# Module name \ No newline at end of file +# Huawei Cloud CCE Addon Autoscaler + ++ [Add-ons documentation](https://support.huaweicloud.com/intl/en-us/usermanual-cce/cce_10_0064.html) ++ [CCE Addon autoscaler template](https://github.com/huaweicloud/terraform-provider-huaweicloud/blob/master/examples/cce/basic/cce-addon-templates.md#autoscaler) ++ [Autoscaler repo](https://github.com/kubernetes/autoscaler/tree/master) +To get information about add-on use the following data `huaweicloud_cce_addon_template`. + +Example for `autoscaler` add-on + +```hcl +data "huaweicloud_cce_addon_template" "test" { + cluster_id = var.cce_cluster_id + name = "autoscaler" + version = "1.25.46" +} + +output "version" { + value = data.huaweicloud_cce_addon_template.test +} +``` diff --git a/main.tf b/main.tf index e69de29..a4b12d0 100644 --- a/main.tf +++ b/main.tf @@ -0,0 +1,81 @@ +data "huaweicloud_cce_cluster" "cluster" { + id = var.cluster_id +} + +resource "huaweicloud_cce_addon" "autoscaler" { + cluster_id = var.cluster_id + region = var.region + template_name = "autoscaler" + version = var.addon_version + + values { + basic_json = jsonencode( + { + "cceEndpoint" : format("https://cce.%s.myhuaweicloud.com", var.region), + "cluster_version" : data.huaweicloud_cce_cluster.cluster.cluster_version, + "ecsEndpoint" : format("https://ecs.%s.myhuaweicloud.com", var.region), + "image_version" : var.addon_version, + "platform" : var.platform, + "rbac_enabled" : var.rbac_enabled, + "region" : var.region, + "swr_addr" : var.swr_addr == null ? format("swr.%s.myhuaweicloud.com", var.region) : var.swr_addr, + "swr_user" : var.swr_user + } + ) + custom_json = jsonencode( + { + "cluster_id" : data.huaweicloud_cce_cluster.cluster.id, + "coresTotal" : var.cores_total, + "memoryTotal" : var.memory_total, + "expander" : var.expander, + "localVolumeNodeScalingEnabled" : var.local_volume_node_scaling_enabled, + "logLevel" : var.log_level, + "maxEmptyBulkDeleteFlag" : var.max_empty_bulk_delete_flag, + "maxNodeProvisionTime" : var.max_node_provision_time, + "maxNodesTotal" : var.max_nodes_total, + + "multiAZEnabled" : var.multi_az_enabled, + "networkMode" : data.huaweicloud_cce_cluster.cluster.container_network_type, + "newEphemeralVolumesPodScaleUpDelay" : var.new_ephemeral_volumes_pod_scale_up_delay, + "node_match_expressions" : var.node_match_expressions, + "skipNodesWithCustomControllerPods" : var.skip_nodes_with_custom_controller_pods, + "tenant_id" : var.tenant_id, + "tolerations" : var.tolerations, + "unremovableNodeRecheckTimeout" : var.unremovable_node_recheck_timeout, + + # Scale down + "scaleDownDelayAfterAdd" : var.scale_down.delay_after_add, + "scaleDownDelayAfterDelete" : var.scale_down.delay_after_delete, + "scaleDownDelayAfterFailure" : var.scale_down.delay_after_failure, + "scaleDownEnabled" : var.scale_down.enabled, + "scaleDownUnneededTime" : var.scale_down.unneeded_time, + "scaleDownUtilizationThreshold" : var.scale_down.utilization_threshold, + + # Scale up + "scaleUpCpuUtilizationThreshold" : var.scale_up.cpu_utilization_threshold, + "scaleUpMemUtilizationThreshold" : var.scale_up.mem_utilization_threshold, + "scaleUpUnscheduledPodEnabled" : var.scale_up.unscheduled_pod_enabled, + "scaleUpUtilizationEnabled" : var.scale_up.utilization_enabled + } + ) + flavor_json = jsonencode( + { + "category" : var.category, + "description" : "Autoscaler", + "is_default" : true, + "name" : "autoscaler", + "recommend_cluster_flavor_types" : var.recommend_cluster_flavor_types, + "replicas" : var.replicas, + "resources" : [ + { + "name" : "autoscaler", + "limitsCpu" : var.resources.limits_cpu, + "limitsMem" : var.resources.limits_mem, + "requestsCpu" : var.resources.requests_cpu, + "requestsMem" : var.resources.requests_mem + } + ] + } + ) + } +} diff --git a/variables.tf b/variables.tf index e69de29..63beea2 100644 --- a/variables.tf +++ b/variables.tf @@ -0,0 +1,219 @@ +## +# Common variables +## +variable "region" { + description = "Specifies the region in which to create the CCE node pool resource, if omitted, the provider-level region will be used" + type = string +} + +variable "cluster_id" { + description = "Specifies the CCE cluster ID" + type = string +} + +## +# Add-on Autoscaler Basic +## +variable "addon_version" { + description = "Version of Autoscaler addon" + type = string + default = "1.25.46" +} + +variable "platform" { + description = "Platform" + type = string + default = "linux-amd64" +} + +variable "rbac_enabled" { + description = "Kubernetes rbac enabled" + type = bool + default = true +} + +variable "swr_addr" { + description = "SWR Address" + type = string + default = null +} + +variable "swr_user" { + description = "SWR User name" + type = string + default = "hwofficial" +} + +## +# Add-on Autoscaler Custom +## +variable "cores_total" { + description = "Total number of cores" + type = number + default = 64 +} + +variable "memory_total" { + description = "Memory total" + type = number + default = 128 +} + +variable "local_volume_node_scaling_enabled" { + description = "Local volume node scaling enabled" + type = bool + default = false +} + +variable "log_level" { + description = "Log level" + type = number + default = 4 +} + +variable "max_empty_bulk_delete_flag" { + description = "Max empty bulk delete flag" + type = number + default = 10 +} + +variable "max_node_provision_time" { + description = "Max node provision time" + type = number + default = 15 +} + +variable "max_nodes_total" { + description = "Max nodes total" + type = number + default = 10 +} + +variable "multi_az_enabled" { + description = "Multi AZ" + type = bool + default = false +} + +variable "new_ephemeral_volumes_pod_scale_up_delay" { + description = "New ephemeral volumes pod scale up delay" + type = number + default = 10 +} + +variable "node_match_expressions" { + description = "Node match expressions" + type = list(string) + default = [] +} + +variable "expander" { + description = "Expander" + type = string + default = "priority,least-waste" +} + +variable "scale_down" { + description = "Scale down configuration" + type = object({ + delay_after_add = optional(number, 10) + delay_after_delete = optional(number, 10) + delay_after_failure = optional(number, 3) + enabled = optional(bool, true) + unneeded_time = optional(number, 10) + utilization_threshold = optional(number, 0.5) + }) + default = {} +} + +variable "scale_up" { + description = "Scale up configuration" + type = object({ + cpu_utilization_threshold = optional(number, 1) + mem_utilization_threshold = optional(number, 1) + unscheduled_pod_enabled = optional(bool, true) + utilization_enabled = optional(bool, true) + }) + default = {} +} + +variable "skip_nodes_with_custom_controller_pods" { + description = "Skip nodes with custom controller pods" + type = bool + default = true +} + +variable "tenant_id" { + description = "Tenant ID" + type = string +} + +variable "tolerations" { + description = "Kubernetes tolerations" + #TODO: fix type + type = any + default = [ + { + "effect" : "NoExecute", + "key" : "node.kubernetes.io/not-ready", + "operator" : "Exists", + "tolerationSeconds" : 60 + }, + { + "effect" : "NoExecute", + "key" : "node.kubernetes.io/unreachable", + "operator" : "Exists", + "tolerationSeconds" : 60 + } + ] +} + +variable "unremovable_node_recheck_timeout" { + description = "Unremovable node recheck timeout" + type = number + default = 5 +} + +## +# Add-on Autoscaler Flavor +## + +variable "replicas" { + description = "Number of replicas" + type = number + default = 1 +} + +variable "metrics_server_version" { + description = "Version of Autoscaler addon" + type = string + default = "1.3.2" +} + +variable "resources" { + description = "Kubernetes resources" + type = object({ + limits_cpu = optional(string, "1000m") + limits_mem = optional(string, "1000Mi") + requests_cpu = optional(string, "500m") + requests_mem = optional(string, "500Mi") + }) + default = {} +} + +variable "recommend_cluster_flavor_types" { + description = "Recommend cluster flavor types" + type = list(string) + default = [ + "small" + ] +} + +variable "category" { + description = "Category" + type = list(string) + default = [ + "CCE", + "Turbo" + ] +} diff --git a/versions.tf b/versions.tf index 74655e1..af0509e 100644 --- a/versions.tf +++ b/versions.tf @@ -1,3 +1,10 @@ terraform { required_version = "~> 1.4" + + required_providers { + huaweicloud = { + source = "huaweicloud/huaweicloud" + version = "~>1.47" + } + } }