lib/IO/K8s/Role/Routable.pm

filter: Statementstmt 97.7% 88 / 90 Branchbran 78.3% 47 / 60 Conditioncond 88.0% 22 / 25 MC/DCmcdc 76.9% 10 / 13 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 89.6% 181 / 202 CC 42 SCAR 28.1
CC
42
Cov
90%
add_path_match
28.4
add_backend
6.0
add_hostname
6.0
CRAP 16.6
« lib/IO/K8s/CRD/Emitter.pm ↑ Summary lib/IO/K8s/CRD.pm »
1 package IO::K8s::Role::Routable;
2 # ABSTRACT: Role for building HTTP/gRPC routing rules
3 17 our $VERSION = '1.112';
4 17 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use Scalar::Util qw( looks_like_number );
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 17 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 17 requires '_route_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 17 requires qw( spec_get spec_push spec_set );
20  
21 # The 'ingress' branch builds core networking.k8s.io/v1 objects by name, and
22 # a role cannot `use` them at the top: composing this role would then drag
23 # the whole Ingress class family into every Gateway API and Traefik consumer
24 # that never touches that branch. Load them only on the branch that needs
25 # them, the way IO::K8s::Role::APIObject loads ObjectMeta/OwnerReference and
26 # IO::K8s::Role::NetworkPolicy loads its own spec class.
27 24 ▶sub _load_ingress_classes {
Subroutine _load_ingress_classes: called CC=1 SCAR=0.0
28 24 my ($self) = @_;
29 24 require IO::K8s::Api::Networking::V1::IngressSpec;
30 24 require IO::K8s::Api::Networking::V1::IngressRule;
31 24 require IO::K8s::Api::Networking::V1::HTTPIngressRuleValue;
32 24 require IO::K8s::Api::Networking::V1::HTTPIngressPath;
33 24 require IO::K8s::Api::Networking::V1::IngressBackend;
34 24 require IO::K8s::Api::Networking::V1::IngressServiceBackend;
35 24 require IO::K8s::Api::Networking::V1::ServiceBackendPort;
36 }
37  
38 # The single place the ingress spec is vivified; before k117 the same lines
39 # sat inlined in three method bodies.
40 18 ▶sub _ensure_ingress_spec {
Subroutine _ensure_ingress_spec: called CC=2 SCAR=6.9
41 18 my ($self) = @_;
42 18 $self->_load_ingress_classes;
43  
44 18 my $spec = $self->spec;
45 18 ▶ return $spec if $spec;
Branch: 1 branch
BranchTrueFalseTotal
if $spec 12 6 18
46 6 $spec = IO::K8s::Api::Networking::V1::IngressSpec->new;
47 6 $self->spec($spec);
48 6 return $spec;
49 }
50  
51 6 ▶sub _build_ingress_path_backend {
Subroutine _build_ingress_path_backend: called CC=2 SCAR=6.9
52 6 my ($self, $service, $port) = @_;
53 6 ▶ return IO::K8s::Api::Networking::V1::IngressBackend->new(
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($port) ? : 5 1 6
54 service => IO::K8s::Api::Networking::V1::IngressServiceBackend->new(
55 name => $service,
56 port => IO::K8s::Api::Networking::V1::ServiceBackendPort->new(
57 looks_like_number($port) ? ( number => $port ) : ( name => $port )
58 )
59 )
60 );
61 }
62  
63  
64 14 ▶sub add_hostname {
Subroutine add_hostname: called CC=6 SCAR=17.9
Pod documented
65 14 my ($self, @hostnames) = @_;
66 14 my $format = $self->_route_format;
67 14 ▶ if ($format eq 'gateway') {
Branch: 3 branches
BranchTrueFalseTotal
if ($format eq 'gateway') { } 5 9 14
elsif ($format eq 'traefik') { } 2 7 9
elsif ($format eq 'ingress') { } 7 0 7
68 5 $self->spec_push('hostnames', @hostnames);
69 } elsif ($format eq 'traefik') {
70 # Traefik uses match rules like Host(`example.com`)
71 # We add a route with the host match
72 2 my $hosts = join ', ', map { "Host(`$_`)" } @hostnames;
73 2 $self->spec_push('routes', { match => $hosts, kind => 'Rule', services => [] });
74 } elsif ($format eq 'ingress') {
75 7 my $spec = $self->_ensure_ingress_spec;
76 7 ▶ my $rules = $spec->rules // [];
Condition100%2/2
$spec->rules // []
l!l
16
MC/DC100%1/1
$spec->rules // []
$spec->rules
Truth table100%2/2
A
A= $spec->rules
Aresult
11
00
77 7 for my $hostname (@hostnames) {
78 9 push @$rules, IO::K8s::Api::Networking::V1::IngressRule->new(
79 host => $hostname,
80 );
81 }
82 7 $spec->rules($rules);
83 }
84 14 return $self;
85 }
86  
87  
88 14 ▶sub add_backend {
Subroutine add_backend: called CC=6 SCAR=18.0
Pod documented
89 14 my ($self, $name, %opts) = @_;
90 14 my $format = $self->_route_format;
91 my %backend = (
92 name => $name,
93 $opts{port} ? (port => $opts{port}) : (),
94 14 ▶ $opts{weight} ? (weight => $opts{weight}) : (),
Branch: 2 branches
BranchTrueFalseTotal
$opts{'port'} ? : 14 0 14
$opts{'weight'} ? : 5 9 14
95 );
96 14 ▶ if ($format eq 'gateway') {
Branch: 3 branches
BranchTrueFalseTotal
if ($format eq 'gateway') { } 6 8 14
elsif ($format eq 'traefik') { } 3 5 8
elsif ($format eq 'ingress') { } 5 0 5
97 6 $self->spec_push('rules.-1.backendRefs', \%backend);
98 } elsif ($format eq 'traefik') {
99 3 $self->spec_push('routes.-1.services', \%backend);
100 } elsif ($format eq 'ingress') {
101 5 my $spec = $self->_ensure_ingress_spec;
102 $spec->defaultBackend(IO::K8s::Api::Networking::V1::IngressBackend->new(
103 service => IO::K8s::Api::Networking::V1::IngressServiceBackend->new(
104 name => $name,
105 port => IO::K8s::Api::Networking::V1::ServiceBackendPort->new(
106 number => $opts{port},
107 5 ),
108 ),
109 ));
110 }
111 14 return $self;
112 }
113  
114  
115 19 ▶sub add_path_match {
Subroutine add_path_match: called CC=26 SCAR=33.5
Pod documented
116 19 my ($self, $path, %opts) = @_;
117 19 ▶ my $type = $opts{type} // 'Prefix';
Condition100%2/2
$opts{"type"} // "Prefix"
l!l
154
MC/DC100%1/1
$opts{"type"} // "Prefix"
$opts{"type"}
Truth table100%2/2
A
A= $opts{"type"}
Aresult
11
00
118 19 my $format = $self->_route_format;
119 19 ▶ if ($format eq 'gateway') {
Branch: 3 branches
BranchTrueFalseTotal
if ($format eq 'gateway') { } 2 17 19
elsif ($format eq 'traefik') { } 2 15 17
elsif ($format eq 'ingress') { } 15 0 15
120 # This role's own vocabulary (Prefix/Exact/Regex, documented above)
121 # is not the Gateway API HTTPPathMatch.type enum (Exact/PathPrefix/
122 # RegularExpression, k95/D5) -- translate it the same way the
123 # 'traefik' branch below translates it into Traefik's match syntax.
124 2 ▶ my $gw_type = $type eq 'Prefix' ? 'PathPrefix'
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'Prefix' ? : 1 1 2
$type eq 'Regex' ? : 0 1 1
125 : $type eq 'Regex' ? 'RegularExpression'
126 : $type; # 'Exact' is spelled the same in both vocabularies
127 2 $self->spec_push('rules.-1.matches', { path => { type => $gw_type, value => $path } });
128 } elsif ($format eq 'traefik') {
129 2 ▶ my $match = $type eq 'Prefix' ? "PathPrefix(`$path`)"
Branch: 3 branches
BranchTrueFalseTotal
$type eq 'Prefix' ? : 2 0 2
$type eq 'Exact' ? : 0 0 0
$type eq 'Regex' ? : 0 0 0
130 : $type eq 'Exact' ? "Path(`$path`)"
131 : $type eq 'Regex' ? "PathRegexp(`$path`)"
132 : undef;
133 2 ▶ $self->spec_set('routes.-1.match', $match) if defined $match;
Branch: 1 branch
BranchTrueFalseTotal
if defined $match 2 0 2
134 } elsif ($format eq 'ingress') {
135 croak __PACKAGE__.'->add_path_match service is required'
136 15 ▶ unless defined $opts{service};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"service"} 2 13 15
137 croak __PACKAGE__.'->add_path_match port is required'
138 13 ▶ unless defined $opts{port};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"port"} 1 12 13
139 12 ▶ croak __PACKAGE__.'->add_path_match does not support Ingress path type '
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "Prefix" or $type eq "Exact" or $type eq "ImplementationSpecific" 1 11 12
Condition100%6/6
$type eq "Prefix" or $type eq "Exact" or $type eq "ImplementationSpecific"
l!l&&r!l&&!r
741
$type eq "Prefix" or $type eq "Exact"
l!l&&r!l&&!r
435
MC/DC100%3/3
$type eq "Prefix" or $type eq "Exact" or $type eq "ImplementationSpecific"
$type eq "Prefix" $type eq "Exact" $type eq "ImplementationSpecific"
Truth table100%4/4
A or B or C
A= $type eq "Prefix"B= $type eq "Exact"C= $type eq "ImplementationSpecific"
ABCresult
1XX1
01X1
0011
0000
140 . $type
141 unless $type eq 'Prefix'
142 || $type eq 'Exact'
143 || $type eq 'ImplementationSpecific';
144 11 ▶ if ($type eq 'ImplementationSpecific') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'ImplementationSpecific') { } 4 7 11
145 4 ▶ croak __PACKAGE__.'->add_path_match path is required and must start with /'
Branch: 1 branch
BranchTrueFalseTotal
if defined $path and length $path and not $path =~ m[\A/] 1 3 4
Condition100%6/6
defined $path and length $path and not $path =~ m[\A/]
!ll&&!rl&&r
211
defined $path and length $path
!ll&&!rl&&r
112
MC/DC100%3/3
defined $path and length $path and not $path =~ m[\A/]
defined $path length $path not $path =~ m[\A/]
Truth table100%4/4
A and B and not(C)
A= defined $pathB= length $pathC= not $path =~ m[\A/]
ABCresult
0XX0
10X0
1100
1111
146 if defined $path && length $path && $path !~ m{\A/};
147 } else {
148 7 ▶ croak __PACKAGE__.'->add_path_match path is required and must start with /'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and $path =~ m[\A/] 4 3 7
Condition100%3/3
defined $path and $path =~ m[\A/]
!ll&&!rl&&r
223
MC/DC100%2/2
defined $path and $path =~ m[\A/]
defined $path $path =~ m[\A/]
Truth table100%3/3
A and B
A= defined $pathB= $path =~ m[\A/]
ABresult
0X0
100
111
149 unless defined $path && $path =~ m{\A/};
150 }
151  
152 6 $self->_load_ingress_classes;
153 6 my $backend = $self->_build_ingress_path_backend($opts{service}, $opts{port});
154 6 my $path_rule = IO::K8s::Api::Networking::V1::HTTPIngressPath->new(
155 backend => $backend,
156 path => $path,
157 pathType => $type
158 );
159  
160 6 my $spec = $self->_ensure_ingress_spec;
161 6 ▶ my $rules = $spec->rules // [];
Condition50%1/2
$spec->rules // []
l!l
60
MC/DC0%0/1
$spec->rules // []
$spec->rules
Truth table50%1/2
A
A= $spec->rules
Aresult
11
00
162 6 my $rule = $rules->[-1];
163 6 ▶ if ($rule) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule) { } 6 0 6
164 6 my $http = $rule->http;
165 6 ▶ if ($http) {
Branch: 1 branch
BranchTrueFalseTotal
if ($http) { } 2 4 6
166 2 ▶ my $paths = $http->paths // [];
Condition50%1/2
$http->paths // []
l!l
20
MC/DC0%0/1
$http->paths // []
$http->paths
Truth table50%1/2
A
A= $http->paths
Aresult
11
00
167 2 push @$paths, $path_rule;
168 2 $http->paths($paths);
169 } else {
170 4 $rule->http(IO::K8s::Api::Networking::V1::HTTPIngressRuleValue->new(
171 paths => [$path_rule]
172 ));
173 }
174 } else {
175 0 push @$rules, IO::K8s::Api::Networking::V1::IngressRule->new(
176 http => IO::K8s::Api::Networking::V1::HTTPIngressRuleValue->new(
177 paths => [$path_rule]
178 )
179 );
180 0 $spec->rules($rules);
181 }
182 }
183 10 return $self;
184 }
185  
186  
187 6 ▶sub add_header_match {
Subroutine add_header_match: called CC=5 SCAR=16.2
Pod documented
188 6 my ($self, $header, $value) = @_;
189 6 my $format = $self->_route_format;
190 6 ▶ if ($format eq 'gateway') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'gateway') { } 2 4 6
elsif ($format eq 'traefik') { } 2 2 4
191 2 $self->spec_push('rules.-1.matches.-1.headers', { name => $header, value => $value });
192 } elsif ($format eq 'traefik') {
193 2 ▶ my $existing = $self->spec_get('routes.-1.match') // '';
Condition50%1/2
$self->spec_get("routes.-1.match") // ""
l!l
20
MC/DC0%0/1
$self->spec_get("routes.-1.match") // ""
$self->spec_get("routes.-1.match")
Truth table50%1/2
A
A= $self->spec_get("routes.-1.match")
Aresult
11
00
194 2 my $header_match = "Header(`$header`, `$value`)";
195 2 ▶ $self->spec_set('routes.-1.match', $existing ? "$existing && $header_match" : $header_match);
Branch: 1 branch
BranchTrueFalseTotal
$existing ? : 2 0 2
196 }
197 # Ingress doesn't support header matching natively
198 6 return $self;
199 }
200  
201 17 1;
202  
203 __END__
« lib/IO/K8s/CRD/Emitter.pm ↑ Summary lib/IO/K8s/CRD.pm »