1+ // Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
2+ // Licensed under the Mozilla Public License v2.0
3+
4+ variable "tenancy_ocid" {
5+ }
6+
7+ variable "user_ocid" {
8+ }
9+
10+ variable "fingerprint" {
11+ }
12+
13+ variable "private_key_path" {
14+ }
15+
16+ variable "compartment_ocid" {
17+ }
18+
19+ variable "kms_vault_id" {
20+ }
21+
22+ variable "region" {
23+ default = " us-ashburn-1"
24+ }
25+
26+ # Provide the SSH public key to be set on each node in the node pool on launch.
27+ variable "node_pool_ssh_public_key" {
28+
29+ }
30+
31+ variable "node_pool_node_config_details_size" {
32+ default = 1
33+ }
34+
35+ variable "cluster_defined_tags_value" {
36+ default = " value"
37+ }
38+
39+ variable "cluster_freeform_tags" {
40+ default = { " Department" = " Finance" }
41+ }
42+
43+ variable "cluster_options_persistent_volume_config_defined_tags_value" {
44+ default = " value"
45+ }
46+
47+ variable "cluster_options_persistent_volume_config_freeform_tags" {
48+ default = { " Department" = " Finance" }
49+ }
50+
51+ variable "cluster_options_service_lb_config_defined_tags_value" {
52+ default = " value"
53+ }
54+
55+ variable "cluster_options_service_lb_config_freeform_tags" {
56+ default = { " Department" = " Finance" }
57+ }
58+
59+ variable "node_pool_defined_tags_value" {
60+ default = " value"
61+ }
62+
63+ variable "node_pool_freeform_tags" {
64+ default = { " Department" = " Finance" }
65+ }
66+
67+ variable "node_pool_node_config_details_defined_tags_value" {
68+ default = " value"
69+ }
70+
71+ variable "node_pool_node_config_details_freeform_tags" {
72+ default = { " Department" = " Finance" }
73+ }
74+
75+ provider "oci" {
76+ region = var. region
77+ tenancy_ocid = var. tenancy_ocid
78+ user_ocid = var. user_ocid
79+ fingerprint = var. fingerprint
80+ private_key_path = var. private_key_path
81+ }
82+
83+ data "oci_identity_availability_domain" "ad1" {
84+ compartment_id = var. tenancy_ocid
85+ ad_number = 1
86+ }
87+
88+ data "oci_identity_availability_domain" "ad2" {
89+ compartment_id = var. tenancy_ocid
90+ ad_number = 2
91+ }
92+
93+ // DEPENDENCIES
94+ variable defined_tag_namespace_name {
95+ default = " test"
96+ }
97+ resource "oci_identity_tag_namespace" "tag-namespace1" {
98+ # Required
99+ compartment_id = var. tenancy_ocid
100+ description = " example tag namespace"
101+ name = var. defined_tag_namespace_name != " " ? var. defined_tag_namespace_name : " example-tag-namespace-all"
102+
103+ is_retired = false
104+ }
105+
106+ resource "oci_identity_tag" "tag1" {
107+ # Required
108+ description = " example tag"
109+ name = " example-tag"
110+ tag_namespace_id = oci_identity_tag_namespace. tag-namespace1 . id
111+
112+ is_retired = false
113+ }
114+
115+ resource "oci_kms_vault" "test_vault" {
116+ # Required
117+ compartment_id = var. compartment_ocid
118+ display_name = " tf_test"
119+ vault_type = " DEFAULT"
120+ }
121+
122+ resource "oci_kms_key" "test_key" {
123+ # Required
124+ compartment_id = var. compartment_ocid
125+ display_name = " tf-test-key"
126+ key_shape {
127+ # Required
128+ algorithm = " AES"
129+ length = 32
130+ }
131+ management_endpoint = oci_kms_vault. test_vault . management_endpoint
132+ }
133+
134+ resource "oci_core_vcn" "test_vcn" {
135+ cidr_block = " 10.0.0.0/16"
136+ compartment_id = var. compartment_ocid
137+ display_name = " tfVcnForClusters"
138+ }
139+
140+ resource "oci_core_internet_gateway" "test_ig" {
141+ compartment_id = var. compartment_ocid
142+ display_name = " tfClusterInternetGateway"
143+ vcn_id = oci_core_vcn. test_vcn . id
144+ }
145+
146+ resource "oci_core_route_table" "test_route_table" {
147+ compartment_id = var. compartment_ocid
148+ vcn_id = oci_core_vcn. test_vcn . id
149+ display_name = " tfClustersRouteTable"
150+
151+ route_rules {
152+ destination = " 0.0.0.0/0"
153+ destination_type = " CIDR_BLOCK"
154+ network_entity_id = oci_core_internet_gateway. test_ig . id
155+ }
156+ }
157+
158+ resource "oci_core_subnet" "nodePool_Subnet_1" {
159+ # Required
160+ availability_domain = data. oci_identity_availability_domain . ad1 . name
161+ cidr_block = " 10.0.22.0/24"
162+ compartment_id = var. compartment_ocid
163+ vcn_id = oci_core_vcn. test_vcn . id
164+
165+ # Provider code tries to maintain compatibility with old versions.
166+ security_list_ids = [oci_core_vcn . test_vcn . default_security_list_id ]
167+ display_name = " tfSubNet1ForNodePool"
168+ route_table_id = oci_core_route_table. test_route_table . id
169+ }
170+
171+ resource "oci_core_subnet" "clusterSubnet_1" {
172+ # Required
173+ availability_domain = data. oci_identity_availability_domain . ad1 . name
174+ cidr_block = " 10.0.20.0/24"
175+ compartment_id = var. compartment_ocid
176+ vcn_id = oci_core_vcn. test_vcn . id
177+
178+ # Provider code tries to maintain compatibility with old versions.
179+ security_list_ids = [oci_core_vcn . test_vcn . default_security_list_id ]
180+ display_name = " tfSubNet1ForClusters"
181+ route_table_id = oci_core_route_table. test_route_table . id
182+ }
183+
184+ resource "oci_core_subnet" "clusterSubnet_2" {
185+ # Required
186+ availability_domain = data. oci_identity_availability_domain . ad2 . name
187+ cidr_block = " 10.0.21.0/24"
188+ compartment_id = var. compartment_ocid
189+ vcn_id = oci_core_vcn. test_vcn . id
190+ display_name = " tfSubNet1ForClusters"
191+
192+ # Provider code tries to maintain compatibility with old versions.
193+ security_list_ids = [oci_core_vcn . test_vcn . default_security_list_id ]
194+ route_table_id = oci_core_route_table. test_route_table . id
195+ }
196+
197+ resource "oci_containerengine_cluster" "test_cluster" {
198+ # Required
199+ compartment_id = var. compartment_ocid
200+ kubernetes_version = " v1.20.11"
201+ name = " tfTestCluster"
202+ vcn_id = oci_core_vcn. test_vcn . id
203+
204+ # Optional
205+ # defined_tags = map(oci_identity_tag_namespace.tag-namespace1.name.oci_identity_tag.tag1.name, var.cluster_defined_tags_value)
206+ defined_tags = {" ${ oci_identity_tag_namespace . tag-namespace1 . name } .${ oci_identity_tag . tag1 . name } " = " ${ var . cluster_defined_tags_value } " }
207+ freeform_tags = var. cluster_freeform_tags
208+ options {
209+ service_lb_subnet_ids = [oci_core_subnet . clusterSubnet_1 . id , oci_core_subnet . clusterSubnet_2 . id ]
210+
211+ # Optional
212+ add_ons {
213+ # Optional
214+ is_kubernetes_dashboard_enabled = " true"
215+ is_tiller_enabled = " true"
216+ }
217+
218+ admission_controller_options {
219+ # Optional
220+ is_pod_security_policy_enabled = true
221+ }
222+
223+ kubernetes_network_config {
224+ # Optional
225+ pods_cidr = " 10.1.0.0/16"
226+ services_cidr = " 10.2.0.0/16"
227+ }
228+
229+ persistent_volume_config {
230+
231+ # Optional
232+ # defined_tags = map(oci_identity_tag_namespace.tag-namespace1.name.oci_identity_tag.tag1.name, var.cluster_options_persistent_volume_config_defined_tags_value)
233+ defined_tags = {" ${ oci_identity_tag_namespace . tag-namespace1 . name } .${ oci_identity_tag . tag1 . name } " = " ${ var . cluster_defined_tags_value } " }
234+ freeform_tags = var. cluster_options_persistent_volume_config_freeform_tags
235+ }
236+ service_lb_config {
237+
238+ # Optional
239+ # defined_tags = map(oci_identity_tag_namespace.tag-namespace1.name.oci_identity_tag.tag1.name, var.cluster_options_service_lb_config_defined_tags_value)
240+ defined_tags = {" ${ oci_identity_tag_namespace . tag-namespace1 . name } .${ oci_identity_tag . tag1 . name } " = " ${ var . cluster_defined_tags_value } " }
241+ freeform_tags = var. cluster_options_service_lb_config_freeform_tags
242+ }
243+ }
244+ }
245+
246+ resource "oci_containerengine_node_pool" "test_node_pool" {
247+ # Required
248+ cluster_id = oci_containerengine_cluster. test_cluster . id
249+ compartment_id = var. compartment_ocid
250+ kubernetes_version = " v1.20.11"
251+ name = " tfPool"
252+ node_shape = " VM.Standard2.1"
253+
254+ # Optional
255+ defined_tags = {" ${ oci_identity_tag_namespace . tag-namespace1 . name } .${ oci_identity_tag . tag1 . name } " = " ${ var . node_pool_defined_tags_value } " }
256+ freeform_tags = var. node_pool_freeform_tags
257+ initial_node_labels {
258+ # Optional
259+ key = " key"
260+ value = " value"
261+ }
262+
263+ node_source_details {
264+ # Required
265+ image_id = local. image_id
266+ source_type = " IMAGE"
267+ }
268+
269+ node_config_details {
270+ # Required
271+ placement_configs {
272+ # Required
273+ availability_domain = data. oci_identity_availability_domain . ad1 . name
274+ subnet_id = oci_core_subnet. nodePool_Subnet_1 . id
275+ }
276+ size = var. node_pool_node_config_details_size
277+
278+ defined_tags = {" ${ oci_identity_tag_namespace . tag-namespace1 . name } .${ oci_identity_tag . tag1 . name } " = " ${ var . node_pool_defined_tags_value } " }
279+ freeform_tags = var. node_pool_node_config_details_freeform_tags
280+ }
281+
282+ ssh_public_key = var. node_pool_ssh_public_key
283+ }
284+
285+ output "node_pool" {
286+ value = {
287+ id = oci_containerengine_node_pool.test_node_pool.id
288+ kubernetes_version = oci_containerengine_node_pool.test_node_pool.kubernetes_version
289+ name = oci_containerengine_node_pool.test_node_pool.name
290+ subnet_ids = oci_containerengine_node_pool.test_node_pool.subnet_ids
291+ }
292+ }
293+
294+ data "oci_containerengine_node_pool_option" "test_node_pool_option" {
295+ node_pool_option_id = " all"
296+ }
297+
298+ data "oci_core_images" "shape_specific_images" {
299+ # Required
300+ compartment_id = var. tenancy_ocid
301+ shape = " VM.Standard2.1"
302+ }
303+
304+ locals {
305+ all_images = " ${ data . oci_core_images . shape_specific_images . images } "
306+ all_sources = " ${ data . oci_containerengine_node_pool_option . test_node_pool_option . sources } "
307+
308+ compartment_images = [for image in local . all_images : image . id if length (regexall (" Oracle-Linux-[0-9]*.[0-9]*-20[0-9]*" ,image. display_name )) > 0 ]
309+
310+ oracle_linux_images = [for source in local . all_sources : source . image_id if length (regexall (" Oracle-Linux-[0-9]*.[0-9]*-20[0-9]*" ,source. source_name )) > 0 ]
311+
312+ image_id = tolist (setintersection ( toset (local. compartment_images ), toset (local. oracle_linux_images )))[0 ]
313+
314+ }
0 commit comments