lib/Kubernetes/REST.pm

filter: Statementstmt 98.6% 1076 / 1091 Branchbran 86.2% 509 / 590 Conditioncond 65.5% 245 / 374 MC/DCmcdc 41.7% 86 / 206 Subroutinesub 100% 137 / 137 Podpod 67.3% 33 / 49 totaltotal 85.2% 2086 / 2447 CC 473 SCAR 24.5
CC
473
Cov
89%
_build_path
31.7
exec
26.2
schema_for
19.4
CRAP 11.6
« lib/Kubernetes/REST/V0Group.pm ↑ Summary lib/Kubernetes/REST/CLI/Minikube.pm »
1 package Kubernetes::REST;
2 97 our $VERSION = '1.113';
3 # ABSTRACT: A Perl REST Client for the Kubernetes API
4 97 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 97 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 97 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 97 ▶use Module::Runtime qw(require_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 97 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 97 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 97 ▶use Kubernetes::REST::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 97 ▶use Kubernetes::REST::AuthToken;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 97 ▶use Kubernetes::REST::LWPIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 97 ▶use Kubernetes::REST::HTTPRequest;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 97 ▶use IO::K8s;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 97 ▶use IO::K8s::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 97 ▶use IO::K8s::CRD;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 97 ▶use IO::K8s::Unstructured ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 97 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 97 ▶use Kubernetes::REST::WatchEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 97 ▶use Kubernetes::REST::LogEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 97 ▶use Kubernetes::REST::APIError;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 97 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 has server => (
25 is => 'ro',
26 required => 1,
27 556 ▶ coerce => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
28 556 my $val = $_[0];
29 556 ▶ return $val if blessed($val) && $val->isa('Kubernetes::REST::Server');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($val) and $val->isa("Kubernetes::REST::Server") 324 232 556
Condition66%2/3
builtin::blessed($val) and $val->isa("Kubernetes::REST::Server")
!ll&&!rl&&r
2320324
MC/DC50%1/2
builtin::blessed($val) and $val->isa("Kubernetes::REST::Server")
builtin::blessed($val) $val->isa("Kubernetes::REST::Server")
Truth table66%2/3
A and B
A= builtin::blessed($val)B= $val->isa("Kubernetes::REST::Server")
ABresult
0X0
100
111
30 232 Kubernetes::REST::Server->new($val);
31 },
32 97 );
33  
34  
35 has credentials => (
36 is => 'ro',
37 required => 1,
38 557 ▶ coerce => sub {
Subroutine __ANON__: called CC=4 SCAR=14.6
39 557 my $val = $_[0];
40 557 ▶ return $val if blessed($val) && $val->can('token');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($val) and $val->can("token") 324 233 557
Condition66%2/3
builtin::blessed($val) and $val->can("token")
!ll&&!rl&&r
2330324
MC/DC50%1/2
builtin::blessed($val) and $val->can("token")
builtin::blessed($val) $val->can("token")
Truth table66%2/3
A and B
A= builtin::blessed($val)B= $val->can("token")
ABresult
0X0
100
111
41 233 ▶ return Kubernetes::REST::AuthToken->new($val) if ref($val) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "HASH" 233 0 233
42 0 return $val;
43 }
44 97 );
45  
46  
47 has io => (
48 is => 'ro',
49 lazy => 1,
50 3 ▶ default => sub {
Subroutine __ANON__: called CC=7 SCAR=23.2
51 3 my $self = shift;
52 3 my $s = $self->server;
53 3 ▶ Kubernetes::REST::LWPIO->new(
Branch: 6 branches
BranchTrueFalseTotal
defined $s->ssl_cert_pem ? : 0 3 3
defined $s->ssl_cert_file ? : 0 3 3
defined $s->ssl_key_pem ? : 0 3 3
defined $s->ssl_key_file ? : 0 3 3
defined $s->ssl_ca_pem ? : 0 3 3
defined $s->ssl_ca_file ? : 0 3 3
54 ssl_verify_server => $s->ssl_verify_server,
55 (defined $s->ssl_cert_pem ? (ssl_cert_pem => $s->ssl_cert_pem) : ()),
56 (defined $s->ssl_cert_file ? (ssl_cert_file => $s->ssl_cert_file) : ()),
57 (defined $s->ssl_key_pem ? (ssl_key_pem => $s->ssl_key_pem) : ()),
58 (defined $s->ssl_key_file ? (ssl_key_file => $s->ssl_key_file) : ()),
59 (defined $s->ssl_ca_pem ? (ssl_ca_pem => $s->ssl_ca_pem) : ()),
60 (defined $s->ssl_ca_file ? (ssl_ca_file => $s->ssl_ca_file) : ()),
61 );
62 },
63 97 );
64  
65  
66 # utf8 => 1 makes encode() return UTF-8 bytes and decode() expect them, which is
67 # what HTTP::Request->content requires and what IO::K8s already assumes. See the
68 # ENCODING section below.
69 has _json => (
70 is => 'ro',
71 557 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 557 JSON::MaybeXS->new(utf8 => 1, canonical => 1, convert_blessed => 1);
73 },
74 97 );
75  
76 # External IO::K8s resource-map providers, mirrored onto the inner instance.
77 has with => (
78 is => 'ro',
79 546 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
80 97 );
81  
82  
83 # `with` is the one registration every rebuild of the inner IO::K8s merges
84 # again - after invalidate_discovery, absorb_discovery or the openapi fetch -
85 # so that is where the providers go (karr k78). An inner IO::K8s that already
86 # exists gets them through its own add(), which merges into this client's
87 # resource_map hashref exactly as a build does, and keeps the AutoGen classes
88 # and whatever else was registered on that instance; clearing it would lose
89 # those. Nothing built means nothing to do now, and no request.
90 12 ▶sub add_provider {
Subroutine add_provider: called CC=13 SCAR=26.3
Pod documented
91 12 my ($self, @providers) = @_;
92  
93  
94 # Checked before anything is merged or kept: a provider that cannot work
95 # would otherwise sit in `with` and break every later build of the inner
96 # IO::K8s, far from this call. The message names the position, never the
97 # value.
98 12 my $position = 0;
99 12 for my $provider (@providers) {
100 12 $position++;
101 12 ▶ next if ref $provider eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $provider eq "HASH" 8 4 12
102 4 ▶ next if blessed($provider) && $provider->can('resource_map');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($provider) and $provider->can("resource_map") 1 3 4
Condition66%2/3
builtin::blessed($provider) and $provider->can("resource_map")
!ll&&!rl&&r
301
MC/DC50%1/2
builtin::blessed($provider) and $provider->can("resource_map")
builtin::blessed($provider) $provider->can("resource_map")
Truth table66%2/3
A and B
A= builtin::blessed($provider)B= $provider->can("resource_map")
ABresult
0X0
100
111
103 3 ▶ croak "add_provider: provider $position is not a class name,"
Branch: 1 branch
BranchTrueFalseTotal
if ref $provider or not defined $provider or not length $provider 0 3 3
Condition33%2/6
ref $provider or not defined $provider or not length $provider
l!l&&r!l&&!r
003
ref $provider or not defined $provider
l!l&&r!l&&!r
003
MC/DC0%0/3
ref $provider or not defined $provider or not length $provider
ref $provider not defined $provider not length $provider
Truth table25%1/4
A or not(B) or not(C)
A= ref $providerB= not defined $providerC= not length $provider
ABCresult
1XX1
01X1
0011
0000
104 . ' an object with resource_map or a hashref'
105 if ref $provider or !defined $provider or !length $provider;
106 croak "add_provider: provider $position is a class name that does not"
107 . ' load as a provider (one with new and resource_map)'
108 3 ▶ unless eval { require_module($provider); 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { &require_module($provider); 1 } } and $provider->can("new") and $provider->can("resource_map") 2 1 3
Condition83%5/6
eval { do { &require_module($provider); 1 } } and $provider->can("new") and $provider->can("resource_map")
!ll&&!rl&&r
201
eval { do { &require_module($provider); 1 } } and $provider->can("new")
!ll&&!rl&&r
111
MC/DC66%2/3
eval { do { &require_module($provider); 1 } } and $provider->can("new") and $provider->can("resource_map")
eval { do { &require_module($provider); 1 } } $provider->can("new") $provider->can("resource_map")
Truth table75%3/4
A and B and C
A= eval { do { &require_module($provider); 1 } }B= $provider->can("new")C= $provider->can("resource_map")
ABCresult
0XX0
10X0
1100
1111
109 && $provider->can('new') && $provider->can('resource_map');
110 }
111  
112 # One at a time, so a provider whose new() dies on a built client leaves
113 # `with` and the live map agreeing on every provider before it.
114 10 for my $provider (@providers) {
115 10 ▶ $self->k8s->add($provider) if $self->_has_k8s;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_k8s 5 5 10
116 10 push @{ $self->with }, $provider;
117 }
118 10 return $self;
119 }
120  
121 # IO::K8s instance - configured with same resource_map
122 has k8s => (
123 is => 'ro',
124 lazy => 1,
125 predicate => '_has_k8s',
126 clearer => '_clear_k8s',
127 309 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
128 309 my $self = shift;
129 return IO::K8s->new(
130 resource_map => $self->resource_map,
131 309 ▶ with => [ @{ $self->with } ],
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_openapi_spec ? : 13 296 309
132 # openapi_spec is an eager hashref on IO::K8s: handing it over here
133 # only once it has actually been fetched keeps constructing/using
134 # the inner instance from forcing the /openapi/v2 download (D12).
135 # Until then it is absent; _fetch_openapi_spec rebuilds this
136 # instance once the spec exists.
137 ($self->_has_openapi_spec ? (openapi_spec => $self->_openapi_spec) : ()),
138 );
139 },
140 97 handles => [qw(
141 new_object
142 inflate
143 json_to_object
144 struct_to_object
145 object_to_json
146 object_to_struct
147 load
148 load_yaml
149 )],
150 );
151  
152  
153 # Set to 0 to use IO::K8s defaults instead of loading from cluster
154 188 ▶has resource_map_from_cluster => (is => 'ro', default => sub { 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
155  
156  
157 # The fieldValidation values the API server takes, spelled as it spells them.
158 97 my @FIELD_VALIDATIONS = qw(Strict Warn Ignore);
159  
160 # Sent as the fieldValidation query parameter of every write - create, update,
161 # patch, patch_status, update_status, and ensure() through them - that does
162 # not pass its own (karr k79). Unset, a write carries none and the server's
163 # default applies, which on current clusters drops an unknown field and says
164 # so only in a Warning header (see on_warning). The message names the
165 # attribute and the allowed values, never the value given.
166 has field_validation => (
167 is => 'ro',
168 18 ▶ isa => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
169 18 my ($value) = @_;
170 croak 'Unknown field_validation value (use: ' . join(', ', @FIELD_VALIDATIONS) . ')'
171 18 ▶ if defined $value && !grep { $_ eq $value } @FIELD_VALIDATIONS;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and not grep({$_ eq $value;} @FIELD_VALIDATIONS) 1 17 18
Condition66%2/3
defined $value and not grep({$_ eq $value;} @FIELD_VALIDATIONS)
!ll&&!rl&&r
0171
MC/DC50%1/2
defined $value and not grep({$_ eq $value;} @FIELD_VALIDATIONS)
defined $value not grep({$_ eq $value;} @FIELD_VALIDATIONS)
Truth table66%2/3
A and not(B)
A= defined $valueB= not grep({$_ eq $value;} @FIELD_VALIDATIONS)
ABresult
0X0
100
111
172 },
173 97 );
174  
175  
176 # Called as ->($text, $context) for each Warning header value of every
177 # response _check_response sees - check_response, the seam async clients use,
178 # included (karr k79). Unset, warnings are not looked at.
179 has on_warning => (
180 is => 'ro',
181 8 ▶ isa => sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
182 8 ▶ croak 'on_warning must be a code reference'
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and (builtin::reftype($_[0]) // "") ne "CODE" 0 8 8
Condition40%2/5
defined $_[0] and (builtin::reftype($_[0]) // "") ne "CODE"
!ll&&!rl&&r
080
builtin::reftype($_[0]) // ""
l!l
80
MC/DC0%0/2
defined $_[0] and (builtin::reftype($_[0]) // "") ne "CODE"
defined $_[0] builtin::reftype($_[0])
Truth table33%1/3
A and B
A= defined $_[0]B= builtin::reftype($_[0])
ABresult
0X0
100
111
183 if defined $_[0] && (reftype($_[0]) // '') ne 'CODE';
184 },
185 97 );
186  
187  
188 # Cluster version - fetched once per instance
189 has cluster_version => (
190 is => 'ro',
191 lazy => 1,
192 2 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
193 2 my $self = shift;
194 2 my $response = $self->_request('GET', '/version');
195 2 ▶ return 'unknown' if $response->status >= 400;
Branch: 1 branch
BranchTrueFalseTotal
if $response->status >= 400 1 1 2
196 1 my $info = $self->_json->decode($response->content);
197 1 ▶ return $info->{gitVersion} // 'unknown';
Condition50%1/2
$info->{"gitVersion"} // "unknown"
l!l
10
MC/DC0%0/1
$info->{"gitVersion"} // "unknown"
$info->{"gitVersion"}
Truth table50%1/2
A
A= $info->{"gitVersion"}
Aresult
11
00
198 },
199 97 );
200  
201  
202 # Resource map - loads from cluster by default, cached per instance (lazy)
203 # The predicate is true once the map exists: passed in by the caller, or
204 # built (fetched) on first use. expand_class() keys its cheap path off it.
205 has resource_map => (
206 is => 'ro',
207 lazy => 1,
208 predicate => '_has_resource_map',
209 clearer => '_clear_resource_map',
210 # Always a hash of our own: the inner IO::K8s merges the `with` providers
211 # into this hashref in place (IO::K8s::add). A map passed to the
212 # constructor is the caller's, and the fallback of a failed cluster fetch
213 # is IO::K8s's process-wide built-in map - neither may grow by the
214 # provider Kinds (karr k57). Moo coerces the default's value too.
215 313 ▶ coerce => sub { ref $_[0] eq 'HASH' ? { %{ $_[0] } } : $_[0] },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 313 0 313
216 273 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
217 273 my $self = shift;
218 # Built here, not passed in: absorb_discovery may rebuild it.
219 273 $self->_resource_map_built(1);
220 # A private copy, never IO::K8s's shared global map: the inner IO::K8s
221 # is handed this hashref and merges any `with` providers into it with
222 # add(), which mutates it in place. Returning the global ref would leak
223 # provider Kinds into IO::K8s->default_resource_map process-wide.
224 273 ▶ return { %{ IO::K8s->default_resource_map } } unless $self->resource_map_from_cluster;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->resource_map_from_cluster 212 61 273
225 61 return $self->_load_resource_map_from_cluster;
226 },
227 97 );
228  
229  
230 # Whether the resource map in place was built by the default above rather
231 # than passed to the constructor. absorb_discovery rebuilds a built map from
232 # the catalog it takes; a map the caller passed is theirs and stays (karr k51).
233 97 has _resource_map_built => (is => 'rw', init_arg => undef);
234  
235 # Deliberately NOT delegated to the k8s attribute like the other IO::K8s
236 # methods: building that instance forces the lazy resource_map, which on the
237 # default resource_map_from_cluster => 1 is a full GET /openapi/v2 - paid for
238 # resolving a name like 'Pod' whose answer already sits in the built-in map
239 # (karr #15).
240 429 ▶sub expand_class {
Subroutine expand_class: called CC=12 SCAR=24.9
Pod documented
241 429 my ($self, @args) = @_;
242  
243  
244 429 ▶ if ($self->resource_map_from_cluster && !$self->_has_resource_map
Branch: 1 branch
BranchTrueFalseTotal
if ($self->resource_map_from_cluster and not $self->_has_resource_map and defined $args[0] and not ref $args[0]) 54 375 429
Condition77%7/9
$self->resource_map_from_cluster and not $self->_has_resource_map and defined $args[0] and not ref $args[0]
!ll&&!rl&&r
375054
$self->resource_map_from_cluster and not $self->_has_resource_map and defined $args[0]
!ll&&!rl&&r
375054
$self->resource_map_from_cluster and not $self->_has_resource_map
!ll&&!rl&&r
3215454
MC/DC50%2/4
$self->resource_map_from_cluster and not $self->_has_resource_map and defined $args[0] and not ref $args[0]
$self->resource_map_from_cluster not $self->_has_resource_map defined $args[0] not ref $args[0]
Truth table60%3/5
A and not(B) and C and not(D)
A= $self->resource_map_from_clusterB= not $self->_has_resource_mapC= defined $args[0]D= not ref $args[0]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
245 && defined $args[0] && !ref $args[0]) {
246 # '+Full::Class::Name' names an exact class - no map consulted either
247 # way, and by contract it is returned without being loaded.
248 54 ▶ return substr($args[0], 1) if $args[0] =~ /^\+/;
Branch: 1 branch
BranchTrueFalseTotal
if $args[0] =~ /^\+/ 2 52 54
249 # As a class method, IO::K8s->expand_class resolves against the
250 # built-in map. Its fallback for an unknown Kind is the *name*
251 # 'IO::K8s::<Kind>' whether or not such a class exists, so a cheap
252 # answer only counts when it names a class that actually loads.
253 # Anything else falls through to the cluster-backed instance below,
254 # which fetches the cluster map exactly as it always did.
255 52 my $class = IO::K8s->expand_class(@args);
256 return $class if defined $class
257 52 ▶ && ($class->can('new') || eval { require_module($class); 1 });
Branch: 1 branch
BranchTrueFalseTotal
if defined $class and $class->can("new") || eval { do { &require_module($class); 1 } } 11 41 52
Condition100%6/6
defined $class and $class->can("new") || eval { do { &require_module($class); 1 } }
!ll&&!rl&&r
73411
$class->can("new") or eval { do { &require_module($class); 1 } }
l!l&&r!l&&!r
10134
MC/DC100%3/3
defined $class and $class->can("new") || eval { do { &require_module($class); 1 } }
defined $class $class->can("new") eval { do { &require_module($class); 1 } }
Truth table100%4/4
A and B or C
A= defined $classB= $class->can("new")C= eval { do { &require_module($class); 1 } }
ABCresult
0XX0
1000
11X1
1011
258 }
259  
260 # Fall through to the cluster-backed inner IO::K8s, which owns the
261 # resolution order (design D13, extending the 2026-08-10 exact-GVK
262 # contract):
263 # rung 1 an explicit '+Full::Class' in the caller's resource_map
264 # rung 2 provider classes merged from `with`
265 # rung 3 AutoGen from openapi_spec, once the spec has been fetched
266 # rung 5 the existing fail-closed GVK error
267 # The discovery-built map (see _resource_map_from_catalog) no longer
268 # short-circuits this order with invented IO::K8s::Api::<Group> names for
269 # groups this distribution does not ship (D12): a foreign Kind with no
270 # provider and no loaded spec now reaches rung 4/5 instead of resolving to
271 # a bogus class.
272 416 my $class = $self->k8s->expand_class(@args);
273  
274 # How IO::K8s signals "nothing resolved this Kind" depends on the argument
275 # shape, and only that exact signal may be diverted to rung 4 -- every real
276 # resolution (rungs 1-3: a '+Full::Class', a provider from `with`, a builtin
277 # or an AutoGen class) must be returned untouched, including a '+'-class
278 # that names a class not yet loaded (returned by contract without loading,
279 # so a load probe would wrongly reject it -- karr t/36 subtest 3).
280 #
281 # For a bare Kind IO::K8s fails *open*: it fabricates the name
282 # 'IO::K8s::<Kind>' whether or not such a class exists, and the fail-closed
283 # error only surfaces later when require_module() cannot load it. For an
284 # exact-GVK or domain-qualified request it fails *closed*, returning undef.
285 # Both -- and nothing else -- mean unresolved (_is_unresolved).
286 416 my ($kind, $api_version) = $self->_kind_from_expand_args(@args);
287 416 ▶ return $class unless defined $kind && $self->_is_unresolved($class, $kind);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kind and $self->_is_unresolved($class, $kind) 312 104 416
Condition100%3/3
defined $kind and $self->_is_unresolved($class, $kind)
!ll&&!rl&&r
54258104
MC/DC100%2/2
defined $kind and $self->_is_unresolved($class, $kind)
defined $kind $self->_is_unresolved($class, $kind)
Truth table100%3/3
A and B
A= defined $kindB= $self->_is_unresolved($class, $kind)
ABresult
0X0
100
111
288  
289 # Rung 4 (D16): the Kind resolved to nothing that ships. If the cluster
290 # confirms this GVK through aggregated discovery, resolve to
291 # IO::K8s::Unstructured -- its plural and scope come from the discovery
292 # catalog at path-build time (_build_path), since Unstructured carries no
293 # api_version()/resource_plural()/Namespaced role of its own. On by default
294 # (no opt-in here, unlike bare IO::K8s): D16 gates it on discovery
295 # confirmation instead. _discovery_path_meta is a no-op (undef) unless
296 # resource_map_from_cluster is set, and by the time control reaches here the
297 # catalog was already fetched building the cluster map, so this consults a
298 # cache and never adds a round-trip. A Kind discovery does not serve stays
299 # fail-closed (rung 5): the fabricated name (or undef) is returned so the
300 # load error still names the Kind. A qualified name, or a Kind plus
301 # apiVersion, is confirmed only in exactly its group/version: another
302 # group or version serving the Kind leaves it at rung 5 too (karr k43).
303 # A discovery *failure* (cluster unreachable, expired token) is
304 # deliberately treated the same as "not served" here -- rung 5,
305 # fail-closed -- with the reason kept in
306 # _discovery_error; on the CRUD path the carp from
307 # _load_resource_map_from_cluster has already named it while building the
308 # cluster map.
309 104 ▶ return 'IO::K8s::Unstructured'
Branch: 1 branch
BranchTrueFalseTotal
if $self->_discovery_path_meta($kind, $api_version) 38 66 104
310 if $self->_discovery_path_meta($kind, $api_version);
311  
312 66 return $class;
313 }
314  
315 # Whether $class, what expand_class() made of a name whose Kind is $kind (from
316 # _kind_from_expand_args), is the signal that nothing resolved it: undef, or
317 # the 'IO::K8s::<Kind>' IO::K8s fabricates for a bare Kind when that class does
318 # not load (see expand_class). Every real resolution is resolved, including a
319 # '+'-class that is not loaded yet.
320 660 ▶sub _is_unresolved {
Subroutine _is_unresolved: called CC=5 SCAR=16.8
321 660 my ($self, $class, $kind) = @_;
322 660 ▶ return 1 unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 73 587 660
323 return defined $kind
324 && $class eq "IO::K8s::$kind"
325 0 ▶ && !($class->can('new') || eval { require_module($class); 1 });
Condition66%6/9
defined $kind && $class eq "IO::K8s::$kind" && !($class->can('new') || eval { do { &require_module($class); 1 } })
!ll&&!rl&&r
507080
defined $kind and $class eq "IO::K8s::$kind"
!ll&&!rl&&r
5245580
$class->can('new') || eval { do { &require_module($class); 1 } }
l!l&&r!l&&!r
0080
MC/DC50%2/4
defined $kind && $class eq "IO::K8s::$kind" && !($class->can('new') || eval { do { &require_module($class); 1 } })
defined $kind $class eq "IO::K8s::$kind" $class->can('new') eval { do { &require_module($class); 1 } }
Truth table60%3/5
A and B && not(C || D)
A= defined $kindB= $class eq "IO::K8s::$kind"C= $class->can('new')D= eval { do { &require_module($class); 1 } }
ABCDresult
0XXX0
10XX0
111X0
11010
11001
326 }
327  
328 # expand_class() for the methods that take a resource name and build a request
329 # path from its class (list, get, patch, patch_status, delete, watch, log,
330 # port_forward, exec, attach) or load it (compare_schema). A name nothing
331 # resolves croaks here, naming it the way Net::Async::Kubernetes does, before
332 # any request (karr k46) - instead of in _build_path, where require_module saw
333 # only expand_class's answer: undef for a qualified name ("argument is not a
334 # module name"), or a bare Kind's fabricated class ("Can't locate
335 # IO/K8s/<Kind>.pm", naming a module that does not exist). A discovery
336 # failure is named with it: an unreachable cluster could not confirm the name,
337 # which is not the same as a cluster that does not serve it (karr k28). Only
338 # for a name with a Kind - expand_class consulted discovery for exactly that
339 # one, so the recorded failure is this call's. expand_class itself keeps
340 # returning undef or the fabricated name: that is its public contract, and
341 # _manifest_to_object reads the undef.
342 298 ▶sub _expand_class_or_croak {
Subroutine _expand_class_or_croak: called CC=6 SCAR=18.0
343 298 my ($self, $name) = @_;
344 298 my $class = $self->expand_class($name);
345 298 my ($kind) = $self->_kind_from_expand_args($name);
346 298 ▶ return $class unless $self->_is_unresolved($class, $kind);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_unresolved($class, $kind) 249 49 298
347 49 ▶ my $discovery_error = defined $kind ? $self->_discovery_error : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $kind ? : 49 0 49
348 49 ▶ $discovery_error = $self->_error_reason($discovery_error) if defined $discovery_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $discovery_error 3 46 49
349 49 ▶ croak "unknown resource '" . ($name // '(undef)') . "': no IO::K8s class"
Branch: 1 branch
BranchTrueFalseTotal
defined $discovery_error ? : 3 46 49
Condition50%1/2
$name // "(undef)"
l!l
490
MC/DC0%0/1
$name // "(undef)"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
350 . " for this apiVersion/kind (add it to resource_map if it is a CRD)"
351 . (defined $discovery_error
352 ? "; discovery failed, so the cluster could not confirm it: $discovery_error"
353 : '');
354 }
355  
356 # A caught error - a croak's string, or an APIError - as the reason another
357 # message embeds: its text without the location it ends with. That location
358 # names a line in here, or whatever frame a lazy builder left; the message it
359 # goes into names the caller's line itself (karr k59). Once a file handle has
360 # been read, Carp's location ends ', <$fh> line N.' (or 'chunk N').
361 44 ▶sub _error_reason {
Subroutine _error_reason: called CC=1 SCAR=0.0
362 44 my ($self, $error) = @_;
363 44 my $reason = "$error";
364 44 $reason =~ s/(?: at \S+ line \d+(?:, <[^>]*> (?:line|chunk) \d+)?\.)?\s*\z//;
365 44 return $reason;
366 }
367  
368 # carp, for a warning raised while a lazy attribute is built - the discovery
369 # catalog, the resource map built from it. Carp skips the frames of Moo's
370 # generated accessors (Moo marks them internal), but not the frame where this
371 # package calls into one, so a plain carp named the line in here that first
372 # asked for the attribute (karr k63, k64). Trusting the generated accessors
373 # for this one warning lets it name the caller's line, as a warning from a
374 # plain method does. Not package-wide: that would move every other croak.
375 16 ▶sub _carp_past_builders {
Subroutine _carp_past_builders: called CC=1 SCAR=0.0
376 16 my ($self, $message) = @_;
377 16 local our @CARP_NOT = ('Method::Generate::Accessor::_Generated');
378 16 carp $message;
379 16 return;
380 }
381  
382 # Extract the Kubernetes Kind (and any explicitly supplied apiVersion) from an
383 # expand_class() argument list, for the D16 rung-4 discovery-confirmation
384 # check. Only bare short names ('Widget') and domain-qualified strings
385 # ('example.com/v1/Widget') name a Kind that Unstructured should stand in for;
386 # a '+Full::Class', an 'IO::K8s::...' name or any multi-segment package name is
387 # an explicit class request and must never be diverted to Unstructured.
388 724 ▶sub _kind_from_expand_args {
Subroutine _kind_from_expand_args: called CC=7 SCAR=19.6
389 724 my ($self, $name, $api_version) = @_;
390 724 ▶ return () unless defined $name && !ref $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name 0 724 724
Condition33%1/3
defined $name and not ref $name
!ll&&!rl&&r
00724
MC/DC0%0/2
defined $name and not ref $name
defined $name not ref $name
Truth table33%1/3
A and not(B)
A= defined $nameB= not ref $name
ABresult
0X0
100
111
391 724 ▶ return () if $name =~ /^\+/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\+/ 5 719 724
392 719 ▶ return () if $name =~ /^IO::K8s::/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^IO::K8s::/ 100 619 719
393 619 ▶ if ($name =~ m{/}) { # 'group/version/Kind' -> ('Kind', 'group/version')
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[/]) 86 533 619
394 86 my ($av, $kind) = $name =~ m{\A(.*)/([^/]+)\z};
395 86 return ($kind, $av);
396 }
397 533 ▶ return () if $name =~ /::/; # a multi-segment class name, not a Kind
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /::/ 1 532 533
398 532 return ($name, $api_version);
399 }
400  
401 # Path metadata (apiVersion, resource plural, namespaced) for a Kind, read from
402 # the cached discovery catalog. This is how a Kind resolved to
403 # IO::K8s::Unstructured (D16 rung 4) gets what _build_path needs: Unstructured
404 # has apiVersion/kind on the *instance* but no api_version()/resource_plural()
405 # class method and no Namespaced role, so plural and scope cannot come from the
406 # class the way a typed resource's do -- they come from here.
407 #
408 # Returns undef (fail-closed) unless resource_map_from_cluster is set: with no
409 # cluster there is no discovery to confirm a GVK, and this must never trigger a
410 # fetch on the fetch-free path. When it does run, the catalog is already cached
411 # (building the cluster resource map fetched it), so the lookup is free.
412 #
413 # A fetch that fails outright (cluster unreachable, expired token) returns the
414 # same undef as a healthy catalog that lacks the Kind -- deliberately
415 # fail-closed (D16: an unreachable cluster must not let anything pass as
416 # confirmed) -- but the reason is kept in _discovery_error so _build_path can
417 # name it instead of claiming a missing entry.
418 #
419 # $want_api_version, when given (a qualified name's group/version, or an
420 # Unstructured object's own apiVersion), pins the group/version, and nothing
421 # else will do; without it the group's discovery-preferred version wins,
422 # matching _resource_map_from_catalog and D17.
423 141 ▶sub _discovery_path_meta {
Subroutine _discovery_path_meta: called CC=15 SCAR=27.8
424 141 my ($self, $kind, $want_api_version) = @_;
425 141 ▶ return unless $self->resource_map_from_cluster;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->resource_map_from_cluster 36 105 141
426 105 my $catalog = eval { $self->_discovery };
427 105 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 7 98 105
428 7 $self->_discovery_error($error);
429 7 return;
430 }
431 98 $self->_clear_discovery_error;
432 98 ▶ return unless $catalog && $catalog->{groups};
Branch: 1 branch
BranchTrueFalseTotal
unless $catalog and $catalog->{"groups"} 0 98 98
Condition33%1/3
$catalog and $catalog->{"groups"}
!ll&&!rl&&r
0098
MC/DC0%0/2
$catalog and $catalog->{"groups"}
$catalog $catalog->{"groups"}
Truth table33%1/3
A and B
A= $catalogB= $catalog->{"groups"}
ABresult
0X0
100
111
433  
434 64 ▶ my $meta_for = sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
435 64 my ($group, $version, $res) = @_;
436 return {
437 api_version => ($group eq '' ? $version : "$group/$version"),
438 resource => $res->{resource},
439 64 ▶ namespaced => ($res->{scope} && $res->{scope} eq 'Namespaced' ? 1 : 0),
Branch: 2 branches
BranchTrueFalseTotal
$group eq '' ? : 0 64 64
$res->{'scope'} && $res->{'scope'} eq 'Namespaced' ? : 62 2 64
Condition66%2/3
$res->{"scope"} and $res->{"scope"} eq "Namespaced"
!ll&&!rl&&r
0262
MC/DC50%1/2
$res->{"scope"} and $res->{"scope"} eq "Namespaced"
$res->{"scope"} $res->{"scope"} eq "Namespaced"
Truth table66%2/3
A and B
A= $res->{"scope"}B= $res->{"scope"} eq "Namespaced"
ABresult
0X0
100
111
440 };
441 98 };
442  
443 # An explicit apiVersion pins the exact group/version, and only that one.
444 # It names one GVK, and a cluster that does not serve that GVK has not
445 # confirmed it (D16) - not even when another group, or another version of
446 # the same group, serves a Kind of that name. Falling back there would
447 # address that other resource: list, delete and ensure_only's prune would
448 # land in it (karr k43). The same rule as IO::K8s's exact-GVK resolution,
449 # where an explicit version never falls back to the bare Kind's.
450 98 ▶ if (defined $want_api_version && length $want_api_version) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $want_api_version and length $want_api_version) 48 50 98
Condition66%2/3
defined $want_api_version and length $want_api_version
!ll&&!rl&&r
50048
MC/DC50%1/2
defined $want_api_version and length $want_api_version
defined $want_api_version length $want_api_version
Truth table66%2/3
A and B
A= defined $want_api_versionB= length $want_api_version
ABresult
0X0
100
111
451 48 ▶ my ($g, $v) = $want_api_version =~ m{/}
Branch: 1 branch
BranchTrueFalseTotal
$want_api_version =~ m[/] ? : 48 0 48
452 ? split(m{/}, $want_api_version, 2)
453 : ('', $want_api_version);
454 48 my $res = $catalog->{groups}{$g}{versions}{$v}{kinds}{$kind};
455 48 ▶ return unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 24 24 48
456 24 return $meta_for->($g, $v, $res);
457 }
458  
459 # A bare Kind: the preferred version of whichever group serves it (D17).
460 50 for my $group (sort keys %{$catalog->{groups}}) {
461 106 my $gdata = $catalog->{groups}{$group};
462 106 ▶ my @order = @{$gdata->{version_order} // []};
Condition50%1/2
$gdata->{"version_order"} // []
l!l
1060
MC/DC0%0/1
$gdata->{"version_order"} // []
$gdata->{"version_order"}
Truth table50%1/2
A
A= $gdata->{"version_order"}
Aresult
11
00
463 106 my $pref = $gdata->{preferred};
464 107 @order = ($pref, grep { $_ ne $pref } @order)
465 106 ▶ if defined $pref && grep { $_ eq $pref } @order;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pref and grep {$_ eq $pref;} @order 106 0 106
Condition33%1/3
defined $pref and grep {$_ eq $pref;} @order
!ll&&!rl&&r
00106
MC/DC0%0/2
defined $pref and grep {$_ eq $pref;} @order
defined $pref grep {$_ eq $pref;} @order
Truth table33%1/3
A and B
A= defined $prefB= grep {$_ eq $pref;} @order
ABresult
0X0
100
111
466 106 for my $version (@order) {
467 106 my $res = $gdata->{versions}{$version}{kinds}{$kind};
468 106 ▶ return $meta_for->($group, $version, $res) if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 40 66 106
469 }
470 }
471 10 return;
472 }
473  
474 # The extra _build_path arguments an IO::K8s::Unstructured resolution needs,
475 # derived from whatever identifier expand_class was given: an IO::K8s object
476 # (its kind/apiVersion accessors) or a name string. A string is split the way
477 # expand_class split it to confirm the GVK, so a qualified
478 # 'example.org/v1/Widget' keeps its group/version and does not land in
479 # whichever other group serving a Widget discovery lists first. Empty for
480 # every typed class, so typed path building is completely unchanged.
481 415 ▶sub _unstructured_hint {
Subroutine _unstructured_hint: called CC=7 SCAR=19.7
482 415 my ($self, $class, $ident) = @_;
483 415 ▶ return () unless defined $class && $class eq 'IO::K8s::Unstructured';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class and $class eq "IO::K8s::Unstructured" 391 24 415
Condition66%2/3
defined $class and $class eq "IO::K8s::Unstructured"
!ll&&!rl&&r
039124
MC/DC50%1/2
defined $class and $class eq "IO::K8s::Unstructured"
defined $class $class eq "IO::K8s::Unstructured"
Truth table66%2/3
A and B
A= defined $classB= $class eq "IO::K8s::Unstructured"
ABresult
0X0
100
111
484 24 ▶ if (blessed($ident)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($ident)) 15 9 24
485 return (
486 15 ▶ kind => $ident->kind,
Branch: 1 branch
BranchTrueFalseTotal
defined $ident->apiVersion ? : 15 0 15
487 (defined $ident->apiVersion ? (api_version => $ident->apiVersion) : ()),
488 );
489 }
490 9 my ($kind, $api_version) = $self->_kind_from_expand_args($ident);
491 return (
492 9 ▶ (defined $kind ? (kind => $kind) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $kind ? : 9 0 9
defined $api_version ? : 3 6 9
493 (defined $api_version ? (api_version => $api_version) : ()),
494 );
495 }
496  
497 # Kubernetes groups whose IO::K8s classes do NOT live under IO::K8s::Api::.
498 # Their namespace follows the upstream Go staging repository the types are
499 # generated from (apiextensions-apiserver, kube-aggregator), not the API group
500 # name, so it cannot be derived - it has to be listed here.
501 #
502 # Keyed on the full group name, because that is the only globally unique
503 # identifier: a cluster is free to serve a CRD group called
504 # apiextensions.example.com, and that group is an ordinary one.
505 97 my %NON_API_NAMESPACE = (
506 'apiextensions.k8s.io' => 'ApiextensionsApiserver::Pkg::Apis::Apiextensions',
507 'apiregistration.k8s.io' => 'KubeAggregator::Pkg::Apis::Apiregistration',
508 );
509  
510 # The same table, indexed by the IO::K8s group directory instead - the trailing
511 # segment of each namespace above is exactly that directory name. Derived, so
512 # there is still only one place to add a group.
513 my %NON_API_NAMESPACE_BY_GROUP_PATH =
514 97 map { ((split /::/)[-1] => $_) } values %NON_API_NAMESPACE;
515  
516 # Namespace below IO::K8s:: for a Kubernetes API group name as the cluster's
517 # OpenAPI spec reports it: '' for core, 'apps', 'apiextensions.k8s.io',
518 # 'cert-manager.io', ... The exception table is matched exactly on the full
519 # name - a CRD group that merely starts with 'apiextensions' is an ordinary
520 # group and must keep landing under Api::.
521 209 ▶sub _io_k8s_namespace_for_group {
Subroutine _io_k8s_namespace_for_group: called CC=3 SCAR=11.0
522 209 my ($self, $group) = @_;
523 209 ▶ return $NON_API_NAMESPACE{$group} if exists $NON_API_NAMESPACE{$group};
Branch: 1 branch
BranchTrueFalseTotal
if exists $NON_API_NAMESPACE{$group} 15 194 209
524 194 ▶ return 'Api::' . ($group eq '' ? 'Core' : ucfirst(lc((split /\./, $group)[0])));
Branch: 1 branch
BranchTrueFalseTotal
$group eq '' ? : 79 115 194
525 }
526  
527 # Namespace below IO::K8s:: for an IO::K8s group directory name - 'Core',
528 # 'Apps', 'Apiextensions'. That is the form the v0 compatibility layer carries,
529 # and it is a closed set of names this distribution ships, so unlike a group
530 # name off the wire it needs no exact-match guard.
531 80 ▶sub _io_k8s_namespace_for_group_path {
Subroutine _io_k8s_namespace_for_group_path: called CC=2 SCAR=6.9
532 80 my ($self, $group_path) = @_;
533 80 ▶ return $NON_API_NAMESPACE_BY_GROUP_PATH{$group_path} // "Api::${group_path}";
Condition100%2/2
$NON_API_NAMESPACE_BY_GROUP_PATH{$group_path} // "Api::$group_path"
l!l
971
MC/DC100%1/1
$NON_API_NAMESPACE_BY_GROUP_PATH{$group_path} // "Api::$group_path"
$NON_API_NAMESPACE_BY_GROUP_PATH{$group_path}
Truth table100%2/2
A
A= $NON_API_NAMESPACE_BY_GROUP_PATH{$group_path}
Aresult
11
00
534 }
535  
536 # Public method to build the resource map from the cluster's discovery catalog
537 85 ▶sub fetch_resource_map {
Subroutine fetch_resource_map: called CC=2 SCAR=6.9
Pod documented
538 85 my ($self) = @_;
539  
540  
541 # The cluster resource map is built from aggregated discovery, not from the
542 # multi-MB /openapi/v2 spec (design D11): a small GET /api + GET /apis gives
543 # every group, version, plural, Kind and scope. The catalog is cached in the
544 # _discovery attribute and shared by later rebuilds; /openapi/v2 stays lazy
545 # for schema_for/compare_schema only. The failure keeps this method's
546 # documented wording; the underlying error rides along.
547 85 my $catalog = eval { $self->_discovery };
548 85 ▶ croak 'Could not load resource map from cluster: ' . $self->_error_reason($@)
Branch: 1 branch
BranchTrueFalseTotal
unless $catalog 17 68 85
549 unless $catalog;
550  
551 68 return $self->_resource_map_from_catalog($catalog);
552 }
553  
554 # Turn the cached discovery catalog into the short-name -> IO::K8s class map.
555 # The namespace comes from _io_k8s_namespace_for_group (exception tables
556 # included) and List kinds are skipped. Two rules shape which entries land:
557 #
558 # D17 preferred version: among the versions a group serves a Kind in, the
559 # version the cluster marks preferred wins the bare short name (not "stable
560 # beats alpha/beta" -- that heuristic is gone). A Kind served only outside
561 # the preferred version still maps, to its first served version, so nothing
562 # the cluster serves is dropped.
563 #
564 # D12/D13 stop inventing: an entry is recorded only when the candidate
565 # IO::K8s class actually ships. A foreign group (cilium.io, cert-manager.io)
566 # has no IO::K8s::Api::<Group> class, so its Kinds are omitted here and left
567 # to resolve through the inner IO::K8s pipeline (provider from `with` ->
568 # AutoGen from openapi_spec -> D16 Unstructured -> fail-closed) rather than
569 # short-circuiting to a bogus name. Core-API groups and the two exception
570 # groups (apiextensions/apiregistration) do ship, so their Kinds map exactly
571 # as before.
572 68 ▶sub _resource_map_from_catalog {
Subroutine _resource_map_from_catalog: called CC=11 SCAR=24.0
573 68 my ($self, $catalog) = @_;
574  
575 68 my %map;
576 68 ▶ for my $group (keys %{$catalog->{groups} // {}}) {
Condition50%1/2
$catalog->{"groups"} // {}
l!l
680
MC/DC0%0/1
$catalog->{"groups"} // {}
$catalog->{"groups"}
Truth table50%1/2
A
A= $catalog->{"groups"}
Aresult
11
00
577 168 my $gdata = $catalog->{groups}{$group};
578  
579 # D17: try the cluster's preferred version first, then the rest in
580 # discovery order; the first version to yield a shipping class for a
581 # Kind keeps the short name. Where the preferred version's class does
582 # not ship but another served version's does, the Kind still maps to
583 # that other version rather than vanishing.
584 168 ▶ my @order = @{$gdata->{version_order} // []};
Condition100%2/2
$gdata->{"version_order"} // []
l!l
1671
MC/DC100%1/1
$gdata->{"version_order"} // []
$gdata->{"version_order"}
Truth table100%2/2
A
A= $gdata->{"version_order"}
Aresult
11
00
585 168 my $pref = $gdata->{preferred};
586 180 @order = ($pref, grep { $_ ne $pref } @order)
587 168 ▶ if defined $pref && grep { $_ eq $pref } @order;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pref and grep {$_ eq $pref;} @order 167 1 168
Condition66%2/3
defined $pref and grep {$_ eq $pref;} @order
!ll&&!rl&&r
10167
MC/DC50%1/2
defined $pref and grep {$_ eq $pref;} @order
defined $pref grep {$_ eq $pref;} @order
Truth table66%2/3
A and B
A= defined $prefB= grep {$_ eq $pref;} @order
ABresult
0X0
100
111
588  
589 168 for my $version (@order) {
590 180 my $kinds = $gdata->{versions}{$version}{kinds};
591 180 for my $kind (keys %$kinds) {
592 210 ▶ next if $kind =~ /List$/;
Branch: 1 branch
BranchTrueFalseTotal
if $kind =~ /List$/ 1 209 210
593 209 ▶ next if exists $map{$kind}; # a preferred/earlier version won it
Branch: 1 branch
BranchTrueFalseTotal
if exists $map{$kind} 7 202 209
594  
595 202 my $version_path = ucfirst($version);
596 202 my $new_path = $self->_io_k8s_namespace_for_group($group)
597 . "::${version_path}::${kind}";
598  
599 # D12/D13: only record a class name this distribution ships.
600 202 ▶ next unless $self->_io_k8s_class_ships($new_path);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_io_k8s_class_ships($new_path) 88 114 202
601  
602 114 $map{$kind} = $new_path;
603 }
604 }
605 }
606  
607 68 return \%map;
608 }
609  
610 # Process-wide cache for the D12/D13 "stop inventing" loadability probe: a
611 # relative class path is either shipped (a real IO::K8s class that loads) or it
612 # is not, and that never changes within a run. Keyed by the relative path, so
613 # it is shared across instances.
614 97 my %_CLASS_SHIPS;
615  
616 # True when 'IO::K8s::<rel_path>' is a real, loadable class. The discovery map
617 # is built from group/version/Kind triples off the wire; without this probe
618 # every foreign CRD group would map to an IO::K8s::Api::<Group>::... name that
619 # has no class behind it (design D12). require the candidate once -- already
620 # loaded classes short-circuit on ->can('new') -- and remember the answer.
621 202 ▶sub _io_k8s_class_ships {
Subroutine _io_k8s_class_ships: called CC=4 SCAR=13.9
622 202 my ($self, $rel_path) = @_;
623 202 ▶ return $_CLASS_SHIPS{$rel_path} //= do {
Condition66%2/3
$_CLASS_SHIPS{$rel_path} //= do { my $full = "IO::K8s::$rel_path"; $full->can("new") || eval { do { &require_module($full); 1 } } ? 1 : 0 }
l!l&&r!l&&!r
148540
MC/DC0%0/2
$_CLASS_SHIPS{$rel_path} //= do { my $full = "IO::K8s::$rel_path"; $full->can("new") || eval { do { &require_module($full); 1 } } ? 1 : 0 }
$_CLASS_SHIPS{$rel_path} do { my $full = "IO::K8s::$rel_path"; $full->can("new") || eval { do { &require_module($full); 1 } } ? 1 : 0 }
Truth table66%2/3
A //= B
A= $_CLASS_SHIPS{$rel_path}B= do { my $full = "IO::K8s::$rel_path"; $full->can("new") || eval { do { &require_module($full); 1 } } ? 1 : 0 }
ABresult
1X1
011
000
624 54 my $full = "IO::K8s::$rel_path";
625 54 ▶ ($full->can('new') || eval { require_module($full); 1 }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$full->can('new') || eval { do { &require_module($full); 1 } } ? : 32 22 54
Condition100%3/3
$full->can("new") or eval { do { &require_module($full); 1 } }
l!l&&r!l&&!r
72522
MC/DC100%2/2
$full->can("new") or eval { do { &require_module($full); 1 } }
$full->can("new") eval { do { &require_module($full); 1 } }
Truth table100%3/3
A or B
A= $full->can("new")B= eval { do { &require_module($full); 1 } }
ABresult
1X1
011
000
626 };
627 }
628  
629 # Per-instance discovery catalog, keyed by group. Shape:
630 # { groups => { <group> => {
631 # preferred => <version>, # first version the cluster serves
632 # version_order => [ <version>, ... ], # discovery order, preferred first
633 # versions => { <version> => { kinds => {
634 # <Kind> => { resource => <plural>, scope => 'Namespaced'|'Cluster' },
635 # } } },
636 # } } }
637 # Built lazily on first use - or handed over by absorb_discovery - and
638 # invalidated by invalidate_discovery.
639 has _discovery => (
640 is => 'lazy',
641 predicate => '_has_discovery',
642 clearer => '_clear_discovery',
643 writer => '_set_discovery', # absorb_discovery
644 52 ▶ builder => sub { $_[0]->_fetch_discovery },
Subroutine _build__discovery: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
645 97 );
646  
647 # The reason the last discovery fetch attempted from _discovery_path_meta
648 # failed - its $@, for an HTTP error status an APIError (karr k59), rendered
649 # with _error_reason - kept so the croak in _build_path can name it: a
650 # failed fetch and a healthy catalog without the Kind both come back from
651 # _discovery_path_meta as undef (fail-closed, D16), and only this tells them
652 # apart. Cleared by the next successful fetch and by invalidate_discovery.
653 97 has _discovery_error => (
654 is => 'rw',
655 clearer => '_clear_discovery_error',
656 );
657  
658 # Aggregated discovery v2 (Kubernetes >= 1.27): GET /api and GET /apis with an
659 # Accept header selecting APIGroupDiscoveryList answer every group, version,
660 # resource plural, Kind and scope in one small response each. A server that does
661 # not understand the header ignores the as= parameter and returns the legacy
662 # document (APIVersions / APIGroupList) - detected by the kind - and the
663 # per-group APIResourceList fallback fills the catalog instead.
664 97 my $DISCOVERY_ACCEPT =
665 'application/json;g=apidiscovery.k8s.io;v=v2;as=APIGroupDiscoveryList';
666  
667 # The two discovery documents, in the order they are read.
668 97 my @DISCOVERY_ROOTS = ('/api', '/apis');
669  
670 89 ▶sub _fetch_discovery {
Subroutine _fetch_discovery: called CC=4 SCAR=13.9
671 89 my ($self) = @_;
672  
673 89 my $catalog = { groups => {} };
674  
675 89 for my $root (@DISCOVERY_ROOTS) {
676 154 my ($body, $aggregated) = $self->_discovery_document($root,
677 $self->io->call($self->_discovery_request($root)));
678  
679 130 ▶ if ($aggregated) {
Branch: 2 branches
BranchTrueFalseTotal
if ($aggregated) { } 122 8 130
elsif ($root eq '/api') { } 4 4 8
680 122 $self->_absorb_discovery_list($catalog, $body);
681 } elsif ($root eq '/api') {
682 4 $self->_fetch_discovery_legacy_core($catalog, $body);
683 } else {
684 4 $self->_fetch_discovery_legacy_groups($catalog, $body);
685 }
686 }
687  
688 65 return $catalog;
689 }
690  
691 # The request for one discovery root: what _fetch_discovery sends, and what
692 # prepare_discovery_requests hands an async client to send.
693 156 ▶sub _discovery_request {
Subroutine _discovery_request: called CC=1 SCAR=0.0
694 156 my ($self, $root) = @_;
695 156 return $self->_prepare_request('GET', $root,
696 headers => { Accept => $DISCOVERY_ACCEPT });
697 }
698  
699 # One discovery root's response, checked and decoded - for the synchronous
700 # fetch and absorb_discovery alike. An HTTP error dies as an APIError like
701 # any other checked response, carrying the body that says why (karr k59);
702 # the second value says whether the document is aggregated discovery
703 # (APIGroupDiscoveryList) rather than the legacy one.
704 178 ▶sub _discovery_document {
Subroutine _discovery_document: called CC=3 SCAR=11.3
705 178 my ($self, $root, $response) = @_;
706 178 $self->_check_response($response, "discovery GET $root");
707 151 my $body = $self->_json->decode($response->content);
708 151 ▶ my $kind = ref $body eq 'HASH' ? ($body->{kind} // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $body eq 'HASH' ? : 151 0 151
Condition50%1/2
$body->{"kind"} // ""
l!l
1510
MC/DC0%0/1
$body->{"kind"} // ""
$body->{"kind"}
Truth table50%1/2
A
A= $body->{"kind"}
Aresult
11
00
709 151 return ($body, $kind eq 'APIGroupDiscoveryList');
710 }
711  
712 # APIGroupDiscoveryList (aggregated discovery v2). Each item is one group; the
713 # order of its versions is the cluster's preference, so the first is preferred.
714 136 ▶sub _absorb_discovery_list {
Subroutine _absorb_discovery_list: called CC=15 SCAR=29.5
715 136 my ($self, $catalog, $body) = @_;
716  
717 136 ▶ for my $group_item (@{$body->{items} // []}) {
Condition50%1/2
$body->{"items"} // []
l!l
1360
MC/DC0%0/1
$body->{"items"} // []
$body->{"items"}
Truth table50%1/2
A
A= $body->{"items"}
Aresult
11
00
718 168 ▶ my $group = $group_item->{metadata}{name} // '';
Condition50%1/2
$group_item->{"metadata"}{"name"} // ""
l!l
1680
MC/DC0%0/1
$group_item->{"metadata"}{"name"} // ""
$group_item->{"metadata"}{"name"}
Truth table50%1/2
A
A= $group_item->{"metadata"}{"name"}
Aresult
11
00
719 168 ▶ my @versions = @{$group_item->{versions} // []};
Condition50%1/2
$group_item->{"versions"} // []
l!l
1680
MC/DC0%0/1
$group_item->{"versions"} // []
$group_item->{"versions"}
Truth table50%1/2
A
A= $group_item->{"versions"}
Aresult
11
00
720 168 for my $i (0 .. $#versions) {
721 180 my $vd = $versions[$i];
722 180 my $version = $vd->{version};
723 180 ▶ next unless defined $version && length $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version and length $version 0 180 180
Condition33%1/3
defined $version and length $version
!ll&&!rl&&r
00180
MC/DC0%0/2
defined $version and length $version
defined $version length $version
Truth table33%1/3
A and B
A= defined $versionB= length $version
ABresult
0X0
100
111
724 180 ▶ for my $res (@{$vd->{resources} // []}) {
Condition50%1/2
$vd->{"resources"} // []
l!l
1800
MC/DC0%0/1
$vd->{"resources"} // []
$vd->{"resources"}
Truth table50%1/2
A
A= $vd->{"resources"}
Aresult
11
00
725 211 my $plural = $res->{resource};
726 211 my $rkind = $res->{responseKind}{kind};
727 211 ▶ next unless defined $plural && defined $rkind;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plural and defined $rkind 0 211 211
Condition33%1/3
defined $plural and defined $rkind
!ll&&!rl&&r
00211
MC/DC0%0/2
defined $plural and defined $rkind
defined $plural defined $rkind
Truth table33%1/3
A and B
A= defined $pluralB= defined $rkind
ABresult
0X0
100
111
728 $self->_catalog_add($catalog, $group, $version, $rkind,
729 211 ▶ $plural, $res->{scope} // '');
Condition50%1/2
$res->{"scope"} // ""
l!l
2110
MC/DC0%0/1
$res->{"scope"} // ""
$res->{"scope"}
Truth table50%1/2
A
A= $res->{"scope"}
Aresult
11
00
730 }
731 # First version served = preferred version for the group.
732 $catalog->{groups}{$group}{preferred} = $version
733 180 ▶ if $i == 0 && exists $catalog->{groups}{$group};
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 and exists $catalog->{"groups"}{$group} 167 13 180
Condition100%3/3
$i == 0 and exists $catalog->{"groups"}{$group}
!ll&&!rl&&r
121167
MC/DC100%2/2
$i == 0 and exists $catalog->{"groups"}{$group}
$i == 0 exists $catalog->{"groups"}{$group}
Truth table100%3/3
A and B
A= $i == 0B= exists $catalog->{"groups"}{$group}
ABresult
0X0
100
111
734 }
735 }
736 }
737  
738 # Legacy fallback for GET /api: an APIVersions document. Each version's
739 # APIResourceList is fetched from /api/<version>.
740 4 ▶sub _fetch_discovery_legacy_core {
Subroutine _fetch_discovery_legacy_core: called CC=6 SCAR=18.0
741 4 my ($self, $catalog, $body) = @_;
742  
743 4 ▶ my @versions = @{$body->{versions} // []};
Condition50%1/2
$body->{"versions"} // []
l!l
40
MC/DC0%0/1
$body->{"versions"} // []
$body->{"versions"}
Truth table50%1/2
A
A= $body->{"versions"}
Aresult
11
00
744 4 for my $version (@versions) {
745 4 my $path = "/api/$version";
746 4 my $response = $self->_request('GET', $path);
747 4 ▶ next unless $self->_legacy_resource_list_ok($response, $version, $path);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_legacy_resource_list_ok($response, $version, $path) 1 3 4
748 3 my $list = $self->_json->decode($response->content);
749 3 $self->_absorb_api_resource_list($catalog, '', $version, $list);
750 }
751 $catalog->{groups}{''}{preferred} = $versions[0]
752 4 ▶ if @versions && exists $catalog->{groups}{''};
Branch: 1 branch
BranchTrueFalseTotal
if @versions and exists $catalog->{"groups"}{""} 3 1 4
Condition66%2/3
@versions and exists $catalog->{"groups"}{""}
!ll&&!rl&&r
013
MC/DC50%1/2
@versions and exists $catalog->{"groups"}{""}
@versions exists $catalog->{"groups"}{""}
Truth table66%2/3
A and B
A= @versionsB= exists $catalog->{"groups"}{""}
ABresult
0X0
100
111
753 }
754  
755 # Legacy fallback for GET /apis: an APIGroupList document. Each group/version's
756 # APIResourceList is fetched from /apis/<group>/<version>.
757 4 ▶sub _fetch_discovery_legacy_groups {
Subroutine _fetch_discovery_legacy_groups: called CC=12 SCAR=26.4
758 4 my ($self, $catalog, $body) = @_;
759  
760 4 ▶ for my $group (@{$body->{groups} // []}) {
Condition50%1/2
$body->{"groups"} // []
l!l
40
MC/DC0%0/1
$body->{"groups"} // []
$body->{"groups"}
Truth table50%1/2
A
A= $body->{"groups"}
Aresult
11
00
761 10 my $gname = $group->{name};
762 10 ▶ next unless defined $gname && length $gname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gname and length $gname 0 10 10
Condition33%1/3
defined $gname and length $gname
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $gname and length $gname
defined $gname length $gname
Truth table33%1/3
A and B
A= defined $gnameB= length $gname
ABresult
0X0
100
111
763 10 ▶ for my $v (@{$group->{versions} // []}) {
Condition50%1/2
$group->{"versions"} // []
l!l
100
MC/DC0%0/1
$group->{"versions"} // []
$group->{"versions"}
Truth table50%1/2
A
A= $group->{"versions"}
Aresult
11
00
764 10 my $version = $v->{version};
765 10 ▶ next unless defined $version && length $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version and length $version 0 10 10
Condition33%1/3
defined $version and length $version
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $version and length $version
defined $version length $version
Truth table33%1/3
A and B
A= defined $versionB= length $version
ABresult
0X0
100
111
766 10 my $path = "/apis/$gname/$version";
767 10 my $response = $self->_request('GET', $path);
768 10 ▶ next unless $self->_legacy_resource_list_ok($response, "$gname/$version", $path);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_legacy_resource_list_ok($response, "$gname/$version", $path) 6 4 10
769 4 my $list = $self->_json->decode($response->content);
770 4 $self->_absorb_api_resource_list($catalog, $gname, $version, $list);
771 }
772 10 my $pref = $group->{preferredVersion}{version};
773 $catalog->{groups}{$gname}{preferred} = $pref
774 10 ▶ if defined $pref && exists $catalog->{groups}{$gname};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pref and exists $catalog->{"groups"}{$gname} 4 6 10
Condition66%2/3
defined $pref and exists $catalog->{"groups"}{$gname}
!ll&&!rl&&r
064
MC/DC50%1/2
defined $pref and exists $catalog->{"groups"}{$gname}
defined $pref exists $catalog->{"groups"}{$gname}
Truth table66%2/3
A and B
A= defined $prefB= exists $catalog->{"groups"}{$gname}
ABresult
0X0
100
111
775 }
776 }
777  
778 # Whether the legacy APIResourceList response for one group/version
779 # ($api_version, fetched from $path) can be read into the catalog. An error
780 # status leaves that group/version's Kinds out of the catalog, and so out of
781 # the resource map: any other status than 404 warns, naming it and the reason
782 # the APIError gives, and the other groups are read on (karr k63). A 404 is
783 # silent - the group/version went away between the group list and this
784 # request.
785 14 ▶sub _legacy_resource_list_ok {
Subroutine _legacy_resource_list_ok: called CC=3 SCAR=11.0
786 14 my ($self, $response, $api_version, $path) = @_;
787 14 ▶ return 1 if $response->status < 400;
Branch: 1 branch
BranchTrueFalseTotal
if $response->status < 400 7 7 14
788 7 ▶ return 0 if $response->status == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $response->status == 404 4 3 7
789 3 eval { $self->_check_response($response, "discovery GET $path") };
790 3 $self->_carp_past_builders("discovery: cannot read apiVersion '$api_version',"
791 . ' its Kinds are missing from the resource map: ' . $self->_error_reason($@));
792 3 return 0;
793 }
794  
795 # APIResourceList (legacy per-group discovery). Subresource entries carry a
796 # slash in their name (pods/status) and are skipped; scope comes from the
797 # namespaced boolean.
798 7 ▶sub _absorb_api_resource_list {
Subroutine _absorb_api_resource_list: called CC=8 SCAR=21.6
799 7 my ($self, $catalog, $group, $version, $list) = @_;
800  
801 7 ▶ for my $res (@{$list->{resources} // []}) {
Condition50%1/2
$list->{"resources"} // []
l!l
70
MC/DC0%0/1
$list->{"resources"} // []
$list->{"resources"}
Truth table50%1/2
A
A= $list->{"resources"}
Aresult
11
00
802 9 my $name = $res->{name};
803 9 ▶ next unless defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 9 9
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
009
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
804 9 ▶ next if $name =~ m{/};
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[/] 1 8 9
805 8 my $kind = $res->{kind};
806 8 ▶ next unless defined $kind;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kind 0 8 8
807 8 ▶ my $scope = $res->{namespaced} ? 'Namespaced' : 'Cluster';
Branch: 1 branch
BranchTrueFalseTotal
$res->{'namespaced'} ? : 7 1 8
808 8 $self->_catalog_add($catalog, $group, $version, $kind, $name, $scope);
809 }
810 }
811  
812 # Register one Kind (plural, scope) under a group/version in the catalog,
813 # tracking version discovery order.
814 219 ▶sub _catalog_add {
Subroutine _catalog_add: called CC=3 SCAR=11.0
815 219 my ($self, $catalog, $group, $version, $kind, $plural, $scope) = @_;
816  
817 219 ▶ my $gdata = $catalog->{groups}{$group} ||= {
Condition100%2/2
$catalog->{"groups"}{$group} ||= {"preferred", undef, "version_order", [], "versions", {}}
l!l
45174
MC/DC100%1/1
$catalog->{"groups"}{$group} ||= {"preferred", undef, "version_order", [], "versions", {}}
$catalog->{"groups"}{$group}
Truth table100%2/2
A
A= $catalog->{"groups"}{$group}
Aresult
11
00
818 preferred => undef, version_order => [], versions => {},
819 };
820 219 ▶ unless (exists $gdata->{versions}{$version}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $gdata->{"versions"}{$version}) 186 33 219
821 186 $gdata->{versions}{$version} = { kinds => {} };
822 186 push @{$gdata->{version_order}}, $version;
823 }
824 219 $gdata->{versions}{$version}{kinds}{$kind} = {
825 resource => $plural,
826 scope => $scope,
827 };
828 }
829  
830 13 ▶sub invalidate_discovery {
Subroutine invalidate_discovery: called CC=4 SCAR=13.9
Pod documented
831 13 my ($self) = @_;
832  
833  
834 13 $self->_clear_discovery;
835 13 $self->_clear_discovery_error;
836 # Only a map built from the catalog goes, and with it the inner IO::K8s
837 # that captured it at build time, so both are rebuilt on next use. A map
838 # passed to the constructor is the caller's: rebuilding it would drop
839 # their '+My::Class' entries (karr k52), as absorb_discovery knows too.
840 13 ▶ if ($self->_has_resource_map && $self->_resource_map_built) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_resource_map and $self->_resource_map_built) 6 7 13
Condition100%3/3
$self->_has_resource_map and $self->_resource_map_built
!ll&&!rl&&r
166
MC/DC100%2/2
$self->_has_resource_map and $self->_resource_map_built
$self->_has_resource_map $self->_resource_map_built
Truth table100%3/3
A and B
A= $self->_has_resource_mapB= $self->_resource_map_built
ABresult
0X0
100
111
841 6 $self->_clear_resource_map;
842 6 ▶ $self->_clear_k8s if $self->_has_k8s;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_k8s 5 1 6
843 }
844 13 return 1;
845 }
846  
847 1 ▶sub prepare_discovery_requests {
Subroutine prepare_discovery_requests: called CC=1 SCAR=0.0
Pod documented
848 1 my ($self) = @_;
849  
850  
851 1 return map { ($_ => $self->_discovery_request($_)) } @DISCOVERY_ROOTS;
852 }
853  
854 14 ▶sub absorb_discovery {
Subroutine absorb_discovery: called CC=9 SCAR=22.0
Pod documented
855 14 my ($self, %responses) = @_;
856  
857  
858 14 $self->_croak_unknown_args('absorb_discovery', \%responses, @DISCOVERY_ROOTS);
859 13 my @lists;
860 13 for my $root (@DISCOVERY_ROOTS) {
861 25 ▶ my $response = $responses{$root}
Branch: 1 branch
BranchTrueFalseTotal
my $response = $responses{$root} or croak("absorb_discovery requires the response for $root") 24 1 25
862 or croak "absorb_discovery requires the response for $root";
863 24 my ($body, $aggregated) = $self->_discovery_document($root, $response);
864 21 ▶ push @lists, $aggregated ? $body : undef;
Branch: 1 branch
BranchTrueFalseTotal
$aggregated ? : 19 2 21
865 }
866 # Legacy discovery needs a request per group/version on top, which only
867 # the synchronous path makes: nothing is cached, and the caller falls
868 # back to that path.
869 9 ▶ return if grep { !defined } @lists;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @lists 2 7 9
870  
871 7 my $catalog = { groups => {} };
872 7 $self->_absorb_discovery_list($catalog, $_) for @lists;
873  
874 # What an earlier catalog built goes, so this one counts: a resource map
875 # built from it, and the inner IO::K8s holding that map. A map passed to
876 # the constructor stays - a caller's '+My::Class' entries must survive.
877 7 ▶ if ($self->_has_resource_map && $self->_resource_map_built) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_resource_map and $self->_resource_map_built) 3 4 7
Condition100%3/3
$self->_has_resource_map and $self->_resource_map_built
!ll&&!rl&&r
223
MC/DC100%2/2
$self->_has_resource_map and $self->_resource_map_built
$self->_has_resource_map $self->_resource_map_built
Truth table100%3/3
A and B
A= $self->_has_resource_mapB= $self->_resource_map_built
ABresult
0X0
100
111
878 3 $self->_clear_resource_map;
879 3 ▶ $self->_clear_k8s if $self->_has_k8s;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_k8s 3 0 3
880 }
881 7 $self->_clear_discovery_error;
882 7 $self->_set_discovery($catalog);
883 7 return 1;
884 }
885  
886 # The full OpenAPI spec from the cluster, once _fetch_openapi_spec has fetched
887 # it (cached). The only /openapi/v2 download in the client, paid for by
888 # schema_for/compare_schema and (once resolution needs it) AutoGen, never by
889 # construction.
890 97 has _openapi_spec => (
891 is => 'ro',
892 predicate => '_has_openapi_spec',
893 writer => '_set_openapi_spec',
894 );
895  
896 # Fetch /openapi/v2 and keep it. Not a lazy builder: an error status dies as
897 # an APIError like any other checked response, naming the caller's line (karr
898 # k55) - and a Moo-generated accessor between the caller and the check breaks
899 # Carp's trust chain, so from a builder it named a line in here. A failed
900 # fetch keeps nothing, and the next use fetches again.
901 20 ▶sub _fetch_openapi_spec {
Subroutine _fetch_openapi_spec: called CC=2 SCAR=6.9
902 20 my ($self) = @_;
903 20 my $response = $self->_request('GET', '/openapi/v2');
904 20 $self->_check_response($response, 'fetch OpenAPI spec');
905 16 my $spec = $self->_json->decode($response->content);
906 16 $self->_set_openapi_spec($spec);
907 # D12: the inner IO::K8s was built without a spec (so building it never
908 # forced this fetch). Now that the spec exists, drop the cached instance
909 # so its next build passes it through as openapi_spec for AutoGen -- the
910 # same rebuild-on-next-use pattern invalidate_discovery uses.
911 16 ▶ $self->_clear_k8s if $self->_has_k8s;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_k8s 3 13 16
912 16 return $spec;
913 }
914  
915 # Get schema definition for a specific type
916 # $kind can be: 'Pod', 'IO::K8s::Api::Core::V1::Pod', or OpenAPI name like 'io.k8s.api.core.v1.Pod'
917 30 ▶sub schema_for {
Subroutine schema_for: called CC=19 SCAR=29.7
Pod documented
918 30 my ($self, $kind) = @_;
919  
920  
921 30 ▶ my $spec = $self->_has_openapi_spec ? $self->_openapi_spec : $self->_fetch_openapi_spec;
Branch: 1 branch
BranchTrueFalseTotal
$self->_has_openapi_spec ? : 10 20 30
922 26 ▶ my $defs = $spec->{definitions} // {};
Condition50%1/2
$spec->{"definitions"} // {}
l!l
260
MC/DC0%0/1
$spec->{"definitions"} // {}
$spec->{"definitions"}
Truth table50%1/2
A
A= $spec->{"definitions"}
Aresult
11
00
923  
924 # If it's already an OpenAPI definition name
925 26 ▶ if (exists $defs->{$kind}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $defs->{$kind}) 7 19 26
926 7 return $defs->{$kind};
927 }
928  
929 # Convert class name to OpenAPI definition name. A name nothing resolves
930 # (expand_class answers undef for a qualified one) has no definition to
931 # look up: the same undef a missing definition gets, without turning undef
932 # into a definition name first (karr k47).
933 19 my $class = $self->expand_class($kind);
934 19 ▶ return unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 2 17 19
935 # IO::K8s::Api::Core::V1::Pod -> io.k8s.api.core.v1.Pod
936 17 my $def_name = $class;
937 17 $def_name =~ s/^IO::K8s:://;
938 17 $def_name =~ s/::/./g;
939 17 $def_name = 'io.k8s.' . $def_name;
940 # Lowercase all path components except the final type name
941 17 my @parts = split /\./, $def_name;
942 17 $parts[$_] = lc($parts[$_]) for 0 .. $#parts - 1;
943 17 $def_name = join '.', @parts;
944 17 ▶ return $defs->{$def_name} if exists $defs->{$def_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $defs->{$def_name} 4 13 17
945  
946 # That holds for the IO::K8s::Api:: classes only. Upstream names
947 # apiextensions and apiregistration after their staging repositories
948 # (io.k8s.apiextensions-apiserver..., io.k8s.kube-aggregator...), a CRD's
949 # definition after its group (com.example.v1.Widget), and Unstructured or
950 # a '+My::Class' map onto nothing. Every Kind's definition carries
951 # x-kubernetes-group-version-kind, so the class's GVK finds it; no match
952 # stays undef (karr k54).
953 13 my ($api_version, $gvk_kind) = $self->_schema_gvk($class, $kind);
954 13 ▶ return unless defined $api_version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $api_version 3 10 13
955 10 ▶ my ($group, $version) = $api_version =~ m{/}
Branch: 1 branch
BranchTrueFalseTotal
$api_version =~ m[/] ? : 10 0 10
956 ? split(m{/}, $api_version, 2)
957 : ('', $api_version);
958 10 for my $name (sort keys %$defs) {
959 my $gvks = ref $defs->{$name} eq 'HASH'
960 46 ▶ ? $defs->{$name}{'x-kubernetes-group-version-kind'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $defs->{$name} eq 'HASH' ? : 46 0 46
961 46 ▶ next unless ref $gvks eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $gvks eq "ARRAY" 4 42 46
962 42 for my $gvk (grep { ref $_ eq 'HASH' } @$gvks) {
963 return $defs->{$name}
964 if ($gvk->{group} // '') eq $group
965 && ($gvk->{version} // '') eq $version
966 42 ▶ && ($gvk->{kind} // '') eq $gvk_kind;
Branch: 1 branch
BranchTrueFalseTotal
if ($gvk->{"group"} // "") eq $group and ($gvk->{"version"} // "") eq $version and ($gvk->{"kind"} // "") eq $gvk_kind 9 33 42
Condition75%9/12
($gvk->{"group"} // "") eq $group and ($gvk->{"version"} // "") eq $version and ($gvk->{"kind"} // "") eq $gvk_kind
!ll&&!rl&&r
2859
($gvk->{"group"} // "") eq $group and ($gvk->{"version"} // "") eq $version
!ll&&!rl&&r
23514
$gvk->{"group"} // ""
l!l
420
$gvk->{"version"} // ""
l!l
190
$gvk->{"kind"} // ""
l!l
140
MC/DC0%0/3
($gvk->{"group"} // "") eq $group and ($gvk->{"version"} // "") eq $version and ($gvk->{"kind"} // "") eq $gvk_kind
$gvk->{"group"} $gvk->{"version"} $gvk->{"kind"}
Truth table25%1/4
A and B and C
A= $gvk->{"group"}B= $gvk->{"version"}C= $gvk->{"kind"}
ABCresult
0XX0
10X0
1100
1111
967 }
968 }
969 1 return;
970 }
971  
972 # The apiVersion and Kind schema_for looks a definition up by, for $class as
973 # expand_class resolved $name: the class's own api_version() and kind(), or -
974 # for IO::K8s::Unstructured, whose class has neither - the Kind of the name
975 # and the apiVersion discovery confirmed for it, exactly as expand_class and
976 # _build_path read it (_discovery_path_meta). Empty when there is none.
977 13 ▶sub _schema_gvk {
Subroutine _schema_gvk: called CC=11 SCAR=25.7
978 13 my ($self, $class, $name) = @_;
979 13 ▶ if ($class eq 'IO::K8s::Unstructured') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "IO::K8s::Unstructured") 1 12 13
980 1 my ($kind, $api_version) = $self->_kind_from_expand_args($name);
981 1 ▶ return unless defined $kind;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kind 0 1 1
982 1 ▶ my $meta = $self->_discovery_path_meta($kind, $api_version) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $self->_discovery_path_meta($kind, $api_version) or return 1 0 1
983 1 return ($meta->{api_version}, $kind);
984 }
985 12 ▶ return unless eval { require_module($class); 1 } && $class->can('api_version');
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { &require_module($class); 1 } } and $class->can("api_version") 3 9 12
Condition66%2/3
eval { do { &require_module($class); 1 } } and $class->can("api_version")
!ll&&!rl&&r
309
MC/DC50%1/2
eval { do { &require_module($class); 1 } } and $class->can("api_version")
eval { do { &require_module($class); 1 } } $class->can("api_version")
Truth table66%2/3
A and B
A= eval { do { &require_module($class); 1 } }B= $class->can("api_version")
ABresult
0X0
100
111
986 # Asked as class methods, and only an answer without error counts - a
987 # class of your own may have them as instance attributes.
988 9 my $api_version = eval { $class->api_version };
989 9 ▶ return unless defined $api_version && length $api_version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $api_version and length $api_version 0 9 9
Condition33%1/3
defined $api_version and length $api_version
!ll&&!rl&&r
009
MC/DC0%0/2
defined $api_version and length $api_version
defined $api_version length $api_version
Truth table33%1/3
A and B
A= defined $api_versionB= length $api_version
ABresult
0X0
100
111
990 9 ▶ my $kind = $class->can('kind') ? eval { $class->kind } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$class->can('kind') ? : 9 0 9
991 9 ▶ ($kind = $class) =~ s/.*::// unless defined $kind && length $kind;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kind and length $kind 0 9 9
Condition33%1/3
defined $kind and length $kind
!ll&&!rl&&r
009
MC/DC0%0/2
defined $kind and length $kind
defined $kind length $kind
Truth table33%1/3
A and B
A= defined $kindB= length $kind
ABresult
0X0
100
111
992 9 return ($api_version, $kind);
993 }
994  
995 # Compare local class against cluster schema
996 # Returns comparison result from IO::K8s::Role::Resource->compare_to_schema
997 7 ▶sub compare_schema {
Subroutine compare_schema: called CC=3 SCAR=11.0
Pod documented
998 7 my ($self, $kind) = @_;
999  
1000  
1001 7 my $class = $self->_expand_class_or_croak($kind);
1002 # Unstructured declares only the envelope; held against a definition it
1003 # would report every other field as missing locally, which says nothing
1004 # about skew (karr k60). Before the /openapi/v2 download, which it would
1005 # waste.
1006 5 ▶ croak "compare_schema: '$kind' resolves to IO::K8s::Unstructured, which has"
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "IO::K8s::Unstructured" 3 2 5
1007 . " no local schema to compare (add a typed class for it to resource_map"
1008 . " or with)"
1009 if $class eq 'IO::K8s::Unstructured';
1010 2 require_module($class);
1011  
1012 2 my $schema = $self->schema_for($kind);
1013 1 ▶ croak "Schema not found for $kind" unless $schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $schema 0 1 1
1014  
1015 1 return $class->compare_to_schema($schema);
1016 }
1017  
1018 # Internal wrapper with fallback for lazy loading. It runs in the
1019 # resource_map builder, most often reached through the k8s one: the warning
1020 # goes through _carp_past_builders to name the caller's line, not the line of
1021 # the k8s builder that asked for the map (karr k64).
1022 62 ▶sub _load_resource_map_from_cluster {
Subroutine _load_resource_map_from_cluster: called CC=2 SCAR=6.9
1023 62 my ($self) = @_;
1024 62 my $map = eval { $self->fetch_resource_map };
1025 62 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 13 49 62
1026 13 $self->_carp_past_builders('Falling back to the built-in resource map: '
1027 . $self->_error_reason($@));
1028 13 return IO::K8s->default_resource_map;
1029 }
1030 49 return $map;
1031 }
1032  
1033 # V0 API compatibility - returns group wrapper objects
1034 #
1035 # Called as a plain function, not a method: each accessor below shares its name
1036 # with the group module it wraps, so once this package is loaded Perl resolves
1037 # a bareword like Kubernetes::REST::Core->new(...) against the sub instead of
1038 # the class and calls it as Kubernetes::REST::Core()->new(...) - with no
1039 # invocant. Returning the class name in that case puts the ->new(...) back on
1040 # the class the caller was aiming at.
1041 107 ▶sub _v0_group {
Subroutine _v0_group: called CC=2 SCAR=6.9
1042 107 my ($group, $self) = @_;
1043 107 my $class = "Kubernetes::REST::$group";
1044 107 require_module($class);
1045 107 ▶ return $class unless defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 17 90 107
1046 90 return $class->new(api => $self);
1047 }
1048  
1049 39 ▶sub Core { _v0_group('Core', @_) }
Subroutine Core: called CC=1 SCAR=0.0
Pod undocumented
1050 6 ▶sub Apps { _v0_group('Apps', @_) }
Subroutine Apps: called CC=1 SCAR=0.0
Pod undocumented
1051 4 ▶sub Batch { _v0_group('Batch', @_) }
Subroutine Batch: called CC=1 SCAR=0.0
Pod undocumented
1052 4 ▶sub Networking { _v0_group('Networking', @_) }
Subroutine Networking: called CC=1 SCAR=0.0
Pod undocumented
1053 4 ▶sub Storage { _v0_group('Storage', @_) }
Subroutine Storage: called CC=1 SCAR=0.0
Pod undocumented
1054 4 ▶sub Policy { _v0_group('Policy', @_) }
Subroutine Policy: called CC=1 SCAR=0.0
Pod undocumented
1055 4 ▶sub Autoscaling { _v0_group('Autoscaling', @_) }
Subroutine Autoscaling: called CC=1 SCAR=0.0
Pod undocumented
1056 4 ▶sub RbacAuthorization { _v0_group('RbacAuthorization', @_) }
Subroutine RbacAuthorization: called CC=1 SCAR=0.0
Pod undocumented
1057 4 ▶sub Certificates { _v0_group('Certificates', @_) }
Subroutine Certificates: called CC=1 SCAR=0.0
Pod undocumented
1058 4 ▶sub Coordination { _v0_group('Coordination', @_) }
Subroutine Coordination: called CC=1 SCAR=0.0
Pod undocumented
1059 4 ▶sub Events { _v0_group('Events', @_) }
Subroutine Events: called CC=1 SCAR=0.0
Pod undocumented
1060 4 ▶sub Scheduling { _v0_group('Scheduling', @_) }
Subroutine Scheduling: called CC=1 SCAR=0.0
Pod undocumented
1061 4 ▶sub Authentication { _v0_group('Authentication', @_) }
Subroutine Authentication: called CC=1 SCAR=0.0
Pod documented
1062 4 ▶sub Authorization { _v0_group('Authorization', @_) }
Subroutine Authorization: called CC=1 SCAR=0.0
Pod undocumented
1063 4 ▶sub Admissionregistration { _v0_group('Admissionregistration', @_) }
Subroutine Admissionregistration: called CC=1 SCAR=0.0
Pod undocumented
1064 5 ▶sub Apiextensions { _v0_group('Apiextensions', @_) }
Subroutine Apiextensions: called CC=1 SCAR=0.0
Pod undocumented
1065 5 ▶sub Apiregistration { _v0_group('Apiregistration', @_) }
Subroutine Apiregistration: called CC=1 SCAR=0.0
Pod undocumented
1066  
1067 # Build URL path from class metadata
1068 462 ▶sub _build_path {
Subroutine _build_path: called CC=26 SCAR=34.6
1069 462 my ($self, $class, %args) = @_;
1070  
1071 462 require_module($class);
1072  
1073 # An Unstructured resolution (D16 rung 4) carries no api_version()/
1074 # resource_plural()/Namespaced role -- its Kind is data on the instance,
1075 # not a class identity -- so the path metadata comes from the discovery
1076 # catalog instead of the class. The caller (a CRUD method, or a seam
1077 # consumer) passes the Kind (and an object's own apiVersion) via
1078 # _unstructured_hint; api_version/resource/namespaced overrides are honoured
1079 # directly when given, which keeps build_path usable by async wrappers.
1080 462 my ($api_version, $resource, $is_namespaced);
1081 462 my ($kind_hint, $av_hint) = (delete $args{kind}, delete $args{api_version});
1082 462 my $override_resource = delete $args{resource};
1083 462 my $override_namespaced = delete $args{namespaced};
1084 462 ▶ if ($class eq 'IO::K8s::Unstructured') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'IO::K8s::Unstructured') { } 44 418 462
1085 44 ▶ if (defined $override_resource && defined $override_namespaced
Branch: 1 branch
BranchTrueFalseTotal
if (defined $override_resource and defined $override_namespaced and defined $av_hint) { } 6 38 44
Condition83%5/6
defined $override_resource and defined $override_namespaced and defined $av_hint
!ll&&!rl&&r
3806
defined $override_resource and defined $override_namespaced
!ll&&!rl&&r
3716
MC/DC66%2/3
defined $override_resource and defined $override_namespaced and defined $av_hint
defined $override_resource defined $override_namespaced defined $av_hint
Truth table75%3/4
A and B and C
A= defined $override_resourceB= defined $override_namespacedC= defined $av_hint
ABCresult
0XX0
10X0
1100
1111
1086 && defined $av_hint) {
1087 6 ($api_version, $resource, $is_namespaced) =
1088 ($av_hint, $override_resource, $override_namespaced);
1089 } else {
1090 38 ▶ defined $kind_hint
Branch: 1 branch
BranchTrueFalseTotal
defined $kind_hint or croak("IO::K8s::Unstructured needs a Kind to build a path (pass kind => 'Kind')") 36 2 38
1091 or croak "IO::K8s::Unstructured needs a Kind to build a path"
1092 . " (pass kind => 'Kind')";
1093 36 my $meta = $self->_discovery_path_meta($kind_hint, $av_hint);
1094 36 ▶ unless ($meta) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meta) 11 25 36
1095 # A failed fetch and a catalog without the Kind both come back
1096 # undef (fail-closed, see _discovery_path_meta); only the
1097 # recorded reason tells them apart, and a catalog that was
1098 # never read must not be reported as one lacking an entry. A
1099 # pinned apiVersion is named: the Kind may well be served in
1100 # another group or version, just not in this one (karr k43).
1101 11 my $reason = $self->_discovery_error;
1102 11 ▶ $reason = $self->_error_reason($reason) if defined $reason;
Branch: 1 branch
BranchTrueFalseTotal
if defined $reason 3 8 11
1103 11 ▶ my $gvk = "Kind '$kind_hint'"
Branch: 1 branch
BranchTrueFalseTotal
defined $av_hint && length $av_hint ? : 5 6 11
Condition66%2/3
defined $av_hint and length $av_hint
!ll&&!rl&&r
605
MC/DC50%1/2
defined $av_hint and length $av_hint
defined $av_hint length $av_hint
Truth table66%2/3
A and B
A= defined $av_hintB= length $av_hint
ABresult
0X0
100
111
1104 . (defined $av_hint && length $av_hint
1105 ? " in apiVersion '$av_hint'" : '');
1106 11 ▶ croak defined $reason
Branch: 1 branch
BranchTrueFalseTotal
defined $reason ? : 3 8 11
1107 ? "discovery failed, so $gvk is unconfirmed"
1108 . " - cannot build a path for IO::K8s::Unstructured:"
1109 . " $reason"
1110 : "no discovery entry for $gvk - cannot"
1111 . " build a path for IO::K8s::Unstructured";
1112 }
1113 ($api_version, $resource, $is_namespaced) =
1114 25 @{$meta}{qw(api_version resource namespaced)};
1115 }
1116 } else {
1117 # Get metadata from class
1118 418 ▶ $api_version = $class->can('api_version') ? $class->api_version : undef;
Branch: 1 branch
BranchTrueFalseTotal
$class->can('api_version') ? : 418 0 418
1119 418 ▶ croak "Cannot determine api_version for $class - override api_version() in your CRD class"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $api_version 0 418 418
1120 unless defined $api_version;
1121 418 ▶ my $kind = $class->can('kind') ? $class->kind : (split('::', $class))[-1];
Branch: 1 branch
BranchTrueFalseTotal
$class->can('kind') ? : 418 0 418
1122 418 $is_namespaced = $class->does('IO::K8s::Role::Namespaced');
1123  
1124 # Use explicit resource_plural if available, otherwise auto-pluralize
1125 418 ▶ if ($class->can('resource_plural') && $class->resource_plural) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can('resource_plural') and $class->resource_plural) { } 418 0 418
Condition33%1/3
$class->can("resource_plural") and $class->resource_plural
!ll&&!rl&&r
00418
MC/DC0%0/2
$class->can("resource_plural") and $class->resource_plural
$class->can("resource_plural") $class->resource_plural
Truth table33%1/3
A and B
A= $class->can("resource_plural")B= $class->resource_plural
ABresult
0X0
100
111
1126 418 $resource = $class->resource_plural;
1127 } else {
1128 0 $resource = lc($kind);
1129 0 ▶ if ($resource =~ /(?:ss|sh|ch|x|z)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($resource =~ /(?:ss|sh|ch|x|z)$/) { } 0 0 0
elsif ($resource =~ /[^aeiou]y$/) { } 0 0 0
elsif (not $resource =~ /s$/) { } 0 0 0
1130 0 $resource .= 'es'; # class -> classes, ingress -> ingresses
1131 } elsif ($resource =~ /[^aeiou]y$/) {
1132 0 $resource =~ s/y$/ies/; # policy -> policies
1133 } elsif ($resource !~ /s$/) {
1134 0 $resource .= 's'; # pod -> pods
1135 }
1136 }
1137 }
1138  
1139 # Build path based on API group
1140 449 my $path;
1141 449 ▶ if ($api_version =~ m{/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($api_version =~ m[/]) { } 171 278 449
1142 # Has group: apps/v1 -> /apis/apps/v1/...
1143 171 $path = "/apis/$api_version";
1144 } else {
1145 # Core: v1 -> /api/v1/...
1146 278 $path = "/api/$api_version";
1147 }
1148  
1149 449 ▶ if ($is_namespaced && $args{namespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_namespaced and $args{"namespace"}) 371 78 449
Condition100%3/3
$is_namespaced and $args{"namespace"}
!ll&&!rl&&r
726371
MC/DC100%2/2
$is_namespaced and $args{"namespace"}
$is_namespaced $args{"namespace"}
Truth table100%3/3
A and B
A= $is_namespacedB= $args{"namespace"}
ABresult
0X0
100
111
1150 371 $path .= "/namespaces/$args{namespace}";
1151 }
1152  
1153 449 $path .= "/$resource";
1154  
1155 449 ▶ if ($args{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"name"}) 262 187 449
1156 262 $path .= "/$args{name}";
1157 }
1158  
1159 # Subresource suffix: /status, /log, /exec, /attach, /portforward.
1160 # Every subresource addresses one named resource - without a name the
1161 # suffix would silently land on the collection endpoint instead.
1162 449 ▶ if (defined $args{subresource} && length $args{subresource}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"subresource"} and length $args{"subresource"}) 72 377 449
Condition66%2/3
defined $args{"subresource"} and length $args{"subresource"}
!ll&&!rl&&r
377072
MC/DC50%1/2
defined $args{"subresource"} and length $args{"subresource"}
defined $args{"subresource"} length $args{"subresource"}
Truth table66%2/3
A and B
A= defined $args{"subresource"}B= length $args{"subresource"}
ABresult
0X0
100
111
1163 croak "subresource '$args{subresource}' requires a name for $class"
1164 72 ▶ unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 1 71 72
1165 71 $path .= "/$args{subresource}";
1166 }
1167  
1168 448 return $path;
1169 }
1170  
1171 # ============================================================================
1172 # REQUEST / RESPONSE PIPELINE
1173 #
1174 # The API methods (list, get, create, etc.) are built on a 3-step pipeline:
1175 #
1176 # 1. _prepare_request - builds an HTTPRequest (method, url, headers, body)
1177 # 2. io->call - executes the request (pluggable: HTTP::Tiny, async, mock)
1178 # 3. _check_response / _inflate_object / _inflate_list - processes the response
1179 #
1180 # This separation allows different IO backends (sync, async, mock) to slot in
1181 # at step 2 without touching request preparation or response processing.
1182 # ============================================================================
1183  
1184 695 ▶sub _prepare_request {
Subroutine _prepare_request: called CC=15 SCAR=27.1
1185 695 my ($self, $method, $path, %opts) = @_;
1186  
1187 695 my $url = $self->server->endpoint . $path;
1188 695 ▶ my $content_type = $opts{content_type} // 'application/json';
Condition100%2/2
$opts{"content_type"} // "application/json"
l!l
42653
MC/DC100%1/1
$opts{"content_type"} // "application/json"
$opts{"content_type"}
Truth table100%2/2
A
A= $opts{"content_type"}
Aresult
11
00
1189 695 my $body = $opts{body};
1190 695 my $parameters = $opts{parameters};
1191 695 ▶ my $extra_headers = $opts{headers} // {};
Condition100%2/2
$opts{"headers"} // {}
l!l
168527
MC/DC100%1/1
$opts{"headers"} // {}
$opts{"headers"}
Truth table100%2/2
A
A= $opts{"headers"}
Aresult
11
00
1192  
1193 # Append query parameters to URL, keys and values through _query_escape
1194 695 ▶ if ($parameters && %$parameters) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parameters and %$parameters) 303 392 695
Condition100%3/3
$parameters and %$parameters
!ll&&!rl&&r
3902303
MC/DC100%2/2
$parameters and %$parameters
$parameters %$parameters
Truth table100%3/3
A and B
A= $parametersB= %$parameters
ABresult
0X0
100
111
1195 303 my @pairs;
1196 303 for my $key (sort keys %$parameters) {
1197 468 my $val = $parameters->{$key};
1198 468 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 125 343 468
1199 343 my $k = $self->_query_escape($key);
1200 343 ▶ if (ref($val) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 10 333 343
1201 10 push @pairs, map { "$k=" . $self->_query_escape($_) } grep { defined } @$val;
1202 } else {
1203 333 push @pairs, "$k=" . $self->_query_escape($val);
1204 }
1205 }
1206 303 ▶ if (@pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs) 178 125 303
1207 178 ▶ $url .= ($url =~ /\?/ ? '&' : '?') . join('&', @pairs);
Branch: 1 branch
BranchTrueFalseTotal
$url =~ /\?/ ? : 1 177 178
1208 }
1209 }
1210  
1211 695 my %headers = (
1212 'Content-Type' => $content_type,
1213 'Accept' => 'application/json',
1214 );
1215  
1216 # Only add Authorization header when a token is available
1217 # (client-certificate auth doesn't need a Bearer token)
1218 695 my $token = $self->credentials->token;
1219 695 ▶ if (defined $token && length $token) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token and length $token) 694 1 695
Condition66%2/3
defined $token and length $token
!ll&&!rl&&r
01694
MC/DC50%1/2
defined $token and length $token
defined $token length $token
Truth table66%2/3
A and B
A= defined $tokenB= length $token
ABresult
0X0
100
111
1220 694 $headers{'Authorization'} = 'Bearer ' . $token;
1221 }
1222 695 ▶ if ($extra_headers && ref($extra_headers) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra_headers and ref $extra_headers eq "HASH") 695 0 695
Condition33%1/3
$extra_headers and ref $extra_headers eq "HASH"
!ll&&!rl&&r
00695
MC/DC0%0/2
$extra_headers and ref $extra_headers eq "HASH"
$extra_headers ref $extra_headers eq "HASH"
Truth table33%1/3
A and B
A= $extra_headersB= ref $extra_headers eq "HASH"
ABresult
0X0
100
111
1223 695 @headers{keys %$extra_headers} = values %$extra_headers;
1224 }
1225  
1226 695 ▶ return Kubernetes::REST::HTTPRequest->new(
Branch: 1 branch
BranchTrueFalseTotal
$body ? : 146 549 695
1227 method => $method,
1228 url => $url,
1229 headers => \%headers,
1230 ($body ? (content => $self->_json->encode($body)) : ()),
1231 );
1232 }
1233  
1234 # Percent-encode a query key or value - but only what would otherwise change
1235 # what the API server reads (karr k35). The server splits a query with Go's
1236 # net/url ParseQuery: into pairs at '&', dropping a pair that holds a ';'
1237 # whole, then key from value at the FIRST '=', reading '+' as a space and
1238 # '%XX' as a byte, and dropping a pair with a malformed '%'. A '#' ends the
1239 # URL before it is sent (URI and HTTP::Tiny take the rest for a fragment), and
1240 # a space, a control character or a non-ASCII byte has no place in a request
1241 # target. Those are encoded - characters as their UTF-8 bytes, the contract
1242 # the JSON body follows too. Everything else stays as it is, above all '=',
1243 # ',', '!', '/', '(', ')' and ':': the server reads them the same either way
1244 # (a value's '=' is past the first one), a selector stays readable in the URL,
1245 # and whoever compares the rendered query - this distribution's mock harness,
1246 # Net::Async::Kubernetes's - sees the string it always did. Keys go the same
1247 # way; they are the API's parameter names, none of which holds an '='.
1248 694 ▶sub _query_escape {
Subroutine _query_escape: called CC=1 SCAR=0.0
1249 694 my ($self, $string) = @_;
1250 694 my $bytes = Encode::encode('UTF-8', "$string");
1251 694 $bytes =~ s/([\x00-\x20\x7F-\xFF%&+#;])/sprintf('%%%02X', ord $1)/ge;
1252 694 return $bytes;
1253 }
1254  
1255 581 ▶sub _check_response {
Subroutine _check_response: called CC=3 SCAR=11.0
1256 581 my ($self, $response, $context) = @_;
1257 # Before the status is looked at, so an error response delivers its
1258 # warnings ahead of the APIError (karr k79).
1259 581 $self->_deliver_warnings($response, $context);
1260 581 ▶ if ($response->status >= 400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->status >= 400) 77 504 581
1261 # Response bodies are bytes; the error message is read by humans, so
1262 # decode it (leniently - a truncated or non-UTF-8 body must not turn a
1263 # useful API error into an encoding croak).
1264 77 ▶ my $body = Encode::decode('UTF-8', $response->content // '', Encode::FB_DEFAULT);
Condition50%1/2
$response->content // ""
l!l
770
MC/DC0%0/1
$response->content // ""
$response->content
Truth table50%1/2
A
A= $response->content
Aresult
11
00
1265 # An object, so a caller can branch on the status instead of parsing
1266 # the text; it stringifies to the message this croaked with before,
1267 # at the same caller line (karr k50).
1268 77 Kubernetes::REST::APIError->throw(
1269 code => 0 + $response->status,
1270 body => $body,
1271 context => $context,
1272 response => $response,
1273 );
1274 }
1275 504 return $response;
1276 }
1277  
1278 # The API server says what it did not do as asked - a field it dropped
1279 # (unknown field "spec.nodeIP"), a deprecated API - only in Warning response
1280 # headers (karr k79). Each value goes to on_warning with the context an
1281 # APIError would carry, in header order. A response object without headers -
1282 # a test double, another backend's, an async client's own - has none.
1283 581 ▶sub _deliver_warnings {
Subroutine _deliver_warnings: called CC=6 SCAR=17.9
1284 581 my ($self, $response, $context) = @_;
1285 581 ▶ my $on_warning = $self->on_warning or return;
Branch: 1 branch
BranchTrueFalseTotal
my $on_warning = $self->on_warning or return 9 572 581
1286 9 ▶ return unless $response->can('headers');
Branch: 1 branch
BranchTrueFalseTotal
unless $response->can("headers") 1 8 9
1287 8 my $headers = $response->headers;
1288 8 ▶ my $warnings = ref $headers eq 'HASH' ? $headers->{warning} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $headers eq 'HASH' ? : 8 0 8
1289 8 ▶ return unless ref $warnings eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $warnings eq "ARRAY" 1 7 8
1290 7 for my $value (@$warnings) {
1291 9 $on_warning->($self->_warning_text($value), $context);
1292 }
1293 7 return;
1294 }
1295  
1296 # The text of one Warning header value. The API server writes RFC 7234's
1297 # warn-code, warn-agent, quoted warn-text and optional quoted warn-date -
1298 # 299 - "..." - and the text is the quoted part with its quoted-pairs (\" and
1299 # \\) undone. Any other value is taken whole. Header values are bytes; the
1300 # text is read by humans, so it is decoded, leniently, as an APIError's body.
1301 9 ▶sub _warning_text {
Subroutine _warning_text: called CC=3 SCAR=11.0
1302 9 my ($self, $value) = @_;
1303 9 ▶ my $text = Encode::decode('UTF-8', $value // '', Encode::FB_DEFAULT);
Condition50%1/2
$value // ""
l!l
90
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
1304 9 ▶ return $text
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\A\s*\d{3}\s+\S+\s+"((?:[^"\\]|\\.)*)"(?:\s+"[^"]*")?\s*\z/s 1 8 9
1305 unless $text =~ /\A\s*\d{3}\s+\S+\s+"((?:[^"\\]|\\.)*)"(?:\s+"[^"]*")?\s*\z/s;
1306 8 my $quoted = $1;
1307 8 $quoted =~ s/\\(.)/$1/gs;
1308 8 return $quoted;
1309 }
1310  
1311 # The name to hand IO::K8s's json_to_object()/struct_to_object() for $class.
1312 # Both resolve a name again, and a single-segment class of your own - '+Gizmo'
1313 # in the resource_map, which expand_class returns as 'Gizmo' - reads to them
1314 # as a Kind: IO::K8s::Gizmo, or whatever class the map gives the Kind Gizmo
1315 # (karr k42). A name that already is a loaded IO::K8s class, which is what
1316 # expand_class resolved and _build_path loaded, therefore goes over as
1317 # '+Class', which IO::K8s takes exactly. Any other name - a short or qualified
1318 # one a seam caller passed - is left for IO::K8s to resolve. The role check
1319 # keeps an unrelated package that merely shares a Kind's name from being
1320 # taken for the class.
1321 299 ▶sub _exact_class {
Subroutine _exact_class: called CC=7 SCAR=23.2
1322 299 my ($self, $class) = @_;
1323 299 ▶ return "+$class"
Branch: 1 branch
BranchTrueFalseTotal
if defined $class and not ref $class and length $class and not $class =~ /\A\+/ and $class->can("does") and $class->does("IO::K8s::Role::Resource") 294 5 299
Condition46%7/15
defined $class and not ref $class and length $class and not $class =~ /\A\+/ and $class->can("does") and $class->does("IO::K8s::Role::Resource")
!ll&&!rl&&r
50294
defined $class and not ref $class and length $class and not $class =~ /\A\+/ and $class->can("does")
!ll&&!rl&&r
05294
defined $class and not ref $class and length $class and not $class =~ /\A\+/
!ll&&!rl&&r
00299
defined $class and not ref $class and length $class
!ll&&!rl&&r
00299
defined $class and not ref $class
!ll&&!rl&&r
00299
MC/DC16%1/6
defined $class and not ref $class and length $class and not $class =~ /\A\+/ and $class->can("does") and $class->does("IO::K8s::Role::Resource")
defined $class not ref $class length $class not $class =~ /\A\+/ $class->can("does") $class->does("IO::K8s::Role::Resource")
Truth table28%2/7
A and not(B) and C and not(D) and E and F
A= defined $classB= not ref $classC= length $classD= not $class =~ /\A\+/E= $class->can("does")F= $class->does("IO::K8s::Role::Resource")
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
1324 if defined $class && !ref $class && length $class && $class !~ /\A\+/
1325 && $class->can('does') && $class->does('IO::K8s::Role::Resource');
1326 5 return $class;
1327 }
1328  
1329 191 ▶sub _inflate_object {
Subroutine _inflate_object: called CC=1 SCAR=0.0
1330 191 my ($self, $class, $response) = @_;
1331 191 return $self->k8s->json_to_object($self->_exact_class($class), $response->content);
1332 }
1333  
1334 76 ▶sub _inflate_list {
Subroutine _inflate_list: called CC=11 SCAR=24.3
1335 76 my ($self, $class, $response) = @_;
1336 76 my $struct = $self->_json->decode($response->content);
1337 76 ▶ my $items = $struct->{items} // [];
Condition50%1/2
$struct->{"items"} // []
l!l
760
MC/DC0%0/1
$struct->{"items"} // []
$struct->{"items"}
Truth table50%1/2
A
A= $struct->{"items"}
Aresult
11
00
1338 76 my $exact_class = $self->_exact_class($class);
1339 76 my (@objects, @dropped);
1340 76 for my $i (0 .. $#$items) {
1341 89 my $item = $items->[$i];
1342 89 my $obj = eval { $self->k8s->struct_to_object($exact_class, $item) };
1343 89 ▶ if (defined $obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) 85 4 89
1344 85 push @objects, $obj;
1345 85 next;
1346 }
1347 # An item the object model rejects must not disappear without a
1348 # trace: the caller would read a cluster with N unreadable Pods as a
1349 # cluster with N fewer Pods (karr #16). Say which items were dropped
1350 # and why - typically the installed IO::K8s not knowing a field the
1351 # cluster version serves.
1352 4 ▶ my $err = $@ || 'inflated to undef';
Condition50%1/2
$@ || 'inflated to undef'
l!l
40
MC/DC0%0/1
$@ || 'inflated to undef'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
1353 4 $err =~ s/\s+\z//;
1354 my $name = ref $item eq 'HASH' && ref $item->{metadata} eq 'HASH'
1355 ? $item->{metadata}{name}
1356 4 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $item eq 'HASH' && ref $item->{'metadata'} eq 'HASH' ? : 4 0 4
Condition33%1/3
ref $item eq "HASH" and ref $item->{"metadata"} eq "HASH"
!ll&&!rl&&r
004
MC/DC0%0/2
ref $item eq "HASH" and ref $item->{"metadata"} eq "HASH"
ref $item eq "HASH" ref $item->{"metadata"} eq "HASH"
Truth table33%1/3
A and B
A= ref $item eq "HASH"B= ref $item->{"metadata"} eq "HASH"
ABresult
0X0
100
111
1357 4 ▶ push @dropped,
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 4 0 4
1358 "item $i" . (defined $name ? " (name '$name')" : '') . ": $err";
1359 }
1360 76 ▶ if (@dropped) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dropped) 3 73 76
1361 3 carp sprintf
1362 "inflate_list: dropped %d of %d %s items - the returned list is"
1363 . " INCOMPLETE (often IO::K8s version drift against the cluster):\n %s",
1364 scalar @dropped, scalar @$items, $class, join("\n ", @dropped);
1365 }
1366 75 my $meta = $struct->{metadata};
1367 75 ▶ my $metadata = ref $meta eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $meta eq 'HASH' ? : 10 65 75
1368 ? $self->k8s->struct_to_object(
1369 'IO::K8s::Apimachinery::Pkg::Apis::Meta::V1::ListMeta', $meta)
1370 : undef;
1371 75 ▶ return IO::K8s::List->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $metadata ? : 10 65 75
1372 items => \@objects, item_class => $class,
1373 (defined $metadata ? (metadata => $metadata) : ()),
1374 );
1375 }
1376  
1377 32 ▶sub _process_watch_chunk {
Subroutine _process_watch_chunk: called CC=13 SCAR=26.0
1378 32 my ($self, $class, $buffer_ref, $chunk) = @_;
1379 32 $$buffer_ref .= $chunk;
1380  
1381 32 my $exact_class = $self->_exact_class($class);
1382 32 my @events;
1383 32 ▶ while ($$buffer_ref =~ s/^([^\n]*)\n//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$buffer_ref =~ s/^([^\n]*)\n//) 32 32 64
1384 32 my $line = $1;
1385 32 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 32 32
1386  
1387 32 my $data = eval { $self->_json->decode($line) };
1388 32 ▶ next unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 32 32
1389  
1390 32 ▶ my $type = $data->{type} // '';
Condition50%1/2
$data->{"type"} // ""
l!l
320
MC/DC0%0/1
$data->{"type"} // ""
$data->{"type"}
Truth table50%1/2
A
A= $data->{"type"}
Aresult
11
00
1391 32 ▶ my $raw_object = $data->{object} // {};
Condition50%1/2
$data->{"object"} // {}
l!l
320
MC/DC0%0/1
$data->{"object"} // {}
$data->{"object"}
Truth table50%1/2
A
A= $data->{"object"}
Aresult
11
00
1392  
1393 # Track resourceVersion for resumability
1394 32 my $rv;
1395 32 ▶ if ($raw_object->{metadata} && $raw_object->{metadata}{resourceVersion}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_object->{"metadata"} and $raw_object->{"metadata"}{"resourceVersion"}) 28 4 32
Condition100%3/3
$raw_object->{"metadata"} and $raw_object->{"metadata"}{"resourceVersion"}
!ll&&!rl&&r
1328
MC/DC100%2/2
$raw_object->{"metadata"} and $raw_object->{"metadata"}{"resourceVersion"}
$raw_object->{"metadata"} $raw_object->{"metadata"}{"resourceVersion"}
Truth table100%3/3
A and B
A= $raw_object->{"metadata"}B= $raw_object->{"metadata"}{"resourceVersion"}
ABresult
0X0
100
111
1396 28 $rv = $raw_object->{metadata}{resourceVersion};
1397 }
1398  
1399 # Inflate the object (ERROR events stay as hashrefs)
1400 32 my $object;
1401 32 ▶ if ($type eq 'ERROR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'ERROR') { } 1 31 32
1402 1 $object = $raw_object;
1403 } else {
1404 31 ▶ $object = eval { $self->k8s->struct_to_object($exact_class, $raw_object) }
Condition33%1/3
eval { do { $self->k8s->struct_to_object($exact_class, $raw_object) } } // $raw_object
l!l&&r!l&&!r
3100
MC/DC0%0/2
eval { do { $self->k8s->struct_to_object($exact_class, $raw_object) } } // $raw_object
eval { do { $self->k8s->struct_to_object($exact_class, $raw_object) } } $raw_object
Truth table33%1/3
A // B
A= eval { do { $self->k8s->struct_to_object($exact_class, $raw_object) } }B= $raw_object
ABresult
1X1
011
000
1405 // $raw_object;
1406 }
1407  
1408 push @events, {
1409 event => Kubernetes::REST::WatchEvent->new(
1410 type => $type,
1411 object => $object,
1412 raw => $raw_object,
1413 ),
1414 resourceVersion => $rv,
1415 is_error => ($type eq 'ERROR' ? 1 : 0),
1416 32 ▶ error_code => ($type eq 'ERROR' ? ($raw_object->{code} // 0) : 0),
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'ERROR' ? : 1 31 32
$type eq 'ERROR' ? : 1 31 32
Condition50%1/2
$raw_object->{"code"} // 0
l!l
10
MC/DC0%0/1
$raw_object->{"code"} // 0
$raw_object->{"code"}
Truth table50%1/2
A
A= $raw_object->{"code"}
Aresult
11
00
1417 };
1418 }
1419  
1420 32 return @events;
1421 }
1422  
1423 11 ▶sub _process_log_chunk {
Subroutine _process_log_chunk: called CC=2 SCAR=6.9
1424 11 my ($self, $buffer_ref, $chunk) = @_;
1425 11 $$buffer_ref .= $chunk;
1426  
1427 11 my @events;
1428 11 ▶ while ($$buffer_ref =~ s/^([^\n]*)\n//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$buffer_ref =~ s/^([^\n]*)\n//) 14 11 25
1429 14 my $line = $1;
1430 14 push @events, Kubernetes::REST::LogEvent->new(line => $line);
1431 }
1432  
1433 11 return @events;
1434 }
1435  
1436 # ============================================================================
1437 # PUBLIC BUILDING BLOCKS FOR ASYNC WRAPPERS
1438 #
1439 # These methods expose the internal request/response pipeline as a stable API
1440 # for async wrappers (e.g. Net::Async::Kubernetes) that need to build requests,
1441 # process responses, and handle streaming without going through the sync
1442 # convenience methods (list, get, watch, log, port_forward, exec, attach, etc.).
1443 # ============================================================================
1444  
1445 26 ▶sub build_path {
Subroutine build_path: called CC=1 SCAR=0.0
Pod documented
1446 26 my ($self, @args) = @_;
1447  
1448  
1449 26 return $self->_build_path(@args);
1450 }
1451  
1452 75 ▶sub prepare_request {
Subroutine prepare_request: called CC=1 SCAR=0.0
Pod documented
1453 75 my ($self, @args) = @_;
1454  
1455  
1456 75 return $self->_prepare_request(@args);
1457 }
1458  
1459 8 ▶sub check_response {
Subroutine check_response: called CC=1 SCAR=0.0
Pod documented
1460 8 my ($self, @args) = @_;
1461  
1462  
1463 8 return $self->_check_response(@args);
1464 }
1465  
1466 5 ▶sub inflate_object {
Subroutine inflate_object: called CC=1 SCAR=0.0
Pod documented
1467 5 my ($self, @args) = @_;
1468  
1469  
1470 5 return $self->_inflate_object(@args);
1471 }
1472  
1473 4 ▶sub inflate_list {
Subroutine inflate_list: called CC=1 SCAR=0.0
Pod documented
1474 4 my ($self, @args) = @_;
1475  
1476  
1477 4 return $self->_inflate_list(@args);
1478 }
1479  
1480 2 ▶sub process_watch_chunk {
Subroutine process_watch_chunk: called CC=1 SCAR=0.0
Pod documented
1481 2 my ($self, @args) = @_;
1482  
1483  
1484 2 return $self->_process_watch_chunk(@args);
1485 }
1486  
1487 6 ▶sub process_log_chunk {
Subroutine process_log_chunk: called CC=1 SCAR=0.0
Pod documented
1488 6 my ($self, @args) = @_;
1489  
1490  
1491 6 return $self->_process_log_chunk(@args);
1492 }
1493  
1494 # Convenience: prepare + call in one step (used by sync CRUD methods)
1495 419 ▶sub _request {
Subroutine _request: called CC=1 SCAR=0.0
1496 419 my ($self, $method, $path, $body, %opts) = @_;
1497 419 my $req = $self->_prepare_request($method, $path,
1498 body => $body,
1499 %opts,
1500 );
1501 419 return $self->io->call($req);
1502 }
1503  
1504 78 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
1505 78 my ($self, $short_class, %args) = @_;
1506  
1507  
1508 # A misspelt option is not ignored (karr k58): label_selector listed
1509 # unfiltered, namespce across the cluster. name and subresource are out
1510 # too: they made the request a GET of one object, read as an empty list.
1511 78 $self->_croak_unknown_args('list', \%args, qw(namespace labelSelector fieldSelector limit continue resourceVersion));
1512 69 my ($class, $response) = $self->_list_request($short_class, %args);
1513 60 $self->_check_response($response, "list $short_class");
1514  
1515 57 return $self->_inflate_list($class, $response);
1516 }
1517  
1518 # list() up to the response, unchecked: returns the resolved class and the raw
1519 # response. ensure_only() needs the status itself - a 404 there means the Kind
1520 # is not served, not a failure - and must not read it back out of the text
1521 # _check_response croaks with.
1522 92 ▶sub _list_request {
Subroutine _list_request: called CC=6 SCAR=17.9
1523 92 my ($self, $short_class, %args) = @_;
1524  
1525 # Extract query parameters before building path
1526 92 my $label_selector = delete $args{labelSelector};
1527 92 my $field_selector = delete $args{fieldSelector};
1528 92 my %paging = map { $_ => delete $args{$_} } qw(limit continue resourceVersion);
1529  
1530 92 my $class = $self->_expand_class_or_croak($short_class);
1531 79 my $path = $self->_build_path($class, %args,
1532 $self->_unstructured_hint($class, $short_class));
1533  
1534 79 my %params;
1535 79 ▶ $params{labelSelector} = $label_selector if defined $label_selector;
Branch: 1 branch
BranchTrueFalseTotal
if defined $label_selector 21 58 79
1536 79 ▶ $params{fieldSelector} = $field_selector if defined $field_selector;
Branch: 1 branch
BranchTrueFalseTotal
if defined $field_selector 2 77 79
1537 79 ▶ defined $paging{$_} and $params{$_} = $paging{$_} for sort keys %paging;
Branch: 1 branch
BranchTrueFalseTotal
defined $paging{$_} and $params{$_} = $paging{$_} 8 229 237
1538  
1539 79 ▶ my $response = %params
Branch: 1 branch
BranchTrueFalseTotal
%params ? : 26 53 79
1540 ? $self->_request('GET', $path, undef, parameters => \%params)
1541 : $self->_request('GET', $path);
1542 79 return ($class, $response);
1543 }
1544  
1545 58 ▶sub get {
Subroutine get: called CC=6 SCAR=17.9
Pod documented
1546 58 my ($self, $short_class, @rest) = @_;
1547  
1548  
1549 # Support: get('Kind', 'name'), get('Kind', 'name', namespace => 'ns'),
1550 # get('Kind', name => 'name'), get('Kind', name => 'name', namespace => 'ns'),
1551 # each with subresource => ... as well
1552 58 my %args;
1553 58 ▶ if (@rest == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (@rest == 1) { } 3 55 58
elsif (@rest >= 2 and not $rest[0] =~ /^(name|namespace|subresource)$/) { } 36 19 55
elsif (@rest % 2 == 0) { } 19 0 19
1554 3 $args{name} = $rest[0];
1555 ▶ } elsif (@rest >= 2 && $rest[0] !~ /^(name|namespace|subresource)$/) {
Condition100%3/3
@rest >= 2 and not $rest[0] =~ /^(name|namespace|subresource)$/
!ll&&!rl&&r
21736
MC/DC100%2/2
@rest >= 2 and not $rest[0] =~ /^(name|namespace|subresource)$/
@rest >= 2 not $rest[0] =~ /^(name|namespace|subresource)$/
Truth table100%3/3
A and not(B)
A= @rest >= 2B= not $rest[0] =~ /^(name|namespace|subresource)$/
ABresult
0X0
100
111
1556 # First arg is name, rest are key=value pairs
1557 36 $args{name} = shift @rest;
1558 36 %args = (%args, @rest);
1559 } elsif (@rest % 2 == 0) {
1560 19 %args = @rest;
1561 } else {
1562 0 croak "Invalid arguments to get()";
1563 }
1564 # A misspelt option is not ignored (karr k58). subresource is build_path's
1565 # and works: status answers with the object.
1566 58 $self->_croak_unknown_args('get', \%args, qw(name namespace subresource));
1567  
1568 54 my $class = $self->_expand_class_or_croak($short_class);
1569 45 ▶ croak "name required for get" unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 2 43 45
1570  
1571 43 my $path = $self->_build_path($class, %args,
1572 $self->_unstructured_hint($class, $short_class));
1573 43 my $response = $self->_request('GET', $path);
1574 43 $self->_check_response($response, "get $short_class");
1575  
1576 33 return $self->_inflate_object($class, $response);
1577 }
1578  
1579 42 ▶sub create {
Subroutine create: called CC=1 SCAR=0.0
Pod documented
1580 42 my ($self, $object, @opts) = @_;
1581  
1582  
1583 42 my ($class, $response) = $self->_create_request($object, @opts);
1584 39 $self->_check_response($response, "create $class");
1585  
1586 35 return $self->_inflate_object($class, $response);
1587 }
1588  
1589 # create() up to the response, unchecked, for the same reason as
1590 # _list_request: ensure() takes a 409 as its cue that the object appeared
1591 # since its GET, and must read that off the status, not out of the text
1592 # _check_response croaks with (karr k44). ensure() passes no options, so its
1593 # create carries the client's field_validation only (karr k79).
1594 72 ▶sub _create_request {
Subroutine _create_request: called CC=3 SCAR=11.6
1595 72 my ($self, $object, @opts) = @_;
1596 72 my $field_validation = $self->_write_opts_or_croak('create', @opts);
1597  
1598 70 my $class = ref($object);
1599 70 ▶ my $namespace = $object->can('metadata') && $object->metadata
Branch: 1 branch
BranchTrueFalseTotal
$object->can('metadata') && $object->metadata ? : 70 0 70
Condition33%1/3
$object->can("metadata") and $object->metadata
!ll&&!rl&&r
0070
MC/DC0%0/2
$object->can("metadata") and $object->metadata
$object->can("metadata") $object->metadata
Truth table33%1/3
A and B
A= $object->can("metadata")B= $object->metadata
ABresult
0X0
100
111
1600 ? $object->metadata->namespace
1601 : undef;
1602  
1603 70 my $path = $self->_build_path($class, namespace => $namespace,
1604 $self->_unstructured_hint($class, $object));
1605 69 return ($class, $self->_request('POST', $path, $object->TO_JSON,
1606 parameters => { fieldValidation => $field_validation }));
1607 }
1608  
1609 14 ▶sub update {
Subroutine update: called CC=1 SCAR=0.0
Pod documented
1610 14 my ($self, $object, @opts) = @_;
1611  
1612  
1613 14 my ($class, $response) = $self->_update_request($object, @opts);
1614 11 $self->_check_response($response, "update $class");
1615  
1616 9 return $self->_inflate_object($class, $response);
1617 }
1618  
1619 # update() up to the response, unchecked, like _create_request: ensure()
1620 # re-fetches and retries on a 409 Conflict and must tell it apart by status.
1621 27 ▶sub _update_request {
Subroutine _update_request: called CC=3 SCAR=11.1
1622 27 my ($self, $object, @opts) = @_;
1623 27 my $field_validation = $self->_write_opts_or_croak('update', @opts);
1624  
1625 25 my $class = ref($object);
1626 25 ▶ my $metadata = $object->metadata or croak "object must have metadata";
Branch: 1 branch
BranchTrueFalseTotal
my $metadata = $object->metadata or croak("object must have metadata") 25 0 25
1627 25 ▶ my $name = $metadata->name or croak "object must have metadata.name";
Branch: 1 branch
BranchTrueFalseTotal
my $name = $metadata->name or croak("object must have metadata.name") 25 0 25
1628 25 my $namespace = $metadata->namespace;
1629  
1630 25 my $path = $self->_build_path($class, name => $name, namespace => $namespace,
1631 $self->_unstructured_hint($class, $object));
1632 24 return ($class, $self->_request('PUT', $path, $object->TO_JSON,
1633 parameters => { fieldValidation => $field_validation }));
1634 }
1635  
1636 97 my %PATCH_TYPES = (
1637 strategic => 'application/strategic-merge-patch+json',
1638 merge => 'application/merge-patch+json',
1639 json => 'application/json-patch+json',
1640 );
1641  
1642 # Shared argument unpacking for patch() and patch_status(): accepts either a
1643 # blessed object or a class plus name, in both the shorthand
1644 # (patch('Pod', 'name', ...)) and the fully keyed (patch('Pod', name => ...))
1645 # call form. $label only appears in croak messages, $default_type is the patch
1646 # strategy used when the caller does not pass one.
1647 #
1648 # Any argument it does not read croaks before anything is sent (karr k61): a
1649 # misspelt namespce patched the object of that name at cluster scope, a
1650 # patch_type => 'merge' went out as a strategic merge patch. An object names
1651 # itself, so with one only the patch, its type and fieldValidation are taken.
1652 # fieldValidation is checked as soon as the arguments are known, before the
1653 # class is resolved - that may read discovery (karr k79).
1654 77 ▶sub _unpack_patch_args {
Subroutine _unpack_patch_args: called CC=15 SCAR=27.6
1655 77 my ($self, $label, $default_type, $class_or_object, @rest) = @_;
1656  
1657 77 my ($class, $name, $namespace, $patch, $patch_type, $field_validation);
1658  
1659 77 ▶ if (ref($class_or_object) && blessed($class_or_object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class_or_object and builtin::blessed($class_or_object)) { } 17 60 77
Condition66%2/3
ref $class_or_object and builtin::blessed($class_or_object)
!ll&&!rl&&r
60017
MC/DC50%1/2
ref $class_or_object and builtin::blessed($class_or_object)
ref $class_or_object builtin::blessed($class_or_object)
Truth table66%2/3
A and B
A= ref $class_or_objectB= builtin::blessed($class_or_object)
ABresult
0X0
100
111
1660 # Object passed: patch($object, patch => {...})
1661 17 my $object = $class_or_object;
1662 17 my %args = @rest;
1663 17 $self->_croak_unknown_args($label, \%args, qw(patch type fieldValidation));
1664 9 $field_validation = $self->_field_validation_or_croak($label, $args{fieldValidation});
1665 8 $class = ref($object);
1666 8 ▶ my $metadata = $object->metadata or croak "object must have metadata";
Branch: 1 branch
BranchTrueFalseTotal
my $metadata = $object->metadata or croak("object must have metadata") 8 0 8
1667 8 ▶ $name = $metadata->name or croak "object must have metadata.name";
Branch: 1 branch
BranchTrueFalseTotal
$name = $metadata->name or croak("object must have metadata.name") 8 0 8
1668 8 $namespace = $metadata->namespace;
1669 8 ▶ $patch = $args{patch} // croak "$label requires 'patch' parameter";
Condition50%1/2
$args{"patch"} // croak("$label requires 'patch' parameter")
l!l
80
MC/DC0%0/1
$args{"patch"} // croak("$label requires 'patch' parameter")
$args{"patch"}
Truth table50%1/2
A
A= $args{"patch"}
Aresult
11
00
1670 8 ▶ $patch_type = $args{type} // $default_type;
Condition66%2/3
$args{"type"} // $default_type
l!l&&r!l&&!r
170
MC/DC0%0/2
$args{"type"} // $default_type
$args{"type"} $default_type
Truth table66%2/3
A // B
A= $args{"type"}B= $default_type
ABresult
1X1
011
000
1671 } else {
1672 # Class + name: patch('Pod', 'name', namespace => 'ns', patch => {...})
1673 60 my %args;
1674 60 ▶ if (@rest >= 1 && !ref($rest[0]) && $rest[0] !~ /^(name|namespace|patch|type|fieldValidation)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|patch|type|fieldValidation)$/) { } 41 19 60
elsif (@rest % 2 == 0) { } 17 2 19
Condition50%3/6
@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|patch|type|fieldValidation)$/
!ll&&!rl&&r
01941
@rest >= 1 and not ref $rest[0]
!ll&&!rl&&r
0060
MC/DC33%1/3
@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|patch|type|fieldValidation)$/
@rest >= 1 not ref $rest[0] not $rest[0] =~ /^(name|namespace|patch|type|fieldValidation)$/
Truth table50%2/4
A and not(B) and not(C)
A= @rest >= 1B= not ref $rest[0]C= not $rest[0] =~ /^(name|namespace|patch|type|fieldValidation)$/
ABCresult
0XX0
10X0
1100
1111
1675 41 $args{name} = shift @rest;
1676 41 %args = (%args, @rest);
1677 } elsif (@rest % 2 == 0) {
1678 17 %args = @rest;
1679 } else {
1680 2 croak "Invalid arguments to $label()";
1681 }
1682 58 $self->_croak_unknown_args($label, \%args, qw(name namespace patch type fieldValidation));
1683 48 $field_validation = $self->_field_validation_or_croak($label, $args{fieldValidation});
1684  
1685 46 $class = $self->_expand_class_or_croak($class_or_object);
1686 42 ▶ $name = $args{name} or croak "name required for $label";
Branch: 1 branch
BranchTrueFalseTotal
$name = $args{"name"} or croak("name required for $label") 39 3 42
1687 39 $namespace = $args{namespace};
1688 39 ▶ $patch = $args{patch} // croak "$label requires 'patch' parameter";
Condition100%2/2
$args{"patch"} // croak("$label requires 'patch' parameter")
l!l
363
MC/DC100%1/1
$args{"patch"} // croak("$label requires 'patch' parameter")
$args{"patch"}
Truth table100%2/2
A
A= $args{"patch"}
Aresult
11
00
1689 36 ▶ $patch_type = $args{type} // $default_type;
Condition66%2/3
$args{"type"} // $default_type
l!l&&r!l&&!r
16200
MC/DC0%0/2
$args{"type"} // $default_type
$args{"type"} $default_type
Truth table66%2/3
A // B
A= $args{"type"}B= $default_type
ABresult
1X1
011
000
1690 }
1691  
1692 44 ▶ my $content_type = $PATCH_TYPES{$patch_type}
Condition100%2/2
$PATCH_TYPES{$patch_type} // croak("Unknown patch type '${patch_type}' (use: strategic, merge, json)")
l!l
422
MC/DC100%1/1
$PATCH_TYPES{$patch_type} // croak("Unknown patch type '${patch_type}' (use: strategic, merge, json)")
$PATCH_TYPES{$patch_type}
Truth table100%2/2
A
A= $PATCH_TYPES{$patch_type}
Aresult
11
00
1693 // croak "Unknown patch type '$patch_type' (use: strategic, merge, json)";
1694  
1695 42 return ($class, $name, $namespace, $patch, $content_type, $field_validation);
1696 }
1697  
1698 42 ▶sub patch {
Subroutine patch: called CC=1 SCAR=0.0
Pod documented
1699 42 my ($self, $class_or_object, @rest) = @_;
1700  
1701  
1702 42 my ($class, $name, $namespace, $patch, $content_type, $field_validation)
1703 = $self->_unpack_patch_args('patch', 'strategic', $class_or_object, @rest);
1704  
1705 23 my $path = $self->_build_path($class, name => $name, namespace => $namespace,
1706 $self->_unstructured_hint($class, $class_or_object));
1707 23 my $response = $self->_request('PATCH', $path, $patch,
1708 content_type => $content_type,
1709 parameters => { fieldValidation => $field_validation },
1710 );
1711 23 $self->_check_response($response, "patch $class");
1712  
1713 22 return $self->_inflate_object($class, $response);
1714 }
1715  
1716 35 ▶sub patch_status {
Subroutine patch_status: called CC=1 SCAR=0.0
Pod documented
1717 35 my ($self, $class_or_object, @rest) = @_;
1718  
1719  
1720 35 my ($class, $name, $namespace, $patch, $content_type, $field_validation)
1721 = $self->_unpack_patch_args('patch_status', 'merge', $class_or_object, @rest);
1722  
1723 19 my $path = $self->_build_path($class,
1724 name => $name,
1725 namespace => $namespace,
1726 subresource => 'status',
1727 $self->_unstructured_hint($class, $class_or_object),
1728 );
1729 19 my $response = $self->_request('PATCH', $path, $patch,
1730 content_type => $content_type,
1731 parameters => { fieldValidation => $field_validation },
1732 );
1733 19 $self->_check_response($response, "patch_status $class");
1734  
1735 18 return $self->_inflate_object($class, $response);
1736 }
1737  
1738 13 ▶sub update_status {
Subroutine update_status: called CC=3 SCAR=11.0
Pod documented
1739 13 my ($self, $object, @opts) = @_;
1740  
1741  
1742 13 my $field_validation = $self->_write_opts_or_croak('update_status', @opts);
1743 11 my $class = ref($object);
1744 11 ▶ my $metadata = $object->metadata or croak "object must have metadata";
Branch: 1 branch
BranchTrueFalseTotal
my $metadata = $object->metadata or croak("object must have metadata") 11 0 11
1745 11 ▶ my $name = $metadata->name or croak "object must have metadata.name";
Branch: 1 branch
BranchTrueFalseTotal
my $name = $metadata->name or croak("object must have metadata.name") 10 1 11
1746 10 my $namespace = $metadata->namespace;
1747  
1748 10 my $path = $self->_build_path($class,
1749 name => $name,
1750 namespace => $namespace,
1751 subresource => 'status',
1752 $self->_unstructured_hint($class, $object),
1753 );
1754 10 my $response = $self->_request('PUT', $path, $object->TO_JSON,
1755 parameters => { fieldValidation => $field_validation });
1756 10 $self->_check_response($response, "update_status " . ref($object));
1757  
1758 9 return $self->_inflate_object($class, $response);
1759 }
1760  
1761 41 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
1762 41 my ($self, $class_or_object, @rest) = @_;
1763  
1764  
1765 41 my ($class, $response) = $self->_delete_request($class_or_object, @rest);
1766 26 $self->_check_response($response, "delete $class");
1767  
1768 25 return 1;
1769 }
1770  
1771 # delete() up to the response, unchecked: returns the resolved class and the
1772 # raw response, for the same reason as _list_request - ensure_only() treats a
1773 # 404 (already gone) differently from a failure.
1774 #
1775 # propagationPolicy goes out as the query parameter the API server reads
1776 # DeleteOptions from. Any other argument croaks before anything is sent: a
1777 # misspelt propagationPolicy that was silently dropped would leave a Job's
1778 # Pods orphaned (karr k49).
1779 57 ▶sub _delete_request {
Subroutine _delete_request: called CC=11 SCAR=24.1
1780 57 my ($self, $class_or_object, @rest) = @_;
1781  
1782 57 my ($class, $name, $namespace, %args);
1783  
1784 57 ▶ if (ref($class_or_object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class_or_object) { } 25 32 57
1785 # Object passed: delete($object), delete($object, propagationPolicy => ...)
1786 25 my $object = $class_or_object;
1787 25 ▶ croak "Invalid arguments to delete()" if @rest % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest % 2 0 25 25
1788 25 %args = @rest;
1789 25 $self->_croak_unknown_args('delete', \%args, 'propagationPolicy');
1790 23 $self->_propagation_policy_or_croak('delete', $args{propagationPolicy});
1791 23 $class = ref($object);
1792 23 ▶ my $metadata = $object->metadata or croak "object must have metadata";
Branch: 1 branch
BranchTrueFalseTotal
my $metadata = $object->metadata or croak("object must have metadata") 23 0 23
1793 23 ▶ $name = $metadata->name or croak "object must have metadata.name";
Branch: 1 branch
BranchTrueFalseTotal
$name = $metadata->name or croak("object must have metadata.name") 23 0 23
1794 23 $namespace = $metadata->namespace;
1795 } else {
1796 # Support: delete('Kind', 'name'), delete('Kind', 'name', namespace => 'ns'),
1797 # delete('Kind', name => 'name'), delete('Kind', name => 'name', namespace => 'ns'),
1798 # each with propagationPolicy => ... as well
1799 32 ▶ if (@rest == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (@rest == 1) { } 2 30 32
elsif (@rest >= 2 and not $rest[0] =~ /^(name|namespace|propagationPolicy)$/) { } 22 8 30
elsif (@rest % 2 == 0) { } 8 0 8
1800 2 $args{name} = $rest[0];
1801 ▶ } elsif (@rest >= 2 && $rest[0] !~ /^(name|namespace|propagationPolicy)$/) {
Condition100%3/3
@rest >= 2 and not $rest[0] =~ /^(name|namespace|propagationPolicy)$/
!ll&&!rl&&r
2622
MC/DC100%2/2
@rest >= 2 and not $rest[0] =~ /^(name|namespace|propagationPolicy)$/
@rest >= 2 not $rest[0] =~ /^(name|namespace|propagationPolicy)$/
Truth table100%3/3
A and not(B)
A= @rest >= 2B= not $rest[0] =~ /^(name|namespace|propagationPolicy)$/
ABresult
0X0
100
111
1802 # First arg is name, rest are key=value pairs
1803 22 $args{name} = shift @rest;
1804 22 %args = (%args, @rest);
1805 } elsif (@rest % 2 == 0) {
1806 8 %args = @rest;
1807 } else {
1808 0 croak "Invalid arguments to delete()";
1809 }
1810 32 $self->_croak_unknown_args('delete', \%args,
1811 qw(name namespace propagationPolicy));
1812 30 $self->_propagation_policy_or_croak('delete', $args{propagationPolicy});
1813  
1814 29 $class = $self->_expand_class_or_croak($class_or_object);
1815 22 ▶ $name = $args{name} or croak "name required for delete";
Branch: 1 branch
BranchTrueFalseTotal
$name = $args{"name"} or croak("name required for delete") 20 2 22
1816 20 $namespace = $args{namespace};
1817 }
1818  
1819 43 my $path = $self->_build_path($class, name => $name, namespace => $namespace,
1820 $self->_unstructured_hint($class, $class_or_object));
1821 42 my $policy = $args{propagationPolicy};
1822 42 ▶ return ($class, defined $policy
Branch: 1 branch
BranchTrueFalseTotal
defined $policy ? : 25 17 42
1823 ? $self->_request('DELETE', $path, undef,
1824 parameters => { propagationPolicy => $policy })
1825 : $self->_request('DELETE', $path));
1826 }
1827  
1828 # The propagationPolicy values DeleteOptions takes.
1829 97 my @PROPAGATION_POLICIES = qw(Background Foreground Orphan);
1830  
1831 # Croak unless $policy is undef (none given) or one of @PROPAGATION_POLICIES.
1832 # $label only appears in the message.
1833 76 ▶sub _propagation_policy_or_croak {
Subroutine _propagation_policy_or_croak: called CC=3 SCAR=11.0
1834 76 my ($self, $label, $policy) = @_;
1835 76 ▶ return $policy if !defined $policy || grep { $_ eq $policy } @PROPAGATION_POLICIES;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $policy or grep {$_ eq $policy;} @PROPAGATION_POLICIES 74 2 76
Condition100%3/3
not defined $policy or grep {$_ eq $policy;} @PROPAGATION_POLICIES
l!l&&r!l&&!r
27472
MC/DC100%2/2
not defined $policy or grep {$_ eq $policy;} @PROPAGATION_POLICIES
not defined $policy grep {$_ eq $policy;} @PROPAGATION_POLICIES
Truth table100%3/3
not(A) or B
A= not defined $policyB= grep {$_ eq $policy;} @PROPAGATION_POLICIES
ABresult
1X1
011
000
1836 2 croak "Unknown propagationPolicy '$policy' for $label() (use: "
1837 . join(', ', @PROPAGATION_POLICIES) . ")";
1838 }
1839  
1840 # The fieldValidation a write sends: the request's own when it gives one, else
1841 # the client's field_validation, else none (undef - the URL then has no
1842 # query). A value the API server does not take croaks before anything is sent,
1843 # naming the option and the allowed values, never the value (karr k79).
1844 # $label only appears in the message.
1845 166 ▶sub _field_validation_or_croak {
Subroutine _field_validation_or_croak: called CC=3 SCAR=11.0
1846 166 my ($self, $label, $requested) = @_;
1847 166 ▶ return $self->field_validation unless defined $requested;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $requested 148 18 166
1848 18 ▶ return $requested if grep { $_ eq $requested } @FIELD_VALIDATIONS;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $requested;} @FIELD_VALIDATIONS 12 6 18
1849 6 croak "Unknown fieldValidation value for $label() (use: "
1850 . join(', ', @FIELD_VALIDATIONS) . ")";
1851 }
1852  
1853 # The options after the object of create(), update() and update_status():
1854 # fieldValidation only, any other key croaking as everywhere else. Returns
1855 # the fieldValidation to send. $label only appears in the messages.
1856 112 ▶sub _write_opts_or_croak {
Subroutine _write_opts_or_croak: called CC=2 SCAR=7.0
1857 112 my ($self, $label, @opts) = @_;
1858 112 ▶ croak "Invalid arguments to $label()" if @opts % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @opts % 2 0 112 112
1859 112 my %opts = @opts;
1860 112 $self->_croak_unknown_args($label, \%opts, 'fieldValidation');
1861 109 return $self->_field_validation_or_croak($label, $opts{fieldValidation});
1862 }
1863  
1864 # Croak on the first key of %$args that is not in @allowed, naming it and
1865 # what is allowed, instead of ignoring it. $label only appears in the message.
1866 534 ▶sub _croak_unknown_args {
Subroutine _croak_unknown_args: called CC=2 SCAR=6.9
1867 534 my ($self, $label, $args, @allowed) = @_;
1868 534 my %allowed = map { $_ => 1 } @allowed;
1869 534 my ($unknown) = sort grep { !$allowed{$_} } keys %$args;
1870 534 ▶ return unless defined $unknown;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $unknown 464 70 534
1871 70 croak "Unknown argument '$unknown' to $label() (allowed: "
1872 . join(', ', @allowed) . ")";
1873 }
1874  
1875 # Shared hashref handling for ensure() and ensure_only(): turns a manifest into
1876 # a typed object. A manifest's apiVersion is authoritative - with one, the
1877 # class is resolved as that exact group/version/Kind, and an apiVersion no
1878 # class serves croaks instead of falling back to the version the bare Kind
1879 # happens to map to (HorizontalPodAutoscaler alone means autoscaling/v2, a
1880 # different endpoint and schema than an autoscaling/v1 manifest). Without an
1881 # apiVersion the bare Kind resolves as it always did, and a Kind nothing
1882 # resolves croaks naming it, as for every method taking a resource name -
1883 # not with the module loader's "Can't locate IO/K8s/<Kind>.pm" (karr k48).
1884 # Either way the class is resolved here, so it goes to struct_to_object with
1885 # its '+': IO::K8s takes it as that exact class instead of resolving the name
1886 # again (see _exact_class; the class need not be loaded yet, so this does not
1887 # ask it). $label only appears in croak messages.
1888 27 ▶sub _manifest_to_object {
Subroutine _manifest_to_object: called CC=5 SCAR=16.2
1889 27 my ($self, $label, $manifest) = @_;
1890 27 ▶ my $kind = $manifest->{kind} or croak "$label: hashref must have 'kind'";
Branch: 1 branch
BranchTrueFalseTotal
my $kind = $manifest->{"kind"} or croak("${label}: hashref must have 'kind'") 27 0 27
1891 27 my $api_version = $manifest->{apiVersion};
1892  
1893 27 ▶ return $self->k8s->struct_to_object('+' . $self->_expand_class_or_croak($kind), $manifest)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $api_version and length $api_version 5 22 27
Condition66%2/3
defined $api_version and length $api_version
!ll&&!rl&&r
5022
MC/DC50%1/2
defined $api_version and length $api_version
defined $api_version length $api_version
Truth table66%2/3
A and B
A= defined $api_versionB= length $api_version
ABresult
0X0
100
111
1894 unless defined $api_version && length $api_version;
1895  
1896 22 ▶ my $class = $self->expand_class($kind, $api_version)
Condition100%2/2
$self->expand_class($kind, $api_version) // croak("${label}: no IO::K8s class for apiVersion '${api_version}', kind '${kind}'" . ' (add it to resource_map if it is a CRD)')
l!l
193
MC/DC100%1/1
$self->expand_class($kind, $api_version) // croak("${label}: no IO::K8s class for apiVersion '${api_version}', kind '${kind}'" . ' (add it to resource_map if it is a CRD)')
$self->expand_class($kind, $api_version)
Truth table100%2/2
A
A= $self->expand_class($kind, $api_version)
Aresult
11
00
1897 // croak "$label: no IO::K8s class for apiVersion '$api_version', kind '$kind'"
1898 . " (add it to resource_map if it is a CRD)";
1899 19 return $self->k8s->struct_to_object("+$class", $manifest);
1900 }
1901  
1902 # The apiVersion and Kind an object is an instance of, for ensure() and
1903 # ensure_only() to tell resources apart by. A typed object answers from its
1904 # class (api_version(), kind()); IO::K8s::Unstructured from its instance data,
1905 # since its class name says nothing about what it holds. The last segment of a
1906 # class name is not enough on its own: a CRD is free to reuse a built-in Kind
1907 # name in its own group.
1908 88 ▶sub _api_version_and_kind {
Subroutine _api_version_and_kind: called CC=6 SCAR=19.0
1909 88 my ($self, $object) = @_;
1910 88 ▶ my $api_version = ref($object) eq 'IO::K8s::Unstructured' ? $object->apiVersion
Branch: 2 branches
BranchTrueFalseTotal
ref $object eq 'IO::K8s::Unstructured' ? : 11 77 88
$object->can('api_version') ? : 77 0 77
1911 : $object->can('api_version') ? $object->api_version
1912 : undef;
1913 88 ▶ my $kind = $object->can('kind') ? $object->kind : undef;
Branch: 1 branch
BranchTrueFalseTotal
$object->can('kind') ? : 88 0 88
1914 88 ▶ ($kind = ref $object) =~ s/.*::// unless defined $kind;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kind 0 88 88
1915 88 ▶ return ($api_version // '', $kind);
Condition50%1/2
$api_version // ""
l!l
880
MC/DC0%0/1
$api_version // ""
$api_version
Truth table50%1/2
A
A= $api_version
Aresult
11
00
1916 }
1917  
1918 54 ▶sub ensure {
Subroutine ensure: called CC=17 SCAR=28.4
Pod documented
1919 54 my ($self, $object, @extra) = @_;
1920  
1921  
1922 # Nothing after the object is read (karr k58): namespace => ... would not
1923 # move it, a second object would not be applied.
1924 54 ▶ croak 'Invalid arguments to ensure(): it takes one object or hashref'
Branch: 1 branch
BranchTrueFalseTotal
if @extra 2 52 54
1925 . ' (ensure_all takes several)'
1926 if @extra;
1927 52 ▶ $object = $self->_manifest_to_object('ensure', $object) if ref($object) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $object eq "HASH" 18 34 52
1928  
1929 48 my $class = ref($object);
1930 48 ▶ croak "ensure requires an IO::K8s object or hashref" unless blessed($object);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($object) 0 48 48
1931 48 my ($api_version, $kind) = $self->_api_version_and_kind($object);
1932 # The special cases below are the built-in core v1 PersistentVolumeClaim
1933 # and batch/v1 Job only. The apiVersion is compared exactly, not just its
1934 # group: the Job branch reads batch/v1's status fields and deletes what it
1935 # takes for a failed Job, so an apiVersion it was not written for falls
1936 # through to the plain update, where a mismatch fails loudly instead.
1937 48 ▶ my $is_pvc = $api_version eq 'v1' && $kind eq 'PersistentVolumeClaim';
Condition100%3/3
$api_version eq 'v1' && $kind eq 'PersistentVolumeClaim'
!ll&&!rl&&r
27192
MC/DC100%2/2
$api_version eq 'v1' && $kind eq 'PersistentVolumeClaim'
$api_version eq 'v1' $kind eq 'PersistentVolumeClaim'
Truth table100%3/3
A && B
A= $api_version eq 'v1'B= $kind eq 'PersistentVolumeClaim'
ABresult
0X0
100
111
1938 48 ▶ my $is_job = $api_version eq 'batch/v1' && $kind eq 'Job';
Condition66%2/3
$api_version eq 'batch/v1' && $kind eq 'Job'
!ll&&!rl&&r
4206
MC/DC50%1/2
$api_version eq 'batch/v1' && $kind eq 'Job'
$api_version eq 'batch/v1' $kind eq 'Job'
Truth table66%2/3
A && B
A= $api_version eq 'batch/v1'B= $kind eq 'Job'
ABresult
0X0
100
111
1939 48 ▶ my $metadata = $object->metadata or croak "object must have metadata";
Branch: 1 branch
BranchTrueFalseTotal
my $metadata = $object->metadata or croak("object must have metadata") 48 0 48
1940 48 ▶ my $name = $metadata->name or croak "object must have metadata.name";
Branch: 1 branch
BranchTrueFalseTotal
my $name = $metadata->name or croak("object must have metadata.name") 48 0 48
1941 48 my $namespace = $metadata->namespace;
1942  
1943 48 my @unstructured_hint = $self->_unstructured_hint($class, $object);
1944 48 my $path = $self->_build_path($class, name => $name, namespace => $namespace,
1945 @unstructured_hint);
1946  
1947 # Every branch below is taken on the status of the response, never on the
1948 # text of an error: a 500 or 422 whose message merely contains "404" or
1949 # "409" is a failure, not a missing object or a conflict (karr k44).
1950 47 my $existing;
1951 47 my $response = $self->_request('GET', $path);
1952 47 ▶ unless ($response->status == 404) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->status == 404) 17 30 47
1953 17 $self->_check_response($response, "ensure get $kind/$name");
1954 16 $existing = $self->_inflate_object($class, $response);
1955 }
1956  
1957 46 ▶ unless ($existing) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($existing) 30 16 46
1958 30 (undef, $response) = $self->_create_request($object);
1959 30 ▶ unless ($response->status == 409) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->status == 409) 25 5 30
1960 25 $self->_check_response($response, "create $class");
1961 24 return $self->_inflate_object($class, $response);
1962 }
1963 # 409 AlreadyExists: it appeared between the GET and the POST. From
1964 # here on it is an existing object like any other, special cases
1965 # included - a Job must not get a PUT onto its immutable Pod template.
1966 5 $response = $self->_request('GET', $path);
1967 5 $self->_check_response($response, "ensure post-409 get $kind/$name");
1968 5 $existing = $self->_inflate_object($class, $response);
1969 }
1970  
1971 21 ▶ return $existing if $is_pvc;
Branch: 1 branch
BranchTrueFalseTotal
if $is_pvc 2 19 21
1972 19 ▶ if ($is_job) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_job) 6 13 19
1973 # Read from TO_JSON, not status(): an IO::K8s::Unstructured Job has
1974 # no status accessor, its status rides in the unknown-fields bag.
1975 6 ▶ my $status = $existing->TO_JSON->{status} || {};
Condition50%1/2
$existing->TO_JSON->{'status'} || {}
l!l
60
MC/DC0%0/1
$existing->TO_JSON->{'status'} || {}
$existing->TO_JSON->{'status'}
Truth table50%1/2
A
A= $existing->TO_JSON->{'status'}
Aresult
11
00
1976 6 ▶ return $existing if $status->{succeeded} || $status->{active};
Branch: 1 branch
BranchTrueFalseTotal
if $status->{"succeeded"} or $status->{"active"} 3 3 6
Condition100%3/3
$status->{"succeeded"} or $status->{"active"}
l!l&&r!l&&!r
213
MC/DC100%2/2
$status->{"succeeded"} or $status->{"active"}
$status->{"succeeded"} $status->{"active"}
Truth table100%3/3
A or B
A= $status->{"succeeded"}B= $status->{"active"}
ABresult
1X1
011
000
1977 # Background, or the failed Job's Pods would be orphaned - the API
1978 # default for a Job (karr k49) - and the old Job is gone at once, so
1979 # the create does not run into it.
1980 3 eval { $self->delete($existing, propagationPolicy => 'Background') };
1981 3 return $self->create($object);
1982 }
1983 13 $object->metadata->resourceVersion($existing->metadata->resourceVersion);
1984 13 (undef, $response) = $self->_update_request($object);
1985 13 ▶ unless ($response->status == 409) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->status == 409) 12 1 13
1986 12 $self->_check_response($response, "update $class");
1987 11 return $self->_inflate_object($class, $response);
1988 }
1989 # 409 Conflict: the resourceVersion moved on server-side. Re-fetch it and
1990 # retry once; a second conflict croaks.
1991 1 $response = $self->_request('GET', $path);
1992 1 $self->_check_response($response, "ensure refetch $kind/$name");
1993 1 $existing = $self->_inflate_object($class, $response);
1994 1 $object->metadata->resourceVersion($existing->metadata->resourceVersion);
1995 1 return $self->update($object);
1996 }
1997  
1998 20 ▶sub ensure_all {
Subroutine ensure_all: called CC=1 SCAR=0.0
Pod documented
1999 20 my ($self, @objects) = @_;
2000  
2001  
2002 20 return map { $self->ensure($_) } @objects;
2003 }
2004  
2005 26 ▶sub ensure_only {
Subroutine ensure_only: called CC=18 SCAR=28.9
Pod documented
2006 26 my ($self, %args) = @_;
2007  
2008  
2009 # A misspelt option is not ignored, before anything is applied: a
2010 # propagation_policy typo would prune with Background, a namespace typo
2011 # scan cluster scope only (karr k53).
2012 26 $self->_croak_unknown_args('ensure_only', \%args,
2013 qw(label objects kinds namespaces propagationPolicy));
2014 23 ▶ my $label = $args{label} or croak "ensure_only requires 'label'";
Branch: 1 branch
BranchTrueFalseTotal
my $label = $args{"label"} or croak("ensure_only requires 'label'") 23 0 23
2015 23 ▶ my @objects = @{$args{objects} || []};
Condition50%1/2
$args{'objects'} || []
l!l
230
MC/DC0%0/1
$args{'objects'} || []
$args{'objects'}
Truth table50%1/2
A
A= $args{'objects'}
Aresult
11
00
2016 23 ▶ my @kinds = @{$args{kinds} || []};
Condition50%1/2
$args{'kinds'} || []
l!l
230
MC/DC0%0/1
$args{'kinds'} || []
$args{'kinds'}
Truth table50%1/2
A
A= $args{'kinds'}
Aresult
11
00
2017 23 ▶ my @namespaces = @{$args{namespaces} || [undef]};
Condition100%2/2
$args{'namespaces'} || [undef]
l!l
212
MC/DC100%1/1
$args{'namespaces'} || [undef]
$args{'namespaces'}
Truth table100%2/2
A
A= $args{'namespaces'}
Aresult
11
00
2018 # Background by default, as kubectl delete does: a pruned Job would
2019 # otherwise orphan its Pods (karr k49). Checked before anything is applied.
2020 my $propagation = $self->_propagation_policy_or_croak('ensure_only',
2021 23 ▶ $args{propagationPolicy} // 'Background');
Condition100%2/2
$args{"propagationPolicy"} // "Background"
l!l
221
MC/DC100%1/1
$args{"propagationPolicy"} // "Background"
$args{"propagationPolicy"}
Truth table100%2/2
A
A= $args{"propagationPolicy"}
Aresult
11
00
2022  
2023 22 for my $obj (@objects) {
2024 19 ▶ $obj = $self->_manifest_to_object('ensure_only', $obj) if ref($obj) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $obj eq "HASH" 9 10 19
2025 }
2026  
2027 19 my @results = $self->ensure_all(@objects);
2028  
2029 # (group, Kind, namespace, name), taken from the object on both sides -
2030 # never from the kinds entry, which may be qualified ('autoscaling/v1/...')
2031 # and would then match nothing, deleting the objects just applied. Group
2032 # and Kind come from _api_version_and_kind: class-derived for a typed
2033 # object, instance data for IO::K8s::Unstructured. The group keeps the same
2034 # Kind name in two groups apart (Istio's and the Gateway API's Gateway);
2035 # the core group is ''. No version in the key: the same resource listed
2036 # through another version's class is still the same resource.
2037 39 ▶ my $key_of = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
2038 39 my ($obj) = @_;
2039 39 my ($api_version, $kind) = $self->_api_version_and_kind($obj);
2040 39 my ($group) = $api_version =~ m{\A(.*)/[^/]*\z};
2041 39 my $metadata = $obj->metadata;
2042 39 ▶ return join("\0", $group // '', $kind, $metadata->namespace // '', $metadata->name);
Condition75%3/4
$group // ""
l!l
1920
$metadata->namespace // ""
l!l
390
MC/DC100%1/1
$group // ""
$group
MC/DC0%0/1
$metadata->namespace // ""
$metadata->namespace
Truth table100%2/2
A
A= $group
Aresult
11
00
Truth table50%1/2
A
A= $metadata->namespace
Aresult
11
00
2043 19 };
2044 19 my %expected = map { $key_of->($_) => 1 } @objects;
2045  
2046 # A failed list or delete leaves stale objects behind, so it is reported
2047 # rather than swallowed - but only a real failure: a 404 on the list means
2048 # the cluster does not serve the Kind, a 404 on the delete that the object
2049 # is already gone. The status comes from the response, never from the text
2050 # of an error. The caught croak already names the caller's line, which
2051 # carp adds again, so the reason drops it.
2052 8 ▶ my $where = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
2053 8 my ($ns) = @_;
2054 8 ▶ return defined $ns ? "in namespace '$ns'" : 'at cluster scope';
Branch: 1 branch
BranchTrueFalseTotal
defined $ns ? : 7 1 8
2055 19 };
2056 8 ▶ my $reason_of = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2057 8 my ($error) = @_;
2058 8 $error =~ s/\s+\z//;
2059 8 $error =~ s/ at \S+ line \d+\.\z//;
2060 8 return $error;
2061 19 };
2062  
2063 19 for my $kind (@kinds) {
2064 22 for my $ns (@namespaces) {
2065 23 my %list_args = (labelSelector => $label);
2066 23 ▶ $list_args{namespace} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 22 1 23
2067 23 my $list = eval {
2068 23 my ($class, $response) = $self->_list_request($kind, %list_args);
2069 19 ▶ return if $response->status == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $response->status == 404 1 18 19
2070 18 $self->_check_response($response, "list $kind");
2071 15 $self->_inflate_list($class, $response);
2072 };
2073 23 ▶ unless ($list) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($list) 8 15 23
2074 8 ▶ carp "ensure_only: cannot list $kind " . $where->($ns)
Branch: 1 branch
BranchTrueFalseTotal
if $@ 7 1 8
2075 . ', nothing pruned there: ' . $reason_of->($@)
2076 if $@;
2077 7 next;
2078 }
2079 15 for my $item (@{$list->items}) {
2080 26 ▶ next if $expected{ $key_of->($item) };
Branch: 1 branch
BranchTrueFalseTotal
if $expected{&$key_of($item)} 10 16 26
2081 16 ▶ next if eval {
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { () = $self->_delete_request($item, "propagationPolicy", $propagation); $response->status == 404 or $self->_check_response($response, "delete $class") } } 15 1 16
2082 16 my ($class, $response) = $self->_delete_request($item,
2083 propagationPolicy => $propagation);
2084 16 ▶ $response->status == 404
Condition66%2/3
$response->status == 404 || $self->_check_response($response, "delete $class")
l!l&&r!l&&!r
1150
MC/DC0%0/2
$response->status == 404 || $self->_check_response($response, "delete $class")
$response->status == 404 $self->_check_response($response, "delete $class")
Truth table66%2/3
A || B
A= $response->status == 404B= $self->_check_response($response, "delete $class")
ABresult
1X1
011
000
2085 || $self->_check_response($response, "delete $class");
2086 };
2087 1 my (undef, $item_kind) = $self->_api_version_and_kind($item);
2088 1 carp "ensure_only: cannot delete $item_kind '" . $item->metadata->name
2089 . "' " . $where->($item->metadata->namespace)
2090 . ': ' . $reason_of->($@);
2091 }
2092 }
2093 }
2094  
2095 18 return @results;
2096 }
2097  
2098 12 ▶sub ensure_crd {
Subroutine ensure_crd: called CC=13 SCAR=25.7
Pod documented
2099 12 my $self = shift;
2100  
2101  
2102 12 my (@classes, %opts);
2103 12 ▶ if (@_ && ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and ref $_[0] eq 'ARRAY') { } 8 4 12
Condition100%3/3
@_ and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
138
MC/DC100%2/2
@_ and ref $_[0] eq "ARRAY"
@_ ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= @_B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
2104 8 @classes = @{ shift() };
2105 8 %opts = @_;
2106 } else {
2107 4 @classes = @_;
2108 }
2109 # A misspelt option is not ignored, before any class is asked for its CRD
2110 # (karr k61): tiemout => 5 waited the default 30 seconds.
2111 12 $self->_croak_unknown_args('ensure_crd', \%opts, qw(timeout poll_interval storage));
2112 8 ▶ croak "ensure_crd requires at least one CRD class" unless @classes;
Branch: 1 branch
BranchTrueFalseTotal
unless @classes 1 7 8
2113  
2114 7 my $storage = $opts{storage};
2115  
2116 # Group the classes by the CRD they name (metadata.name = "$plural.$group"),
2117 # preserving first-seen order so the applied order is predictable.
2118 7 my (@order, %group);
2119 7 for my $class (@classes) {
2120 9 my $single = $class->to_crd;
2121 9 my $crd_name = $single->metadata->name;
2122 9 ▶ push @order, $crd_name unless exists $group{$crd_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $group{$crd_name} 7 2 9
2123 9 push @{ $group{$crd_name} }, { class => $class, single => $single };
2124 }
2125  
2126 7 my @crds;
2127 7 for my $crd_name (@order) {
2128 7 my $members = $group{$crd_name};
2129 7 ▶ if (@$members == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$members == 1) 5 2 7
2130 5 push @crds, $members->[0]{single};
2131 5 next;
2132 }
2133 # Two or more classes are versions of the SAME CRD: assemble one
2134 # multi-version CRD rather than apply competing single-version ones.
2135 2 my @version_classes = map { $_->{class} } @$members;
2136 2 my @versions = map { $_->{single}->spec->versions->[0]->name } @$members;
2137 my $storage_version =
2138 !defined $storage ? undef
2139 2 ▶ : ref $storage eq 'HASH' ? $storage->{$crd_name}
Branch: 2 branches
BranchTrueFalseTotal
!defined($storage) ? : 1 1 2
ref $storage eq 'HASH' ? : 0 1 1
2140 : $storage;
2141 2 ▶ croak "ensure_crd: '$crd_name' is defined by multiple classes ("
Branch: 1 branch
BranchTrueFalseTotal
unless defined $storage_version and length $storage_version 1 1 2
Condition66%2/3
defined $storage_version and length $storage_version
!ll&&!rl&&r
101
MC/DC50%1/2
defined $storage_version and length $storage_version
defined $storage_version length $storage_version
Truth table66%2/3
A and B
A= defined $storage_versionB= length $storage_version
ABresult
0X0
100
111
2142 . join(', ', @version_classes) . ") naming versions ("
2143 . join(', ', @versions) . "); pass storage => '<version>' (or "
2144 . "storage => { '$crd_name' => '<version>' }) to name the storage version"
2145 unless defined $storage_version && length $storage_version;
2146 1 push @crds,
2147 IO::K8s::CRD->new(classes => \@version_classes, storage => $storage_version);
2148 }
2149  
2150 # Apply every CRD first, then wait for each to establish (so establishment
2151 # happens in parallel), then invalidate discovery exactly once.
2152 6 $self->ensure($_) for @crds;
2153 6 my @established = map { $self->_wait_crd_established($_, %opts) } @crds;
2154 3 $self->invalidate_discovery;
2155 3 return @established;
2156 }
2157  
2158 # Poll GET on a CustomResourceDefinition by name until its Established condition
2159 # is True, or croak on timeout. A 404 during polling means "not registered yet"
2160 # and is treated as not-established, not an error. That is read off the status
2161 # of the response, never out of the text of an error: any other failure - a
2162 # 500 whose message merely contains "404" too - ends the wait with that error
2163 # instead of being polled away into a timeout (karr k45).
2164 6 ▶sub _wait_crd_established {
Subroutine _wait_crd_established: called CC=7 SCAR=19.5
2165 6 my ($self, $crd, %opts) = @_;
2166  
2167 6 my $class = ref $crd;
2168 6 my $name = $crd->metadata->name;
2169 6 my $path = $self->_build_path($class, name => $name);
2170 6 ▶ my $timeout = defined $opts{timeout} ? $opts{timeout} : 30;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'timeout'} ? : 4 2 6
2171 6 ▶ my $interval = defined $opts{poll_interval} ? $opts{poll_interval} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'poll_interval'} ? : 2 4 6
2172 6 my $deadline = Time::HiRes::time() + $timeout;
2173  
2174 6 while (1) {
2175 8 my $response = $self->_request('GET', $path);
2176 8 ▶ unless ($response->status == 404) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->status == 404) 4 4 8
2177 4 $self->_check_response($response, "ensure_crd wait $name");
2178 3 my $current = $self->_inflate_object($class, $response);
2179 3 ▶ return $current if $current && $self->_crd_established($current);
Branch: 1 branch
BranchTrueFalseTotal
if $current and $self->_crd_established($current) 3 0 3
Condition33%1/3
$current and $self->_crd_established($current)
!ll&&!rl&&r
003
MC/DC0%0/2
$current and $self->_crd_established($current)
$current $self->_crd_established($current)
Truth table33%1/3
A and B
A= $currentB= $self->_crd_established($current)
ABresult
0X0
100
111
2180 }
2181 4 ▶ last if Time::HiRes::time() >= $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if &Time::HiRes::time() >= $deadline 2 2 4
2182 2 Time::HiRes::sleep($interval);
2183 }
2184  
2185 2 croak "ensure_crd: CustomResourceDefinition '$name' did not reach the "
2186 . "Established condition within ${timeout}s";
2187 }
2188  
2189 # True when a CustomResourceDefinition object carries an Established=True
2190 # condition in status.conditions.
2191 5 ▶sub _crd_established {
Subroutine _crd_established: called CC=8 SCAR=25.5
2192 5 my ($self, $crd) = @_;
2193 5 ▶ my $status = $crd->status or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $status = $crd->status or return 0 5 0 5
2194 5 ▶ my $conditions = $status->conditions or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $conditions = $status->conditions or return 0 5 0 5
2195 5 for my $cond (@$conditions) {
2196 5 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if ($cond->type // "") eq "Established" and ($cond->status // "") eq "True" 5 0 5
Condition42%3/7
($cond->type // "") eq "Established" and ($cond->status // "") eq "True"
!ll&&!rl&&r
005
$cond->type // ""
l!l
50
$cond->status // ""
l!l
50
MC/DC0%0/2
($cond->type // "") eq "Established" and ($cond->status // "") eq "True"
$cond->type $cond->status
Truth table33%1/3
A and B
A= $cond->typeB= $cond->status
ABresult
0X0
100
111
2197 if ($cond->type // '') eq 'Established'
2198 && ($cond->status // '') eq 'True';
2199 }
2200 0 return 0;
2201 }
2202  
2203 36 ▶sub watch {
Subroutine watch: called CC=10 SCAR=23.0
Pod documented
2204 36 my ($self, $short_class, %args) = @_;
2205  
2206  
2207 # A misspelt option is not ignored (karr k58): timeoutSeconds ran for the
2208 # default 300. name is out too: it made the request a GET of one object,
2209 # which the server answers with the object, not a watch.
2210 36 $self->_croak_unknown_args('watch', \%args, qw(on_event timeout
2211 resourceVersion labelSelector fieldSelector namespace));
2212 my $on_event = delete $args{on_event}
2213 30 ▶ or croak "watch requires 'on_event' callback";
Branch: 1 branch
BranchTrueFalseTotal
my $on_event = delete $args{"on_event"} or croak("watch requires 'on_event' callback") 29 1 30
2214 29 ▶ my $timeout = delete $args{timeout} // 300;
Condition100%2/2
delete $args{"timeout"} // 300
l!l
326
MC/DC100%1/1
delete $args{"timeout"} // 300
delete $args{"timeout"}
Truth table100%2/2
A
A= delete $args{"timeout"}
Aresult
11
00
2215 29 my $resource_version = delete $args{resourceVersion};
2216 29 my $label_selector = delete $args{labelSelector};
2217 29 my $field_selector = delete $args{fieldSelector};
2218  
2219 29 my $class = $self->_expand_class_or_croak($short_class);
2220 27 my $path = $self->_build_path($class, %args,
2221 $self->_unstructured_hint($class, $short_class));
2222  
2223 27 my %params = (
2224 watch => 'true',
2225 timeoutSeconds => $timeout,
2226 # BOOKMARK events carry the server's current resourceVersion (k69).
2227 allowWatchBookmarks => 'true',
2228 );
2229 27 ▶ $params{resourceVersion} = $resource_version if defined $resource_version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $resource_version 12 15 27
2230 27 ▶ $params{labelSelector} = $label_selector if defined $label_selector;
Branch: 1 branch
BranchTrueFalseTotal
if defined $label_selector 3 24 27
2231 27 ▶ $params{fieldSelector} = $field_selector if defined $field_selector;
Branch: 1 branch
BranchTrueFalseTotal
if defined $field_selector 3 24 27
2232  
2233 27 my $req = $self->_prepare_request('GET', $path, parameters => \%params);
2234  
2235 27 my $buffer = '';
2236 27 my $last_rv = $resource_version;
2237 # Without a start resourceVersion the server first replays the existing
2238 # objects as ADDED events, each with its own older version: no resume
2239 # point until the first BOOKMARK (k69).
2240 27 ▶ my $trusted = defined $resource_version ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $resource_version ? : 12 15 27
2241 27 my $got_410 = 0;
2242  
2243 30 ▶ my $data_callback = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
2244 30 my ($chunk) = @_;
2245 30 for my $result ($self->_process_watch_chunk($class, \$buffer, $chunk)) {
2246 30 my $is_bookmark = $result->{event}->type eq 'BOOKMARK';
2247 30 ▶ $trusted = 1 if $is_bookmark;
Branch: 1 branch
BranchTrueFalseTotal
if $is_bookmark 4 26 30
2248 $last_rv = $result->{resourceVersion}
2249 30 ▶ if $result->{resourceVersion} && $trusted;
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"resourceVersion"} and $trusted 13 17 30
Condition100%3/3
$result->{"resourceVersion"} and $trusted
!ll&&!rl&&r
31413
MC/DC100%2/2
$result->{"resourceVersion"} and $trusted
$result->{"resourceVersion"} $trusted
Truth table100%3/3
A and B
A= $result->{"resourceVersion"}B= $trusted
ABresult
0X0
100
111
2250 30 ▶ $got_410 = 1 if $result->{error_code} == 410;
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"error_code"} == 410 1 29 30
2251 30 ▶ next if $is_bookmark;
Branch: 1 branch
BranchTrueFalseTotal
if $is_bookmark 4 26 30
2252 26 $on_event->($result->{event});
2253 }
2254 27 };
2255  
2256 27 my $response = $self->io->call_streaming($req, $data_callback);
2257  
2258 # 410 on the request itself is the same expired resourceVersion as the
2259 # ERROR event; without a resourceVersion it stays an APIError (k69).
2260 25 ▶ croak "Watch expired (410 Gone): resourceVersion too old, re-list to get a fresh resourceVersion"
Branch: 1 branch
BranchTrueFalseTotal
if $response->status == 410 and defined $resource_version 1 24 25
Condition100%3/3
$response->status == 410 and defined $resource_version
!ll&&!rl&&r
2311
MC/DC100%2/2
$response->status == 410 and defined $resource_version
$response->status == 410 defined $resource_version
Truth table100%3/3
A and B
A= $response->status == 410B= defined $resource_version
ABresult
0X0
100
111
2261 if $response->status == 410 && defined $resource_version;
2262 24 $self->_check_response($response, "watch $short_class");
2263  
2264 19 ▶ croak "Watch expired (410 Gone): resourceVersion too old, re-list to get a fresh resourceVersion"
Branch: 1 branch
BranchTrueFalseTotal
if $got_410 1 18 19
2265 if $got_410;
2266  
2267 18 return $last_rv;
2268 }
2269  
2270 23 ▶sub log {
Subroutine log: called CC=17 SCAR=28.5
Pod documented
2271 23 my ($self, $short_class, @rest) = @_;
2272  
2273  
2274 # Support: log('Pod', 'name', ...) and log('Pod', name => 'name', ...)
2275 23 my %args;
2276 23 ▶ if (@rest >= 1 && !ref($rest[0]) && $rest[0] !~ /^(name|namespace|container|follow|tailLines|sinceSeconds|sinceTime|timestamps|previous|limitBytes|on_line)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|container|follow|tailLines|sinceSeconds|sinceTime|timestamps|previous|limitBytes|on_line)$/) { } 20 3 23
elsif (@rest % 2 == 0) { } 3 0 3
Condition50%3/6
@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|container|follow|tailLines|sinceSeconds|sinceTime|timestamps|previous|limitBytes|on_line)$/
!ll&&!rl&&r
0320
@rest >= 1 and not ref $rest[0]
!ll&&!rl&&r
0023
MC/DC33%1/3
@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|container|follow|tailLines|sinceSeconds|sinceTime|timestamps|previous|limitBytes|on_line)$/
@rest >= 1 not ref $rest[0] not $rest[0] =~ /^(name|namespace|container|follow|tailLines|sinceSeconds|sinceTime|timestamps|previous|limitBytes|on_line)$/
Truth table50%2/4
A and not(B) and not(C)
A= @rest >= 1B= not ref $rest[0]C= not $rest[0] =~ /^(name|namespace|container|follow|tailLines|sinceSeconds|sinceTime|timestamps|previous|limitBytes|on_line)$/
ABCresult
0XX0
10X0
1100
1111
2277 20 $args{name} = shift @rest;
2278 20 %args = (%args, @rest);
2279 } elsif (@rest % 2 == 0) {
2280 3 %args = @rest;
2281 } else {
2282 0 croak "Invalid arguments to log()";
2283 }
2284 # A misspelt option is not ignored (karr k53): tail_lines => 10 would
2285 # fetch the whole log.
2286 23 $self->_croak_unknown_args('log', \%args, qw(name namespace container
2287 follow tailLines sinceSeconds sinceTime timestamps previous limitBytes
2288 on_line));
2289  
2290 20 ▶ croak "name required for log" unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 1 19 20
2291  
2292 19 my $on_line = delete $args{on_line};
2293 19 my $container = delete $args{container};
2294 19 my $follow = delete $args{follow};
2295 19 my $tail_lines = delete $args{tailLines};
2296 19 my $since_seconds = delete $args{sinceSeconds};
2297 19 my $since_time = delete $args{sinceTime};
2298 19 my $timestamps = delete $args{timestamps};
2299 19 my $previous = delete $args{previous};
2300 19 my $limit_bytes = delete $args{limitBytes};
2301  
2302 19 my $class = $self->_expand_class_or_croak($short_class);
2303 17 my $path = $self->_build_path($class, %args, subresource => 'log',
2304 $self->_unstructured_hint($class, $short_class));
2305  
2306 17 my %params;
2307 17 ▶ $params{container} = $container if defined $container;
Branch: 1 branch
BranchTrueFalseTotal
if defined $container 2 15 17
2308 17 ▶ $params{follow} = 'true' if $follow;
Branch: 1 branch
BranchTrueFalseTotal
if $follow 1 16 17
2309 17 ▶ $params{tailLines} = $tail_lines if defined $tail_lines;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tail_lines 2 15 17
2310 17 ▶ $params{sinceSeconds} = $since_seconds if defined $since_seconds;
Branch: 1 branch
BranchTrueFalseTotal
if defined $since_seconds 2 15 17
2311 17 ▶ $params{sinceTime} = $since_time if defined $since_time;
Branch: 1 branch
BranchTrueFalseTotal
if defined $since_time 1 16 17
2312 17 ▶ $params{timestamps} = 'true' if $timestamps;
Branch: 1 branch
BranchTrueFalseTotal
if $timestamps 2 15 17
2313 17 ▶ $params{previous} = 'true' if $previous;
Branch: 1 branch
BranchTrueFalseTotal
if $previous 2 15 17
2314 17 ▶ $params{limitBytes} = $limit_bytes if defined $limit_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit_bytes 2 15 17
2315  
2316 17 ▶ if ($on_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_line) { } 4 13 17
2317 # Streaming mode
2318 4 my $req = $self->_prepare_request('GET', $path, parameters => \%params);
2319  
2320 4 my $buffer = '';
2321 5 ▶ my $data_callback = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
2322 5 my ($chunk) = @_;
2323 5 for my $event ($self->_process_log_chunk(\$buffer, $chunk)) {
2324 5 $on_line->($event);
2325 }
2326 4 };
2327  
2328 4 my $response = $self->io->call_streaming($req, $data_callback);
2329 4 $self->_check_response($response, "log $short_class");
2330  
2331 # Process any remaining data in buffer (last line without trailing newline)
2332 4 ▶ if (length $buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer) 0 4 4
2333 0 $on_line->(Kubernetes::REST::LogEvent->new(line => $buffer));
2334 }
2335  
2336 4 return;
2337 } else {
2338 # One-shot mode
2339 13 ▶ my $response = $self->_request('GET', $path, undef,
Branch: 1 branch
BranchTrueFalseTotal
%params ? : 7 6 13
2340 %params ? (parameters => \%params) : (),
2341 );
2342 13 $self->_check_response($response, "log $short_class");
2343  
2344 12 return $response->content;
2345 }
2346 }
2347  
2348 15 ▶sub port_forward {
Subroutine port_forward: called CC=16 SCAR=28.1
Pod documented
2349 15 my ($self, $short_class, @rest) = @_;
2350  
2351  
2352 15 my %args;
2353 15 ▶ if (@rest >= 1 && !ref($rest[0]) && $rest[0] !~ /^(name|namespace|ports|subprotocol|on_open|on_frame|on_close|on_error)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|ports|subprotocol|on_open|on_frame|on_close|on_error)$/) { } 13 2 15
elsif (@rest % 2 == 0) { } 2 0 2
Condition50%3/6
@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|ports|subprotocol|on_open|on_frame|on_close|on_error)$/
!ll&&!rl&&r
0213
@rest >= 1 and not ref $rest[0]
!ll&&!rl&&r
0015
MC/DC33%1/3
@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|ports|subprotocol|on_open|on_frame|on_close|on_error)$/
@rest >= 1 not ref $rest[0] not $rest[0] =~ /^(name|namespace|ports|subprotocol|on_open|on_frame|on_close|on_error)$/
Truth table50%2/4
A and not(B) and not(C)
A= @rest >= 1B= not ref $rest[0]C= not $rest[0] =~ /^(name|namespace|ports|subprotocol|on_open|on_frame|on_close|on_error)$/
ABCresult
0XX0
10X0
1100
1111
2354 13 $args{name} = shift @rest;
2355 13 %args = (%args, @rest);
2356 } elsif (@rest % 2 == 0) {
2357 2 %args = @rest;
2358 } else {
2359 0 croak "Invalid arguments to port_forward()";
2360 }
2361 # A misspelt option is not ignored (karr k61): whatever was not read went
2362 # on to build_path, which dropped it - on_message never saw a frame.
2363 15 $self->_croak_unknown_args('port_forward', \%args, qw(name namespace ports
2364 subprotocol on_open on_frame on_close on_error));
2365  
2366 10 ▶ croak "name required for port_forward" unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 1 9 10
2367  
2368 9 my $ports = delete $args{ports};
2369 9 ▶ croak "ports required for port_forward" unless defined $ports;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ports 1 8 9
2370 8 ▶ $ports = [$ports] unless ref($ports) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ports eq "ARRAY" 0 8 8
2371 8 ▶ croak "ports required for port_forward" unless @$ports;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ports 1 7 8
2372 7 for my $p (@$ports) {
2373 9 ▶ croak "invalid port '$p' for port_forward"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p and $p =~ /^\d+$/ and $p > 0 and $p <= 65535 2 7 9
Condition77%7/9
defined $p and $p =~ /^\d+$/ and $p > 0 and $p <= 65535
!ll&&!rl&&r
117
defined $p and $p =~ /^\d+$/ and $p > 0
!ll&&!rl&&r
108
defined $p and $p =~ /^\d+$/
!ll&&!rl&&r
018
MC/DC50%2/4
defined $p and $p =~ /^\d+$/ and $p > 0 and $p <= 65535
defined $p $p =~ /^\d+$/ $p > 0 $p <= 65535
Truth table60%3/5
A and B and C and D
A= defined $pB= $p =~ /^\d+$/C= $p > 0D= $p <= 65535
ABCDresult
0XXX0
10XX0
110X0
11100
11111
2374 unless defined($p) && $p =~ /^\d+$/ && $p > 0 && $p <= 65535;
2375 }
2376  
2377 5 ▶ my $subprotocol = delete $args{subprotocol} // 'v4.channel.k8s.io';
Condition100%2/2
delete $args{"subprotocol"} // "v4.channel.k8s.io"
l!l
23
MC/DC100%1/1
delete $args{"subprotocol"} // "v4.channel.k8s.io"
delete $args{"subprotocol"}
Truth table100%2/2
A
A= delete $args{"subprotocol"}
Aresult
11
00
2378 5 my $on_open = delete $args{on_open};
2379 5 my $on_frame = delete $args{on_frame};
2380 5 my $on_close = delete $args{on_close};
2381 5 my $on_error = delete $args{on_error};
2382  
2383 5 my $class = $self->_expand_class_or_croak($short_class);
2384 3 my $path = $self->_build_path($class, %args, subresource => 'portforward',
2385 $self->_unstructured_hint($class, $short_class));
2386  
2387 3 my $req = $self->_prepare_request('GET', $path,
2388 parameters => { ports => $ports },
2389 headers => {
2390 Accept => '*/*',
2391 Connection => 'Upgrade',
2392 Upgrade => 'websocket',
2393 'Sec-WebSocket-Protocol' => $subprotocol,
2394 },
2395 );
2396  
2397 3 my $io = $self->io;
2398 3 ▶ unless ($io->can('call_duplex')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($io->can("call_duplex")) 1 2 3
2399 1 croak "IO backend does not support port_forward(): missing call_duplex()";
2400 }
2401  
2402 2 return $io->call_duplex($req,
2403 on_open => $on_open,
2404 on_frame => $on_frame,
2405 on_close => $on_close,
2406 on_error => $on_error,
2407 );
2408 }
2409  
2410 16 ▶sub exec {
Subroutine exec: called CC=26 SCAR=32.7
Pod documented
2411 16 my ($self, $short_class, @rest) = @_;
2412  
2413  
2414 16 my %args;
2415 16 ▶ if (@rest >= 1 && !ref($rest[0]) && $rest[0] !~ /^(name|namespace|command|container|stdin|stdout|stderr|tty|subprotocol|on_open|on_frame|on_close|on_error)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|command|container|stdin|stdout|stderr|tty|subprotocol|on_open|on_frame|on_close|on_error)$/) { } 14 2 16
elsif (@rest % 2 == 0) { } 2 0 2
Condition50%3/6
@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|command|container|stdin|stdout|stderr|tty|subprotocol|on_open|on_frame|on_close|on_error)$/
!ll&&!rl&&r
0214
@rest >= 1 and not ref $rest[0]
!ll&&!rl&&r
0016
MC/DC33%1/3
@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|command|container|stdin|stdout|stderr|tty|subprotocol|on_open|on_frame|on_close|on_error)$/
@rest >= 1 not ref $rest[0] not $rest[0] =~ /^(name|namespace|command|container|stdin|stdout|stderr|tty|subprotocol|on_open|on_frame|on_close|on_error)$/
Truth table50%2/4
A and not(B) and not(C)
A= @rest >= 1B= not ref $rest[0]C= not $rest[0] =~ /^(name|namespace|command|container|stdin|stdout|stderr|tty|subprotocol|on_open|on_frame|on_close|on_error)$/
ABCresult
0XX0
10X0
1100
1111
2416 14 $args{name} = shift @rest;
2417 14 %args = (%args, @rest);
2418 } elsif (@rest % 2 == 0) {
2419 2 %args = @rest;
2420 } else {
2421 0 croak "Invalid arguments to exec()";
2422 }
2423 # A misspelt option is not ignored (karr k61): containr => 'app' ran the
2424 # command in the default container.
2425 16 $self->_croak_unknown_args('exec', \%args, qw(name namespace command
2426 container stdin stdout stderr tty subprotocol on_open on_frame on_close
2427 on_error));
2428  
2429 11 ▶ croak "name required for exec" unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 1 10 11
2430  
2431 10 my $command = delete $args{command};
2432 10 ▶ croak "command required for exec" unless defined $command;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command 1 9 10
2433 9 ▶ $command = [$command] unless ref($command) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $command eq "ARRAY" 1 8 9
2434 9 ▶ croak "command required for exec" unless @$command;
Branch: 1 branch
BranchTrueFalseTotal
unless @$command 1 8 9
2435 8 for my $part (@$command) {
2436 10 ▶ croak "invalid command element for exec"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part and not ref $part and length $part 2 8 10
Condition83%5/6
defined $part and not ref $part and length $part
!ll&&!rl&&r
208
defined $part and not ref $part
!ll&&!rl&&r
118
MC/DC66%2/3
defined $part and not ref $part and length $part
defined $part not ref $part length $part
Truth table75%3/4
A and not(B) and C
A= defined $partB= not ref $partC= length $part
ABCresult
0XX0
10X0
1100
1111
2437 unless defined($part) && !ref($part) && length $part;
2438 }
2439  
2440 6 my $container = delete $args{container};
2441 6 ▶ my $stdin = delete($args{stdin}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $args{'stdin'} ? : 2 4 6
2442 6 ▶ my $stdout = exists($args{stdout}) ? (delete($args{stdout}) ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'stdout'} ? : 2 4 6
delete $args{'stdout'} ? : 1 1 2
2443 6 ▶ my $stderr = exists($args{stderr}) ? (delete($args{stderr}) ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'stderr'} ? : 2 4 6
delete $args{'stderr'} ? : 1 1 2
2444 6 ▶ my $tty = delete($args{tty}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $args{'tty'} ? : 2 4 6
2445  
2446 6 ▶ my $subprotocol = delete $args{subprotocol} // 'v4.channel.k8s.io';
Condition100%2/2
delete $args{"subprotocol"} // "v4.channel.k8s.io"
l!l
24
MC/DC100%1/1
delete $args{"subprotocol"} // "v4.channel.k8s.io"
delete $args{"subprotocol"}
Truth table100%2/2
A
A= delete $args{"subprotocol"}
Aresult
11
00
2447 6 my $on_open = delete $args{on_open};
2448 6 my $on_frame = delete $args{on_frame};
2449 6 my $on_close = delete $args{on_close};
2450 6 my $on_error = delete $args{on_error};
2451  
2452 6 my $class = $self->_expand_class_or_croak($short_class);
2453 4 my $path = $self->_build_path($class, %args, subresource => 'exec',
2454 $self->_unstructured_hint($class, $short_class));
2455  
2456 4 ▶ my %params = (
Branch: 4 branches
BranchTrueFalseTotal
$stdin ? : 2 2 4
$stdout ? : 3 1 4
$stderr ? : 3 1 4
$tty ? : 2 2 4
2457 command => $command,
2458 stdin => $stdin ? 'true' : 'false',
2459 stdout => $stdout ? 'true' : 'false',
2460 stderr => $stderr ? 'true' : 'false',
2461 tty => $tty ? 'true' : 'false',
2462 );
2463 4 ▶ $params{container} = $container if defined $container;
Branch: 1 branch
BranchTrueFalseTotal
if defined $container 2 2 4
2464  
2465 4 my $req = $self->_prepare_request('GET', $path,
2466 parameters => \%params,
2467 headers => {
2468 Accept => '*/*',
2469 Connection => 'Upgrade',
2470 Upgrade => 'websocket',
2471 'Sec-WebSocket-Protocol' => $subprotocol,
2472 },
2473 );
2474  
2475 4 my $io = $self->io;
2476 4 ▶ unless ($io->can('call_duplex')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($io->can("call_duplex")) 1 3 4
2477 1 croak "IO backend does not support exec(): missing call_duplex()";
2478 }
2479  
2480 3 return $io->call_duplex($req,
2481 on_open => $on_open,
2482 on_frame => $on_frame,
2483 on_close => $on_close,
2484 on_error => $on_error,
2485 );
2486 }
2487  
2488 12 ▶sub attach {
Subroutine attach: called CC=19 SCAR=29.5
Pod documented
2489 12 my ($self, $short_class, @rest) = @_;
2490  
2491  
2492 12 my %args;
2493 12 ▶ if (@rest >= 1 && !ref($rest[0]) && $rest[0] !~ /^(name|namespace|container|stdin|stdout|stderr|tty|subprotocol|on_open|on_frame|on_close|on_error)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|container|stdin|stdout|stderr|tty|subprotocol|on_open|on_frame|on_close|on_error)$/) { } 9 3 12
elsif (@rest % 2 == 0) { } 3 0 3
Condition50%3/6
@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|container|stdin|stdout|stderr|tty|subprotocol|on_open|on_frame|on_close|on_error)$/
!ll&&!rl&&r
039
@rest >= 1 and not ref $rest[0]
!ll&&!rl&&r
0012
MC/DC33%1/3
@rest >= 1 and not ref $rest[0] and not $rest[0] =~ /^(name|namespace|container|stdin|stdout|stderr|tty|subprotocol|on_open|on_frame|on_close|on_error)$/
@rest >= 1 not ref $rest[0] not $rest[0] =~ /^(name|namespace|container|stdin|stdout|stderr|tty|subprotocol|on_open|on_frame|on_close|on_error)$/
Truth table50%2/4
A and not(B) and not(C)
A= @rest >= 1B= not ref $rest[0]C= not $rest[0] =~ /^(name|namespace|container|stdin|stdout|stderr|tty|subprotocol|on_open|on_frame|on_close|on_error)$/
ABCresult
0XX0
10X0
1100
1111
2494 9 $args{name} = shift @rest;
2495 9 %args = (%args, @rest);
2496 } elsif (@rest % 2 == 0) {
2497 3 %args = @rest;
2498 } else {
2499 0 croak "Invalid arguments to attach()";
2500 }
2501 # A misspelt option is not ignored (karr k61): stdn => 1 attached without
2502 # stdin.
2503 12 $self->_croak_unknown_args('attach', \%args, qw(name namespace container
2504 stdin stdout stderr tty subprotocol on_open on_frame on_close on_error));
2505  
2506 7 ▶ croak "name required for attach" unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 1 6 7
2507  
2508 6 my $container = delete $args{container};
2509 6 ▶ my $stdin = delete($args{stdin}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $args{'stdin'} ? : 2 4 6
2510 6 ▶ my $stdout = exists($args{stdout}) ? (delete($args{stdout}) ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'stdout'} ? : 2 4 6
delete $args{'stdout'} ? : 1 1 2
2511 6 ▶ my $stderr = exists($args{stderr}) ? (delete($args{stderr}) ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'stderr'} ? : 2 4 6
delete $args{'stderr'} ? : 1 1 2
2512 6 ▶ my $tty = delete($args{tty}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $args{'tty'} ? : 2 4 6
2513  
2514 6 ▶ my $subprotocol = delete $args{subprotocol} // 'v4.channel.k8s.io';
Condition100%2/2
delete $args{"subprotocol"} // "v4.channel.k8s.io"
l!l
24
MC/DC100%1/1
delete $args{"subprotocol"} // "v4.channel.k8s.io"
delete $args{"subprotocol"}
Truth table100%2/2
A
A= delete $args{"subprotocol"}
Aresult
11
00
2515 6 my $on_open = delete $args{on_open};
2516 6 my $on_frame = delete $args{on_frame};
2517 6 my $on_close = delete $args{on_close};
2518 6 my $on_error = delete $args{on_error};
2519  
2520 6 my $class = $self->_expand_class_or_croak($short_class);
2521 4 my $path = $self->_build_path($class, %args, subresource => 'attach',
2522 $self->_unstructured_hint($class, $short_class));
2523  
2524 4 ▶ my %params = (
Branch: 4 branches
BranchTrueFalseTotal
$stdin ? : 2 2 4
$stdout ? : 3 1 4
$stderr ? : 3 1 4
$tty ? : 2 2 4
2525 stdin => $stdin ? 'true' : 'false',
2526 stdout => $stdout ? 'true' : 'false',
2527 stderr => $stderr ? 'true' : 'false',
2528 tty => $tty ? 'true' : 'false',
2529 );
2530 4 ▶ $params{container} = $container if defined $container;
Branch: 1 branch
BranchTrueFalseTotal
if defined $container 2 2 4
2531  
2532 4 my $req = $self->_prepare_request('GET', $path,
2533 parameters => \%params,
2534 headers => {
2535 Accept => '*/*',
2536 Connection => 'Upgrade',
2537 Upgrade => 'websocket',
2538 'Sec-WebSocket-Protocol' => $subprotocol,
2539 },
2540 );
2541  
2542 4 my $io = $self->io;
2543 4 ▶ unless ($io->can('call_duplex')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($io->can("call_duplex")) 1 3 4
2544 1 croak "IO backend does not support attach(): missing call_duplex()";
2545 }
2546  
2547 3 return $io->call_duplex($req,
2548 on_open => $on_open,
2549 on_frame => $on_frame,
2550 on_close => $on_close,
2551 on_error => $on_error,
2552 );
2553 }
2554  
2555 97 1;
2556  
2557 __END__
« lib/Kubernetes/REST/V0Group.pm ↑ Summary lib/Kubernetes/REST/CLI/Minikube.pm »