Skip to content

Commit dcc33fb

Browse files
tostiemeBBBmau
andauthored
support endPort on network policy resource (#2494)
* support endPort on network policy resource * add changelog + doc entry --------- Co-authored-by: Mauricio Alvarez Leon <[email protected]>
1 parent bfbf331 commit dcc33fb

File tree

6 files changed

+103
-0
lines changed

6 files changed

+103
-0
lines changed

.changelog/2494.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
`resource/resource_kubernetes_network_policy_v1`: add support for `end_port`
3+
```

docs/resources/network_policy.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ Optional:
227227

228228
- `port` (String) port represents the port on the given protocol. This can either be a numerical or named port on a pod. If this field is not provided, this matches all port names and numbers. If present, only traffic on the specified protocol AND port will be matched.
229229
- `protocol` (String) protocol represents the protocol (TCP, UDP, or SCTP) which traffic must match. If not specified, this field defaults to TCP.
230+
- `end_port` - (Optional) The end_port indicates that the range of ports from port to endPort if set, inclusive, should be allowed by the policy. Cannot be defined if port is undefined or if port is defined as a named (string) port.
230231

231232

232233

docs/resources/network_policy_v1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ Optional:
227227

228228
- `port` (String) port represents the port on the given protocol. This can either be a numerical or named port on a pod. If this field is not provided, this matches all port names and numbers. If present, only traffic on the specified protocol AND port will be matched.
229229
- `protocol` (String) protocol represents the protocol (TCP, UDP, or SCTP) which traffic must match. If not specified, this field defaults to TCP.
230+
- `end_port` - (Optional) The end_port indicates that the range of ports from port to endPort if set, inclusive, should be allowed by the policy. Cannot be defined if port is undefined or if port is defined as a named (string) port.
230231

231232

232233

@@ -287,6 +288,7 @@ resource "kubernetes_network_policy_v1" "example" {
287288
}
288289
```
289290

291+
290292
## Import
291293

292294
Network policies can be imported using their identifier consisting of `<namespace-name>/<network-policy-name>`, e.g.:

kubernetes/resource_kubernetes_network_policy_v1.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var (
2525
networkPolicyV1EgressRulePortsDoc = networking.NetworkPolicyEgressRule{}.SwaggerDoc()["ports"]
2626
networkPolicyV1EgressRuleToDoc = networking.NetworkPolicyEgressRule{}.SwaggerDoc()["to"]
2727
networkPolicyV1PortPortDoc = networking.NetworkPolicyPort{}.SwaggerDoc()["port"]
28+
networkPolicyV1PortEndPortDoc = networking.NetworkPolicyPort{}.SwaggerDoc()["endPort"]
2829
networkPolicyV1PortProtocolDoc = networking.NetworkPolicyPort{}.SwaggerDoc()["protocol"]
2930
networkPolicyV1PeerIpBlockDoc = networking.NetworkPolicyPeer{}.SwaggerDoc()["ipBlock"]
3031
ipBlockCidrDoc = networking.IPBlock{}.SwaggerDoc()["cidr"]
@@ -72,6 +73,11 @@ func resourceKubernetesNetworkPolicyV1() *schema.Resource {
7273
Description: networkPolicyV1PortPortDoc,
7374
Optional: true,
7475
},
76+
"end_port": {
77+
Type: schema.TypeInt,
78+
Description: networkPolicyV1PortEndPortDoc,
79+
Optional: true,
80+
},
7581
"protocol": {
7682
Type: schema.TypeString,
7783
Description: networkPolicyV1PortProtocolDoc,
@@ -149,6 +155,11 @@ func resourceKubernetesNetworkPolicyV1() *schema.Resource {
149155
Description: networkPolicyV1PortPortDoc,
150156
Optional: true,
151157
},
158+
"end_port": {
159+
Type: schema.TypeInt,
160+
Description: networkPolicyV1PortEndPortDoc,
161+
Optional: true,
162+
},
152163
"protocol": {
153164
Type: schema.TypeString,
154165
Description: networkPolicyV1PortProtocolDoc,

kubernetes/resource_kubernetes_network_policy_v1_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,41 @@ func TestAccKubernetesNetworkPolicyV1_basic(t *testing.T) {
108108
resource.TestCheckResourceAttr(resourceName, "spec.0.policy_types.0", "Ingress"),
109109
),
110110
},
111+
{
112+
Config: testAccKubernetesNetworkPolicyV1Config_endPorts(name),
113+
Check: resource.ComposeAggregateTestCheckFunc(
114+
testAccCheckKubernetesNetworkPolicyV1Exists(resourceName, &conf),
115+
resource.TestCheckResourceAttr(resourceName, "metadata.0.annotations.%", "0"),
116+
resource.TestCheckResourceAttr(resourceName, "metadata.0.labels.%", "0"),
117+
resource.TestCheckResourceAttr(resourceName, "metadata.0.name", name),
118+
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.generation"),
119+
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.resource_version"),
120+
resource.TestCheckResourceAttrSet(resourceName, "metadata.0.uid"),
121+
resource.TestCheckResourceAttr(resourceName, "spec.#", "1"),
122+
resource.TestCheckResourceAttr(resourceName, "spec.0.pod_selector.#", "1"),
123+
resource.TestCheckResourceAttr(resourceName, "spec.0.pod_selector.0.match_expressions.#", "1"),
124+
resource.TestCheckResourceAttr(resourceName, "spec.0.pod_selector.0.match_expressions.0.key", "name"),
125+
resource.TestCheckResourceAttr(resourceName, "spec.0.pod_selector.0.match_expressions.0.operator", "In"),
126+
resource.TestCheckResourceAttr(resourceName, "spec.0.pod_selector.0.match_expressions.0.values.#", "2"),
127+
resource.TestCheckResourceAttr(resourceName, "spec.0.pod_selector.0.match_expressions.0.values.1", "webfront"),
128+
resource.TestCheckResourceAttr(resourceName, "spec.0.pod_selector.0.match_expressions.0.values.0", "api"),
129+
resource.TestCheckResourceAttr(resourceName, "spec.0.ingress.#", "1"),
130+
resource.TestCheckResourceAttr(resourceName, "spec.0.ingress.0.ports.#", "1"),
131+
resource.TestCheckResourceAttr(resourceName, "spec.0.ingress.0.ports.0.port", "8126"),
132+
resource.TestCheckResourceAttr(resourceName, "spec.0.ingress.0.ports.0.protocol", "TCP"),
133+
resource.TestCheckResourceAttr(resourceName, "spec.0.ingress.0.ports.0.end_port", "9000"),
134+
resource.TestCheckResourceAttr(resourceName, "spec.0.ingress.0.from.#", "1"),
135+
resource.TestCheckResourceAttr(resourceName, "spec.0.ingress.0.from.0.namespace_selector.#", "1"),
136+
resource.TestCheckResourceAttr(resourceName, "spec.0.ingress.0.from.0.namespace_selector.0.match_labels.name", "default"),
137+
resource.TestCheckResourceAttr(resourceName, "spec.0.ingress.0.from.0.pod_selector.#", "0"),
138+
resource.TestCheckResourceAttr(resourceName, "spec.0.egress.0.ports.#", "1"),
139+
resource.TestCheckResourceAttr(resourceName, "spec.0.egress.0.ports.0.port", "10000"),
140+
resource.TestCheckResourceAttr(resourceName, "spec.0.egress.0.ports.0.protocol", "TCP"),
141+
resource.TestCheckResourceAttr(resourceName, "spec.0.egress.0.ports.0.end_port", "65535"),
142+
resource.TestCheckResourceAttr(resourceName, "spec.0.policy_types.#", "1"),
143+
resource.TestCheckResourceAttr(resourceName, "spec.0.policy_types.0", "Ingress"),
144+
),
145+
},
111146
{
112147
Config: testAccKubernetesNetworkPolicyV1Config_specModified_allow_all_namespaces(name),
113148
Check: resource.ComposeAggregateTestCheckFunc(
@@ -482,6 +517,50 @@ func testAccKubernetesNetworkPolicyV1Config_specModified(name string) string {
482517
`, name)
483518
}
484519

520+
func testAccKubernetesNetworkPolicyV1Config_endPorts(name string) string {
521+
return fmt.Sprintf(`resource "kubernetes_network_policy_v1" "test" {
522+
metadata {
523+
name = "%s"
524+
namespace = "default"
525+
}
526+
527+
spec {
528+
pod_selector {
529+
match_expressions {
530+
key = "name"
531+
operator = "In"
532+
values = ["webfront", "api"]
533+
}
534+
}
535+
536+
ingress {
537+
ports {
538+
port = "8126"
539+
protocol = "TCP"
540+
end_port = "9000"
541+
}
542+
543+
from {
544+
namespace_selector {
545+
match_labels = {
546+
name = "default"
547+
}
548+
}
549+
}
550+
}
551+
egress {
552+
ports {
553+
port = "10000"
554+
protocol = "TCP"
555+
end_port = "65535"
556+
}
557+
}
558+
policy_types = ["Ingress"]
559+
}
560+
}
561+
`, name)
562+
}
563+
485564
func testAccKubernetesNetworkPolicyV1Config_specModified_allow_all_namespaces(name string) string {
486565
return fmt.Sprintf(`resource "kubernetes_network_policy_v1" "test" {
487566
metadata {

kubernetes/structure_network_policy.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
corev1 "k8s.io/api/core/v1"
1111
networkingv1 "k8s.io/api/networking/v1"
1212
"k8s.io/apimachinery/pkg/util/intstr"
13+
"k8s.io/utils/ptr"
1314
)
1415

1516
// Flatteners
@@ -66,6 +67,9 @@ func flattenNetworkPolicyV1Ports(in []networkingv1.NetworkPolicyPort) []interfac
6667
if port.Port != nil {
6768
m["port"] = port.Port.String()
6869
}
70+
if port.EndPort != nil && *port.EndPort != 0 {
71+
m["end_port"] = int(*port.EndPort)
72+
}
6973
if port.Protocol != nil {
7074
m["protocol"] = string(*port.Protocol)
7175
}
@@ -198,6 +202,9 @@ func expandNetworkPolicyV1Ports(l []interface{}) *[]networkingv1.NetworkPolicyPo
198202
val := intstr.Parse(v)
199203
policyPorts[i].Port = &val
200204
}
205+
if v, ok := in["end_port"].(int); ok && v != 0 {
206+
policyPorts[i].EndPort = ptr.To(int32(v))
207+
}
201208
if in["protocol"] != nil && in["protocol"] != "" {
202209
v := corev1.Protocol(in["protocol"].(string))
203210
policyPorts[i].Protocol = &v

0 commit comments

Comments
 (0)