lib/IO/K8s/Role/NetworkPolicy.pm

filter: Statementstmt 93.3% 154 / 165 Branchbran 70.8% 51 / 72 Conditioncond 60.8% 14 / 23 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 95.2% 20 / 21 Podpod 100% 9 / 9 totaltotal 84.1% 254 / 302 CC 49 SCAR 14.6
CC
49
Cov
84%
_core_ports
12.0
_add_core_ingress_rule_multi
5.9
_add_core_egress_rule_multi
5.6
CRAP 4.3
« lib/IO/K8s/Role/CertManaged.pm ↑ Summary lib/IO/K8s/Role/Loadbalanced.pm »
1 package IO::K8s::Role::NetworkPolicy;
2 # ABSTRACT: Role for building network policies (core K8s and Cilium)
3 23 our $VERSION = '1.112';
4 23 ▶use IO::K8s::Types::Net qw( cidr_contains );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # Imports above `use Moo::Role` on purpose: Role::Tiny treats subs already in
7 # the package as not-methods, so their names stay off every consumer. A `use`
8 # below that line composes its exports onto all shipped classes (k118).
9 23 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 23 requires '_netpol_format';
12  
13 # The fluent setters below build the spec through IO::K8s::Role::SpecBuilder
14 # rather than by hand, so that role is a hard dependency of this one (k103).
15 # IO::K8s::Role::APIObject composes SpecBuilder for every top-level Kind, so
16 # these are satisfied for anything built with IO::K8s::APIObject; a class
17 # that composes this role without them now fails at composition time,
18 # naming the missing method, instead of at the first setter call.
19 23 requires qw( spec_push spec_set );
20  
21 # ---------------------------------------------------------------------------
22 # Facts both formats need, written once.
23 #
24 # The two branches spell a selector in different vocabularies -- core
25 # Kubernetes selects a namespace by the kubernetes.io/metadata.name label the
26 # API server sets on every namespace, Cilium by its own k8s: label prefixes --
27 # but they address the same namespaces and the same pods. Keeping the facts
28 # here and the spelling in the branches is what stops the two from drifting:
29 # before k117 the CoreDNS target below existed only in the cilium branch, and
30 # the core branch quietly emitted a ports-only rule that allowed DNS to any
31 # destination, which is looser than what its own documentation promised.
32 # ---------------------------------------------------------------------------
33  
34 23 my $CORE_NAMESPACE_LABEL = 'kubernetes.io/metadata.name';
35 23 my $CILIUM_NAMESPACE_LABEL = 'k8s:io.kubernetes.pod.namespace';
36  
37 # The cluster's DNS: the CoreDNS pods in kube-system, which carry
38 # k8s-app=kube-dns (the label CoreDNS inherited from kube-dns and every
39 # distribution still sets).
40 23 my %DNS_TARGET = (
41 namespace => 'kube-system',
42 pod_labels => { 'k8s-app' => 'kube-dns' },
43 );
44  
45  
46 8 ▶sub select_pods {
Subroutine select_pods: called CC=3 SCAR=11.0
Pod documented
47 8 my ($self, %labels) = @_;
48 8 my $format = $self->_netpol_format;
49  
50 8 ▶ if ($format eq 'core') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'core') { } 4 4 8
elsif ($format eq 'cilium') { } 4 0 4
51 4 $self->_ensure_spec;
52 4 $self->spec->podSelector(
53 IO::K8s::Apimachinery::Pkg::Apis::Meta::V1::LabelSelector->new(
54 matchLabels => \%labels,
55 )
56 );
57 } elsif ($format eq 'cilium') {
58 4 $self->spec_set('endpointSelector', { matchLabels => \%labels });
59 }
60 8 return $self;
61 }
62  
63  
64 6 ▶sub allow_ingress_from_pods {
Subroutine allow_ingress_from_pods: called CC=3 SCAR=11.0
Pod documented
65 6 my ($self, $labels, %opts) = @_;
66 6 my $format = $self->_netpol_format;
67  
68 6 ▶ if ($format eq 'core') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'core') { } 3 3 6
elsif ($format eq 'cilium') { } 3 0 3
69 $self->_add_core_ingress_rule(
70 { podSelector => { matchLabels => $labels } },
71 $opts{ports},
72 3 );
73 } elsif ($format eq 'cilium') {
74 $self->_add_cilium_ingress_rule(
75 { matchLabels => $labels },
76 $opts{ports},
77 3 );
78 }
79 6 return $self;
80 }
81  
82  
83 5 ▶sub allow_ingress_from_cidrs {
Subroutine allow_ingress_from_cidrs: called CC=4 SCAR=14.0
Pod documented
84 5 my ($self, $cidrs, %opts) = @_;
85 5 _validate_cidrs($cidrs);
86 4 my $format = $self->_netpol_format;
87  
88 4 ▶ if ($format eq 'core') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'core') { } 2 2 4
elsif ($format eq 'cilium') { } 2 0 2
89 2 my @from = map { { ipBlock => { cidr => $_ } } } @$cidrs;
90 2 $self->_add_core_ingress_rule_multi(\@from, $opts{ports});
91 } elsif ($format eq 'cilium') {
92 $self->spec_push('ingress', {
93 fromCIDR => $cidrs,
94 2 ▶ $opts{ports} ? (toPorts => [ { ports => $opts{ports} } ]) : (),
Branch: 1 branch
BranchTrueFalseTotal
$opts{'ports'} ? : 0 2 2
95 });
96 }
97 4 return $self;
98 }
99  
100  
101 4 ▶sub allow_ingress_from_namespace {
Subroutine allow_ingress_from_namespace: called CC=4 SCAR=13.9
Pod documented
102 4 my ($self, $namespace, %opts) = @_;
103 4 my $format = $self->_netpol_format;
104  
105 4 ▶ if ($format eq 'core') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'core') { } 2 2 4
elsif ($format eq 'cilium') { } 2 0 2
106 $self->_add_core_ingress_rule(
107 { namespaceSelector => { matchLabels => { $CORE_NAMESPACE_LABEL => $namespace } } },
108 $opts{ports},
109 2 );
110 } elsif ($format eq 'cilium') {
111 $self->spec_push('ingress', {
112 fromEndpoints => [ { matchLabels => { $CILIUM_NAMESPACE_LABEL => $namespace } } ],
113 2 ▶ $opts{ports} ? (toPorts => [ { ports => $opts{ports} } ]) : (),
Branch: 1 branch
BranchTrueFalseTotal
$opts{'ports'} ? : 1 1 2
114 });
115 }
116 4 return $self;
117 }
118  
119  
120 4 ▶sub allow_egress_to_pods {
Subroutine allow_egress_to_pods: called CC=4 SCAR=13.9
Pod documented
121 4 my ($self, $labels, %opts) = @_;
122 4 my $format = $self->_netpol_format;
123  
124 4 ▶ if ($format eq 'core') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'core') { } 2 2 4
elsif ($format eq 'cilium') { } 2 0 2
125 $self->_add_core_egress_rule(
126 { podSelector => { matchLabels => $labels } },
127 $opts{ports},
128 2 );
129 } elsif ($format eq 'cilium') {
130 $self->spec_push('egress', {
131 toEndpoints => [ { matchLabels => $labels } ],
132 2 ▶ $opts{ports} ? (toPorts => [ { ports => $opts{ports} } ]) : (),
Branch: 1 branch
BranchTrueFalseTotal
$opts{'ports'} ? : 1 1 2
133 });
134 }
135 4 return $self;
136 }
137  
138  
139 7 ▶sub allow_egress_to_cidrs {
Subroutine allow_egress_to_cidrs: called CC=3 SCAR=11.0
Pod documented
140 7 my ($self, $cidrs) = @_;
141 7 _validate_cidrs($cidrs);
142 5 my $format = $self->_netpol_format;
143  
144 5 ▶ if ($format eq 'core') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'core') { } 3 2 5
elsif ($format eq 'cilium') { } 2 0 2
145 $self->_add_core_egress_rule_multi(
146 3 [ map { { ipBlock => { cidr => $_ } } } @$cidrs ],
147 );
148 } elsif ($format eq 'cilium') {
149 2 $self->spec_push('egress', { toCIDR => $cidrs });
150 }
151 5 return $self;
152 }
153  
154  
155 5 ▶sub allow_egress_to_dns {
Subroutine allow_egress_to_dns: called CC=3 SCAR=11.0
Pod documented
156 5 my ($self) = @_;
157 5 my $dns_ports = [
158 { port => 53, protocol => 'UDP' },
159 { port => 53, protocol => 'TCP' },
160 ];
161 5 my $format = $self->_netpol_format;
162  
163 5 ▶ if ($format eq 'core') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'core') { } 2 3 5
elsif ($format eq 'cilium') { } 3 0 3
164 # Both selectors in ONE peer: within a single `to` element
165 # namespaceSelector and podSelector intersect, while two elements
166 # would union and reach every pod in kube-system plus every
167 # k8s-app=kube-dns pod anywhere.
168 $self->_add_core_egress_rule({
169 namespaceSelector => { matchLabels => { $CORE_NAMESPACE_LABEL => $DNS_TARGET{namespace} } },
170 2 podSelector => { matchLabels => { %{ $DNS_TARGET{pod_labels} } } },
171 }, $dns_ports);
172 } elsif ($format eq 'cilium') {
173 # Cilium matches a single endpoint set, so the same two facts are
174 # one matchLabels hash in its k8s: vocabulary.
175 $self->spec_push('egress', {
176 toEndpoints => [ { matchLabels => {
177 $CILIUM_NAMESPACE_LABEL => $DNS_TARGET{namespace},
178 3 map { ('k8s:'.$_ => $DNS_TARGET{pod_labels}{$_}) }
179 3 keys %{ $DNS_TARGET{pod_labels} },
180 } } ],
181 toPorts => [ { ports => $dns_ports } ],
182 });
183 }
184 5 return $self;
185 }
186  
187  
188 4 ▶sub deny_all_ingress {
Subroutine deny_all_ingress: called CC=3 SCAR=11.0
Pod documented
189 4 my ($self) = @_;
190 4 my $format = $self->_netpol_format;
191  
192 4 ▶ if ($format eq 'core') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'core') { } 1 3 4
elsif ($format eq 'cilium') { } 3 0 3
193 1 $self->_ensure_spec;
194 1 $self->_ensure_policy_types('Ingress');
195 # Empty ingress array = deny all
196 1 $self->spec->ingress([]);
197 } elsif ($format eq 'cilium') {
198 3 $self->spec_set('ingress', []);
199 3 $self->spec_set('ingressDeny', [ {} ]);
200 }
201 4 return $self;
202 }
203  
204  
205 2 ▶sub deny_all_egress {
Subroutine deny_all_egress: called CC=3 SCAR=12.6
Pod documented
206 2 my ($self) = @_;
207 2 my $format = $self->_netpol_format;
208  
209 2 ▶ if ($format eq 'core') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'core') { } 0 2 2
elsif ($format eq 'cilium') { } 2 0 2
210 0 $self->_ensure_spec;
211 0 $self->_ensure_policy_types('Egress');
212 0 $self->spec->egress([]);
213 } elsif ($format eq 'cilium') {
214 2 $self->spec_set('egress', []);
215 2 $self->spec_set('egressDeny', [ {} ]);
216 }
217 2 return $self;
218 }
219  
220 # --- Private helpers ---
221  
222 12 ▶sub _validate_cidrs {
Subroutine _validate_cidrs: called CC=4 SCAR=13.9
223 12 my ($cidrs) = @_;
224 12 require IO::K8s::Types::Net;
225 12 for my $cidr (@$cidrs) {
226 13 ▶ croak "'$cidr' is not valid CIDR notation"
Branch: 1 branch
BranchTrueFalseTotal
unless $cidr =~ m[/] and defined "Net::IP"->new($cidr) 3 10 13
Condition66%2/3
$cidr =~ m[/] and defined "Net::IP"->new($cidr)
!ll&&!rl&&r
3010
MC/DC50%1/2
$cidr =~ m[/] and defined "Net::IP"->new($cidr)
$cidr =~ m[/] defined "Net::IP"->new($cidr)
Truth table66%2/3
A and B
A= $cidr =~ m[/]B= defined "Net::IP"->new($cidr)
ABresult
0X0
100
111
227 unless $cidr =~ /\// && defined Net::IP->new($cidr);
228 }
229 }
230  
231 # Core K8s helpers (work on typed spec objects)
232 #
233 # Every core-format path reaches this first -- select_pods, deny_all_ingress,
234 # deny_all_egress call it directly, and so does each of the four
235 # _add_core_*_rule helpers -- which makes it the one place to load the core
236 # networking.k8s.io/v1 classes the branch names below. A role cannot `use`
237 # them at the top: composing this role would then drag the whole core
238 # NetworkPolicy family into IO::K8s::Cilium::V2::CiliumNetworkPolicy, which
239 # never runs this branch. Loaded when the branch first runs instead, the way
240 # IO::K8s::Role::APIObject loads ObjectMeta and OwnerReference.
241 #
242 # Until k117 only NetworkPolicySpec was required, and only inside the
243 # vivify block -- so select_pods on a freshly loaded
244 # IO::K8s::Api::Networking::V1::NetworkPolicy died with 'Can't locate object
245 # method "new" via package ...Meta::V1::LabelSelector', and every rule
246 # builder died the same way on NetworkPolicyPeer. It only ever worked in a
247 # process that had loaded those classes for some other reason.
248 19 ▶sub _ensure_spec {
Subroutine _ensure_spec: called CC=3 SCAR=11.0
249 19 my ($self) = @_;
250 19 ▶ return unless $self->_netpol_format eq 'core';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_netpol_format eq "core" 0 19 19
251  
252 19 require IO::K8s::Api::Networking::V1::NetworkPolicySpec;
253 19 require IO::K8s::Api::Networking::V1::NetworkPolicyEgressRule;
254 19 require IO::K8s::Api::Networking::V1::NetworkPolicyIngressRule;
255 19 require IO::K8s::Api::Networking::V1::NetworkPolicyPeer;
256 19 require IO::K8s::Api::Networking::V1::NetworkPolicyPort;
257 19 require IO::K8s::Apimachinery::Pkg::Apis::Meta::V1::LabelSelector;
258  
259 19 ▶ return if $self->spec;
Branch: 1 branch
BranchTrueFalseTotal
if $self->spec 15 4 19
260 4 $self->spec(IO::K8s::Api::Networking::V1::NetworkPolicySpec->new(
261 podSelector => IO::K8s::Apimachinery::Pkg::Apis::Meta::V1::LabelSelector->new,
262 ));
263 4 return;
264 }
265  
266 15 ▶sub _ensure_policy_types {
Subroutine _ensure_policy_types: called CC=4 SCAR=13.9
267 15 my ($self, $type) = @_;
268 15 ▶ return unless $self->_netpol_format eq 'core';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_netpol_format eq "core" 0 15 15
269 15 my $spec = $self->spec;
270 15 ▶ my $types = $spec->policyTypes // [];
Condition100%2/2
$spec->policyTypes // []
l!l
114
MC/DC100%1/1
$spec->policyTypes // []
$spec->policyTypes
Truth table100%2/2
A
A= $spec->policyTypes
Aresult
11
00
271 15 ▶ unless (grep { $_ eq $type } @$types) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $type;} @$types) 8 7 15
272 8 push @$types, $type;
273 8 $spec->policyTypes($types);
274 }
275 }
276  
277 0 ▶sub _core_ports {
Subroutine _core_ports: not called CC=3 SCAR=24.8
278 0 my ($ports) = @_;
279 0 ▶ return () unless $ports;
Branch: 1 branch
BranchTrueFalseTotal
unless $ports 0 0 0
280 return (ports => [
281 map {
282 0 IO::K8s::Api::Networking::V1::NetworkPolicyPort->new(
283 port => $_->{port},
284 0 ▶ protocol => $_->{protocol} // 'TCP',
Condition0%0/2
$_->{"protocol"} // "TCP"
l!l
00
MC/DC0%0/1
$_->{"protocol"} // "TCP"
$_->{"protocol"}
Truth table0%0/2
A
A= $_->{"protocol"}
Aresult
11
00
285 )
286 } @$ports
287 ]);
288 }
289  
290 5 ▶sub _add_core_ingress_rule {
Subroutine _add_core_ingress_rule: called CC=5 SCAR=16.1
291 5 my ($self, $from, $ports) = @_;
292 5 $self->_ensure_spec;
293 5 $self->_ensure_policy_types('Ingress');
294 5 my $spec = $self->spec;
295 5 ▶ my $ingress = $spec->ingress // [];
Condition100%2/2
$spec->ingress // []
l!l
23
MC/DC100%1/1
$spec->ingress // []
$spec->ingress
Truth table100%2/2
A
A= $spec->ingress
Aresult
11
00
296  
297 5 my %rule;
298 $rule{from} = [
299 5 ▶ IO::K8s::Api::Networking::V1::NetworkPolicyPeer->new(%$from)
Branch: 1 branch
BranchTrueFalseTotal
if $from 5 0 5
300 ] if $from;
301 5 ▶ if ($ports) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ports) 3 2 5
302 $rule{ports} = [
303 map {
304 3 IO::K8s::Api::Networking::V1::NetworkPolicyPort->new(
305 3 ▶ port => $_->{port}, protocol => $_->{protocol} // 'TCP',
Condition50%1/2
$_->{"protocol"} // "TCP"
l!l
03
MC/DC0%0/1
$_->{"protocol"} // "TCP"
$_->{"protocol"}
Truth table50%1/2
A
A= $_->{"protocol"}
Aresult
11
00
306 )
307 } @$ports
308 ];
309 }
310  
311 5 push @$ingress, IO::K8s::Api::Networking::V1::NetworkPolicyIngressRule->new(%rule);
312 5 $spec->ingress($ingress);
313 }
314  
315 2 ▶sub _add_core_ingress_rule_multi {
Subroutine _add_core_ingress_rule_multi: called CC=5 SCAR=17.8
316 2 my ($self, $from_list, $ports) = @_;
317 2 $self->_ensure_spec;
318 2 $self->_ensure_policy_types('Ingress');
319 2 my $spec = $self->spec;
320 2 ▶ my $ingress = $spec->ingress // [];
Condition50%1/2
$spec->ingress // []
l!l
20
MC/DC0%0/1
$spec->ingress // []
$spec->ingress
Truth table50%1/2
A
A= $spec->ingress
Aresult
11
00
321  
322 2 my %rule;
323 $rule{from} = [
324 2 ▶ map { IO::K8s::Api::Networking::V1::NetworkPolicyPeer->new(%$_) } @$from_list
Branch: 1 branch
BranchTrueFalseTotal
if $from_list 2 0 2
325 ] if $from_list;
326 2 ▶ if ($ports) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ports) 0 2 2
327 $rule{ports} = [
328 map {
329 0 IO::K8s::Api::Networking::V1::NetworkPolicyPort->new(
330 0 ▶ port => $_->{port}, protocol => $_->{protocol} // 'TCP',
Condition0%0/2
$_->{"protocol"} // "TCP"
l!l
00
MC/DC0%0/1
$_->{"protocol"} // "TCP"
$_->{"protocol"}
Truth table0%0/2
A
A= $_->{"protocol"}
Aresult
11
00
331 )
332 } @$ports
333 ];
334 }
335  
336 2 push @$ingress, IO::K8s::Api::Networking::V1::NetworkPolicyIngressRule->new(%rule);
337 2 $spec->ingress($ingress);
338 }
339  
340 4 ▶sub _add_core_egress_rule {
Subroutine _add_core_egress_rule: called CC=5 SCAR=16.1
341 4 my ($self, $to, $ports) = @_;
342 4 $self->_ensure_spec;
343 4 $self->_ensure_policy_types('Egress');
344 4 my $spec = $self->spec;
345 4 ▶ my $egress = $spec->egress // [];
Condition100%2/2
$spec->egress // []
l!l
13
MC/DC100%1/1
$spec->egress // []
$spec->egress
Truth table100%2/2
A
A= $spec->egress
Aresult
11
00
346  
347 4 my %rule;
348 $rule{to} = [
349 4 ▶ IO::K8s::Api::Networking::V1::NetworkPolicyPeer->new(%$to)
Branch: 1 branch
BranchTrueFalseTotal
if $to 4 0 4
350 ] if $to;
351 4 ▶ if ($ports) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ports) 3 1 4
352 $rule{ports} = [
353 map {
354 3 IO::K8s::Api::Networking::V1::NetworkPolicyPort->new(
355 5 ▶ port => $_->{port}, protocol => $_->{protocol} // 'TCP',
Condition100%2/2
$_->{"protocol"} // "TCP"
l!l
41
MC/DC100%1/1
$_->{"protocol"} // "TCP"
$_->{"protocol"}
Truth table100%2/2
A
A= $_->{"protocol"}
Aresult
11
00
356 )
357 } @$ports
358 ];
359 }
360  
361 4 push @$egress, IO::K8s::Api::Networking::V1::NetworkPolicyEgressRule->new(%rule);
362 4 $spec->egress($egress);
363 }
364  
365 3 ▶sub _add_core_egress_rule_multi {
Subroutine _add_core_egress_rule_multi: called CC=5 SCAR=17.2
366 3 my ($self, $to_list, $ports) = @_;
367 3 $self->_ensure_spec;
368 3 $self->_ensure_policy_types('Egress');
369 3 my $spec = $self->spec;
370 3 ▶ my $egress = $spec->egress // [];
Condition100%2/2
$spec->egress // []
l!l
21
MC/DC100%1/1
$spec->egress // []
$spec->egress
Truth table100%2/2
A
A= $spec->egress
Aresult
11
00
371  
372 3 my %rule;
373 $rule{to} = [
374 3 ▶ map { IO::K8s::Api::Networking::V1::NetworkPolicyPeer->new(%$_) } @$to_list
Branch: 1 branch
BranchTrueFalseTotal
if $to_list 3 0 3
375 ] if $to_list;
376 3 ▶ if ($ports) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ports) 0 3 3
377 $rule{ports} = [
378 map {
379 0 IO::K8s::Api::Networking::V1::NetworkPolicyPort->new(
380 0 ▶ port => $_->{port}, protocol => $_->{protocol} // 'TCP',
Condition0%0/2
$_->{"protocol"} // "TCP"
l!l
00
MC/DC0%0/1
$_->{"protocol"} // "TCP"
$_->{"protocol"}
Truth table0%0/2
A
A= $_->{"protocol"}
Aresult
11
00
381 )
382 } @$ports
383 ];
384 }
385  
386 3 push @$egress, IO::K8s::Api::Networking::V1::NetworkPolicyEgressRule->new(%rule);
387 3 $spec->egress($egress);
388 }
389  
390 3 ▶sub _add_cilium_ingress_rule {
Subroutine _add_cilium_ingress_rule: called CC=2 SCAR=6.9
391 3 my ($self, $endpoint_selector, $ports) = @_;
392 3 ▶ $self->spec_push('ingress', {
Branch: 1 branch
BranchTrueFalseTotal
$ports ? : 2 1 3
393 fromEndpoints => [ $endpoint_selector ],
394 $ports ? (toPorts => [ { ports => $ports } ]) : (),
395 });
396 }
397  
398 23 1;
399  
400 __END__
« lib/IO/K8s/Role/CertManaged.pm ↑ Summary lib/IO/K8s/Role/Loadbalanced.pm »