Skip to content

Commit 318dc10

Browse files
authored
test: replace landing zone vpc in examples and fix ingress (#206)
1 parent d517dcc commit 318dc10

File tree

14 files changed

+337
-722
lines changed

14 files changed

+337
-722
lines changed

cra-tf-validate-ignore-rules.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
{
22
"scc_rules": [
3-
43
{
54
"scc_rule_id": "rule-ded212fe-7def-44ce-9480-0487067b64c4",
6-
"description:": "Check whether Kubernetes Service is accessible only by using private endpoints",
5+
"description": "Check whether Kubernetes Service is accessible only by using private endpoints",
76
"ignore_reason": "This is a valid issue - tracking in https://github.ibm.com/GoldenEye/issues/issues/174",
87
"is_valid": true
8+
},
9+
{
10+
"scc_rule_id": "rule-216e2449-27d7-4afc-929a-b66e196a9cf9",
11+
"description": "Check whether Flow Logs for VPC are enabled",
12+
"ignore_reason": "Rule is related to VPC, not OCP Clusters",
13+
"is_valid": true
14+
},
15+
{
16+
"scc_rule_id": "rule-64c0bea0-8760-4a6b-a56c-ee375a48961e",
17+
"description": "Check whether Virtual Private Cloud (VPC) has no public gateways attached",
18+
"ignore_reason": "Rule is related to VPC, not OCP Clusters",
19+
"is_valid": true
920
}
1021
]
1122
}

examples/add_rules_to_sg/main.tf

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,57 @@ module "resource_group" {
1010
existing_resource_group_name = var.resource_group
1111
}
1212

13-
###############################################################################
14-
# VPC
15-
###############################################################################
16-
17-
module "vpc" {
18-
source = "terraform-ibm-modules/landing-zone-vpc/ibm"
19-
version = "7.4.0"
20-
resource_group_id = module.resource_group.resource_group_id
21-
region = var.region
22-
prefix = var.prefix
23-
tags = var.resource_tags
24-
name = var.vpc_name
25-
address_prefixes = var.addresses
26-
subnets = var.subnets
27-
use_public_gateways = var.public_gateway
13+
##############################################################################
14+
# Create a VPC with single subnet and zone, and public gateway
15+
# NOTE: this is a very simple VPC/Subnet configuration for example purposes only,
16+
# that will allow all traffic ingress/egress by default.
17+
# For production use cases this would need to be enhanced by adding more subnets
18+
# and zones for resiliency, and ACLs/Security Groups for network security.
19+
##############################################################################
20+
21+
resource "ibm_is_vpc" "vpc" {
22+
name = "${var.prefix}-vpc"
23+
resource_group = module.resource_group.resource_group_id
24+
address_prefix_management = "auto"
25+
tags = var.resource_tags
2826
}
2927

28+
resource "ibm_is_public_gateway" "gateway" {
29+
name = "${var.prefix}-gateway-1"
30+
vpc = ibm_is_vpc.vpc.id
31+
resource_group = module.resource_group.resource_group_id
32+
zone = "${var.region}-1"
33+
}
34+
35+
resource "ibm_is_subnet" "subnet_zone_1" {
36+
name = "${var.prefix}-subnet-1"
37+
vpc = ibm_is_vpc.vpc.id
38+
resource_group = module.resource_group.resource_group_id
39+
zone = "${var.region}-1"
40+
total_ipv4_address_count = 256
41+
public_gateway = ibm_is_public_gateway.gateway.id
42+
}
3043

3144
##############################################################################
3245
# Security Group Rules addition.
3346
##############################################################################
3447

48+
locals {
49+
standard_cluster_allow_rules = [
50+
{ name = "allow-port-8080", direction = "inbound", tcp = { port_max = 8080, port_min = 8080 }, udp = null, icmp = null, remote = ibm_is_subnet.subnet_zone_1.ipv4_cidr_block },
51+
{ name = "allow-port-443", direction = "inbound", tcp = { port_max = 443, port_min = 443 }, udp = null, icmp = null, remote = ibm_is_subnet.subnet_zone_1.ipv4_cidr_block },
52+
{ name = "udp-range", direction = "inbound", udp = { port_max = 30103, port_min = 30103 }, tcp = null, icmp = null, remote = ibm_is_subnet.subnet_zone_1.ipv4_cidr_block },
53+
]
54+
}
55+
3556
# Kube-<vpc id> Security Group
3657
data "ibm_is_security_group" "kube_vpc_sg" {
3758
name = "kube-${module.ocp_base.vpc_id}"
3859
}
3960

4061
resource "ibm_is_security_group_rule" "kube_vpc_rules" {
4162

42-
for_each = { for rule in var.sg_rules_vpc : rule.name => rule }
63+
for_each = { for rule in local.standard_cluster_allow_rules : rule.name => rule }
4364
group = data.ibm_is_security_group.kube_vpc_sg.id
4465
direction = each.value.direction
4566
remote = each.value.remote
@@ -76,7 +97,7 @@ data "ibm_is_security_group" "kube_cluster_sg" {
7697

7798
resource "ibm_is_security_group_rule" "kube_cluster_rules" {
7899

79-
for_each = { for rule in var.sg_rules_cluster : rule.name => rule }
100+
for_each = { for rule in local.standard_cluster_allow_rules : rule.name => rule }
80101
group = data.ibm_is_security_group.kube_cluster_sg.id
81102
direction = each.value.direction
82103
remote = each.value.remote
@@ -124,16 +145,39 @@ module "kp_all_inclusive" {
124145
# Base OCP Cluster
125146
##############################################################################
126147

148+
locals {
149+
cluster_vpc_subnets = {
150+
default = [
151+
{
152+
id = ibm_is_subnet.subnet_zone_1.id
153+
cidr_block = ibm_is_subnet.subnet_zone_1.ipv4_cidr_block
154+
zone = ibm_is_subnet.subnet_zone_1.zone
155+
}
156+
]
157+
}
158+
159+
worker_pools = [
160+
{
161+
subnet_prefix = "default"
162+
pool_name = "default" # ibm_container_vpc_cluster automatically names standard pool "standard" (See https://github.com/IBM-Cloud/terraform-provider-ibm/issues/2849)
163+
machine_type = "bx2.4x16"
164+
workers_per_zone = 2
165+
labels = {}
166+
resource_group_id = module.resource_group.resource_group_id
167+
}
168+
]
169+
}
170+
127171
module "ocp_base" {
128172
source = "../.."
129173
cluster_name = var.prefix
130174
ibmcloud_api_key = var.ibmcloud_api_key
131175
resource_group_id = module.resource_group.resource_group_id
132176
region = var.region
133177
force_delete_storage = true
134-
vpc_id = module.vpc.vpc_id
135-
vpc_subnets = module.vpc.subnet_detail_map
136-
worker_pools = var.worker_pools
178+
vpc_id = ibm_is_vpc.vpc.id
179+
vpc_subnets = local.cluster_vpc_subnets
180+
worker_pools = local.worker_pools
137181
ocp_version = var.ocp_version
138182
tags = var.resource_tags
139183
kms_config = {

examples/add_rules_to_sg/variables.tf

Lines changed: 0 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -41,224 +41,3 @@ variable "ocp_version" {
4141
description = "Version of the OCP cluster to provision"
4242
default = null
4343
}
44-
45-
variable "worker_pools" {
46-
type = list(object({
47-
subnet_prefix = string
48-
pool_name = string
49-
machine_type = string
50-
workers_per_zone = number
51-
resource_group_id = optional(string)
52-
labels = optional(map(string))
53-
}))
54-
description = "List of worker pools."
55-
default = [
56-
{
57-
subnet_prefix = "zone-1"
58-
pool_name = "default" # ibm_container_vpc_cluster automatically names standard pool "standard" (See https://github.com/IBM-Cloud/terraform-provider-ibm/issues/2849)
59-
machine_type = "bx2.4x16"
60-
workers_per_zone = 2
61-
},
62-
{
63-
subnet_prefix = "zone-2"
64-
pool_name = "zone-2"
65-
machine_type = "bx2.4x16"
66-
workers_per_zone = 2
67-
}
68-
]
69-
}
70-
71-
##############################################################################
72-
# VPC variables
73-
##############################################################################
74-
75-
variable "vpc_name" {
76-
type = string
77-
description = "Name of the VPC"
78-
default = "management"
79-
}
80-
81-
variable "public_gateway" {
82-
description = "Create a public gateway in any of the three zones with `true`."
83-
type = object({
84-
zone-1 = optional(bool)
85-
zone-2 = optional(bool)
86-
zone-3 = optional(bool)
87-
})
88-
default = {
89-
zone-1 = true
90-
zone-2 = false
91-
zone-3 = false
92-
}
93-
}
94-
95-
variable "addresses" {
96-
description = "OPTIONAL - IP range that will be defined for the VPC for a certain location. Use only with manual address prefixes"
97-
type = object({
98-
zone-1 = optional(list(string))
99-
zone-2 = optional(list(string))
100-
zone-3 = optional(list(string))
101-
})
102-
default = {
103-
zone-1 = ["10.10.10.0/24"]
104-
zone-2 = ["10.20.10.0/24"]
105-
zone-3 = ["10.30.10.0/24"]
106-
}
107-
}
108-
109-
variable "subnets" {
110-
description = "List of subnets for the vpc. For each item in each array, a subnet will be created. Items can be either CIDR blocks or total ipv4 addressess. Public gateways will be enabled only in zones where a gateway has been created"
111-
type = object({
112-
zone-1 = list(object({
113-
acl_name = string
114-
name = string
115-
cidr = string
116-
public_gateway = optional(bool)
117-
}))
118-
zone-2 = list(object({
119-
acl_name = string
120-
name = string
121-
cidr = string
122-
public_gateway = optional(bool)
123-
}))
124-
zone-3 = list(object({
125-
acl_name = string
126-
name = string
127-
cidr = string
128-
public_gateway = optional(bool)
129-
}))
130-
})
131-
132-
default = {
133-
zone-1 = [
134-
{
135-
acl_name = "vpc-acl"
136-
name = "zone-1"
137-
cidr = "10.10.10.0/24"
138-
}
139-
],
140-
zone-2 = [
141-
{
142-
acl_name = "vpc-acl"
143-
name = "zone-2"
144-
cidr = "10.20.10.0/24"
145-
}
146-
],
147-
zone-3 = [
148-
{
149-
acl_name = "vpc-acl"
150-
name = "zone-3"
151-
cidr = "10.30.10.0/24"
152-
}
153-
]
154-
}
155-
}
156-
157-
##############################################################################
158-
# Security Groups
159-
##############################################################################
160-
161-
variable "sg_rules_vpc" {
162-
description = "List of security group rules to be added to the kube-<vpcid> security group"
163-
164-
default = [
165-
{ name = "allow-port-8080", direction = "inbound", tcp = { port_max = 8080, port_min = 8080 }, remote = "10.10.10.0/24" },
166-
{ name = "allow-port-443", direction = "inbound", tcp = { port_max = 443, port_min = 443 }, remote = "10.10.10.0/24" },
167-
{ name = "udp-range", direction = "inbound", udp = { port_max = 30103, port_min = 30103 }, remote = "10.10.10.0/24" },
168-
]
169-
170-
type = list(
171-
object({
172-
name = string
173-
direction = string
174-
remote = string
175-
tcp = optional(
176-
object({
177-
port_max = optional(number)
178-
port_min = optional(number)
179-
})
180-
)
181-
udp = optional(
182-
object({
183-
port_max = optional(number)
184-
port_min = optional(number)
185-
})
186-
)
187-
icmp = optional(
188-
object({
189-
type = optional(number)
190-
code = optional(number)
191-
})
192-
)
193-
})
194-
)
195-
196-
validation {
197-
error_message = "Security group rule direction can only be `inbound` or `outbound`."
198-
condition = (var.sg_rules_vpc == null || length(var.sg_rules_vpc) == 0) ? true : length(distinct(
199-
# Return false if direction is not valid
200-
flatten([for rule in var.sg_rules_vpc : false if !contains(["inbound", "outbound"], rule.direction)])
201-
)) == 0
202-
}
203-
204-
validation {
205-
error_message = "Security group rule names must match the regex pattern ^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$."
206-
condition = (var.sg_rules_vpc == null || length(var.sg_rules_vpc) == 0) ? true : length(distinct(
207-
# Return false if rule name is not valid
208-
flatten([for rule in var.sg_rules_vpc : false if !can(regex("^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$", rule.name))])
209-
)) == 0
210-
}
211-
}
212-
213-
variable "sg_rules_cluster" {
214-
description = "List of security group rules to be added to the kube-<clusterid> security group"
215-
216-
default = [
217-
{ name = "allow-port-8080", direction = "inbound", tcp = { port_max = 8080, port_min = 8080 }, remote = "10.10.10.0/24" },
218-
{ name = "allow-port-443", direction = "inbound", tcp = { port_max = 443, port_min = 443 }, remote = "10.10.10.0/24" },
219-
{ name = "udp-range", direction = "inbound", udp = { port_max = 30103, port_min = 30103 }, remote = "10.10.10.0/24" },
220-
]
221-
222-
type = list(
223-
object({
224-
name = string
225-
direction = string
226-
remote = string
227-
tcp = optional(
228-
object({
229-
port_max = optional(number)
230-
port_min = optional(number)
231-
})
232-
)
233-
udp = optional(
234-
object({
235-
port_max = optional(number)
236-
port_min = optional(number)
237-
})
238-
)
239-
icmp = optional(
240-
object({
241-
type = optional(number)
242-
code = optional(number)
243-
})
244-
)
245-
})
246-
)
247-
248-
validation {
249-
error_message = "Security group rule direction can only be `inbound` or `outbound`."
250-
condition = (var.sg_rules_cluster == null || length(var.sg_rules_cluster) == 0) ? true : length(distinct(
251-
# Return false if direction is not valid
252-
flatten([for rule in var.sg_rules_cluster : false if !contains(["inbound", "outbound"], rule.direction)])
253-
)) == 0
254-
}
255-
256-
validation {
257-
error_message = "Security group rule names must match the regex pattern ^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$."
258-
condition = (var.sg_rules_cluster == null || length(var.sg_rules_cluster) == 0) ? true : length(distinct(
259-
# Return false if rule name is not valid
260-
flatten([for rule in var.sg_rules_cluster : false if !can(regex("^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$", rule.name))])
261-
)) == 0
262-
}
263-
}
264-
##############################################################################

0 commit comments

Comments
 (0)