Skip to content

Commit ad58066

Browse files
Lexsssxiaozhu36
authored andcommitted
improve(managed-k8s): improve the module to adapt new version resources
1 parent 96e977d commit ad58066

File tree

8 files changed

+172
-34
lines changed

8 files changed

+172
-34
lines changed

README.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,31 @@ In other words, the specified vpc has a nat gateway and there are several snat e
6464

6565
This moudle can set [sls project](https://www.terraform.io/docs/providers/alicloud/r/log_project.html) config for this module
6666

67-
1. Create a new sls project with `new_sls_project`:
67+
1. Create a new sls project with `cluster_addons`:
6868

6969
```hcl
70-
new_sls_project = true
71-
```
72-
73-
1. Using existing sls project with `sls_project_name`:
74-
75-
```hcl
76-
sls_project_name = "Your-sls-project-name"
70+
cluster_addons = [
71+
{
72+
name = "flannel",
73+
config = "",
74+
},
75+
{
76+
name = "flexvolume",
77+
config = "",
78+
},
79+
{
80+
name = "alicloud-disk-controller",
81+
config = "",
82+
},
83+
{
84+
name = "logtail-ds",
85+
config = "{\"IngressDashboardEnabled\":\"true\"}",
86+
},
87+
{
88+
name = "nginx-ingress-controller",
89+
config = "{\"IngressSlbNetworkType\":\"internet\"}",
90+
},
91+
]
7792
```
7893
7994
If you want to store kube config and other certificates after the cluster created, you can set the following parameters:
@@ -139,7 +154,7 @@ If you want to store kube config and other certificates after the cluster create
139154
140155
Terraform version
141156
-----------------
142-
Terraform version 0.12.0 or newer and Provider version 1.57.2 or newer are required for this example to work.
157+
Terraform version 0.12.0 or newer and Provider version 1.77.0 or newer are required for this example to work.
143158
144159
Authors
145160
-------

examples/basic/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Managed kubernetes create with kubernetes networking module example
2+
3+
Configuration in this directory creates set of kubernetes cluster in various combinations.
4+
5+
## Usage
6+
7+
To run this example you need to execute:
8+
9+
```bash
10+
$ terraform init
11+
$ terraform plan
12+
$ terraform apply
13+
```
14+
15+
Note that this example may create resources which cost money. Run `terraform destroy` when you don't need these resources.

examples/basic/main.tf

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
variable "profile" {
2+
default = "default"
3+
}
4+
variable "region" {
5+
default = "cn-zhangjiakou"
6+
}
7+
variable "zone_id" {
8+
default = "cn-zhangjiakou-a"
9+
}
10+
variable "vpc_cidr" {
11+
default = "10.1.0.0/21"
12+
}
13+
variable "availability_zones" {
14+
default = ["cn-zhangjiakou-a", "cn-zhangjiakou-b", "cn-zhangjiakou-c"]
15+
}
16+
17+
provider "alicloud" {
18+
region = var.region
19+
profile = var.profile
20+
}
21+
22+
###########################################
23+
# Data sources to get VPC, vswitch details
24+
###########################################
25+
26+
module "managed-k8s" {
27+
source = "../../"
28+
region = var.region
29+
profile = var.profile
30+
k8s_name_prefix = "CreateByTerraform1"
31+
32+
vswitch_ids = [concat(module.kubernetes-networking.this_vswitch_ids, [""])[0]]
33+
cluster_addons = [
34+
{
35+
name = "flannel",
36+
config = "",
37+
},
38+
{
39+
name = "flexvolume",
40+
config = "",
41+
},
42+
{
43+
name = "alicloud-disk-controller",
44+
config = "",
45+
},
46+
{
47+
name = "logtail-ds",
48+
config = "{\"IngressDashboardEnabled\":\"true\"}",
49+
},
50+
{
51+
name = "nginx-ingress-controller",
52+
config = "{\"IngressSlbNetworkType\":\"internet\"}",
53+
},
54+
]
55+
}
56+
57+
module "kubernetes-networking" {
58+
source = "terraform-alicloud-modules/kubernetes-networking/alicloud"
59+
region = var.region
60+
profile = var.profile
61+
vpc_cidr = var.vpc_cidr
62+
availability_zones = var.availability_zones
63+
vswitch_cidrs = [cidrsubnet(var.vpc_cidr, 4, 6)]
64+
create = true
65+
}

examples/basic/outputs.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Output kubernetes resource
2+
output "this_k8s_name" {
3+
description = "Name of the kunernetes cluster."
4+
value = module.managed-k8s.this_k8s_name
5+
}
6+
output "this_k8s_id" {
7+
description = "ID of the kunernetes cluster."
8+
value = module.managed-k8s.this_k8s_id
9+
}
10+
output "this_k8s_nodes" {
11+
description = "List nodes of cluster."
12+
value = module.managed-k8s.this_k8s_nodes
13+
}
14+
// Output VPC
15+
output "this_vpc_id" {
16+
description = "The ID of the VPC."
17+
value = module.managed-k8s.this_vpc_id
18+
}
19+
20+
output "this_vswitch_ids" {
21+
description = "List ID of the VSwitches."
22+
value = module.managed-k8s.this_vswitch_ids
23+
}
24+
output "this_security_group_id" {
25+
description = "ID of the Security Group used to deploy kubernetes cluster."
26+
value = module.managed-k8s.this_security_group_id
27+
}

locals.tf

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ locals {
66
For = "modules/terraform-alicloud-managed-kubernetes"
77
K8s = local.k8s_name
88
}
9-
vswitch_ids = length(var.vswitch_ids) > 0 ? var.vswitch_ids : alicloud_vswitch.new.*.id
10-
sls_project = var.sls_project_name == "" ? concat(alicloud_log_project.new.*.id, [""])[0] : var.sls_project_name
11-
instance_types = length(var.worker_instance_types) > 0 ? var.worker_instance_types : [data.alicloud_instance_types.default.ids.0]
9+
vswitch_ids = length(var.vswitch_ids) > 0 ? var.vswitch_ids : alicloud_vswitch.new.*.id
1210
}
1311

1412
resource "random_uuid" "this" {}

main.tf

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
11
// Provider specific configs
22
provider "alicloud" {
3-
version = ">=1.57.2"
43
profile = var.profile != "" ? var.profile : null
54
shared_credentials_file = var.shared_credentials_file != "" ? var.shared_credentials_file : null
65
region = var.region != "" ? var.region : null
76
skip_region_validation = var.skip_region_validation
87
configuration_source = "terraform-alicloud-modules/managed-kubernetes"
98
}
109

11-
resource "alicloud_log_project" "new" {
12-
count = var.new_sls_project == true ? 1 : 0
13-
// sls project name must end with lower letter
14-
name = format("%s-end", substr("for-${local.k8s_name}", 0, 59))
15-
description = "created by terraform for managedkubernetes cluster"
16-
}
17-
1810
resource "alicloud_cs_managed_kubernetes" "this" {
1911
count = length(local.vswitch_ids) > 0 ? 1 : 0
2012
name = local.k8s_name
21-
vswitch_ids = local.vswitch_ids
13+
worker_vswitch_ids = local.vswitch_ids
2214
new_nat_gateway = var.new_vpc == true ? false : var.new_nat_gateway
2315
worker_disk_category = var.worker_disk_category
2416
password = var.ecs_password
2517
pod_cidr = var.k8s_pod_cidr
2618
service_cidr = var.k8s_service_cidr
2719
slb_internet_enabled = true
2820
install_cloud_monitor = true
29-
cluster_network_type = var.cluster_network_type
3021
worker_instance_types = var.worker_instance_types
3122
worker_number = var.worker_number
32-
log_config {
33-
type = "SLS"
34-
project = local.sls_project == "" ? null : local.sls_project
23+
dynamic "addons" {
24+
for_each = var.cluster_addons
25+
content {
26+
name = lookup(addons.value, "name", var.cluster_addons)
27+
config = lookup(addons.value, "config", var.cluster_addons)
28+
}
3529
}
3630
kube_config = var.kube_config_path
3731
client_cert = var.client_cert_path

outputs.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,3 @@ output "this_security_group_id" {
2525
description = "ID of the Security Group used to deploy kubernetes cluster."
2626
value = concat(alicloud_cs_managed_kubernetes.this.*.security_group_id, [""])[0]
2727
}
28-
29-
//Output SLS
30-
output "this_sls_project_name" {
31-
description = "The sls project name used to configure cluster."
32-
value = local.sls_project
33-
}

variables.tf

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
//Autoscaling group
22
variable "region" {
33
description = "The region ID used to launch this module resources. If not set, it will be sourced from followed by ALICLOUD_REGION environment variable and profile."
4+
type = string
45
default = ""
56
}
67

78
variable "profile" {
89
description = "The profile name as set in the shared credentials file. If not set, it will be sourced from the ALICLOUD_PROFILE environment variable."
10+
type = string
911
default = ""
1012
}
1113
variable "shared_credentials_file" {
1214
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."
15+
type = string
1316
default = ""
1417
}
1518

@@ -28,6 +31,7 @@ variable "new_vpc" {
2831

2932
variable "vpc_cidr" {
3033
description = "The cidr block used to launch a new vpc."
34+
type = string
3135
default = "192.168.0.0/16"
3236
}
3337

@@ -52,6 +56,7 @@ variable "availability_zones" {
5256

5357
variable "new_eip_bandwidth" {
5458
description = "The bandwidth used to create a new EIP when 'new_vpc' is true."
59+
type = number
5560
default = 50
5661
}
5762
variable "new_nat_gateway" {
@@ -62,82 +67,107 @@ variable "new_nat_gateway" {
6267
# Cluster nodes variables
6368
variable "cpu_core_count" {
6469
description = "CPU core count is used to fetch instance types."
70+
type = number
6571
default = 1
6672
}
6773

6874
variable "memory_size" {
6975
description = "Memory size used to fetch instance types."
76+
type = number
7077
default = 2
7178
}
79+
7280
variable "worker_instance_types" {
7381
description = "The ecs instance type used to launch worker nodes. If not set, data source `alicloud_instance_types` will return one based on `cpu_core_count` and `memory_size`."
7482
type = list(string)
7583
default = ["ecs.n4.xlarge"]
7684
}
7785

86+
variable "cluster_addons" {
87+
description = "Addon components in kubernetes cluster"
88+
type = list(object({
89+
name = string
90+
config = string
91+
}))
92+
default = []
93+
}
94+
7895
variable "worker_disk_category" {
7996
description = "The system disk category used to launch one or more worker nodes."
97+
type = string
8098
default = "cloud_efficiency"
8199
}
82100

83101
variable "worker_disk_size" {
84102
description = "The system disk size used to launch one or more worker nodes."
103+
type = number
85104
default = 40
86105
}
87106

88107
variable "ecs_password" {
89108
description = "The password of worker nodes."
109+
type = string
90110
default = "Abc12345"
91111
}
92112

93113
variable "worker_number" {
94114
description = "The number of kubernetes cluster work nodes."
115+
type = number
95116
default = 2
96117
}
97118

98119
variable "k8s_name_prefix" {
99120
description = "The name prefix used to create managed kubernetes cluster."
121+
type = string
100122
default = "terraform-alicloud-managed-kubernetes"
101123
}
102124

103125
variable "k8s_pod_cidr" {
104126
description = "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`."
127+
type = string
105128
default = "172.20.0.0/16"
106129
}
107130

108131
variable "k8s_service_cidr" {
109132
description = "The kubernetes service cidr block. It cannot be equals to vpc's or vswitch's or pod's and cannot be in them. Its setting rule is same as `k8s_pod_cidr`."
133+
type = string
110134
default = "172.21.0.0/20"
111135
}
112136

113137
variable "cluster_network_type" {
114-
description = "Network type, valid options are `flannel` and `terway`."
138+
description = "(Deprecated from v1.3.0, use 'cluster_addons' instead)Network type, valid options are `flannel` and `terway`."
139+
type = string
115140
default = "flannel"
116141
}
117142

118143
variable "new_sls_project" {
119-
description = "Create a new sls project for this module."
144+
description = "(Deprecated from v1.3.0, use 'cluster_addons' instead)Create a new sls project for this module."
120145
type = bool
121146
default = false
122147
}
123148
variable "sls_project_name" {
124-
description = "Specify a existing sls project for this module."
149+
description = "(Deprecated from v1.3.0, use 'cluster_addons' instead)Specify a existing sls project for this module."
150+
type = string
125151
default = ""
126152
}
127153

128154
variable "kube_config_path" {
129155
description = "The path of kube config, like ~/.kube/config"
156+
type = string
130157
default = ""
131158
}
132159
variable "client_cert_path" {
133160
description = "The path of client certificate, like ~/.kube/client-cert.pem"
161+
type = string
134162
default = ""
135163
}
136164
variable "client_key_path" {
137165
description = "The path of client key, like ~/.kube/client-key.pem"
166+
type = string
138167
default = ""
139168
}
140169
variable "cluster_ca_cert_path" {
141170
description = "The path of cluster ca certificate, like ~/.kube/cluster-ca-cert.pem"
171+
type = string
142172
default = ""
143173
}

0 commit comments

Comments
 (0)