Skip to content

Commit 34222ae

Browse files
authored
Merge pull request #87 from worldcoin/feat/add-vpc-cni-network-policy-support
INFRA-6052: Add VPC CNI Network Policy support
2 parents ccc789f + f5b07d7 commit 34222ae

File tree

4 files changed

+86
-12
lines changed

4 files changed

+86
-12
lines changed

cluster-addons.tf

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,27 @@ resource "aws_eks_addon" "vpc_cni" {
7373
addon_version = var.vpc_cni_version_override == "" ? local.vpc_cni_version[var.cluster_version] : var.vpc_cni_version_override
7474
resolve_conflicts_on_create = "OVERWRITE"
7575
resolve_conflicts_on_update = "OVERWRITE"
76-
configuration_values = jsonencode({
77-
env : merge({
78-
ENABLE_PREFIX_DELEGATION : lower(tostring(var.vpc_cni_enable_prefix_delegation)), # Enable prefix delegation for IPv6, allocate IPs in /28 blocks (instead of all at once)
79-
WARM_IP_TARGET : var.vpc_cni_warm_ip_target, # Keep +4 IPs warm for each node to speed up pod scheduling
80-
WARM_ENI_TARGET : var.vpc_cni_warm_eni_target, # Keep +1 ENI warm for each node to speed up pod scheduling
81-
POD_SECURITY_GROUP_ENFORCING_MODE : lower(tostring(var.vpc_cni_pod_security_group_enforcing_mode)), # Enable pod security group enforcing mode
82-
AWS_VPC_K8S_CNI_EXTERNALSNAT : lower(tostring(var.vpc_cni_external_snat)), # Enable external SNAT to enable pod to pod communication across different vpc's
83-
}, var.vpc_cni_enable_pod_eni ? {
84-
ENABLE_POD_ENI : lower(tostring(var.vpc_cni_enable_pod_eni)), # Enable pod ENI support
85-
} : {})
86-
})
76+
configuration_values = jsonencode(merge(
77+
{
78+
env : merge({
79+
ENABLE_PREFIX_DELEGATION : lower(tostring(var.vpc_cni_enable_prefix_delegation)), # Enable prefix delegation for IPv6, allocate IPs in /28 blocks (instead of all at once)
80+
WARM_IP_TARGET : var.vpc_cni_warm_ip_target, # Keep +4 IPs warm for each node to speed up pod scheduling
81+
WARM_ENI_TARGET : var.vpc_cni_warm_eni_target, # Keep +1 ENI warm for each node to speed up pod scheduling
82+
POD_SECURITY_GROUP_ENFORCING_MODE : lower(tostring(var.vpc_cni_pod_security_group_enforcing_mode)), # Enable pod security group enforcing mode
83+
AWS_VPC_K8S_CNI_EXTERNALSNAT : lower(tostring(var.vpc_cni_external_snat)), # Enable external SNAT to enable pod to pod communication across different vpc's
84+
}, var.vpc_cni_enable_pod_eni ? {
85+
ENABLE_POD_ENI : lower(tostring(var.vpc_cni_enable_pod_eni)), # Enable pod ENI support
86+
} : {})
87+
},
88+
var.vpc_cni_enable_network_policy ? {
89+
enableNetworkPolicy : lower(tostring(var.vpc_cni_enable_network_policy))
90+
} : {},
91+
var.vpc_cni_enable_network_policy ? {
92+
nodeAgent : {
93+
enablePolicyEventLogs : lower(tostring(var.vpc_cni_enable_network_policy))
94+
}
95+
} : {}
96+
))
8797
}
8898

8999
resource "aws_eks_addon" "coredns" {

main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@ resource "aws_iam_role_policy_attachment" "aws_load_balancer_controller_explicit
9494
role = each.value
9595
policy_arn = aws_iam_policy.aws_load_balancer_controller_explicit_deny[0].arn
9696
}
97-
97+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Mock (offline) provider
2+
mock_provider "aws" {
3+
source = "./tests/mocks/aws"
4+
}
5+
6+
mock_provider "datadog" {}
7+
mock_provider "cloudflare" {}
8+
9+
mock_provider "kubernetes" {
10+
source = "./tests/mocks/kubernetes"
11+
}
12+
13+
variables {
14+
kubernetes_provider_enabled = false
15+
}
16+
17+
# =============================================================================
18+
# Test: Network policy disabled by default
19+
# =============================================================================
20+
run "vpc_cni_network_policy_disabled_by_default" {
21+
command = plan
22+
23+
assert {
24+
condition = var.vpc_cni_enable_network_policy == false
25+
error_message = "vpc_cni_enable_network_policy should default to false"
26+
}
27+
28+
assert {
29+
condition = !contains(keys(jsondecode(aws_eks_addon.vpc_cni.configuration_values)), "enableNetworkPolicy")
30+
error_message = "enableNetworkPolicy should not be present when disabled"
31+
}
32+
33+
assert {
34+
condition = !contains(keys(jsondecode(aws_eks_addon.vpc_cni.configuration_values)), "nodeAgent")
35+
error_message = "nodeAgent should not be present when network policy is disabled"
36+
}
37+
}
38+
39+
# =============================================================================
40+
# Test: Network policy enabled
41+
# =============================================================================
42+
run "vpc_cni_network_policy_enabled" {
43+
command = plan
44+
45+
variables {
46+
vpc_cni_enable_network_policy = true
47+
}
48+
49+
assert {
50+
condition = jsondecode(aws_eks_addon.vpc_cni.configuration_values)["enableNetworkPolicy"] == "true"
51+
error_message = "enableNetworkPolicy should be 'true' when enabled"
52+
}
53+
54+
assert {
55+
condition = jsondecode(aws_eks_addon.vpc_cni.configuration_values)["nodeAgent"]["enablePolicyEventLogs"] == "true"
56+
error_message = "nodeAgent.enablePolicyEventLogs should be 'true' when network policy is enabled"
57+
}
58+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,12 @@ variable "vpc_cni_pod_security_group_enforcing_mode" {
726726
}
727727
}
728728

729+
variable "vpc_cni_enable_network_policy" {
730+
description = "Enable Kubernetes NetworkPolicy enforcement via the VPC CNI node agent"
731+
type = bool
732+
default = false
733+
}
734+
729735
variable "vpc_cni_external_snat" {
730736
description = "Needed to enable cross-vpc pod-to-pod communication - see: https://github.com/aws/amazon-vpc-cni-k8s?tab=readme-ov-file#aws_vpc_k8s_cni_externalsnat"
731737
type = string

0 commit comments

Comments
 (0)