Skip to content

Commit 174389c

Browse files
committed
feat: add terraform for oci oke
terraform to provision an OKE cluster on OCI
1 parent 50a1137 commit 174389c

File tree

7 files changed

+304
-0
lines changed

7 files changed

+304
-0
lines changed

terraform/oci-oke-cluster/data.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
data "oci_identity_availability_domains" "availability_domains" {
2+
#Required
3+
compartment_id = var.tenancy_ocid
4+
}
5+
6+
data "oci_core_images" "node_pool_images" {
7+
compartment_id = var.compartment_ocid
8+
operating_system = "Oracle Linux"
9+
operating_system_version = "8"
10+
shape = var.node_shape
11+
sort_by = "TIMECREATED"
12+
sort_order = "DESC"
13+
}
14+
15+
data "oci_containerengine_cluster_kube_config" "cluster_kube_config" {
16+
#Required
17+
cluster_id = oci_containerengine_cluster.cluster.id
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
locals {
2+
common_labels = {
3+
"TalosCluster" = var.cluster_name
4+
}
5+
}

terraform/oci-oke-cluster/main.tf

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
resource "oci_containerengine_cluster" "cluster" {
2+
#Required
3+
compartment_id = var.compartment_ocid
4+
kubernetes_version = var.cluster_kubernetes_version
5+
name = var.cluster_name
6+
vcn_id = oci_core_vcn.vcn.id
7+
8+
endpoint_config {
9+
10+
#Optional
11+
is_public_ip_enabled = true
12+
nsg_ids = [oci_core_network_security_group.network_security_group.id]
13+
subnet_id = oci_core_subnet.subnet.id
14+
}
15+
options {
16+
17+
#Optional
18+
add_ons {
19+
20+
#Optional
21+
is_kubernetes_dashboard_enabled = false
22+
is_tiller_enabled = false
23+
}
24+
admission_controller_options {
25+
26+
#Optional
27+
is_pod_security_policy_enabled = false
28+
}
29+
kubernetes_network_config {
30+
31+
#Optional
32+
pods_cidr = var.pod_subnet_block
33+
services_cidr = var.service_subnet_block
34+
}
35+
persistent_volume_config {
36+
37+
#Optional
38+
freeform_tags = local.common_labels
39+
}
40+
service_lb_config {
41+
42+
#Optional
43+
freeform_tags = local.common_labels
44+
}
45+
service_lb_subnet_ids = [oci_core_subnet.subnet.id]
46+
}
47+
type = "ENHANCED_CLUSTER"
48+
}
49+
50+
resource "oci_containerengine_node_pool" "node_pool" {
51+
#Required
52+
cluster_id = oci_containerengine_cluster.cluster.id
53+
compartment_id = var.compartment_ocid
54+
name = "${var.cluster_name}-primary"
55+
node_shape = var.node_shape
56+
57+
#Optional
58+
freeform_tags = local.common_labels
59+
kubernetes_version = var.cluster_kubernetes_version
60+
node_config_details {
61+
#Required
62+
placement_configs {
63+
#Required
64+
availability_domain = data.oci_identity_availability_domains.availability_domains.availability_domains[0].name
65+
subnet_id = oci_core_subnet.node_subnet.id
66+
}
67+
size = var.node_pool_count
68+
69+
freeform_tags = local.common_labels
70+
nsg_ids = [oci_core_network_security_group.network_security_group.id]
71+
}
72+
node_shape_config {
73+
#Optional
74+
memory_in_gbs = var.node_memory_in_gbs
75+
ocpus = var.node_ocpus
76+
}
77+
node_source_details {
78+
#Required
79+
image_id = data.oci_core_images.node_pool_images.images[0].id
80+
source_type = "IMAGE"
81+
}
82+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
resource "oci_core_vcn" "vcn" {
2+
#Required
3+
compartment_id = var.compartment_ocid
4+
5+
#Optional
6+
cidr_blocks = var.cidr_blocks
7+
display_name = "${var.cluster_name}-vcn"
8+
freeform_tags = local.common_labels
9+
is_ipv6enabled = true
10+
}
11+
resource "oci_core_subnet" "subnet" {
12+
#Required
13+
cidr_block = var.subnet_block
14+
compartment_id = var.compartment_ocid
15+
vcn_id = oci_core_vcn.vcn.id
16+
prohibit_internet_ingress = false
17+
prohibit_public_ip_on_vnic = false
18+
19+
#Optional
20+
display_name = "${var.cluster_name}-subnet"
21+
freeform_tags = local.common_labels
22+
security_list_ids = [oci_core_security_list.security_list.id]
23+
route_table_id = oci_core_route_table.route_table.id
24+
}
25+
resource "oci_core_subnet" "node_subnet" {
26+
#Required
27+
cidr_block = var.node_subnet_block
28+
compartment_id = var.compartment_ocid
29+
vcn_id = oci_core_vcn.vcn.id
30+
prohibit_internet_ingress = false
31+
prohibit_public_ip_on_vnic = false
32+
33+
#Optional
34+
display_name = "${var.cluster_name}-subnet"
35+
freeform_tags = local.common_labels
36+
security_list_ids = [oci_core_security_list.security_list.id]
37+
route_table_id = oci_core_route_table.route_table.id
38+
}
39+
resource "oci_core_route_table" "route_table" {
40+
#Required
41+
compartment_id = var.compartment_ocid
42+
vcn_id = oci_core_vcn.vcn.id
43+
44+
#Optional
45+
display_name = "${var.cluster_name}-route-table"
46+
freeform_tags = local.common_labels
47+
route_rules {
48+
#Required
49+
network_entity_id = oci_core_internet_gateway.internet_gateway.id
50+
51+
#Optional
52+
destination_type = "CIDR_BLOCK"
53+
destination = "0.0.0.0/0"
54+
}
55+
}
56+
57+
resource "oci_core_internet_gateway" "internet_gateway" {
58+
#Required
59+
compartment_id = var.compartment_ocid
60+
vcn_id = oci_core_vcn.vcn.id
61+
62+
#Optional
63+
enabled = true
64+
display_name = "${var.cluster_name}-internet-gateway"
65+
freeform_tags = local.common_labels
66+
}
67+
68+
resource "oci_core_network_security_group" "network_security_group" {
69+
#Required
70+
compartment_id = var.compartment_ocid
71+
vcn_id = oci_core_vcn.vcn.id
72+
73+
#Optional
74+
display_name = "${var.cluster_name}-security-group"
75+
freeform_tags = local.common_labels
76+
}
77+
resource "oci_core_network_security_group_security_rule" "allow_all" {
78+
network_security_group_id = oci_core_network_security_group.network_security_group.id
79+
destination_type = "CIDR_BLOCK"
80+
destination = "0.0.0.0/0"
81+
protocol = "all"
82+
direction = "EGRESS"
83+
stateless = false
84+
}
85+
86+
resource "oci_core_security_list" "security_list" {
87+
#Required
88+
compartment_id = var.compartment_ocid
89+
vcn_id = oci_core_vcn.vcn.id
90+
91+
#Optional
92+
display_name = "${var.cluster_name}-security-list"
93+
egress_security_rules {
94+
#Required
95+
destination = "0.0.0.0/0"
96+
protocol = "all"
97+
98+
stateless = true
99+
}
100+
freeform_tags = local.common_labels
101+
ingress_security_rules {
102+
#Required
103+
source = "0.0.0.0/0"
104+
protocol = "all"
105+
106+
stateless = true
107+
}
108+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "kubeconfig" {
2+
value = data.oci_containerengine_cluster_kube_config.cluster_kube_config.content
3+
sensitive = true
4+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
variable "compartment_ocid" {
2+
type = string
3+
sensitive = true
4+
}
5+
variable "tenancy_ocid" {
6+
type = string
7+
sensitive = true
8+
}
9+
variable "user_ocid" {
10+
type = string
11+
sensitive = true
12+
}
13+
variable "fingerprint" {
14+
type = string
15+
sensitive = true
16+
}
17+
variable "private_key_path" {
18+
type = string
19+
default = "~/.oci/oci_main_terraform.pem"
20+
sensitive = true
21+
}
22+
variable "region" {
23+
description = "the OCI region where resources will be created"
24+
type = string
25+
default = null
26+
}
27+
variable "cluster_name" {
28+
type = string
29+
default = "cncfoke"
30+
}
31+
variable "cluster_kubernetes_version" {
32+
type = string
33+
default = "v1.30.1"
34+
}
35+
variable "cidr_blocks" {
36+
type = set(string)
37+
default = ["10.0.0.0/16"]
38+
}
39+
variable "subnet_block" {
40+
type = string
41+
default = "10.0.0.0/24"
42+
}
43+
variable "pod_subnet_block" {
44+
type = string
45+
default = "10.32.0.0/12"
46+
}
47+
variable "service_subnet_block" {
48+
type = string
49+
default = "10.200.0.0/21"
50+
}
51+
variable "node_subnet_block" {
52+
type = string
53+
default = "10.0.7.0/24"
54+
}
55+
variable "node_shape" {
56+
type = string
57+
default = "VM.Standard.A1.Flex"
58+
}
59+
variable "node_memory_in_gbs" {
60+
type = number
61+
default = 128
62+
}
63+
variable "node_ocpus" {
64+
type = number
65+
default = 8
66+
}
67+
variable "node_pool_count" {
68+
type = number
69+
default = 3
70+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
terraform {
2+
required_providers {
3+
oci = {
4+
source = "oracle/oci"
5+
version = "6.7.0" # TODO include version in project root providers
6+
}
7+
}
8+
required_version = ">= 1.2"
9+
}
10+
11+
provider "oci" {
12+
tenancy_ocid = var.tenancy_ocid
13+
user_ocid = var.user_ocid
14+
private_key_path = var.private_key_path
15+
fingerprint = var.fingerprint
16+
region = var.region
17+
}

0 commit comments

Comments
 (0)