1+ # 开通sls服务
2+ data "alicloud_log_service" "open_sls" {
3+ enable = " On"
4+ }
5+
6+ # 开通ack pro
7+ data "alicloud_ack_service" "open_ack" {
8+ enable = " On"
9+ type = " propayasgo"
10+ }
11+
12+ # 查询实例实例规格
13+ data "alicloud_instance_types" "default" {
14+ instance_type_family = " ecs.g7"
15+ sorted_by = " CPU"
16+ memory_size = " 16"
17+ }
18+
19+ # 查询实例规格支持的可用区
20+ data "alicloud_zones" "default" {
21+ available_instance_type = data. alicloud_instance_types . default . ids . 0
22+ available_slb_address_type = " classic_internet"
23+ }
24+
25+ resource "alicloud_vpc" "vpc" {
26+ cidr_block = " 192.168.0.0/16"
27+ vpc_name = " ${ var . common_name } -vpc"
28+ }
29+
30+ resource "alicloud_vswitch" "vsw" {
31+ vpc_id = alicloud_vpc. vpc . id
32+ cidr_block = " 192.168.0.0/24"
33+ zone_id = data. alicloud_zones . default . ids . 0
34+ vswitch_name = " ${ var . common_name } -${ data . alicloud_zones . default . ids . 0 } -vsw"
35+ }
36+
37+ resource "alicloud_cs_managed_kubernetes" "ack" {
38+ depends_on = [data . alicloud_log_service . open_sls , data . alicloud_ack_service . open_ack , alicloud_ram_role_policy_attachment . attach ]
39+ name = var. ack_name
40+ cluster_spec = " ack.pro.small"
41+ vswitch_ids = [alicloud_vswitch . vsw . id ]
42+ pod_vswitch_ids = [alicloud_vswitch . vsw . id ]
43+ service_cidr = " 172.16.0.0/16"
44+ new_nat_gateway = true
45+ slb_internet_enabled = false
46+ is_enterprise_security_group = false
47+
48+ addons {
49+ name = " security-inspector"
50+ }
51+ addons {
52+ name = " terway-eniip"
53+ config = jsonencode ({
54+ IPVlan = " false"
55+ NetworkPolicy = " false"
56+ ENITrunking = " false"
57+ })
58+ }
59+ addons {
60+ name = " csi-plugin"
61+ }
62+ addons {
63+ name = " csi-provisioner"
64+ }
65+ addons {
66+ name = " storage-operator"
67+ config = jsonencode ({
68+ CnfsOssEnable = " false"
69+ CnfsNasEnable = " false"
70+ })
71+ }
72+ addons {
73+ name = " logtail-ds"
74+ config = jsonencode ({
75+ IngressDashboardEnabled = " true"
76+ })
77+ }
78+ addons {
79+ name = " nginx-ingress-controller"
80+ config = jsonencode ({
81+ IngressSlbNetworkType = " internet"
82+ IngressSlbSpec = " slb.s2.small"
83+ })
84+ }
85+ addons {
86+ name = " ack-node-local-dns"
87+ }
88+ addons {
89+ name = " arms-prometheus"
90+ }
91+ addons {
92+ name = " ack-node-problem-detector"
93+ config = jsonencode ({
94+ sls_project_name = " "
95+ })
96+ }
97+ }
98+
99+ resource "alicloud_cs_kubernetes_node_pool" "node_pool" {
100+ node_pool_name = " ${ var . common_name } -nodepool"
101+ cluster_id = alicloud_cs_managed_kubernetes. ack . id
102+ vswitch_ids = [alicloud_vswitch . vsw . id ]
103+ instance_types = [data . alicloud_instance_types . default . ids . 0 ]
104+ system_disk_category = " cloud_essd"
105+ system_disk_size = 40
106+ desired_size = 3
107+
108+ runtime_name = " containerd"
109+ runtime_version = " 1.6.20"
110+ }
111+
112+ # 定义本地变量,包含所有要创建的 RAM Role 及其策略
113+ locals {
114+ cs_roles = [
115+ {
116+ name = " AliyunCSManagedLogRole"
117+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" cs.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
118+ description = " 集群的日志组件使用此角色来访问您在其他云产品中的资源。"
119+ policy_name = " AliyunCSManagedLogRolePolicy"
120+ },
121+ {
122+ name = " AliyunCSManagedCmsRole"
123+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" cs.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
124+ description = " 集群的CMS组件使用此角色来访问您在其他云产品中的资源。"
125+ policy_name = " AliyunCSManagedCmsRolePolicy"
126+ },
127+ {
128+ name = " AliyunCSManagedCsiRole"
129+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" cs.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
130+ description = " 集群的存储组件使用此角色来访问您在其他云产品中的资源。"
131+ policy_name = " AliyunCSManagedCsiRolePolicy"
132+ },
133+ {
134+ name = " AliyunCSManagedCsiPluginRole"
135+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" cs.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
136+ description = " 集群的存储组件使用此角色来访问您在其他云产品中的资源。"
137+ policy_name = " AliyunCSManagedCsiPluginRolePolicy"
138+ },
139+ {
140+ name = " AliyunCSManagedCsiProvisionerRole"
141+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" cs.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
142+ description = " 集群的存储组件使用此角色来访问您在其他云产品中的资源。"
143+ policy_name = " AliyunCSManagedCsiProvisionerRolePolicy"
144+ },
145+ {
146+ name = " AliyunCSManagedVKRole"
147+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" cs.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
148+ description = " ACK Serverless集群的VK组件使用此角色来访问您在其他云产品中的资源。"
149+ policy_name = " AliyunCSManagedVKRolePolicy"
150+ },
151+ {
152+ name = " AliyunCSServerlessKubernetesRole"
153+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" cs.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
154+ description = " 集群默认使用此角色来访问您在其他云产品中的资源。"
155+ policy_name = " AliyunCSServerlessKubernetesRolePolicy"
156+ },
157+ {
158+ name = " AliyunCSKubernetesAuditRole"
159+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" cs.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
160+ description = " 集群审计功能使用此角色来访问您在其他云产品中的资源。"
161+ policy_name = " AliyunCSKubernetesAuditRolePolicy"
162+ },
163+ {
164+ name = " AliyunCSManagedNetworkRole"
165+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" cs.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
166+ description = " 集群网络组件使用此角色来访问您在其他云产品中的资源。"
167+ policy_name = " AliyunCSManagedNetworkRolePolicy"
168+ },
169+ {
170+ name = " AliyunCSDefaultRole"
171+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" cs.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
172+ description = " 集群操作时默认使用此角色来访问您在其他云产品中的资源。"
173+ policy_name = " AliyunCSDefaultRolePolicy"
174+ },
175+ {
176+ name = " AliyunCSManagedKubernetesRole"
177+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" cs.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
178+ description = " 集群默认使用此角色来访问您在其他云产品中的资源。"
179+ policy_name = " AliyunCSManagedKubernetesRolePolicy"
180+ },
181+ {
182+ name = " AliyunCSManagedArmsRole"
183+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" cs.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
184+ description = " 集群Arms插件使用此角色来访问您在其他云产品中的资源。"
185+ policy_name = " AliyunCSManagedArmsRolePolicy"
186+ },
187+ {
188+ name = " AliyunCISDefaultRole"
189+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" cs.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
190+ description = " 容器服务(CS)智能运维使用此角色来访问您在其他云产品中的资源。"
191+ policy_name = " AliyunCISDefaultRolePolicy"
192+ },
193+ {
194+ name = " AliyunOOSLifecycleHook4CSRole"
195+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" oos.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
196+ description = " 集群扩缩容节点池依赖OOS服务,OOS使用此角色来访问您在其他云产品中的资源。"
197+ policy_name = " AliyunOOSLifecycleHook4CSRolePolicy"
198+ },
199+ {
200+ name = " AliyunCSManagedAutoScalerRole"
201+ policy_document = " {\" Statement\" :[{\" Action\" :\" sts:AssumeRole\" ,\" Effect\" :\" Allow\" ,\" Principal\" :{\" Service\" :[\" cs.aliyuncs.com\" ]}}],\" Version\" :\" 1\" }"
202+ description = " 集群的弹性伸缩组件使用此角色来访问您在其他云产品中的资源。"
203+ policy_name = " AliyunCSManagedAutoScalerRolePolicy"
204+ }
205+ ]
206+ }
207+
208+ // 查询RAM角色列表
209+ data "alicloud_ram_roles" "roles" {
210+ policy_type = " Custom"
211+ name_regex = " ^Aliyun.*Role$"
212+ }
213+
214+ locals {
215+ # 提取所有所需RAM角色name
216+ all_role_names = [for role in local . cs_roles : role . name ]
217+ # 提取已存在的RAM角色name
218+ created_role_names = [for role in data . alicloud_ram_roles . roles . roles : role . name ]
219+ # 计算补集:即找出还未创建的所需RAM角色
220+ complement_names = setsubtract (local. all_role_names , local. created_role_names )
221+ # 待创建的RAM角色
222+ complement_roles = [for role in local . cs_roles : role if contains (local. complement_names , role. name )]
223+ }
224+
225+ // 创建角色。
226+ resource "alicloud_ram_role" "role" {
227+ for_each = { for r in local . complement_roles : r . name => r }
228+ role_name = each. value . name
229+ assume_role_policy_document = each. value . policy_document
230+ description = each. value . description
231+ }
232+
233+ // 角色关联系统权限。
234+ resource "alicloud_ram_role_policy_attachment" "attach" {
235+ for_each = { for r in local . complement_roles : r . name => r }
236+ policy_name = each. value . policy_name
237+ policy_type = " System"
238+ role_name = each. value . name
239+ depends_on = [alicloud_ram_role . role ]
240+ }
0 commit comments