Kubernetes.pm

filter: Statementstmt 96.1% 879 / 914 Branchbran 86.1% 462 / 536 Conditioncond 64.3% 139 / 216 MC/DCmcdc 42.6% 52 / 122 Subroutinesub 95.0% 152 / 160 Podpod 100% 31 / 31 totaltotal 86.6% 1715 / 1979 CC 360 SCAR 22.5
CC
360
Cov
89%
exec
24.0
cp_to_pod
22.4
__ANON__
20.0
CRAP 9.4
« Kubernetes/Watcher.pm ↑ Summary Kubernetes/PortForwardSession.pm »
1 package Net::Async::Kubernetes;
2 # ABSTRACT: Async Kubernetes client for IO::Async
3 48 our $VERSION = '0.009';
4 48 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 48 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 48 ▶use parent 'IO::Async::Notifier';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 48 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 48 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 48 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 48 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 48 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 48 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 48 ▶use Protocol::WebSocket::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 48 ▶use Kubernetes::REST;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 48 ▶use Kubernetes::REST::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 48 ▶use Kubernetes::REST::AuthToken;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 48 ▶use Kubernetes::REST::HTTPRequest;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 48 ▶use Kubernetes::REST::HTTPResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 48 ▶use Kubernetes::REST::WatchEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 48 ▶use Kubernetes::REST::LogEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 48 ▶use Net::Async::Kubernetes::PortForwardSession;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 48 ▶use Net::Async::Kubernetes::Watcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 48 ▶use Net::Async::Kubernetes::Controller;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 324 ▶sub configure {
Subroutine configure: called CC=19 SCAR=29.5
Pod documented
27 324 my ($self, %params) = @_;
28  
29 324 ▶ if (exists $params{kubeconfig}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"kubeconfig"}) 4 320 324
30 4 $self->{kubeconfig} = delete $params{kubeconfig};
31 }
32 324 ▶ if (exists $params{context}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"context"}) 4 320 324
33 4 $self->{context} = delete $params{context};
34 }
35 324 ▶ if (exists $params{server}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"server"}) 315 9 324
36 315 my $val = delete $params{server};
37 315 ▶ $self->{server} = (blessed($val) && $val->isa('Kubernetes::REST::Server'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($val) && $val->isa('Kubernetes::REST::Server') ? : 6 309 315
Condition66%2/3
builtin::blessed($val) and $val->isa("Kubernetes::REST::Server")
!ll&&!rl&&r
30906
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
38 ? $val
39 : Kubernetes::REST::Server->new($val);
40 }
41 324 ▶ if (exists $params{credentials}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"credentials"}) 315 9 324
42 315 my $val = delete $params{credentials};
43 315 ▶ if (blessed($val) && $val->can('token')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($val) and $val->can('token')) { } 6 309 315
elsif (ref $val eq 'HASH') { } 308 1 309
Condition66%2/3
builtin::blessed($val) and $val->can("token")
!ll&&!rl&&r
30906
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
44 6 $self->{credentials} = $val;
45 } elsif (ref($val) eq 'HASH') {
46 308 $self->{credentials} = Kubernetes::REST::AuthToken->new($val);
47 } else {
48 1 $self->{credentials} = $val;
49 }
50 }
51 324 ▶ if (exists $params{resource_map}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"resource_map"}) 18 306 324
52 18 $self->{resource_map} = delete $params{resource_map};
53 }
54 324 ▶ if (exists $params{resource_map_from_cluster}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"resource_map_from_cluster"}) 305 19 324
55 305 $self->{resource_map_from_cluster} = delete $params{resource_map_from_cluster};
56 }
57 324 ▶ if (exists $params{with}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"with"}) 1 323 324
58 1 $self->{with} = delete $params{with};
59 }
60  
61 # Resolve server/credentials via Kubeconfig (handles kubeconfig files
62 # and in-cluster service account auto-detection)
63 324 ▶ if (!$self->{server}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"server"}) 9 315 324
64 9 require Kubernetes::REST::Kubeconfig;
65 my $kc = Kubernetes::REST::Kubeconfig->new(
66 ($self->{kubeconfig} ? (kubeconfig_path => $self->{kubeconfig}) : ()),
67 9 ▶ ($self->{context} ? (context_name => $self->{context}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$self->{'kubeconfig'} ? : 4 5 9
$self->{'context'} ? : 3 6 9
68 );
69 9 ▶ if ($self->{kubeconfig} || $self->{context}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'kubeconfig'} or $self->{'context'}) { } 7 2 9
elsif (my $api = eval { do { $kc->api } }) { } 0 2 2
Condition100%3/3
$self->{"kubeconfig"} or $self->{"context"}
l!l&&r!l&&!r
432
MC/DC100%2/2
$self->{"kubeconfig"} or $self->{"context"}
$self->{"kubeconfig"} $self->{"context"}
Truth table100%3/3
A or B
A= $self->{"kubeconfig"}B= $self->{"context"}
ABresult
1X1
011
000
70 # Explicit kubeconfig or context — must resolve or croak with the
71 # reason; swallowed, it would only resurface as "server or
72 # kubeconfig required" on first use.
73 7 my $api = $kc->api;
74 4 $self->{server} = $api->server;
75 4 $self->{credentials} = $api->credentials;
76 2 } elsif (my $api = eval { $kc->api }) {
77 # Auto-detect: kubeconfig default path or in-cluster
78 0 $self->{server} = $api->server;
79 0 $self->{credentials} = $api->credentials;
80 }
81 }
82  
83 321 $self->SUPER::configure(%params);
84 }
85  
86  
87 # Accessors
88 1 ▶sub kubeconfig { $_[0]->{kubeconfig} }
Subroutine kubeconfig: called CC=1 SCAR=0.0
Pod documented
89  
90  
91 2 ▶sub context { $_[0]->{context} }
Subroutine context: called CC=1 SCAR=0.0
Pod documented
92  
93  
94 300 ▶sub resource_map { $_[0]->{resource_map} }
Subroutine resource_map: called CC=1 SCAR=0.0
Pod documented
95  
96  
97 304 ▶sub resource_map_from_cluster { $_[0]->{resource_map_from_cluster} // 0 }
Subroutine resource_map_from_cluster: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]{"resource_map_from_cluster"} // 0
l!l
3004
MC/DC100%1/1
$_[0]{"resource_map_from_cluster"} // 0
$_[0]{"resource_map_from_cluster"}
Truth table100%2/2
A
A= $_[0]{"resource_map_from_cluster"}
Aresult
11
00
98  
99  
100 283 ▶sub with { $_[0]->{with} // [] }
Subroutine with: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]{"with"} // []
l!l
2281
MC/DC100%1/1
$_[0]{"with"} // []
$_[0]{"with"}
Truth table100%2/2
A
A= $_[0]{"with"}
Aresult
11
00
101  
102  
103 322 ▶sub server {
Subroutine server: called CC=2 SCAR=7.1
Pod documented
104 322 my ($self) = @_;
105 322 ▶ $self->{server} // croak "server or kubeconfig required";
Condition66%2/3
$self->{"server"} // croak("server or kubeconfig required")
l!l&&r!l&&!r
32020
MC/DC0%0/2
$self->{"server"} // croak("server or kubeconfig required")
$self->{"server"} croak("server or kubeconfig required")
Truth table33%1/3
A // B
A= $self->{"server"}B= croak("server or kubeconfig required")
ABresult
1X1
011
000
106 }
107  
108  
109 303 ▶sub credentials {
Subroutine credentials: called CC=2 SCAR=7.1
Pod documented
110 303 my ($self) = @_;
111 303 ▶ $self->{credentials} // croak "credentials or kubeconfig required";
Condition66%2/3
$self->{"credentials"} // croak("credentials or kubeconfig required")
l!l&&r!l&&!r
30210
MC/DC0%0/2
$self->{"credentials"} // croak("credentials or kubeconfig required")
$self->{"credentials"} croak("credentials or kubeconfig required")
Truth table33%1/3
A // B
A= $self->{"credentials"}B= croak("credentials or kubeconfig required")
ABresult
1X1
011
000
112 }
113  
114  
115 3606 ▶sub rest {
Subroutine rest: called CC=3 SCAR=11.1
Pod documented
116 3606 my ($self) = @_;
117 3606 ▶ $self->{_rest} //= Kubernetes::REST->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->resource_map ? : 17 264 281
Condition66%2/3
$self->{"_rest"} //= "Kubernetes::REST"->new("server", $self->server, "credentials", $self->credentials, "resource_map_from_cluster", $self->resource_map_from_cluster, $self->resource_map ? ("resource_map", $self->resource_map) : (), "with", $self->with)
l!l&&r!l&&!r
33252810
MC/DC0%0/2
$self->{"_rest"} //= "Kubernetes::REST"->new("server", $self->server, "credentials", $self->credentials, "resource_map_from_cluster", $self->resource_map_from_cluster, $self->resource_map ? ("resource_map", $self->resource_map) : (), "with", $self->with)
$self->{"_rest"} "Kubernetes::REST"->new("server", $self->server, "credentials", $self->credentials, "resource_map_from_cluster", $self->resource_map_from_cluster, $self->resource_map ? ("resource_map", $self->resource_map) : (), "with", $self->with)
Truth table66%2/3
A //= B
A= $self->{"_rest"}B= "Kubernetes::REST"->new("server", $self->server, "credentials", $self->credentials, "resource_map_from_cluster", $self->resource_map_from_cluster, $self->resource_map ? ("resource_map", $self->resource_map) : (), "with", $self->with)
ABresult
1X1
011
000
118 server => $self->server,
119 credentials => $self->credentials,
120 resource_map_from_cluster => $self->resource_map_from_cluster,
121 ($self->resource_map ? (resource_map => $self->resource_map) : ()),
122 with => $self->with,
123 );
124 }
125  
126  
127 # Lazy internal Kubernetes::REST for request building + response processing
128 3818 ▶sub _rest { $_[0]->rest }
Subroutine _rest: called CC=1 SCAR=0.0
129  
130 66 ▶sub new_object {
Subroutine new_object: called CC=1 SCAR=0.0
Pod documented
131 66 my ($self, @args) = @_;
132 66 return $self->rest->new_object(@args);
133 }
134  
135  
136 # Lazy Net::Async::HTTP instance
137 30 ▶sub _http {
Subroutine _http: called CC=2 SCAR=6.9
138 30 my ($self) = @_;
139 30 ▶ unless ($self->{_http}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_http"}) 16 14 30
140 16 require Net::Async::HTTP;
141 16 $self->{_http} = Net::Async::HTTP->new(
142 user_agent => 'Net::Async::Kubernetes Perl Client',
143 max_connections_per_host => 0,
144 );
145 }
146 30 return $self->{_http};
147 }
148  
149 # SSL options derived from server config, passed to every HTTP request
150 25 ▶sub _ssl_options {
Subroutine _ssl_options: called CC=12 SCAR=24.9
151 25 my ($self) = @_;
152 25 ▶ return @{$self->{_ssl_options}} if $self->{_ssl_options};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_ssl_options"} 8 17 25
153  
154 17 my $server = $self->server;
155 17 my @opts;
156  
157 17 ▶ if ($server->ssl_verify_server) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server->ssl_verify_server) { } 16 1 17
158 16 push @opts, SSL_verify_mode => SSL_VERIFY_PEER;
159 } else {
160 1 push @opts, SSL_verify_mode => SSL_VERIFY_NONE;
161 }
162  
163 17 ▶ push @opts, SSL_ca_file => $server->ssl_ca_file if $server->ssl_ca_file;
Branch: 1 branch
BranchTrueFalseTotal
if $server->ssl_ca_file 3 14 17
164 17 ▶ push @opts, SSL_cert_file => $server->ssl_cert_file if $server->ssl_cert_file;
Branch: 1 branch
BranchTrueFalseTotal
if $server->ssl_cert_file 1 16 17
165 17 ▶ push @opts, SSL_key_file => $server->ssl_key_file if $server->ssl_key_file;
Branch: 1 branch
BranchTrueFalseTotal
if $server->ssl_key_file 1 16 17
166 17 my $ca_pem = $server->ssl_ca_pem;
167 17 ▶ if (defined $ca_pem && length $ca_pem) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ca_pem and length $ca_pem) 3 14 17
Condition66%2/3
defined $ca_pem and length $ca_pem
!ll&&!rl&&r
1403
MC/DC50%1/2
defined $ca_pem and length $ca_pem
defined $ca_pem length $ca_pem
Truth table66%2/3
A and B
A= defined $ca_pemB= length $ca_pem
ABresult
0X0
100
111
168 3 push @opts, SSL_ca_file => $self->_materialize_ssl_pem(ca => $ca_pem);
169 }
170 17 my $cert_pem = $server->ssl_cert_pem;
171 17 ▶ if (defined $cert_pem && length $cert_pem) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cert_pem and length $cert_pem) 3 14 17
Condition66%2/3
defined $cert_pem and length $cert_pem
!ll&&!rl&&r
1403
MC/DC50%1/2
defined $cert_pem and length $cert_pem
defined $cert_pem length $cert_pem
Truth table66%2/3
A and B
A= defined $cert_pemB= length $cert_pem
ABresult
0X0
100
111
172 3 push @opts, SSL_cert_file => $self->_materialize_ssl_pem(cert => $cert_pem);
173 }
174 17 my $key_pem = $server->ssl_key_pem;
175 17 ▶ if (defined $key_pem && length $key_pem) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key_pem and length $key_pem) 3 14 17
Condition66%2/3
defined $key_pem and length $key_pem
!ll&&!rl&&r
1403
MC/DC50%1/2
defined $key_pem and length $key_pem
defined $key_pem length $key_pem
Truth table66%2/3
A and B
A= defined $key_pemB= length $key_pem
ABresult
0X0
100
111
176 3 push @opts, SSL_key_file => $self->_materialize_ssl_pem(key => $key_pem);
177 }
178  
179 17 $self->{_ssl_options} = \@opts;
180 17 return @opts;
181 }
182  
183 9 ▶sub _materialize_ssl_pem {
Subroutine _materialize_ssl_pem: called CC=2 SCAR=6.9
184 9 my ($self, $kind, $pem) = @_;
185  
186 9 my $fh = File::Temp->new(
187 SUFFIX => ".$kind.pem",
188 UNLINK => 1,
189 );
190 9 print {$fh} $pem;
191 9 close $fh;
192  
193 9 ▶ push @{ $self->{_ssl_tempfiles} ||= [] }, $fh;
Condition100%2/2
$self->{"_ssl_tempfiles"} ||= []
l!l
63
MC/DC100%1/1
$self->{"_ssl_tempfiles"} ||= []
$self->{"_ssl_tempfiles"}
Truth table100%2/2
A
A= $self->{"_ssl_tempfiles"}
Aresult
11
00
194 9 return $fh->filename;
195 }
196  
197 # IO::K8s::expand_class fails closed: an unknown, malformed or mismatched
198 # apiVersion yields undef instead of a bare-name guess. Passing that undef on to
199 # build_path dies with "argument is not a module name", naming neither the
200 # resource nor the reason, so _resolve_class reports it with this.
201 46 ▶sub _unknown_resource_error {
Subroutine _unknown_resource_error: called CC=2 SCAR=7.2
202 46 my ($self, $short_class) = @_;
203 46 ▶ return sprintf(
Branch: 1 branch
BranchTrueFalseTotal
defined $short_class ? : 46 0 46
204 "unknown resource '%s': no IO::K8s class for this apiVersion/kind"
205 . " (add it to resource_map if it is a CRD)",
206 defined $short_class ? $short_class : '(undef)',
207 );
208 }
209  
210 # The message for a reference where a resource name - a plain string - is
211 # required, and nothing for a string. A reference reaches a name position two
212 # ways: as the class-name argument (a manifest hashref handed to patch(),
213 # caught by _resolve_class below), or as the object-name argument of get() and
214 # delete() (a manifest hashref or an IO::K8s object, karr k70). Left unchecked
215 # either one stringifies into the request path (.../pods/HASH(0x...)) and only
216 # the server's 404 shows the error. One wording for both positions, so a
217 # reference is refused the same way wherever it lands.
218 690 ▶sub _resource_name_error {
Subroutine _resource_name_error: called CC=3 SCAR=11.0
219 690 my ($self, $name) = @_;
220 690 ▶ return unless ref $name;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $name 673 17 690
221 17 ▶ return 'resource name must be a string, got '
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($name) ? : 4 13 17
222 . (blessed($name) ? 'an object of class ' . ref($name)
223 : 'a ' . ref($name) . ' reference');
224 }
225  
226 # Resolve a resource name to its IO::K8s class. Returns the class, or
227 # (undef, $message) when no usable class comes out of the name, which every
228 # caller reports per its contract - a failed Future or a croak. Kubernetes::REST's
229 # expand_class fails closed for a qualified name (undef) but open for a bare
230 # Kind: it fabricates 'IO::K8s::<Kind>' whether or not that class exists. Both
231 # are an unknown resource; whatever else can go wrong with the class it did
232 # resolve to is _usable_class's to report. A reference is no name at all -
233 # say a manifest hashref handed to patch() - and is refused as such
234 # (_resource_name_error): expand_class would stringify it into a class name
235 # ('IO::K8s::HASH(0x...)') and the load error would name that instead.
236 567 ▶sub _resolve_class {
Subroutine _resolve_class: called CC=3 SCAR=11.0
237 567 my ($self, $name, @args) = @_;
238 567 ▶ if (my $error = $self->_resource_name_error($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->_resource_name_error($name)) 9 558 567
239 9 return (undef, $error);
240 }
241 558 ▶ my $class = $self->_rest->expand_class($name, @args)
Condition100%2/2
$self->_rest->expand_class($name, @args) // (return undef, $self->_unknown_resource_error($name))
l!l
53226
MC/DC100%1/1
$self->_rest->expand_class($name, @args) // (return undef, $self->_unknown_resource_error($name))
$self->_rest->expand_class($name, @args)
Truth table100%2/2
A
A= $self->_rest->expand_class($name, @args)
Aresult
11
00
242 // return (undef, $self->_unknown_resource_error($name));
243 532 return $self->_usable_class($name, $class);
244 }
245  
246 # What build_path would otherwise die on, synchronously and without naming the
247 # resource: the class must load, and it must answer api_version as a class
248 # method - build_path's own precondition for a path. That precondition, not a
249 # role, is what tells a resource class from IO::K8s's helpers (List, Resource,
250 # Types, Unstructured) that a bare name can land on: List has an api_version,
251 # but as an instance accessor that dies when called on the class. Returns the
252 # class, or (undef, $message) with the real cause - the load error, or "not a
253 # resource class". A fabricated bare-Kind name that does not load is an
254 # unknown resource, not a load error. IO::K8s::Unstructured is the one
255 # resource without a class-level api_version - its Kind is instance data -
256 # so, reached any other way than its own bare name 'Unstructured' (above all
257 # through Kubernetes::REST's discovery fallback for a Kind), it is passed on
258 # to build_path, which gets the Kind from _unstructured_hint.
259 776 ▶sub _usable_class {
Subroutine _usable_class: called CC=14 SCAR=26.8
260 776 my ($self, $name, $class) = @_;
261 776 my $rest = $self->_rest;
262 776 ▶ my $fabricated = defined $name && !ref $name && $class eq 'IO::K8s::' . $name;
Condition50%3/6
defined $name && !ref($name) && $class eq 'IO::K8s::' . $name
!ll&&!rl&&r
069086
defined $name and not ref $name
!ll&&!rl&&r
00776
MC/DC33%1/3
defined $name && !ref($name) && $class eq 'IO::K8s::' . $name
defined $name not ref $name $class eq 'IO::K8s::' . $name
Truth table50%2/4
A and not(B) && C
A= defined $nameB= not ref $nameC= $class eq 'IO::K8s::' . $name
ABCresult
0XX0
10X0
1100
1111
263  
264 776 ▶ unless ($class->can('new') || eval { $rest->k8s->load_class($class); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("new") or eval { do { $rest->k8s->load_class($class); 1 } }) 71 705 776
Condition100%3/3
$class->can("new") or eval {
l!l&&r!l&&!r
6703571
MC/DC100%2/2
$class->can("new") or eval { do { $rest->k8s->load_class($class); 1 } }
$class->can("new") eval { do { $rest->k8s->load_class($class); 1 } }
Truth table100%3/3
A or B
A= $class->can("new")B= eval { do { $rest->k8s->load_class($class); 1 } }
ABresult
1X1
011
000
265 71 my $load_error = $@;
266 71 ▶ return (undef, $self->_unknown_resource_error($name)) if $fabricated;
Branch: 1 branch
BranchTrueFalseTotal
if $fabricated 20 51 71
267 51 chomp $load_error;
268 51 return (undef, sprintf(
269 "resource '%s' resolves to class %s, which cannot be loaded: %s",
270 $name, $class, $load_error,
271 ));
272 }
273  
274 705 ▶ if ($class eq 'IO::K8s::Unstructured' && !$fabricated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "IO::K8s::Unstructured" and not $fabricated) 90 615 705
Condition100%3/3
$class eq "IO::K8s::Unstructured" and not $fabricated
!ll&&!rl&&r
5981790
MC/DC100%2/2
$class eq "IO::K8s::Unstructured" and not $fabricated
$class eq "IO::K8s::Unstructured" not $fabricated
Truth table100%3/3
A and not(B)
A= $class eq "IO::K8s::Unstructured"B= not $fabricated
ABresult
0X0
100
111
275 # A qualified name counts only in its own group/version (see
276 # _request_path). Asking for its path confirms it exactly, from the
277 # cached catalog, without a request.
278 90 ▶ return $class unless defined $name && !ref $name && $name =~ m{/};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and $name =~ m[/] 80 10 90
Condition50%3/6
defined $name and not ref $name and $name =~ m[/]
!ll&&!rl&&r
08010
defined $name and not ref $name
!ll&&!rl&&r
0090
MC/DC33%1/3
defined $name and not ref $name and $name =~ m[/]
defined $name not ref $name $name =~ m[/]
Truth table50%2/4
A and not(B) and C
A= defined $nameB= not ref $nameC= $name =~ m[/]
ABCresult
0XX0
10X0
1100
1111
279 10 my ($path) = $self->_request_path($class, $name);
280 10 ▶ return defined $path ? $class : (undef, $self->_unknown_resource_error($name));
Branch: 1 branch
BranchTrueFalseTotal
defined $path ? : 10 0 10
281 }
282 615 ▶ return $class if $class->can('api_version') && defined eval { $class->api_version };
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("api_version") and defined eval { do { $class->api_version } } 531 84 615
Condition100%3/3
$class->can("api_version") and defined eval {
!ll&&!rl&&r
5826531
MC/DC100%2/2
$class->can("api_version") and defined eval { do { $class->api_version } }
$class->can("api_version") defined eval { do { $class->api_version } }
Truth table100%3/3
A and B
A= $class->can("api_version")B= defined eval { do { $class->api_version } }
ABresult
0X0
100
111
283 84 return (undef, sprintf(
284 "resource '%s' resolves to %s, which is not a Kubernetes resource class"
285 . " (it has no api_version to build a request path from)",
286 $name, $class,
287 ));
288 }
289  
290 # The class of an object handed to one of the object forms (create, update,
291 # update_status, patch, patch_status, delete, ensure), which build their path
292 # from it. It is checked like a resolved name (_usable_class): an IO::K8s::List,
293 # a nested type such as a PodSpec, or no IO::K8s object at all has no request
294 # path, and build_path - or the metadata lookup before it - would die on it
295 # synchronously. Returns the class, or (undef, $message) naming $label, which
296 # each caller reports per its contract - a failed Future or a croak.
297 235 ▶sub _object_class {
Subroutine _object_class: called CC=3 SCAR=11.0
298 235 my ($self, $label, $object) = @_;
299 235 ▶ return (undef, "$label requires an IO::K8s object") unless blessed($object);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($object) 5 230 235
300 230 my ($class, $error) = $self->_usable_class(ref($object), ref($object));
301 230 ▶ return defined $class ? $class : (undef, "$label: $error");
Branch: 1 branch
BranchTrueFalseTotal
defined $class ? : 212 18 230
302 }
303  
304 # The extra build_path arguments for a class that resolved to
305 # IO::K8s::Unstructured - empty for every other class, whose path comes from
306 # the class alone. Unstructured has no class-level api_version: its Kind and
307 # apiVersion are instance data, so build_path takes them from the caller and
308 # looks plural and scope up in Kubernetes::REST's discovery catalog. $ident is
309 # what the class came from. An object gives its own kind and apiVersion. A
310 # name is split the way Kubernetes::REST's expand_class splits it: a qualified
311 # 'example.org/v1/Widget' keeps its group and version, instead of landing in
312 # whichever group serving a Widget discovery lists first, and an explicit
313 # class name ('+...', 'IO::K8s::...', any '...::...') carries no Kind. Mirrors
314 # the private helper of the same name in Kubernetes::REST, which is not part
315 # of its public seam.
316 632 ▶sub _unstructured_hint {
Subroutine _unstructured_hint: called CC=11 SCAR=24.4
317 632 my ($self, $class, $ident) = @_;
318 632 ▶ return () unless defined $class && $class eq 'IO::K8s::Unstructured';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class and $class eq "IO::K8s::Unstructured" 531 101 632
Condition66%2/3
defined $class and $class eq "IO::K8s::Unstructured"
!ll&&!rl&&r
0531101
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
319 101 my ($kind, $api_version);
320 101 ▶ if (blessed($ident)) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($ident)) { } 50 51 101
elsif (defined $ident and not ref $ident and not $ident =~ /\A(?:\+|IO::K8s::)/) { } 29 22 51
321 50 ($kind, $api_version) = ($ident->kind, $ident->apiVersion);
322 ▶ } elsif (defined $ident && !ref $ident && $ident !~ m{\A(?:\+|IO::K8s::)}) {
Condition50%3/6
defined $ident and not ref $ident and not $ident =~ /\A(?:\+|IO::K8s::)/
!ll&&!rl&&r
02229
defined $ident and not ref $ident
!ll&&!rl&&r
0051
MC/DC33%1/3
defined $ident and not ref $ident and not $ident =~ /\A(?:\+|IO::K8s::)/
defined $ident not ref $ident not $ident =~ /\A(?:\+|IO::K8s::)/
Truth table50%2/4
A and not(B) and not(C)
A= defined $identB= not ref $identC= not $ident =~ /\A(?:\+|IO::K8s::)/
ABCresult
0XX0
10X0
1100
1111
323 29 ▶ if ($ident =~ m{/}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ident =~ m[/]) { } 14 15 29
elsif (not $ident =~ /::/) { } 15 0 15
324 14 ($api_version, $kind) = $ident =~ m{\A(.*)/([^/]+)\z};
325 } elsif ($ident !~ /::/) {
326 15 $kind = $ident;
327 }
328 }
329 return (
330 101 ▶ (defined $kind ? (kind => $kind) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $kind ? : 71 30 101
defined $api_version ? : 64 37 101
331 (defined $api_version ? (api_version => $api_version) : ()),
332 );
333 }
334  
335 # The request path for $class: Kubernetes::REST's build_path with %args and
336 # the Unstructured hint for $ident, the name or object the class came from.
337 # Returns the path, or (undef, $message) when build_path gives up - on
338 # IO::K8s::Unstructured without discovery, or without a Kind (the explicit
339 # class name, an object without kind) - which every caller reports per its
340 # contract, a failed Future or a croak, as it does a resolution error.
341 # build_path croaks for that, and synchronously: from a Future-returning
342 # method it escaped as a die. The message drops the location the croak ends
343 # in, which points here; a croaking caller adds its caller's own.
344 #
345 # An apiVersion in the hint - from a qualified name or the object - names
346 # the one group/version the request may go to; a path outside it is refused
347 # with Kubernetes::REST's own message (its k43), so list, delete and
348 # ensure_only's prune never reach another group serving a Kind of that name.
349 632 ▶sub _request_path {
Subroutine _request_path: called CC=8 SCAR=21.2
350 632 my ($self, $class, $ident, %args) = @_;
351 632 my %hint = $self->_unstructured_hint($class, $ident);
352 632 my $path = eval { $self->_rest->build_path($class, %args, %hint) };
353 632 ▶ unless (defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 45 587 632
354 45 ▶ my $error = $@ ? "$@" : "cannot build a request path for $class";
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 45 0 45
355 45 $error =~ s/\s+\z//;
356 45 $error =~ s/ at \S+ line \d+\.\z//;
357 45 return (undef, $error);
358 }
359 587 my $api_version = $hint{api_version};
360 587 ▶ if (defined $api_version && length $api_version) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $api_version and length $api_version) 41 546 587
Condition66%2/3
defined $api_version and length $api_version
!ll&&!rl&&r
546041
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
361 41 ▶ my $prefix = $api_version =~ m{/} ? "/apis/$api_version/" : "/api/$api_version/";
Branch: 1 branch
BranchTrueFalseTotal
$api_version =~ m[/] ? : 39 2 41
362 41 ▶ return (undef, "no discovery entry for Kind '" . ($hint{kind} // '') . "'"
Branch: 1 branch
BranchTrueFalseTotal
unless index($path, $prefix) == 0 0 41 41
Condition0%0/2
$hint{"kind"} // ""
l!l
00
MC/DC0%0/1
$hint{"kind"} // ""
$hint{"kind"}
Truth table0%0/2
A
A= $hint{"kind"}
Aresult
11
00
363 . " in apiVersion '$api_version' - cannot build a path for IO::K8s::Unstructured")
364 unless index($path, $prefix) == 0;
365 }
366 587 return $path;
367 }
368  
369 # The name to hand Kubernetes::REST's inflate_object, inflate_list and
370 # process_watch_chunk for $class. A single-segment class of the caller's own
371 # ('+Gizmo' in the resource_map, which expand_class returns as 'Gizmo') must
372 # not read to them as the Kind Gizmo - IO::K8s::Gizmo, or whatever class the
373 # map gives that Kind. A loaded IO::K8s resource class therefore goes over as
374 # '+Class', which is taken exactly; anything else is left as it is. Mirrors
375 # the private helper of the same name in Kubernetes::REST, which is not part
376 # of its public seam.
377 379 ▶sub _exact_class {
Subroutine _exact_class: called CC=7 SCAR=28.7
378 379 my ($self, $class) = @_;
379 379 ▶ 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") 379 0 379
Condition33%5/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
00379
defined $class and not ref $class and length $class and not $class =~ /\A\+/ and $class->can("does")
!ll&&!rl&&r
00379
defined $class and not ref $class and length $class and not $class =~ /\A\+/
!ll&&!rl&&r
00379
defined $class and not ref $class and length $class
!ll&&!rl&&r
00379
defined $class and not ref $class
!ll&&!rl&&r
00379
MC/DC0%0/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 table14%1/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
380 if defined $class && !ref $class && length $class && $class !~ /\A\+/
381 && $class->can('does') && $class->does('IO::K8s::Role::Resource');
382 0 return $class;
383 }
384  
385 34 ▶sub expand_class {
Subroutine expand_class: called CC=2 SCAR=6.9
Pod documented
386 34 my ($self, @args) = @_;
387 34 my ($class, $error) = $self->_resolve_class(@args);
388 34 ▶ croak $error unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 19 15 34
389 15 return $class;
390 }
391  
392  
393 7 ▶sub discover {
Subroutine discover: called CC=2 SCAR=6.9
Pod documented
394 7 my ($self) = @_;
395  
396 7 my $rest = $self->_rest;
397 7 ▶ return Future->done unless $self->resource_map_from_cluster;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->resource_map_from_cluster 1 6 7
398  
399 6 my %requests = $rest->prepare_discovery_requests;
400 6 my @roots = sort keys %requests;
401 return Future->needs_all(
402 12 map { $self->_checked_request($requests{$_}, "discovery GET $_") } @roots
403 5 ▶ )->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
404 5 my %responses;
405 5 @responses{@roots} = @_;
406 # False for legacy discovery: nothing is kept, and Kubernetes::REST
407 # reads it through its own io on first use, as without discover.
408 5 $rest->absorb_discovery(%responses);
409 4 return Future->done;
410 6 });
411 }
412  
413  
414 16 ▶sub _add_to_loop {
Subroutine _add_to_loop: called CC=1 SCAR=0.0
415 16 my ($self, $loop) = @_;
416 16 $self->add_child($self->_http);
417 }
418  
419 # ============================================================================
420 # ASYNC CRUD METHODS - return Futures
421 # ============================================================================
422  
423 55 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
424 55 my ($self, $short_class, @args) = @_;
425  
426 55 my $rest = $self->_rest;
427 26 ▶ return $self->_list_request($short_class, @args)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
428 26 my ($class, $response) = @_;
429 23 ▶ return $self->_checked_response($response, "list $short_class")->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
430 23 return Future->done($rest->inflate_list($self->_exact_class($class), $response));
431 26 });
432 55 });
433 }
434  
435  
436 # list() up to the response, unchecked: resolves with the class and the raw
437 # Kubernetes::REST::HTTPResponse, or fails before any request when the name
438 # resolves to no usable class. ensure_only() needs the status itself - a 404
439 # there means the Kind is not served, not a failure - and must not read it
440 # back out of the text check_response croaks with.
441 87 ▶sub _list_request {
Subroutine _list_request: called CC=8 SCAR=20.8
442 87 my ($self, $short_class, @args) = @_;
443  
444 87 ▶ return Future->fail("Invalid arguments to list()") if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 4 83 87
445 83 my %args = @args;
446 83 my $unknown = $self->_unknown_argument_error('list', \%args,
447 qw( namespace labelSelector fieldSelector ));
448 83 ▶ return Future->fail($unknown) if defined $unknown;
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown 4 79 83
449  
450 79 my $rest = $self->_rest;
451 79 my ($class, $error) = $self->_resolve_class($short_class);
452 79 ▶ return Future->fail($error) unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 21 58 79
453  
454 # Selectors are query parameters; build_path only knows path segments and
455 # would drop them silently, turning a filtered list into a full one.
456 58 my %params;
457 58 for my $selector (qw(labelSelector fieldSelector)) {
458 116 my $value = delete $args{$selector};
459 116 ▶ $params{$selector} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 38 78 116
460 }
461  
462 58 (my $path, $error) = $self->_request_path($class, $short_class, %args);
463 58 ▶ return Future->fail($error) unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 4 54 58
464 return $self->_request_unchecked('GET', $path,
465 %params ? (parameters => \%params) : (),
466 54 ▶ )->then(sub { Future->done($class, @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
%params ? : 34 20 54
467 }
468  
469 57 ▶sub get {
Subroutine get: called CC=11 SCAR=24.0
Pod documented
470 57 my ($self, $short_class, @rest_args) = @_;
471  
472 57 my $rest = $self->_rest;
473 57 my %args;
474 57 ▶ if (@rest_args == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (@rest_args == 1) { } 24 33 57
elsif (@rest_args >= 2 and not $rest_args[0] =~ /^(name|namespace)$/) { } 26 7 33
elsif (@rest_args % 2 == 0) { } 5 2 7
475 24 $args{name} = $rest_args[0];
476 ▶ } elsif (@rest_args >= 2 && $rest_args[0] !~ /^(name|namespace)$/) {
Condition66%2/3
@rest_args >= 2 and not $rest_args[0] =~ /^(name|namespace)$/
!ll&&!rl&&r
0726
MC/DC50%1/2
@rest_args >= 2 and not $rest_args[0] =~ /^(name|namespace)$/
@rest_args >= 2 not $rest_args[0] =~ /^(name|namespace)$/
Truth table66%2/3
A and not(B)
A= @rest_args >= 2B= not $rest_args[0] =~ /^(name|namespace)$/
ABresult
0X0
100
111
477 26 $args{name} = shift @rest_args;
478 26 ▶ return Future->fail("Invalid arguments to get()") if @rest_args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest_args % 2 3 23 26
479 23 %args = (%args, @rest_args);
480 } elsif (@rest_args % 2 == 0) {
481 5 %args = @rest_args;
482 } else {
483 2 return Future->fail("Invalid arguments to get()");
484 }
485 52 my $unknown = $self->_unknown_argument_error('get', \%args, qw( name namespace ));
486 52 ▶ return Future->fail($unknown) if defined $unknown;
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown 2 50 52
487  
488 # A reference in the name position (a manifest hashref, an IO::K8s object)
489 # would otherwise be stringified straight into the path (k70).
490 50 ▶ if (my $name_error = $self->_resource_name_error($args{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name_error = $self->_resource_name_error($args{"name"})) 4 46 50
491 4 return Future->fail($name_error);
492 }
493  
494 46 my ($class, $error) = $self->_resolve_class($short_class);
495 46 ▶ return Future->fail($error) unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 19 27 46
496 27 ▶ return Future->fail("name required for get") unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 1 26 27
497  
498 26 (my $path, $error) = $self->_request_path($class, $short_class, %args);
499 26 ▶ return Future->fail($error) unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 2 24 26
500 24 my $req = $rest->prepare_request('GET', $path);
501  
502 19 ▶ return $self->_checked_request($req, "get $short_class")->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
503 19 my ($response) = @_;
504 19 return Future->done($rest->inflate_object($self->_exact_class($class), $response));
505 24 });
506 }
507  
508  
509 34 ▶sub create {
Subroutine create: called CC=5 SCAR=16.3
Pod documented
510 34 my ($self, $object) = @_;
511  
512 34 my $rest = $self->_rest;
513 34 my ($class, $error) = $self->_object_class('create', $object);
514 34 ▶ return Future->fail($error) unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 4 30 34
515 30 ▶ my $namespace = $object->can('metadata') && $object->metadata
Branch: 1 branch
BranchTrueFalseTotal
$object->can('metadata') && $object->metadata ? : 30 0 30
Condition33%1/3
$object->can("metadata") and $object->metadata
!ll&&!rl&&r
0030
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
516 ? $object->metadata->namespace
517 : undef;
518  
519 30 (my $path, $error) = $self->_request_path($class, $object, namespace => $namespace);
520 30 ▶ return Future->fail($error) unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 3 27 30
521 27 my $req = $rest->prepare_request('POST', $path, body => $object->TO_JSON);
522  
523 24 ▶ return $self->_checked_request($req, "create " . ref($object))->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
524 24 my ($response) = @_;
525 24 return Future->done($rest->inflate_object($self->_exact_class($class), $response));
526 27 });
527 }
528  
529  
530 20 ▶sub update {
Subroutine update: called CC=5 SCAR=16.1
Pod documented
531 20 my ($self, $object) = @_;
532  
533 20 my $rest = $self->_rest;
534 20 my ($class, $error) = $self->_object_class('update', $object);
535 20 ▶ croak $error unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 3 17 20
536 17 ▶ my $metadata = $object->metadata or croak "object must have metadata";
Branch: 1 branch
BranchTrueFalseTotal
my $metadata = $object->metadata or croak("object must have metadata") 16 1 17
537 16 ▶ 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") 15 1 16
538 15 my $namespace = $metadata->namespace;
539  
540 15 (my $path, $error) = $self->_request_path($class, $object,
541 name => $name, namespace => $namespace);
542 15 ▶ croak $error unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 3 12 15
543 12 my $req = $rest->prepare_request('PUT', $path, body => $object->TO_JSON);
544  
545 6 ▶ return $self->_checked_request($req, "update " . ref($object))->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
546 6 my ($response) = @_;
547 6 return Future->done($rest->inflate_object($self->_exact_class($class), $response));
548 12 });
549 }
550  
551  
552 18 ▶sub update_status {
Subroutine update_status: called CC=5 SCAR=16.1
Pod documented
553 18 my ($self, $object) = @_;
554  
555 18 my $rest = $self->_rest;
556 18 my ($class, $error) = $self->_object_class('update_status', $object);
557 18 ▶ croak $error unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 3 15 18
558 15 ▶ my $metadata = $object->metadata or croak "object must have metadata";
Branch: 1 branch
BranchTrueFalseTotal
my $metadata = $object->metadata or croak("object must have metadata") 15 0 15
559 15 ▶ 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") 14 1 15
560 14 my $namespace = $metadata->namespace;
561  
562 14 (my $path, $error) = $self->_request_path($class, $object,
563 name => $name,
564 namespace => $namespace,
565 subresource => 'status',
566 );
567 14 ▶ croak $error unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 3 11 14
568 11 my $req = $rest->prepare_request('PUT', $path, body => $object->TO_JSON);
569  
570 6 ▶ return $self->_checked_request($req, "update_status $class")->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
571 6 my ($response) = @_;
572 6 return Future->done($rest->inflate_object($self->_exact_class($class), $response));
573 11 });
574 }
575  
576  
577 # Argument handling shared by patch() and patch_status(): the object form and
578 # both class+name forms, the required patch document and the patch type.
579 # Returns (undef, $class, $name, $namespace, $patch, $content_type), or just
580 # the failure message, which the caller turns into a failed Future. $label
581 # names the calling method in those messages; $default_type is the patch type
582 # used when the caller passes none.
583 119 ▶sub _patch_args {
Subroutine _patch_args: called CC=16 SCAR=27.7
584 119 my ($self, $label, $default_type, $class_or_object, @rest_args) = @_;
585  
586 119 my $rest = $self->_rest;
587 119 my ($class, $name, $namespace, $patch, $patch_type);
588  
589 119 ▶ if (ref($class_or_object) && blessed($class_or_object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class_or_object and builtin::blessed($class_or_object)) { } 38 81 119
Condition100%3/3
ref $class_or_object and builtin::blessed($class_or_object)
!ll&&!rl&&r
77438
MC/DC100%2/2
ref $class_or_object and builtin::blessed($class_or_object)
ref $class_or_object builtin::blessed($class_or_object)
Truth table100%3/3
A and B
A= ref $class_or_objectB= builtin::blessed($class_or_object)
ABresult
0X0
100
111
590 38 my $object = $class_or_object;
591 38 ($class, my $error) = $self->_object_class($label, $object);
592 38 ▶ return $error unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 6 32 38
593 32 ▶ my $metadata = $object->metadata or return "object must have metadata";
Branch: 1 branch
BranchTrueFalseTotal
my $metadata = $object->metadata or return "object must have metadata" 30 2 32
594 30 ▶ $name = $metadata->name or return "object must have metadata.name";
Branch: 1 branch
BranchTrueFalseTotal
$name = $metadata->name or return "object must have metadata.name" 28 2 30
595 28 $namespace = $metadata->namespace;
596 28 ▶ return "Invalid arguments to $label()" if @rest_args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest_args % 2 4 24 28
597 24 my %args = @rest_args;
598 24 my $unknown = $self->_unknown_argument_error($label, \%args, qw( patch type ));
599 24 ▶ return $unknown if defined $unknown;
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown 2 22 24
600 22 ▶ $patch = $args{patch} // return "$label requires 'patch' parameter";
Condition100%2/2
$args{"patch"} // (return "$label requires 'patch' parameter")
l!l
202
MC/DC100%1/1
$args{"patch"} // (return "$label requires 'patch' parameter")
$args{"patch"}
Truth table100%2/2
A
A= $args{"patch"}
Aresult
11
00
601 20 ▶ $patch_type = $args{type} // $default_type;
Condition66%2/3
$args{"type"} // $default_type
l!l&&r!l&&!r
8120
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
602 } else {
603 81 my ($arg_error, %args) = $self->_named_args($label, \@rest_args,
604 qw( name namespace patch type ));
605 81 ▶ return $arg_error if defined $arg_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg_error 11 70 81
606  
607 70 ($class, my $error) = $self->_resolve_class($class_or_object);
608 70 ▶ return $error unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 34 36 70
609 36 ▶ $name = $args{name} or return "name required for $label";
Branch: 1 branch
BranchTrueFalseTotal
$name = $args{"name"} or return "name required for $label" 34 2 36
610 34 $namespace = $args{namespace};
611 34 ▶ $patch = $args{patch} // return "$label requires 'patch' parameter";
Condition100%2/2
$args{"patch"} // (return "$label requires 'patch' parameter")
l!l
331
MC/DC100%1/1
$args{"patch"} // (return "$label requires 'patch' parameter")
$args{"patch"}
Truth table100%2/2
A
A= $args{"patch"}
Aresult
11
00
612 33 ▶ $patch_type = $args{type} // $default_type;
Condition66%2/3
$args{"type"} // $default_type
l!l&&r!l&&!r
19140
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
613 }
614  
615 53 my %patch_types = (
616 strategic => 'application/strategic-merge-patch+json',
617 merge => 'application/merge-patch+json',
618 json => 'application/json-patch+json',
619 );
620 53 ▶ my $content_type = $patch_types{$patch_type}
Condition100%2/2
$patch_types{$patch_type} // (return "Unknown patch type '${patch_type}'")
l!l
503
MC/DC100%1/1
$patch_types{$patch_type} // (return "Unknown patch type '${patch_type}'")
$patch_types{$patch_type}
Truth table100%2/2
A
A= $patch_types{$patch_type}
Aresult
11
00
621 // return "Unknown patch type '$patch_type'";
622  
623 50 return (undef, $class, $name, $namespace, $patch, $content_type);
624 }
625  
626 50 ▶sub patch {
Subroutine patch: called CC=3 SCAR=11.0
Pod documented
627 50 my ($self, $class_or_object, @rest_args) = @_;
628  
629 50 my $rest = $self->_rest;
630 50 my ($error, $class, $name, $namespace, $patch, $content_type)
631 = $self->_patch_args('patch', 'strategic', $class_or_object, @rest_args);
632 50 ▶ return Future->fail($error) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 29 21 50
633  
634 21 (my $path, $error) = $self->_request_path($class, $class_or_object,
635 name => $name, namespace => $namespace);
636 21 ▶ return Future->fail($error) unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 5 16 21
637 16 my $req = $rest->prepare_request('PATCH', $path,
638 body => $patch, content_type => $content_type);
639  
640 13 ▶ return $self->_checked_request($req, "patch $class")->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
641 13 my ($response) = @_;
642 13 return Future->done($rest->inflate_object($self->_exact_class($class), $response));
643 16 });
644 }
645  
646  
647 69 ▶sub patch_status {
Subroutine patch_status: called CC=3 SCAR=11.0
Pod documented
648 69 my ($self, $class_or_object, @rest_args) = @_;
649  
650 69 my $rest = $self->_rest;
651 69 my ($error, $class, $name, $namespace, $patch, $content_type)
652 = $self->_patch_args('patch_status', 'merge', $class_or_object, @rest_args);
653 69 ▶ return Future->fail($error) if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 40 29 69
654  
655 29 (my $path, $error) = $self->_request_path($class, $class_or_object,
656 name => $name,
657 namespace => $namespace,
658 subresource => 'status',
659 );
660 29 ▶ return Future->fail($error) unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 5 24 29
661 24 my $req = $rest->prepare_request('PATCH', $path,
662 body => $patch, content_type => $content_type);
663  
664 19 ▶ return $self->_checked_request($req, "patch_status $class")->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
665 19 my ($response) = @_;
666 19 return Future->done($rest->inflate_object($self->_exact_class($class), $response));
667 24 });
668 }
669  
670  
671 81 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
672 81 my ($self, @args) = @_;
673  
674 81 my $rest = $self->_rest;
675 46 ▶ return $self->_delete_request(@args)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
676 46 my ($class, $response) = @_;
677 40 ▶ return $self->_checked_response($response, "delete $class")->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
678 40 return Future->done(1);
679 46 });
680 81 });
681 }
682  
683  
684 # The message for the first key of %$args (in sort order) that is not in
685 # @allowed, or nothing when there is none - Kubernetes::REST's wording, naming
686 # $label and what is allowed. Each caller reports it per its contract, a
687 # failed Future or a croak, before any request: an option a method does not
688 # take would otherwise be dropped silently (a misspelt labelSelector lists
689 # everything, namespace for namespaces makes ensure_only prune at cluster
690 # scope only).
691 641 ▶sub _unknown_argument_error {
Subroutine _unknown_argument_error: called CC=2 SCAR=6.9
692 641 my ($self, $label, $args, @allowed) = @_;
693 641 my %allowed = map { $_ => 1 } @allowed;
694 641 my ($unknown) = sort grep { !$allowed{$_} } keys %$args;
695 641 ▶ return unless defined $unknown;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $unknown 493 148 641
696 148 return "Unknown argument '$unknown' to $label() (allowed: " . join(', ', @allowed) . ')';
697 }
698  
699 # Argument handling shared by log(), port_forward(), exec(), attach(),
700 # cp_to_pod(), cp_from_pod() and the class form of patch() and patch_status()
701 # (in _patch_args): the name positional - METHOD('Pod', 'web',
702 # %options) - or keyed - METHOD('Pod', name => 'web', %options). A first
703 # argument that is one of @allowed starts the keyed form, so the keys read as
704 # the start of the keyed form and the keys _unknown_argument_error accepts are
705 # one list. Returns (undef, %args), or just the failure message, which the
706 # caller turns into a failed Future; $label names the calling method.
707 422 ▶sub _named_args {
Subroutine _named_args: called CC=7 SCAR=19.5
708 422 my ($self, $label, $rest_args, @allowed) = @_;
709 422 my @rest_args = @$rest_args;
710 422 my $keys = join '|', map { quotemeta } @allowed;
711 422 my %args;
712 422 ▶ if (@rest_args >= 1
Branch: 2 branches
BranchTrueFalseTotal
if (@rest_args >= 1 and not ref $rest_args[0] and not $rest_args[0] =~ /^(?:$keys)$/) { } 312 110 422
elsif (@rest_args % 2 == 0) { } 46 64 110
Condition100%6/6
@rest_args >= 1 and not ref $rest_args[0] and not $rest_args[0] =~ /^(?:$keys)$/
!ll&&!rl&&r
1298312
@rest_args >= 1 and not ref $rest_args[0]
!ll&&!rl&&r
66410
MC/DC100%3/3
@rest_args >= 1 and not ref $rest_args[0] and not $rest_args[0] =~ /^(?:$keys)$/
@rest_args >= 1 not ref $rest_args[0] not $rest_args[0] =~ /^(?:$keys)$/
Truth table100%4/4
A and not(B) and not(C)
A= @rest_args >= 1B= not ref $rest_args[0]C= not $rest_args[0] =~ /^(?:$keys)$/
ABCresult
0XX0
10X0
1100
1111
713 && !ref($rest_args[0])
714 && $rest_args[0] !~ /^(?:$keys)$/
715 ) {
716 312 $args{name} = shift @rest_args;
717 312 ▶ return "Invalid arguments to $label()" if @rest_args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest_args % 2 20 292 312
718 292 %args = (%args, @rest_args);
719 } elsif (@rest_args % 2 == 0) {
720 46 %args = @rest_args;
721 } else {
722 64 return "Invalid arguments to $label()";
723 }
724 338 my $unknown = $self->_unknown_argument_error($label, \%args, @allowed);
725 338 ▶ return $unknown if defined $unknown;
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown 130 208 338
726 208 return (undef, %args);
727 }
728  
729 # The propagationPolicy values the API server accepts in a DELETE's
730 # DeleteOptions.
731 48 my @PROPAGATION_POLICIES = qw( Background Foreground Orphan );
732  
733 # Nothing when $policy is absent or one of @PROPAGATION_POLICIES, else the
734 # message for it, naming $label - in Kubernetes::REST's wording.
735 102 ▶sub _propagation_policy_error {
Subroutine _propagation_policy_error: called CC=3 SCAR=11.0
736 102 my ($self, $label, $policy) = @_;
737 102 ▶ return if !defined $policy || grep { $policy eq $_ } @PROPAGATION_POLICIES;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $policy or grep {$policy eq $_;} @PROPAGATION_POLICIES 98 4 102
Condition100%3/3
not defined $policy or grep {$policy eq $_;} @PROPAGATION_POLICIES
l!l&&r!l&&!r
38604
MC/DC100%2/2
not defined $policy or grep {$policy eq $_;} @PROPAGATION_POLICIES
not defined $policy grep {$policy eq $_;} @PROPAGATION_POLICIES
Truth table100%3/3
not(A) or B
A= not defined $policyB= grep {$policy eq $_;} @PROPAGATION_POLICIES
ABresult
1X1
011
000
738 4 return "Unknown propagationPolicy '$policy' for $label() (use: "
739 . join(', ', @PROPAGATION_POLICIES) . ')';
740 }
741  
742 # delete() up to the response, unchecked: resolves with the class and the raw
743 # Kubernetes::REST::HTTPResponse, or fails before any request on bad
744 # arguments - for the same reason as _list_request: ensure_only() treats a
745 # 404 (already gone) differently from a failure. An option delete() does not
746 # know is a bad argument too: a mistyped propagationPolicy dropped on the
747 # floor would leave a Job's Pods orphaned.
748 99 ▶sub _delete_request {
Subroutine _delete_request: called CC=18 SCAR=28.9
749 99 my ($self, $class_or_object, @rest_args) = @_;
750  
751 99 my ($class, $name, $namespace, %options, @known);
752  
753 99 ▶ if (ref($class_or_object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class_or_object) { } 41 58 99
754 41 my $object = $class_or_object;
755 41 ($class, my $error) = $self->_object_class('delete', $object);
756 41 ▶ return Future->fail($error) unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 3 38 41
757 38 ▶ my $metadata = $object->metadata or return Future->fail("object must have metadata");
Branch: 1 branch
BranchTrueFalseTotal
my $metadata = $object->metadata or return "Future"->fail("object must have metadata") 37 1 38
758 37 ▶ $name = $metadata->name or return Future->fail("object must have metadata.name");
Branch: 1 branch
BranchTrueFalseTotal
$name = $metadata->name or return "Future"->fail("object must have metadata.name") 36 1 37
759 36 $namespace = $metadata->namespace;
760 36 ▶ return Future->fail("Invalid arguments to delete()") if @rest_args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest_args % 2 1 35 36
761 35 %options = @rest_args;
762 35 @known = qw( propagationPolicy );
763 } else {
764 58 my %args;
765 58 ▶ if (@rest_args == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (@rest_args == 1) { } 13 45 58
elsif (@rest_args >= 2 and not $rest_args[0] =~ /^(name|namespace|propagationPolicy)$/) { } 33 12 45
elsif (@rest_args % 2 == 0) { } 11 1 12
766 13 $args{name} = $rest_args[0];
767 ▶ } elsif (@rest_args >= 2 && $rest_args[0] !~ /^(name|namespace|propagationPolicy)$/) {
Condition100%3/3
@rest_args >= 2 and not $rest_args[0] =~ /^(name|namespace|propagationPolicy)$/
!ll&&!rl&&r
11133
MC/DC100%2/2
@rest_args >= 2 and not $rest_args[0] =~ /^(name|namespace|propagationPolicy)$/
@rest_args >= 2 not $rest_args[0] =~ /^(name|namespace|propagationPolicy)$/
Truth table100%3/3
A and not(B)
A= @rest_args >= 2B= not $rest_args[0] =~ /^(name|namespace|propagationPolicy)$/
ABresult
0X0
100
111
768 33 $args{name} = shift @rest_args;
769 33 ▶ return Future->fail("Invalid arguments to delete()") if @rest_args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest_args % 2 0 33 33
770 33 %args = (%args, @rest_args);
771 } elsif (@rest_args % 2 == 0) {
772 11 %args = @rest_args;
773 } else {
774 1 return Future->fail("Invalid arguments to delete()");
775 }
776  
777 57 ($class, my $error) = $self->_resolve_class($class_or_object);
778 57 ▶ return Future->fail($error) unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 10 47 57
779 47 ▶ $name = delete $args{name} or return Future->fail("name required for delete");
Branch: 1 branch
BranchTrueFalseTotal
$name = delete $args{"name"} or return "Future"->fail("name required for delete") 45 2 47
780 45 $namespace = delete $args{namespace};
781 45 %options = %args;
782 45 @known = qw( name namespace propagationPolicy );
783 }
784  
785 80 my $policy = delete $options{propagationPolicy};
786 80 my $unknown = $self->_unknown_argument_error('delete', \%options, @known);
787 80 ▶ return Future->fail($unknown) if defined $unknown;
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown 4 76 80
788 76 my $policy_error = $self->_propagation_policy_error('delete', $policy);
789 76 ▶ return Future->fail($policy_error) if defined $policy_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $policy_error 3 73 76
790  
791 # A reference in the name position of the class form (delete('Pod',
792 # {name => 'web'})) would otherwise be stringified straight into the path
793 # (k70); the object form's name comes from metadata and is always a string.
794 73 ▶ if (my $name_error = $self->_resource_name_error($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name_error = $self->_resource_name_error($name)) 4 69 73
795 4 return Future->fail($name_error);
796 }
797  
798 69 my ($path, $error) = $self->_request_path($class, $class_or_object,
799 name => $name, namespace => $namespace);
800 69 ▶ return Future->fail($error) unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 5 64 69
801 return $self->_request_unchecked('DELETE', $path,
802 defined $policy ? (parameters => { propagationPolicy => $policy }) : (),
803 64 ▶ )->then(sub { Future->done($class, @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
defined $policy ? : 35 29 64
804 }
805  
806 # One request through the Kubernetes::REST seam, resolving with the unchecked
807 # Kubernetes::REST::HTTPResponse -- for ensure() and ensure_only(), which
808 # branch on the status code (404 absent, 409 conflict), directly or through
809 # _list_request and _delete_request. check_response would fold that code into
810 # an error string it could only be read back out of with a regex.
811 249 ▶sub _request_unchecked {
Subroutine _request_unchecked: called CC=1 SCAR=0.0
812 249 my ($self, $method, $path, %opts) = @_;
813 249 return $self->_do_request($self->_rest->prepare_request($method, $path, %opts));
814 }
815  
816 # The Future for a response: done with it when Kubernetes::REST's
817 # check_response accepts it, failed the way Future's convention has it when
818 # it does not (status >= 400) - ->fail($error, 'http', $response). $error is
819 # exactly what check_response throws: the message string, or the error object
820 # of a Kubernetes::REST that has one; $response lets a caller branch on
821 # ->status (404 already gone, 409 conflict) instead of parsing the text.
822 # $context names the operation in the message, as for check_response.
823 345 ▶sub _checked_response {
Subroutine _checked_response: called CC=2 SCAR=6.9
824 345 my ($self, $response, $context) = @_;
825 return Future->done($response)
826 345 ▶ if eval { $self->_rest->check_response($response, $context); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $self->_rest->check_response($response, $context); 1 } } 283 62 345
827 62 return Future->fail($@, http => $response);
828 }
829  
830 # _do_request, then _checked_response: resolves with the response, or fails
831 # as above.
832 146 ▶sub _checked_request {
Subroutine _checked_request: called CC=1 SCAR=0.0
833 146 my ($self, $req, $context) = @_;
834 146 ▶ return $self->_do_request($req)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
835 146 my ($response) = @_;
836 146 return $self->_checked_response($response, $context);
837 146 });
838 }
839  
840 # Shared hashref handling for ensure() and ensure_only(): turns a manifest into
841 # a typed object. A manifest's apiVersion is authoritative - with one, the
842 # class is resolved as that exact group/version/Kind, and an apiVersion no
843 # class serves croaks instead of falling back to the version the bare Kind
844 # happens to map to (HorizontalPodAutoscaler alone means autoscaling/v2, a
845 # different endpoint and schema than an autoscaling/v1 manifest). Without an
846 # apiVersion the bare Kind resolves as it always did. Either way the class
847 # must be usable (_usable_class): one that does not load croaks with its load
848 # error. The class is resolved here, so it goes to struct_to_object with a
849 # '+', which IO::K8s takes as that exact class: struct_to_object resolves a
850 # plain name again, and a single-segment class of the caller's own ('+Gizmo'
851 # in the resource_map, which expand_class returns as 'Gizmo') reads to it as
852 # the Kind Gizmo - IO::K8s::Gizmo, or whatever class the map gives that Kind.
853 # $label only appears in croak messages.
854 26 ▶sub _manifest_to_object {
Subroutine _manifest_to_object: called CC=6 SCAR=17.9
855 26 my ($self, $label, $manifest) = @_;
856 26 ▶ 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'") 25 1 26
857 25 my $api_version = $manifest->{apiVersion};
858 25 my $rest = $self->_rest;
859  
860 25 ▶ return $rest->k8s->struct_to_object('+' . $self->expand_class($kind), $manifest)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $api_version and length $api_version 8 17 25
Condition100%3/3
defined $api_version and length $api_version
!ll&&!rl&&r
7117
MC/DC100%2/2
defined $api_version and length $api_version
defined $api_version length $api_version
Truth table100%3/3
A and B
A= defined $api_versionB= length $api_version
ABresult
0X0
100
111
861 unless defined $api_version && length $api_version;
862  
863 17 ▶ my $resolved = $rest->expand_class($kind, $api_version)
Condition100%3/3
$rest->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&&r!l&&!r
1421
MC/DC0%0/2
$rest->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)')
$rest->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)')
Truth table33%1/3
A // B
A= $rest->expand_class($kind, $api_version)B= croak("${label}: no IO::K8s class for apiVersion '${api_version}', kind '${kind}'" . ' (add it to resource_map if it is a CRD)')
ABresult
1X1
011
000
864 // croak "$label: no IO::K8s class for apiVersion '$api_version', kind '$kind'"
865 . " (add it to resource_map if it is a CRD)";
866 14 my ($class, $error) = $self->_usable_class("$api_version/$kind", $resolved);
867 14 ▶ croak "$label: $error" unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 1 13 14
868 13 return $rest->k8s->struct_to_object('+' . $class, $manifest);
869 }
870  
871 # The apiVersion and Kind an object is an instance of, for ensure() and
872 # ensure_only() to tell resources apart by. A typed object answers from its
873 # class (api_version(), kind()); IO::K8s::Unstructured from its instance data,
874 # since its class name says nothing about what it holds. The last segment of a
875 # class name is not enough on its own: a CRD is free to reuse a built-in Kind
876 # name in its own group. Mirrors the private helper of the same name in
877 # Kubernetes::REST, which is not part of its public seam.
878 124 ▶sub _api_version_and_kind {
Subroutine _api_version_and_kind: called CC=6 SCAR=18.4
879 124 my ($self, $object) = @_;
880 124 ▶ my $api_version = ref($object) eq 'IO::K8s::Unstructured' ? $object->apiVersion
Branch: 2 branches
BranchTrueFalseTotal
ref $object eq 'IO::K8s::Unstructured' ? : 18 106 124
$object->can('api_version') ? : 106 0 106
881 : $object->can('api_version') ? $object->api_version
882 : undef;
883 124 ▶ my $kind = $object->can('kind') ? $object->kind : undef;
Branch: 1 branch
BranchTrueFalseTotal
$object->can('kind') ? : 124 0 124
884 124 ▶ ($kind = ref $object) =~ s/.*::// unless defined $kind;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kind 2 122 124
885 124 ▶ return ($api_version // '', $kind);
Condition50%1/2
$api_version // ""
l!l
1240
MC/DC0%0/1
$api_version // ""
$api_version
Truth table50%1/2
A
A= $api_version
Aresult
11
00
886 }
887  
888 87 ▶sub ensure {
Subroutine ensure: called CC=10 SCAR=23.1
Pod documented
889 87 my ($self, $object) = @_;
890  
891 87 my $rest = $self->_rest;
892 87 ▶ $object = $self->_manifest_to_object('ensure', $object) if ref($object) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $object eq "HASH" 20 67 87
893 78 ▶ croak "ensure requires an IO::K8s object or hashref" unless blessed($object);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($object) 0 78 78
894  
895 78 my ($class, $error) = $self->_object_class('ensure', $object);
896 78 ▶ croak $error unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 2 76 78
897 76 my ($api_version, $kind) = $self->_api_version_and_kind($object);
898 # The special cases below are the built-in core v1 PersistentVolumeClaim
899 # and batch/v1 Job only. The apiVersion is compared exactly, not just its
900 # group: the Job branch reads batch/v1's status fields and deletes what it
901 # takes for a failed Job, so an apiVersion it was not written for falls
902 # through to the plain update, where a mismatch fails loudly instead.
903 76 ▶ my $is_pvc = $api_version eq 'v1' && $kind eq 'PersistentVolumeClaim';
Condition100%3/3
$api_version eq 'v1' && $kind eq 'PersistentVolumeClaim'
!ll&&!rl&&r
38353
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
904 76 ▶ 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
6709
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
905 76 ▶ my $metadata = $object->metadata or croak "object must have metadata";
Branch: 1 branch
BranchTrueFalseTotal
my $metadata = $object->metadata or croak("object must have metadata") 76 0 76
906 76 ▶ 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") 76 0 76
907 76 my $namespace = $metadata->namespace;
908 76 (my $path, $error) = $self->_request_path($class, $object,
909 name => $name, namespace => $namespace);
910 76 ▶ croak $error unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 5 71 76
911 71 (my $collection, $error) = $self->_request_path($class, $object, namespace => $namespace);
912 71 ▶ croak $error unless defined $collection;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $collection 0 71 71
913  
914 # GET the object as the server has it now; $context names the step.
915 13 ▶ my $fetch = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
916 13 my ($context) = @_;
917 return $self->_checked_request($rest->prepare_request('GET', $path), "$context $kind/$name")
918 11 ▶ ->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
919 11 my ($response) = @_;
920 11 return Future->done($rest->inflate_object($self->_exact_class($class), $response));
921 13 });
922 71 };
923  
924 # PUT at the server's resourceVersion. A 409 means the object changed
925 # between GET and PUT: fetch it once more and retry once, no further.
926 20 ▶ my $replace = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
927 20 my ($existing) = @_;
928 20 $metadata->resourceVersion($existing->metadata->resourceVersion);
929 20 ▶ return $self->_request_unchecked('PUT', $path, body => $object->TO_JSON)->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
930 20 my ($response) = @_;
931 20 ▶ if ($response->status == 409) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->status == 409) 7 13 20
932 5 ▶ return $fetch->('ensure refetch')->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
933 5 my ($current) = @_;
934 5 $metadata->resourceVersion($current->metadata->resourceVersion);
935 5 return $self->update($object);
936 7 });
937 }
938 10 ▶ return $self->_checked_response($response, "update $class")->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
939 10 return Future->done($rest->inflate_object($self->_exact_class($class), $response));
940 13 });
941 20 });
942 71 };
943  
944 # The object as the server has it, however ensure found out it exists.
945 32 ▶ my $apply_to_existing = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
946 32 my ($existing) = @_;
947  
948 # An existing claim is never rewritten.
949 32 ▶ return Future->done($existing) if $is_pvc;
Branch: 1 branch
BranchTrueFalseTotal
if $is_pvc 3 29 32
950  
951 # A Job's pod template is immutable: a running or succeeded Job stays,
952 # any other is replaced - deleted with its Pods (Background), which
953 # the API server's default for a Job would orphan. A failing delete
954 # does not stop the create. The status is read from TO_JSON, not
955 # status(): an IO::K8s::Unstructured Job has no status accessor.
956 29 ▶ if ($is_job) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_job) 9 20 29
957 9 ▶ my $status = $existing->TO_JSON->{status} || {};
Condition50%1/2
$existing->TO_JSON->{'status'} || {}
l!l
90
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
958 return Future->done($existing)
959 9 ▶ if $status->{succeeded} || $status->{active};
Branch: 1 branch
BranchTrueFalseTotal
if $status->{"succeeded"} or $status->{"active"} 4 5 9
Condition100%3/3
$status->{"succeeded"} or $status->{"active"}
l!l&&r!l&&!r
225
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
960 5 ▶ return Future->call(sub { $self->delete($existing, propagationPolicy => 'Background') })
Subroutine __ANON__: called CC=1 SCAR=0.0
961 1 ▶ ->else(sub { Future->done })
Subroutine __ANON__: called CC=1 SCAR=0.0
962 5 ▶ ->then(sub { $self->create($object) });
Subroutine __ANON__: called CC=1 SCAR=0.0
963 }
964  
965 20 return $replace->($existing);
966 71 };
967  
968 # POST. A 409 means it was created by someone else after our GET: from
969 # there on it is an existing object like any other, special cases
970 # included - a Job must not get a PUT onto its immutable Pod template.
971 40 ▶ my $create = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
972 40 ▶ return $self->_request_unchecked('POST', $collection, body => $object->TO_JSON)->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
973 40 my ($response) = @_;
974 40 ▶ return $fetch->('ensure post-409 get')->then($apply_to_existing)
Branch: 1 branch
BranchTrueFalseTotal
if $response->status == 409 6 34 40
975 if $response->status == 409;
976 32 ▶ return $self->_checked_response($response, "create $class")->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
977 32 return Future->done($rest->inflate_object($self->_exact_class($class), $response));
978 34 });
979 40 });
980 71 };
981  
982 71 ▶ return $self->_request_unchecked('GET', $path)->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
983 71 my ($response) = @_;
984 71 ▶ return $create->() if $response->status == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $response->status == 404 40 31 71
985 26 ▶ return $self->_checked_response($response, "ensure get $kind/$name")->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
986 26 return $apply_to_existing->($rest->inflate_object($self->_exact_class($class), $response));
987 31 });
988 71 });
989 }
990  
991  
992 34 ▶sub ensure_all {
Subroutine ensure_all: called CC=2 SCAR=6.9
Pod documented
993 34 my ($self, @objects) = @_;
994  
995 # Strictly one after another: object N+1 is only started once object N
996 # is done, so a later object may rely on an earlier one (a Namespace and
997 # what lives in it). Any error, a croak from ensure() included, fails
998 # the chain and nothing after it is started.
999 34 my @results;
1000 34 my $f = Future->done;
1001 34 for my $object (@objects) {
1002 31 ▶ $f = $f->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1003 31 return $self->ensure($object);
1004 23 ▶ })->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1005 23 push @results, @_;
1006 23 return Future->done;
1007 31 });
1008 }
1009  
1010 34 ▶ return $f->then(sub { Future->done(@results) });
Subroutine __ANON__: called CC=1 SCAR=0.0
1011 }
1012  
1013  
1014 31 ▶sub ensure_only {
Subroutine ensure_only: called CC=11 SCAR=24.0
Pod documented
1015 31 my ($self, @args) = @_;
1016  
1017 # Before anything is applied: an odd list drops a value (no objects
1018 # prunes everything carrying the label), namespace for namespaces would
1019 # prune at cluster scope only.
1020 31 ▶ croak "Invalid arguments to ensure_only()" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 2 29 31
1021 29 my %args = @args;
1022 29 my $unknown = $self->_unknown_argument_error('ensure_only', \%args,
1023 qw( label objects kinds namespaces propagationPolicy ));
1024 29 ▶ croak $unknown if defined $unknown;
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown 2 27 29
1025  
1026 27 my $rest = $self->_rest;
1027 27 ▶ my $label = $args{label} or croak "ensure_only requires 'label'";
Branch: 1 branch
BranchTrueFalseTotal
my $label = $args{"label"} or croak("ensure_only requires 'label'") 26 1 27
1028 # Background unless told otherwise: the API server's own default leaves
1029 # the Pods of a pruned Job behind.
1030 26 ▶ my $policy = $args{propagationPolicy} // 'Background';
Condition100%2/2
$args{"propagationPolicy"} // "Background"
l!l
224
MC/DC100%1/1
$args{"propagationPolicy"} // "Background"
$args{"propagationPolicy"}
Truth table100%2/2
A
A= $args{"propagationPolicy"}
Aresult
11
00
1031 26 my $policy_error = $self->_propagation_policy_error('ensure_only', $policy);
1032 26 ▶ croak $policy_error if defined $policy_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $policy_error 1 25 26
1033 25 ▶ my @objects = @{ $args{objects} || [] };
Condition50%1/2
$args{'objects'} || []
l!l
250
MC/DC0%0/1
$args{'objects'} || []
$args{'objects'}
Truth table50%1/2
A
A= $args{'objects'}
Aresult
11
00
1034 25 ▶ my @kinds = @{ $args{kinds} || [] };
Condition50%1/2
$args{'kinds'} || []
l!l
250
MC/DC0%0/1
$args{'kinds'} || []
$args{'kinds'}
Truth table50%1/2
A
A= $args{'kinds'}
Aresult
11
00
1035 25 ▶ my @namespaces = @{ $args{namespaces} || [undef] };
Condition100%2/2
$args{'namespaces'} || [undef]
l!l
223
MC/DC100%1/1
$args{'namespaces'} || [undef]
$args{'namespaces'}
Truth table100%2/2
A
A= $args{'namespaces'}
Aresult
11
00
1036  
1037 # Every hashref is resolved before the first request, so one that cannot
1038 # be (no kind, an apiVersion no class serves) stops the whole call.
1039 25 for my $object (@objects) {
1040 18 ▶ $object = $self->_manifest_to_object('ensure_only', $object)
Branch: 1 branch
BranchTrueFalseTotal
if ref $object eq "HASH" 6 12 18
1041 if ref($object) eq 'HASH';
1042 }
1043  
1044 # (group, Kind, namespace, name), taken from the object on both sides -
1045 # never from the kinds entry, which may be qualified ('autoscaling/v1/...')
1046 # and would then match nothing, deleting the objects just applied. Group
1047 # and Kind come from _api_version_and_kind: class-derived for a typed
1048 # object, instance data for IO::K8s::Unstructured. The group keeps the same
1049 # Kind name in two groups apart (Istio's and the Gateway API's Gateway);
1050 # the core group is ''. No version in the key: the same resource listed
1051 # through another version's class is still the same resource.
1052 46 ▶ my $key_of = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1053 46 my ($object) = @_;
1054 46 my ($api_version, $kind) = $self->_api_version_and_kind($object);
1055 46 my ($group) = $api_version =~ m{\A(.*)/[^/]*\z};
1056 46 my $metadata = $object->metadata;
1057 46 ▶ return join("\0", $group // '', $kind, $metadata->namespace // '', $metadata->name);
Condition75%3/4
$group // ""
l!l
2422
$metadata->namespace // ""
l!l
460
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
1058 24 };
1059  
1060 # A failed list or delete leaves stale objects behind, so it is reported
1061 # rather than swallowed - but only a real failure: a 404 on the list means
1062 # the cluster does not serve the Kind, a 404 on the delete that the object
1063 # is already gone. The status comes from the unchecked response, never
1064 # from the text of an error. A caught croak already ends in its own
1065 # location, which carp adds again, so the reason drops it.
1066 11 ▶ my $where = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1067 11 my ($namespace) = @_;
1068 11 ▶ return defined $namespace ? "in namespace '$namespace'" : 'at cluster scope';
Branch: 1 branch
BranchTrueFalseTotal
defined $namespace ? : 10 1 11
1069 24 };
1070 11 ▶ my $reason_of = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1071 11 my ($error) = @_;
1072 11 ▶ $error = defined $error ? "$error" : 'unknown error';
Branch: 1 branch
BranchTrueFalseTotal
defined $error ? : 11 0 11
1073 11 $error =~ s/\s+\z//;
1074 11 $error =~ s/ at \S+ line \d+\.\z//;
1075 11 return $error;
1076 24 };
1077 18 ▶ my $prune = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1078 18 my ($item) = @_;
1079 18 ▶ return Future->call(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1080 18 $self->_delete_request($item, propagationPolicy => $policy);
1081 18 ▶ })->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1082 18 my ($class, $response) = @_;
1083 18 ▶ return Future->done if $response->status == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $response->status == 404 1 17 18
1084 17 return $self->_checked_response($response, "delete $class");
1085 2 ▶ })->else(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1086 2 my ($error) = @_;
1087 2 my (undef, $kind) = $self->_api_version_and_kind($item);
1088 2 carp "ensure_only: cannot delete $kind '" . $item->metadata->name . "' "
1089 . $where->($item->metadata->namespace) . ': ' . $reason_of->($error);
1090 2 return Future->done;
1091 18 });
1092 24 };
1093  
1094 24 ▶ return $self->ensure_all(@objects)->then(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1095 24 my @applied = @_;
1096 24 my %expected = map { $key_of->($_) => 1 } @objects;
1097  
1098 # One Kind x namespace after another, each delete after the one
1099 # before, and on past every failure.
1100 24 my $f = Future->done;
1101 24 for my $kind (@kinds) {
1102 28 for my $namespace (@namespaces) {
1103 32 ▶ $f = $f->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1104 32 ▶ return Future->call(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1105 32 ▶ $self->_list_request($kind,
Branch: 1 branch
BranchTrueFalseTotal
defined $namespace ? : 29 3 32
1106 labelSelector => $label,
1107 (defined $namespace ? (namespace => $namespace) : ()),
1108 );
1109 27 ▶ })->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1110 27 my ($class, $response) = @_;
1111 27 ▶ return Future->done if $response->status == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $response->status == 404 1 26 27
1112 22 ▶ return $self->_checked_response($response, "list $kind")->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1113 22 return Future->done($rest->inflate_list($self->_exact_class($class), $response));
1114 26 });
1115 9 ▶ })->else(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1116 9 my ($error) = @_;
1117 9 carp "ensure_only: cannot list $kind " . $where->($namespace)
1118 . ', nothing pruned there: ' . $reason_of->($error);
1119 8 return Future->done;
1120 31 ▶ })->then(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1121 31 my ($list) = @_;
1122 31 my $deletes = Future->done;
1123 31 ▶ return $deletes unless $list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 9 22 31
1124 22 for my $item (@{ $list->items }) {
1125 30 ▶ next if $expected{ $key_of->($item) };
Branch: 1 branch
BranchTrueFalseTotal
if $expected{&$key_of($item)} 12 18 30
1126 18 ▶ $deletes = $deletes->then(sub { $prune->($item) });
Subroutine __ANON__: called CC=1 SCAR=0.0
1127 }
1128 22 return $deletes;
1129 32 });
1130 32 });
1131 }
1132 }
1133  
1134 24 ▶ return $f->then(sub { Future->done(@applied) });
Subroutine __ANON__: called CC=1 SCAR=0.0
1135 24 });
1136 }
1137  
1138  
1139 68 ▶sub log {
Subroutine log: called CC=15 SCAR=27.1
Pod documented
1140 68 my ($self, $short_class, @rest_args) = @_;
1141  
1142 68 my $rest = $self->_rest;
1143  
1144 # Support: log('Pod', 'name', ...) and log('Pod', name => 'name', ...)
1145 68 my ($arg_error, %args) = $self->_named_args('log', \@rest_args, qw( name namespace
1146 container follow tailLines sinceSeconds sinceTime timestamps previous limitBytes
1147 on_line ));
1148 68 ▶ return Future->fail($arg_error) if defined $arg_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg_error 40 28 68
1149  
1150 28 ▶ return Future->fail("name required for log") unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 2 26 28
1151  
1152 26 my $on_line = delete $args{on_line};
1153 26 my $container = delete $args{container};
1154 26 my $follow = delete $args{follow};
1155 26 my $tail_lines = delete $args{tailLines};
1156 26 my $since_seconds = delete $args{sinceSeconds};
1157 26 my $since_time = delete $args{sinceTime};
1158 26 my $timestamps = delete $args{timestamps};
1159 26 my $previous = delete $args{previous};
1160 26 my $limit_bytes = delete $args{limitBytes};
1161  
1162 26 my ($class, $error) = $self->_resolve_class($short_class);
1163 26 ▶ return Future->fail($error) unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 11 15 26
1164 15 (my $path, $error) = $self->_request_path($class, $short_class, %args);
1165 15 ▶ return Future->fail($error) unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 2 13 15
1166 13 $path .= '/log';
1167  
1168 13 my %params;
1169 13 ▶ $params{container} = $container if defined $container;
Branch: 1 branch
BranchTrueFalseTotal
if defined $container 2 11 13
1170 13 ▶ $params{follow} = 'true' if $follow;
Branch: 1 branch
BranchTrueFalseTotal
if $follow 5 8 13
1171 13 ▶ $params{tailLines} = $tail_lines if defined $tail_lines;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tail_lines 2 11 13
1172 13 ▶ $params{sinceSeconds} = $since_seconds if defined $since_seconds;
Branch: 1 branch
BranchTrueFalseTotal
if defined $since_seconds 2 11 13
1173 13 ▶ $params{sinceTime} = $since_time if defined $since_time;
Branch: 1 branch
BranchTrueFalseTotal
if defined $since_time 0 13 13
1174 13 ▶ $params{timestamps} = 'true' if $timestamps;
Branch: 1 branch
BranchTrueFalseTotal
if $timestamps 2 11 13
1175 13 ▶ $params{previous} = 'true' if $previous;
Branch: 1 branch
BranchTrueFalseTotal
if $previous 2 11 13
1176 13 ▶ $params{limitBytes} = $limit_bytes if defined $limit_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit_bytes 2 11 13
1177  
1178 13 ▶ if ($on_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_line) 6 7 13
1179 6 my $req = $rest->prepare_request('GET', $path, parameters => \%params);
1180 6 my $buffer = '';
1181  
1182 4 ▶ return $self->_do_streaming_request($req, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1183 4 my ($chunk) = @_;
1184 4 for my $event ($rest->process_log_chunk(\$buffer, $chunk)) {
1185 5 $on_line->($event);
1186 }
1187 6 ▶ })->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1188 6 my ($response) = @_;
1189 6 return $self->_checked_response($response, "log $short_class");
1190 3 ▶ })->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1191 3 ▶ if (length $buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buffer) 1 2 3
1192 1 $on_line->(Kubernetes::REST::LogEvent->new(line => $buffer));
1193 }
1194 3 return Future->done(undef);
1195 6 });
1196 }
1197  
1198 7 ▶ my $req = $rest->prepare_request('GET', $path,
Branch: 1 branch
BranchTrueFalseTotal
%params ? : 1 6 7
1199 %params ? (parameters => \%params) : (),
1200 );
1201 3 ▶ return $self->_checked_request($req, "log $short_class")->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1202 3 my ($response) = @_;
1203 3 return Future->done($response->content);
1204 7 });
1205 }
1206  
1207  
1208 55 ▶sub port_forward {
Subroutine port_forward: called CC=15 SCAR=27.3
Pod documented
1209 55 my ($self, $short_class, @rest_args) = @_;
1210  
1211 55 my $rest = $self->_rest;
1212  
1213 # Support: port_forward('Pod', 'name', ...) and port_forward('Pod', name => 'name', ...)
1214 55 my ($arg_error, %args) = $self->_named_args('port_forward', \@rest_args,
1215 qw( name namespace ports subprotocol on_open on_frame on_close on_error ));
1216 55 ▶ return Future->fail($arg_error) if defined $arg_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg_error 31 24 55
1217  
1218 24 ▶ return Future->fail("name required for port_forward") unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 2 22 24
1219  
1220 22 my $ports = delete $args{ports};
1221 22 ▶ return Future->fail("ports required for port_forward") unless defined $ports;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ports 1 21 22
1222 21 ▶ $ports = [$ports] unless ref($ports) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ports eq "ARRAY" 0 21 21
1223 21 ▶ return Future->fail("ports required for port_forward") unless @$ports;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ports 0 21 21
1224 21 for my $p (@$ports) {
1225 24 ▶ return Future->fail("invalid port '$p' for port_forward")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p and $p =~ /^\d+$/ and $p > 0 and $p <= 65535 1 23 24
Condition66%6/9
defined $p and $p =~ /^\d+$/ and $p > 0 and $p <= 65535
!ll&&!rl&&r
1023
defined $p and $p =~ /^\d+$/ and $p > 0
!ll&&!rl&&r
1023
defined $p and $p =~ /^\d+$/
!ll&&!rl&&r
0123
MC/DC25%1/4
defined $p and $p =~ /^\d+$/ and $p > 0 and $p <= 65535
defined $p $p =~ /^\d+$/ $p > 0 $p <= 65535
Truth table40%2/5
A and B and C and D
A= defined $pB= $p =~ /^\d+$/C= $p > 0D= $p <= 65535
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1226 unless defined($p) && $p =~ /^\d+$/ && $p > 0 && $p <= 65535;
1227 }
1228  
1229 20 ▶ my $subprotocol = delete $args{subprotocol} // 'v4.channel.k8s.io';
Condition100%2/2
delete $args{"subprotocol"} // "v4.channel.k8s.io"
l!l
317
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
1230 20 my $on_open = delete $args{on_open};
1231 20 my $on_frame = delete $args{on_frame};
1232 20 my $on_close = delete $args{on_close};
1233 20 my $on_error = delete $args{on_error};
1234  
1235 20 my ($class, $error) = $self->_resolve_class($short_class);
1236 20 ▶ return Future->fail($error) unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 10 10 20
1237 10 (my $path, $error) = $self->_request_path($class, $short_class, %args);
1238 10 ▶ return Future->fail($error) unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 2 8 10
1239 8 $path .= '/portforward';
1240  
1241 # Keep compatibility with Kubernetes::REST >= 1.100 by expanding repeated
1242 # ports query params here instead of relying on arrayref parameter support.
1243 8 my $query = join('&', map { "ports=$_" } @$ports);
1244 8 ▶ my $path_with_query = $query ? "$path?$query" : $path;
Branch: 1 branch
BranchTrueFalseTotal
$query ? : 8 0 8
1245  
1246 8 my $req = $rest->prepare_request('GET', $path_with_query,
1247 headers => {
1248 Accept => '*/*',
1249 Connection => 'Upgrade',
1250 Upgrade => 'websocket',
1251 'Sec-WebSocket-Protocol' => $subprotocol,
1252 },
1253 );
1254  
1255 8 return $self->_do_duplex_request($req,
1256 caller => 'port_forward',
1257 on_open => $on_open,
1258 on_frame => $on_frame,
1259 on_close => $on_close,
1260 on_error => $on_error,
1261 );
1262 }
1263  
1264  
1265 87 ▶sub exec {
Subroutine exec: called CC=24 SCAR=31.8
Pod documented
1266 87 my ($self, $short_class, @rest_args) = @_;
1267  
1268 87 my $rest = $self->_rest;
1269  
1270 # Support: exec('Pod', 'name', ...) and exec('Pod', name => 'name', ...)
1271 87 my ($arg_error, %args) = $self->_named_args('exec', \@rest_args, qw( name namespace command
1272 container stdin stdout stderr tty subprotocol on_open on_frame on_close on_error ));
1273 87 ▶ return Future->fail($arg_error) if defined $arg_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg_error 46 41 87
1274  
1275 41 ▶ return Future->fail("name required for exec") unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 2 39 41
1276  
1277 39 my $command = delete $args{command};
1278 39 ▶ return Future->fail("command required for exec") unless defined $command;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command 1 38 39
1279 38 ▶ $command = [$command] unless ref($command) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $command eq "ARRAY" 1 37 38
1280 38 ▶ return Future->fail("command required for exec") unless @$command;
Branch: 1 branch
BranchTrueFalseTotal
unless @$command 1 37 38
1281 37 for my $part (@$command) {
1282 99 ▶ return Future->fail("invalid command element for exec")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part and not ref $part and length $part 1 98 99
Condition66%4/6
defined $part and not ref $part and length $part
!ll&&!rl&&r
1098
defined $part and not ref $part
!ll&&!rl&&r
1098
MC/DC33%1/3
defined $part and not ref $part and length $part
defined $part not ref $part length $part
Truth table50%2/4
A and not(B) and C
A= defined $partB= not ref $partC= length $part
ABCresult
0XX0
10X0
1100
1111
1283 unless defined($part) && !ref($part) && length $part;
1284 }
1285  
1286 36 my $container = delete $args{container};
1287 36 ▶ my $stdin = delete($args{stdin}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $args{'stdin'} ? : 12 24 36
1288 36 ▶ my $stdout = exists($args{stdout}) ? (delete($args{stdout}) ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'stdout'} ? : 20 16 36
delete $args{'stdout'} ? : 10 10 20
1289 36 ▶ my $stderr = exists($args{stderr}) ? (delete($args{stderr}) ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'stderr'} ? : 21 15 36
delete $args{'stderr'} ? : 19 2 21
1290 36 ▶ my $tty = delete($args{tty}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $args{'tty'} ? : 2 34 36
1291  
1292 36 ▶ my $subprotocol = delete $args{subprotocol} // 'v4.channel.k8s.io';
Condition100%2/2
delete $args{"subprotocol"} // "v4.channel.k8s.io"
l!l
234
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
1293 36 my $on_open = delete $args{on_open};
1294 36 my $on_frame = delete $args{on_frame};
1295 36 my $on_close = delete $args{on_close};
1296 36 my $on_error = delete $args{on_error};
1297  
1298 36 my ($class, $error) = $self->_resolve_class($short_class);
1299 36 ▶ return Future->fail($error) unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 26 10 36
1300 10 (my $path, $error) = $self->_request_path($class, $short_class, %args);
1301 10 ▶ return Future->fail($error) unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 2 8 10
1302 8 $path .= '/exec';
1303  
1304 8 ▶ my %params = (
Branch: 4 branches
BranchTrueFalseTotal
$stdin ? : 4 4 8
$stdout ? : 6 2 8
$stderr ? : 6 2 8
$tty ? : 2 6 8
1305 command => $command,
1306 stdin => $stdin ? 'true' : 'false',
1307 stdout => $stdout ? 'true' : 'false',
1308 stderr => $stderr ? 'true' : 'false',
1309 tty => $tty ? 'true' : 'false',
1310 );
1311 8 ▶ $params{container} = $container if defined $container;
Branch: 1 branch
BranchTrueFalseTotal
if defined $container 4 4 8
1312  
1313 8 my $req = $rest->prepare_request('GET', $path,
1314 parameters => \%params,
1315 headers => {
1316 Accept => '*/*',
1317 Connection => 'Upgrade',
1318 Upgrade => 'websocket',
1319 'Sec-WebSocket-Protocol' => $subprotocol,
1320 },
1321 );
1322  
1323 8 return $self->_do_duplex_request($req,
1324 caller => 'exec',
1325 on_open => $on_open,
1326 on_frame => $on_frame,
1327 on_close => $on_close,
1328 on_error => $on_error,
1329 );
1330 }
1331  
1332  
1333 62 ▶sub attach {
Subroutine attach: called CC=17 SCAR=28.4
Pod documented
1334 62 my ($self, $short_class, @rest_args) = @_;
1335  
1336 62 my $rest = $self->_rest;
1337  
1338 # Support: attach('Pod', 'name', ...) and attach('Pod', name => 'name', ...)
1339 62 my ($arg_error, %args) = $self->_named_args('attach', \@rest_args, qw( name namespace
1340 container stdin stdout stderr tty subprotocol on_open on_frame on_close on_error ));
1341 62 ▶ return Future->fail($arg_error) if defined $arg_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg_error 42 20 62
1342  
1343 20 ▶ return Future->fail("name required for attach") unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 2 18 20
1344  
1345 18 my $container = delete $args{container};
1346 18 ▶ my $stdin = delete($args{stdin}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $args{'stdin'} ? : 3 15 18
1347 18 ▶ my $stdout = exists($args{stdout}) ? (delete($args{stdout}) ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'stdout'} ? : 2 16 18
delete $args{'stdout'} ? : 2 0 2
1348 18 ▶ my $stderr = exists($args{stderr}) ? (delete($args{stderr}) ? 1 : 0) : 1;
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'stderr'} ? : 3 15 18
delete $args{'stderr'} ? : 0 3 3
1349 18 ▶ my $tty = delete($args{tty}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $args{'tty'} ? : 2 16 18
1350  
1351 18 ▶ my $subprotocol = delete $args{subprotocol} // 'v4.channel.k8s.io';
Condition100%2/2
delete $args{"subprotocol"} // "v4.channel.k8s.io"
l!l
216
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
1352 18 my $on_open = delete $args{on_open};
1353 18 my $on_frame = delete $args{on_frame};
1354 18 my $on_close = delete $args{on_close};
1355 18 my $on_error = delete $args{on_error};
1356  
1357 18 my ($class, $error) = $self->_resolve_class($short_class);
1358 18 ▶ return Future->fail($error) unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 10 8 18
1359 8 (my $path, $error) = $self->_request_path($class, $short_class, %args);
1360 8 ▶ return Future->fail($error) unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 2 6 8
1361 6 $path .= '/attach';
1362  
1363 6 ▶ my %params = (
Branch: 4 branches
BranchTrueFalseTotal
$stdin ? : 3 3 6
$stdout ? : 6 0 6
$stderr ? : 3 3 6
$tty ? : 2 4 6
1364 stdin => $stdin ? 'true' : 'false',
1365 stdout => $stdout ? 'true' : 'false',
1366 stderr => $stderr ? 'true' : 'false',
1367 tty => $tty ? 'true' : 'false',
1368 );
1369 6 ▶ $params{container} = $container if defined $container;
Branch: 1 branch
BranchTrueFalseTotal
if defined $container 3 3 6
1370  
1371 6 my $req = $rest->prepare_request('GET', $path,
1372 parameters => \%params,
1373 headers => {
1374 Accept => '*/*',
1375 Connection => 'Upgrade',
1376 Upgrade => 'websocket',
1377 'Sec-WebSocket-Protocol' => $subprotocol,
1378 },
1379 );
1380  
1381 6 return $self->_do_duplex_request($req,
1382 caller => 'attach',
1383 on_open => $on_open,
1384 on_frame => $on_frame,
1385 on_close => $on_close,
1386 on_error => $on_error,
1387 );
1388 }
1389  
1390  
1391 37 ▶sub cp_to_pod {
Subroutine cp_to_pod: called CC=17 SCAR=31.1
Pod documented
1392 37 my ($self, $short_class, @rest_args) = @_;
1393  
1394 37 my $loop = eval { $self->loop };
1395 37 ▶ return Future->fail("cp_to_pod requires Net::Async::Kubernetes to be added to an IO::Async::Loop")
Branch: 1 branch
BranchTrueFalseTotal
unless $loop 1 36 37
1396 unless $loop;
1397  
1398 36 my ($arg_error, %args) = $self->_named_args('cp_to_pod', \@rest_args,
1399 qw( name namespace container local remote chunk_size ));
1400 36 ▶ return Future->fail($arg_error) if defined $arg_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg_error 24 12 36
1401  
1402 12 ▶ return Future->fail("name required for cp_to_pod") unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 1 11 12
1403  
1404 11 my $local = delete $args{local};
1405 11 my $remote = delete $args{remote};
1406 11 ▶ return Future->fail("local path required for cp_to_pod") unless defined $local && length $local;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $local and length $local 0 11 11
Condition33%1/3
defined $local and length $local
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $local and length $local
defined $local length $local
Truth table33%1/3
A and B
A= defined $localB= length $local
ABresult
0X0
100
111
1407 11 ▶ return Future->fail("remote path required for cp_to_pod") unless defined $remote && length $remote;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $remote and length $remote 0 11 11
Condition33%1/3
defined $remote and length $remote
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $remote and length $remote
defined $remote length $remote
Truth table33%1/3
A and B
A= defined $remoteB= length $remote
ABresult
0X0
100
111
1408 11 ▶ return Future->fail("local file '$local' does not exist for cp_to_pod") unless -e $local;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $local 0 11 11
1409 11 ▶ return Future->fail("local path '$local' is not a file for cp_to_pod") unless -f $local;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $local 0 11 11
1410  
1411 11 ▶ my $chunk_size = delete($args{chunk_size}) // 64 * 1024;
Condition100%2/2
delete $args{"chunk_size"} // 65536
l!l
29
MC/DC100%1/1
delete $args{"chunk_size"} // 65536
delete $args{"chunk_size"}
Truth table100%2/2
A
A= delete $args{"chunk_size"}
Aresult
11
00
1412 11 ▶ return Future->fail("invalid chunk_size '$chunk_size' for cp_to_pod")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chunk_size and $chunk_size =~ /^\d+$/ and $chunk_size > 0 0 11 11
Condition33%2/6
defined $chunk_size and $chunk_size =~ /^\d+$/ and $chunk_size > 0
!ll&&!rl&&r
0011
defined $chunk_size and $chunk_size =~ /^\d+$/
!ll&&!rl&&r
0011
MC/DC0%0/3
defined $chunk_size and $chunk_size =~ /^\d+$/ and $chunk_size > 0
defined $chunk_size $chunk_size =~ /^\d+$/ $chunk_size > 0
Truth table25%1/4
A and B and C
A= defined $chunk_sizeB= $chunk_size =~ /^\d+$/C= $chunk_size > 0
ABCresult
0XX0
10X0
1100
1111
1413 unless defined($chunk_size) && $chunk_size =~ /^\d+$/ && $chunk_size > 0;
1414  
1415 11 ▶ open my $fh, '<:raw', $local
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $local or return "Future"->fail("cannot read local file '${local}' for cp_to_pod: $!") 11 0 11
1416 or return Future->fail("cannot read local file '$local' for cp_to_pod: $!");
1417 11 local $/ = undef;
1418 11 my $bytes = <$fh>;
1419 11 close $fh;
1420 11 ▶ $bytes = '' unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 11 11
1421  
1422 11 my $size = length($bytes);
1423 11 my $stderr = '';
1424 11 my $status_payload = '';
1425 11 my $done = $loop->new_future;
1426  
1427 return $self->exec($short_class, $args{name},
1428 namespace => $args{namespace},
1429 (defined($args{container}) ? (container => $args{container}) : ()),
1430 command => ['sh', '-c', 'head -c "$1" > "$2"', 'k8s-cp', $size, $remote],
1431 stdin => 1,
1432 stdout => 0,
1433 stderr => 1,
1434 tty => 0,
1435 1 ▶ on_frame => sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
1436 1 my ($channel, $payload) = @_;
1437 1 ▶ $stderr .= $payload if $channel == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $channel == 2 0 1 1
1438 1 ▶ $status_payload .= $payload if $channel == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $channel == 3 1 0 1
1439 },
1440 2 ▶ on_close => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1441 2 ▶ return if $done->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $done->is_ready 0 2 2
1442 2 ▶ if ($status_payload =~ /"status"\s*:\s*"Failure"/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status_payload =~ /"status"\s*:\s*"Failure"/i) { } 1 1 2
1443 1 $done->fail("cp_to_pod failed: $status_payload");
1444 } else {
1445 1 $done->done({
1446 local => $local,
1447 remote => $remote,
1448 bytes => $size,
1449 stderr => $stderr,
1450 status => $status_payload,
1451 });
1452 }
1453 },
1454 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1455 0 my ($err) = @_;
1456 0 ▶ $done->fail("cp_to_pod transport error: $err") unless $done->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $done->is_ready 0 0 0
1457 },
1458 3 ▶ )->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1459 3 my ($session) = @_;
1460 return $self->_send_stdin_chunks($session, $bytes, $chunk_size)
1461 3 ▶ ->then(sub { return $done; });
Subroutine __ANON__: called CC=1 SCAR=0.0
1462 11 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'container'} ? : 1 10 11
1463 }
1464  
1465  
1466 34 ▶sub cp_from_pod {
Subroutine cp_from_pod: called CC=10 SCAR=24.4
Pod documented
1467 34 my ($self, $short_class, @rest_args) = @_;
1468  
1469 34 my $loop = eval { $self->loop };
1470 34 ▶ return Future->fail("cp_from_pod requires Net::Async::Kubernetes to be added to an IO::Async::Loop")
Branch: 1 branch
BranchTrueFalseTotal
unless $loop 1 33 34
1471 unless $loop;
1472  
1473 33 my ($arg_error, %args) = $self->_named_args('cp_from_pod', \@rest_args,
1474 qw( name namespace container local remote ));
1475 33 ▶ return Future->fail($arg_error) if defined $arg_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg_error 20 13 33
1476  
1477 13 ▶ return Future->fail("name required for cp_from_pod") unless $args{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"name"} 2 11 13
1478  
1479 11 my $local = delete $args{local};
1480 11 my $remote = delete $args{remote};
1481 11 ▶ return Future->fail("local path required for cp_from_pod") unless defined $local && length $local;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $local and length $local 0 11 11
Condition33%1/3
defined $local and length $local
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $local and length $local
defined $local length $local
Truth table33%1/3
A and B
A= defined $localB= length $local
ABresult
0X0
100
111
1482 11 ▶ return Future->fail("remote path required for cp_from_pod") unless defined $remote && length $remote;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $remote and length $remote 1 10 11
Condition66%2/3
defined $remote and length $remote
!ll&&!rl&&r
1010
MC/DC50%1/2
defined $remote and length $remote
defined $remote length $remote
Truth table66%2/3
A and B
A= defined $remoteB= length $remote
ABresult
0X0
100
111
1483 10 ▶ return Future->fail("local path '$local' is a directory for cp_from_pod") if -d $local;
Branch: 1 branch
BranchTrueFalseTotal
if -d $local 0 10 10
1484  
1485 10 my $stdout = '';
1486 10 my $stderr = '';
1487 10 my $status_payload = '';
1488 10 my $done = $loop->new_future;
1489  
1490 return $self->exec($short_class, $args{name},
1491 namespace => $args{namespace},
1492 (defined($args{container}) ? (container => $args{container}) : ()),
1493 command => ['cat', $remote],
1494 stdin => 0,
1495 stdout => 1,
1496 stderr => 1,
1497 tty => 0,
1498 3 ▶ on_frame => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1499 3 my ($channel, $payload) = @_;
1500 3 ▶ $stdout .= $payload if $channel == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $channel == 1 2 1 3
1501 3 ▶ $stderr .= $payload if $channel == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $channel == 2 1 2 3
1502 3 ▶ $status_payload .= $payload if $channel == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $channel == 3 0 3 3
1503 },
1504 1 ▶ on_close => sub {
Subroutine __ANON__: called CC=4 SCAR=16.3
1505 1 ▶ return if $done->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $done->is_ready 0 1 1
1506 1 ▶ if ($status_payload =~ /"status"\s*:\s*"Failure"/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status_payload =~ /"status"\s*:\s*"Failure"/i) 0 1 1
1507 0 $done->fail("cp_from_pod failed: $status_payload");
1508 0 return;
1509 }
1510  
1511 open my $fh, '>:raw', $local
1512 1 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $local or do { $done->fail("cannot write local file '${local}' for cp_from_pod: $!"); return } 1 0 1
1513 0 $done->fail("cannot write local file '$local' for cp_from_pod: $!");
1514 0 return;
1515 };
1516 1 print {$fh} $stdout;
1517 1 close $fh;
1518  
1519 1 $done->done({
1520 local => $local,
1521 remote => $remote,
1522 bytes => length($stdout),
1523 stderr => $stderr,
1524 status => $status_payload,
1525 });
1526 },
1527 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1528 0 my ($err) = @_;
1529 0 ▶ $done->fail("cp_from_pod transport error: $err") unless $done->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $done->is_ready 0 0 0
1530 },
1531 10 ▶ )->then(sub { return $done; });
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'container'} ? : 1 9 10
1532 }
1533  
1534  
1535 3 ▶sub _send_stdin_chunks {
Subroutine _send_stdin_chunks: called CC=3 SCAR=11.0
1536 3 my ($self, $session, $bytes, $chunk_size) = @_;
1537  
1538 3 my $f = Future->done;
1539 3 ▶ my $len = length($bytes // '');
Condition50%1/2
$bytes // ""
l!l
30
MC/DC0%0/1
$bytes // ""
$bytes
Truth table50%1/2
A
A= $bytes
Aresult
11
00
1540 3 ▶ for (my $off = 0; $off < $len; $off += $chunk_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off < $len) 7 3 10
1541 7 my $chunk = substr($bytes, $off, $chunk_size);
1542 7 ▶ $f = $f->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1543 7 return $session->write_stdin($chunk);
1544 7 });
1545 }
1546  
1547 3 return $f;
1548 }
1549  
1550 # ============================================================================
1551 # WATCHER FACTORY
1552 # ============================================================================
1553  
1554 95 ▶sub watcher {
Subroutine watcher: called CC=2 SCAR=6.9
Pod documented
1555 95 my ($self, $resource, @args) = @_;
1556  
1557 95 ▶ croak "Invalid arguments to watcher()" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 1 94 95
1558 94 my $watcher = Net::Async::Kubernetes::Watcher->new(
1559 kube => $self,
1560 resource => $resource,
1561 @args,
1562 );
1563  
1564 93 $self->add_child($watcher);
1565 80 return $watcher;
1566 }
1567  
1568  
1569 31 ▶sub controller {
Subroutine controller: called CC=2 SCAR=6.9
Pod documented
1570 31 my ($self, @args) = @_;
1571  
1572 31 ▶ croak "Invalid arguments to controller()" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 1 30 31
1573 30 my $controller = Net::Async::Kubernetes::Controller->new(
1574 kube => $self,
1575 @args,
1576 );
1577  
1578 30 $self->add_child($controller);
1579 30 return $controller;
1580 }
1581  
1582  
1583 # ============================================================================
1584 # HTTP TRANSPORT
1585 # ============================================================================
1586  
1587 8 ▶sub _do_request {
Subroutine _do_request: called CC=3 SCAR=13.3
1588 8 my ($self, $req) = @_;
1589  
1590 8 my $uri = URI->new($req->url);
1591  
1592 8 my @content_args;
1593 8 ▶ if (defined $req->content) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $req->content) 0 8 8
1594 0 ▶ my $ct = $req->headers->{'Content-Type'} // 'application/json';
Condition0%0/2
$req->headers->{"Content-Type"} // "application/json"
l!l
00
MC/DC0%0/1
$req->headers->{"Content-Type"} // "application/json"
$req->headers->{"Content-Type"}
Truth table0%0/2
A
A= $req->headers->{"Content-Type"}
Aresult
11
00
1595 0 @content_args = (content => $req->content, content_type => $ct);
1596 }
1597  
1598 return $self->_http->do_request(
1599 method => $req->method,
1600 uri => $uri,
1601 headers => $req->headers,
1602 @content_args,
1603 $self->_ssl_options,
1604 8 ▶ )->then(sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
1605 8 my ($response) = @_;
1606 8 ▶ return Future->done(Kubernetes::REST::HTTPResponse->new(
Condition40%2/5
($response->decoded_content // $response->content) // ""
l!l
80
$response->decoded_content // $response->content
l!l&&r!l&&!r
800
MC/DC0%0/2
($response->decoded_content // $response->content) // ""
$response->decoded_content $response->content
Truth table33%1/3
A // B
A= $response->decoded_contentB= $response->content
ABresult
1X1
011
000
1607 status => $response->code,
1608 content => $response->decoded_content // $response->content // '',
1609 ));
1610 8 });
1611 }
1612  
1613 # Resolves with the response status and, for an error response (>= 400), its
1614 # body as content. An error body is the Status explaining the rejection, not
1615 # stream data: it never reaches $on_chunk, where it would pass for a watch
1616 # event or a log line, and is kept for the caller's check_response instead.
1617 6 ▶sub _do_streaming_request {
Subroutine _do_streaming_request: called CC=1 SCAR=0.0
1618 6 my ($self, $req, $on_chunk) = @_;
1619  
1620 6 my $uri = URI->new($req->url);
1621 6 my $error_body = '';
1622  
1623 return $self->_http->do_request(
1624 method => $req->method,
1625 uri => $uri,
1626 headers => $req->headers,
1627 6 ▶ on_header => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1628 6 my ($response) = @_;
1629 6 my $is_error = $response->code >= 400;
1630 12 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1631 # Called once more without arguments at the end of the body;
1632 # what it returns is what the request Future resolves with.
1633 12 ▶ return $response unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 6 6 12
1634 6 my ($chunk) = @_;
1635 6 ▶ return unless defined $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chunk 0 6 6
1636 6 ▶ if ($is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_error) { } 3 3 6
1637 3 $error_body .= $chunk;
1638 } else {
1639 3 $on_chunk->($chunk);
1640 }
1641 6 return;
1642 6 };
1643 },
1644 $self->_ssl_options,
1645 6 ▶ )->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1646 6 my ($response) = @_;
1647 6 return Future->done(Kubernetes::REST::HTTPResponse->new(
1648 status => $response->code,
1649 content => $error_body,
1650 ));
1651 6 });
1652 }
1653  
1654 9 ▶sub _do_duplex_request {
Subroutine _do_duplex_request: called CC=3 SCAR=13.0
1655 9 my ($self, $req, %callbacks) = @_;
1656 9 ▶ my $caller_name = delete($callbacks{caller}) // 'duplex request';
Condition50%1/2
delete $callbacks{"caller"} // "duplex request"
l!l
90
MC/DC0%0/1
delete $callbacks{"caller"} // "duplex request"
delete $callbacks{"caller"}
Truth table50%1/2
A
A= delete $callbacks{"caller"}
Aresult
11
00
1657 9 my $loop = eval { $self->loop };
1658 9 ▶ return Future->fail("$caller_name requires Net::Async::Kubernetes to be added to an IO::Async::Loop")
Branch: 1 branch
BranchTrueFalseTotal
unless $loop 4 5 9
1659 unless $loop;
1660  
1661 5 my $on_open = $callbacks{on_open};
1662 5 my $on_frame = $callbacks{on_frame};
1663 5 my $on_close = $callbacks{on_close};
1664 5 my $on_error = $callbacks{on_error};
1665  
1666 5 my $ws_url = $self->_build_websocket_url($req->url);
1667 5 my $ws_req = $self->_build_websocket_request($req);
1668  
1669 5 my $client;
1670 my $session;
1671 5 my $close_notified = 0;
1672  
1673 1 ▶ my $detach_client = sub {
Subroutine __ANON__: called CC=5 SCAR=18.6
1674 1 ▶ return unless $client;
Branch: 1 branch
BranchTrueFalseTotal
unless $client 0 1 1
1675 1 ▶ return unless $client->can('parent');
Branch: 1 branch
BranchTrueFalseTotal
unless $client->can("parent") 0 1 1
1676 1 ▶ return unless $client->parent && $client->parent == $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $client->parent and $client->parent == $self 0 1 1
Condition33%1/3
$client->parent and $client->parent == $self
!ll&&!rl&&r
001
MC/DC0%0/2
$client->parent and $client->parent == $self
$client->parent $client->parent == $self
Truth table33%1/3
A and B
A= $client->parentB= $client->parent == $self
ABresult
0X0
100
111
1677 1 $self->remove_child($client);
1678 5 };
1679  
1680 2 ▶ my $notify_error = sub {
Subroutine __ANON__: called CC=3 SCAR=11.6
1681 2 my ($err) = @_;
1682 2 ▶ return unless ref($on_error) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $on_error eq "CODE" 0 2 2
1683 2 my $ok = eval { $on_error->($err); 1 };
1684 2 ▶ return if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 2 0 2
1685 0 warn $@;
1686 5 };
1687  
1688 0 ▶ my $notify_close = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1689 0 ▶ return if $close_notified++;
Branch: 1 branch
BranchTrueFalseTotal
if $close_notified++ 0 0 0
1690 0 ▶ if (ref($on_close) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $on_close eq "CODE") 0 0 0
1691 0 my $ok = eval { $on_close->(@_); 1 };
1692 0 ▶ $notify_error->($@) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
1693 }
1694 0 $detach_client->();
1695 5 };
1696  
1697 1 ▶ my $dispatch_frame = sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
1698 1 my ($bytes) = @_;
1699 1 ▶ return unless ref($on_frame) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $on_frame eq "CODE" 0 1 1
1700 1 ▶ return unless defined $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 1 1
1701 1 ▶ return unless length $bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless length $bytes 0 1 1
1702  
1703 1 my $channel = ord(substr($bytes, 0, 1));
1704 1 my $payload = substr($bytes, 1);
1705 1 my $ok = eval { $on_frame->($channel, $payload); 1 };
1706 1 ▶ $notify_error->($@) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 1 1
1707 5 };
1708  
1709 $client = $self->_make_websocket_client(
1710 1 ▶ on_binary_frame => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1711 1 my (undef, $bytes) = @_;
1712 1 $dispatch_frame->($bytes);
1713 },
1714 0 ▶ on_text_frame => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1715 0 my (undef, $text) = @_;
1716 0 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
1717 0 my $bytes = $text;
1718 0 ▶ utf8::encode($bytes) if utf8::is_utf8($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($bytes) 0 0 0
1719 0 $dispatch_frame->($bytes);
1720 },
1721 0 ▶ on_close_frame => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1722 0 my (undef, $payload) = @_;
1723 0 $notify_close->($payload);
1724 },
1725 1 ▶ on_read_error => sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
1726 1 my (undef, $errno, $msg) = @_;
1727 1 ▶ my $err = defined $msg && length $msg ? $msg : ($errno // 'websocket read error');
Branch: 1 branch
BranchTrueFalseTotal
defined $msg && length $msg ? : 1 0 1
Condition20%1/5
defined $msg and length $msg
!ll&&!rl&&r
001
$errno // "websocket read error"
l!l
00
MC/DC0%0/2
defined $msg and length $msg
defined $msg length $msg
MC/DC0%0/1
$errno // "websocket read error"
$errno
Truth table33%1/3
A and B
A= defined $msgB= length $msg
ABresult
0X0
100
111
Truth table0%0/2
A
A= $errno
Aresult
11
00
1728 1 $notify_error->($err);
1729 },
1730 0 ▶ on_write_error => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1731 0 my (undef, $errno, $msg) = @_;
1732 0 ▶ my $err = defined $msg && length $msg ? $msg : ($errno // 'websocket write error');
Branch: 1 branch
BranchTrueFalseTotal
defined $msg && length $msg ? : 0 0 0
Condition0%0/5
defined $msg and length $msg
!ll&&!rl&&r
000
$errno // "websocket write error"
l!l
00
MC/DC0%0/2
defined $msg and length $msg
defined $msg length $msg
MC/DC0%0/1
$errno // "websocket write error"
$errno
Truth table0%0/3
A and B
A= defined $msgB= length $msg
ABresult
0X0
100
111
Truth table0%0/2
A
A= $errno
Aresult
11
00
1733 0 $notify_error->($err);
1734 },
1735 0 ▶ on_closed => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1736 0 $notify_close->();
1737 },
1738 5 );
1739  
1740 5 $self->add_child($client);
1741  
1742 return $client->connect(
1743 url => $ws_url,
1744 req => $ws_req,
1745 $self->_ssl_options,
1746 4 ▶ )->then(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1747 4 $session = Net::Async::Kubernetes::PortForwardSession->new(
1748 ws_client => $client,
1749 );
1750  
1751 4 ▶ if (ref($on_open) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $on_open eq "CODE") 1 3 4
1752 1 my $ok = eval { $on_open->($session); 1 };
1753 1 ▶ $notify_error->($@) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 1 1
1754 }
1755  
1756 4 return Future->done($session);
1757 1 ▶ })->else(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1758 1 my ($error) = @_;
1759 1 $notify_error->($error);
1760 1 $detach_client->();
1761 1 return Future->fail($error);
1762 5 });
1763 }
1764  
1765 5 ▶sub _build_websocket_url {
Subroutine _build_websocket_url: called CC=1 SCAR=0.0
1766 5 my ($self, $url) = @_;
1767 5 $url =~ s/^https:/wss:/i;
1768 5 $url =~ s/^http:/ws:/i;
1769 5 return $url;
1770 }
1771  
1772 5 ▶sub _build_websocket_request {
Subroutine _build_websocket_request: called CC=11 SCAR=24.5
1773 5 my ($self, $req) = @_;
1774 5 ▶ my $headers = $req->headers || {};
Condition50%1/2
$req->headers || {}
l!l
50
MC/DC0%0/1
$req->headers || {}
$req->headers
Truth table50%1/2
A
A= $req->headers
Aresult
11
00
1775  
1776 5 my @extra_headers;
1777 my $subprotocol;
1778  
1779 5 for my $name (keys %$headers) {
1780 29 my $value = $headers->{$name};
1781 29 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 29 29
1782  
1783 29 my $lc = lc($name);
1784 29 ▶ if ($lc eq 'sec-websocket-protocol') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lc eq "sec-websocket-protocol") 5 24 29
1785 5 $subprotocol = $value;
1786 5 next;
1787 }
1788 24 ▶ next if $lc eq 'connection';
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "connection" 5 19 24
1789 19 ▶ next if $lc eq 'upgrade';
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "upgrade" 5 14 19
1790 14 ▶ next if $lc eq 'host';
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "host" 0 14 14
1791 14 ▶ next if $lc eq 'sec-websocket-key';
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "sec-websocket-key" 0 14 14
1792 14 ▶ next if $lc eq 'sec-websocket-version';
Branch: 1 branch
BranchTrueFalseTotal
if $lc eq "sec-websocket-version" 0 14 14
1793  
1794 14 push @extra_headers, $name, $value;
1795 }
1796  
1797 5 ▶ return Protocol::WebSocket::Request->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $subprotocol ? : 5 0 5
1798 headers => \@extra_headers,
1799 (defined $subprotocol ? (subprotocol => $subprotocol) : ()),
1800 );
1801 }
1802  
1803 0 ▶sub _make_websocket_client {
Subroutine _make_websocket_client: not called CC=1 SCAR=6.9
1804 0 my ($self, %args) = @_;
1805 0 require Net::Async::WebSocket::Client;
1806 0 return Net::Async::WebSocket::Client->new(%args);
1807 }
1808  
1809 48 1;
1810  
1811 __END__
1812  
1813 =pod
1814  
1815 =encoding UTF-8
1816  
1817 =head1 NAME
1818  
1819 Net::Async::Kubernetes - Async Kubernetes client for IO::Async
1820  
1821 =head1 VERSION
1822  
1823 version 0.009
1824  
1825 =head1 SYNOPSIS
1826  
1827 use IO::Async::Loop;
1828 use Net::Async::Kubernetes;
1829  
1830 my $loop = IO::Async::Loop->new;
1831  
1832 # From kubeconfig (easiest)
1833 my $kube = Net::Async::Kubernetes->new(
1834 kubeconfig => "$ENV{HOME}/.kube/config",
1835 );
1836 $loop->add($kube);
1837  
1838 # In-cluster: auto-detects service account token (no config needed)
1839 my $kube = Net::Async::Kubernetes->new;
1840 $loop->add($kube);
1841  
1842 # Or with explicit server/credentials
1843 my $kube = Net::Async::Kubernetes->new(
1844 server => { endpoint => 'https://kubernetes.local:6443' },
1845 credentials => { token => $token },
1846 );
1847 $loop->add($kube);
1848  
1849 # Future-based CRUD
1850 my $pods = $kube->list('Pod', namespace => 'default')->get;
1851  
1852 my $pod = $kube->get('Pod', 'nginx', namespace => 'default')->get;
1853  
1854 my $patched = $kube->patch('Pod', 'nginx',
1855 namespace => 'default',
1856 patch => { metadata => { labels => { env => 'staging' } } },
1857 )->get;
1858  
1859 $kube->delete('Pod', 'nginx', namespace => 'default')->get;
1860  
1861 # Pod logs (one-shot)
1862 my $text = $kube->log('Pod', 'nginx',
1863 namespace => 'default',
1864 tailLines => 100,
1865 )->get;
1866  
1867 # Pod logs (streaming)
1868 $kube->log('Pod', 'nginx',
1869 namespace => 'default',
1870 follow => 1,
1871 on_line => sub { my ($event) = @_; say $event->line },
1872 )->get;
1873  
1874 # Port-forward (built-in websocket duplex support)
1875 my $pf = $kube->port_forward('Pod', 'nginx',
1876 namespace => 'default',
1877 ports => [8080],
1878 on_frame => sub { my ($channel, $payload) = @_; ... },
1879 )->get;
1880  
1881 $pf->write_channel(0, "GET / HTTP/1.1\r\n\r\n");
1882 $pf->close(code => 1000);
1883  
1884 # Pod exec (websocket duplex)
1885 my $exec = $kube->exec('Pod', 'nginx',
1886 namespace => 'default',
1887 command => ['sh', '-c', 'id'],
1888 on_frame => sub { my ($channel, $payload) = @_; ... },
1889 )->get;
1890 $exec->write_stdin("id\n");
1891 $exec->resize(width => 120, height => 40);
1892  
1893 # Pod attach (websocket duplex)
1894 my $attach = $kube->attach('Pod', 'nginx',
1895 namespace => 'default',
1896 container => 'app',
1897 stdin => 1,
1898 stdout => 1,
1899 stderr => 1,
1900 tty => 0,
1901 on_frame => sub { my ($channel, $payload) = @_; ... },
1902 )->get;
1903 $attach->write_stdin("help\n");
1904  
1905 # Copy local file to pod and back (built on exec)
1906 $kube->cp_to_pod('Pod', 'nginx',
1907 namespace => 'default',
1908 local => '/tmp/local.txt',
1909 remote => '/tmp/remote.txt',
1910 )->get;
1911 $kube->cp_from_pod('Pod', 'nginx',
1912 namespace => 'default',
1913 remote => '/tmp/remote.txt',
1914 local => '/tmp/downloaded.txt',
1915 )->get;
1916  
1917 # Watcher with auto-reconnect
1918 my $watcher = $kube->watcher('Pod',
1919 namespace => 'default',
1920 on_added => sub { my ($pod) = @_; say "Added: " . $pod->metadata->name },
1921 on_modified => sub { my ($pod) = @_; say "Modified: " . $pod->metadata->name },
1922 on_deleted => sub { my ($pod) = @_; say "Deleted: " . $pod->metadata->name },
1923 );
1924  
1925 $loop->run;
1926  
1927 =head1 DESCRIPTION
1928  
1929 C<Net::Async::Kubernetes> is an async Kubernetes client built on L<IO::Async>.
1930 It extends L<IO::Async::Notifier> and uses L<Net::Async::HTTP> for
1931 non-blocking HTTP communication, plus L<Net::Async::WebSocket::Client> for
1932 duplex subresources like pod port-forward.
1933  
1934 All CRUD, log, port-forward, exec, attach, and cp helper methods return L<Future> objects. The
1935 L<Net::Async::Kubernetes::Watcher>
1936 provides auto-reconnecting event streaming with separate callbacks per
1937 event type.
1938  
1939 Request preparation and response processing are delegated to
1940 L<Kubernetes::REST>, so the same IO::K8s object inflation, short class
1941 names, and CRD support are available.
1942  
1943 Authentication is automatically resolved in the following order:
1944  
1945 =over 4
1946  
1947 =item 1. Explicit C<server> and C<credentials> parameters
1948  
1949 =item 2. C<kubeconfig> file (via L<Kubernetes::REST::Kubeconfig>)
1950  
1951 =item 3. In-cluster service account token at
1952 C</var/run/secrets/kubernetes.io/serviceaccount/token> (automatic when
1953 running inside a Kubernetes pod)
1954  
1955 =back
1956  
1957 =head2 configure
1958  
1959 Internal L<IO::Async::Notifier> configuration method. Handles initialization
1960 of C<kubeconfig>, C<context>, C<server>, C<credentials>, C<resource_map>,
1961 C<resource_map_from_cluster> and C<with> parameters.
1962  
1963 If C<kubeconfig> is provided without explicit C<server> or C<credentials>,
1964 they are loaded automatically via L<Kubernetes::REST::Kubeconfig>. So are
1965 they for a C<context> without C<kubeconfig>, from the default kubeconfig.
1966 Either way a kubeconfig or context that cannot be resolved croaks with the
1967 reason (a missing file, C<Context not found: ...>).
1968  
1969 When running inside a Kubernetes pod (no C<kubeconfig> or C<server> set),
1970 the service account token at
1971 C</var/run/secrets/kubernetes.io/serviceaccount/token> is used
1972 automatically for in-cluster authentication.
1973  
1974 =head2 kubeconfig
1975  
1976 Path to kubeconfig file. If provided, C<server> and C<credentials> are
1977 extracted automatically (via L<Kubernetes::REST::Kubeconfig>).
1978  
1979 =head2 context
1980  
1981 Kubernetes context to use from the kubeconfig. Defaults to current-context.
1982 Without L</kubeconfig> it is looked up in the default kubeconfig
1983 (C<KUBECONFIG>, else F<~/.kube/config>); when it is not found there, or
1984 there is no kubeconfig at all and no in-cluster service account either, the
1985 constructor croaks with the reason.
1986  
1987 =head2 resource_map
1988  
1989 Optional. Custom resource map for short class names.
1990  
1991 =head2 resource_map_from_cluster
1992  
1993 Optional boolean, defaults to false. When true, L<Kubernetes::REST> reads the
1994 cluster's discovery documents (C<GET /api>, C<GET /apis>) and, unless a
1995 L</resource_map> is given, builds the resource map from them. Left to itself
1996 it fetches them once, on first use, through its own synchronous HTTP backend,
1997 not through this client's transport, so that first use blocks the loop.
1998 Await L</discover> once at start-up to have them read through this client's
1999 transport instead:
2000  
2001 my $kube = Net::Async::Kubernetes->new(
2002 kubeconfig => "$ENV{HOME}/.kube/config",
2003 resource_map_from_cluster => 1,
2004 );
2005 $loop->add($kube);
2006 $kube->discover->get;
2007  
2008 It also makes custom resources usable without a class of their own: a Kind
2009 that no IO::K8s class or C<resource_map> entry serves, but which discovery
2010 lists, resolves to L<IO::K8s::Unstructured>. Requests for it take the
2011 resource's plural and scope from discovery, and a qualified
2012 C<'group/version/Kind'> name stays in that group and version. An
2013 L<IO::K8s::Unstructured> object handed to L</create>, L</update>,
2014 L</ensure> or another object form is addressed the same way, by its own
2015 C<kind> and C<apiVersion> -- without this option there is no discovery to
2016 find its path in.
2017  
2018 That group and version are the only ones such a request goes to. When the
2019 cluster does not serve them, a qualified name is an unknown resource and an
2020 object or manifest with that C<apiVersion> is refused, even if another group
2021 or another version serves a Kind of the same name -- nothing is listed,
2022 changed or deleted there instead.
2023  
2024 A request for L<IO::K8s::Unstructured> that has no path -- without this
2025 option, for the explicit class name C<IO::K8s::Unstructured> (a class name
2026 carries no Kind), or for an object without C<kind> -- is refused before it
2027 is sent, like any other bad argument: the L<Future> fails with the reason,
2028 and L</update>, L</update_status>, L</ensure> and a watcher starting up
2029 croak with it instead, as they do for their other argument errors.
2030  
2031 =head2 with
2032  
2033 Optional arrayref of L<IO::K8s> resource-map providers (CRD bundles),
2034 passed on to L<Kubernetes::REST/with>, so their Kinds resolve to typed
2035 classes -- in names, lists, watches and L</new_object>:
2036  
2037 my $kube = Net::Async::Kubernetes->new(
2038 kubeconfig => "$ENV{HOME}/.kube/config",
2039 with => ['IO::K8s::GatewayAPI'],
2040 );
2041 my $gateways = $kube->list('Gateway', namespace => 'default')->get;
2042  
2043 Defaults to C<[]>. See L<IO::K8s/with> for the accepted provider forms.
2044  
2045 =head2 server
2046  
2047 Returns the L<Kubernetes::REST::Server> instance. Croaks if neither C<server>
2048 nor C<kubeconfig> was provided during initialization.
2049  
2050 =head2 credentials
2051  
2052 Returns the credentials object (typically L<Kubernetes::REST::AuthToken>).
2053 Croaks if neither C<credentials> nor C<kubeconfig> was provided during
2054 initialization.
2055  
2056 =head2 rest
2057  
2058 my $rest = $kube->rest;
2059  
2060 Returns the underlying lazily-built L<Kubernetes::REST> instance used for
2061 request building and response processing. Exposed for advanced use -- most
2062 callers want the higher-level CRUD methods instead. The private C<_rest>
2063 accessor used throughout the internals returns this same cached instance.
2064  
2065 =head2 new_object
2066  
2067 my $cm = $kube->new_object(ConfigMap =>
2068 metadata => { name => 'my-config' },
2069 data => { key => 'value' },
2070 );
2071  
2072 Builds a typed L<IO::K8s> object from a short class name (e.g. C<'Pod'>,
2073 C<'ConfigMap'>) and either a hashref or a hash of attributes. Delegates to
2074 L<Kubernetes::REST/new_object>. This is the public path for constructing the
2075 objects passed to C<create> and C<update>.
2076  
2077 =head2 expand_class
2078  
2079 my $full_class = $kube->expand_class('Pod');
2080 # Returns 'IO::K8s::Api::Core::V1::Pod'
2081  
2082 Expands a short resource name (e.g., C<'Pod'>, C<'Deployment'>) to its full
2083 IO::K8s class name. Delegates to L<Kubernetes::REST/expand_class>.
2084  
2085 The name may also be qualified as C<'group/version/Kind'>, which resolves to
2086 that exact API version instead of the historical default the bare Kind name
2087 carries -- the two forms can and do point at different classes:
2088  
2089 $kube->expand_class('HorizontalPodAutoscaler');
2090 # 'IO::K8s::Api::Autoscaling::V2::HorizontalPodAutoscaler' (bare-name default)
2091  
2092 $kube->expand_class('autoscaling/v1/HorizontalPodAutoscaler');
2093 # 'IO::K8s::Api::Autoscaling::V1::HorizontalPodAutoscaler' (pinned to v1)
2094  
2095 This qualified form is accepted anywhere a resource name is, including
2096 C<list>, C<get>, and C<watcher>.
2097  
2098 The result is a plain class name, without a C<+>. Handed back to a method
2099 that resolves names again -- L</new_object>, L<IO::K8s/struct_to_object> --
2100 a single-segment class of your own (C<'+Gizmo'> in the L</resource_map>,
2101 returned as C<'Gizmo'>) reads as the Kind C<Gizmo> there. Prefix it with
2102 C<+> when you do that yourself. The client's own methods already do, for the
2103 manifests L</ensure> resolves and for every answer they inflate, a watch
2104 event included -- whichever L<Kubernetes::REST> version is installed.
2105  
2106 Croaks when the name cannot be resolved to an IO::K8s class -- a qualified
2107 name no class serves, and equally a bare Kind no class ships for (C<'Bogus'>),
2108 which L<Kubernetes::REST/expand_class> would hand back as a fabricated
2109 C<IO::K8s::Bogus>. It also croaks when the name resolves to a class that
2110 cannot serve as a resource, with the real cause rather than "unknown
2111 resource": a class that does not load or compile (say, a typo in a
2112 C<'+Class'> entry of L</resource_map>) croaks with its load error, and a
2113 class without an C<api_version> of its own -- an IO::K8s helper such as
2114 C<IO::K8s::List> that a bare C<'List'> lands on -- croaks as not being a
2115 Kubernetes resource class. This is the synchronous counterpart of the
2116 C<Future>-returning methods below, which report the same conditions as a
2117 failed L<Future> with the same message.
2118  
2119 =head2 discover
2120  
2121 $kube->discover->get; # once, at start-up
2122  
2123 Reads the cluster's discovery documents (C<GET /api>, C<GET /apis>) through
2124 this client's own asynchronous transport and hands them to
2125 L<Kubernetes::REST>, which then resolves names -- a Kind only the cluster
2126 serves included (see L</resource_map_from_cluster>) -- and builds its
2127 resource map from them without a request of its own. Returns a L<Future>
2128 that resolves, with no value, once that is done.
2129  
2130 With L</resource_map_from_cluster> on, await it once at start-up, before the
2131 first request: otherwise L<Kubernetes::REST> reads discovery itself on first
2132 use, through its synchronous HTTP backend, and that first use blocks the
2133 loop. No other method calls C<discover> for you. Calling it again reads
2134 discovery anew and replaces what was read before -- after installing a
2135 CustomResourceDefinition, for example.
2136  
2137 =over 4
2138  
2139 =item * Without L</resource_map_from_cluster> there is no discovery to read:
2140 the L<Future> is done at once and nothing is sent.
2141  
2142 =item * A cluster older than Kubernetes 1.27 answers with legacy discovery,
2143 which takes a further request per API group and version.
2144 L<Kubernetes::REST> keeps nothing of it; the L<Future> is done all the same,
2145 and discovery is read synchronously on first use, as without C<discover>.
2146  
2147 =item * An error status (C<401>, C<403>, C<5xx>) fails the L<Future> as
2148 described in L</ERRORS>, C<< ->fail($error, 'http', $response) >>. A request
2149 that gets no response fails it the way the transport reports it, and a
2150 document that cannot be read (not JSON) with the reason.
2151  
2152 =back
2153  
2154 =head2 list
2155  
2156 my $future = $kube->list('Pod', namespace => 'default');
2157 my $list = $future->get;
2158 my @pods = @{ $list->items };
2159  
2160 my $future = $kube->list('Pod', labelSelector => 'app=web');
2161  
2162 List resources of the given type. Returns a L<Future> that resolves to an
2163 L<IO::K8s::List>. Its C<items> accessor holds the ArrayRef of inflated
2164 IO::K8s objects.
2165  
2166 C<labelSelector> and C<fieldSelector> are sent as query parameters, so
2167 filtering happens server-side rather than on the list that comes back.
2168 Any other option -- a misspelt C<labelselector> would otherwise list every
2169 object -- fails the L<Future> before a request is sent, naming the options
2170 C<list> takes. So does an odd list of options, as C<Invalid arguments to
2171 list()>: its last key would otherwise go out without a value.
2172  
2173 Arguments:
2174  
2175 =over 4
2176  
2177 =item C<$short_class> - Resource type (e.g., C<'Pod'>, C<'Deployment'>), or a
2178 qualified C<'group/version/Kind'> name to pin a specific API version -- see
2179 L</expand_class>
2180  
2181 =item C<%args> - Optional parameters: C<namespace>, C<labelSelector>,
2182 C<fieldSelector>
2183  
2184 =back
2185  
2186 =head2 get
2187  
2188 my $future = $kube->get('Pod', 'nginx', namespace => 'default');
2189 my $pod = $future->get;
2190  
2191 Get a single resource by name. Returns a L<Future> that resolves to an
2192 inflated IO::K8s object. An option other than C<name> and C<namespace> fails
2193 the L<Future> before a request is sent, and so does an odd list of options,
2194 as C<Invalid arguments to get()>. A reference in place of the name -- a
2195 manifest hashref, an IO::K8s object -- fails it as C<resource name must be a
2196 string, got a HASH reference>, before it is stringified into the request path.
2197  
2198 Arguments:
2199  
2200 =over 4
2201  
2202 =item C<$short_class> - Resource type (e.g., C<'Pod'>), or a qualified
2203 C<'group/version/Kind'> name -- see L</expand_class>
2204  
2205 =item C<$name> - Resource name (required)
2206  
2207 =item C<%args> - Optional parameters: C<namespace>
2208  
2209 =back
2210  
2211 =head2 create
2212  
2213 my $future = $kube->create($pod_object);
2214 my $created = $future->get;
2215  
2216 Create a resource from an IO::K8s object. Returns a L<Future> that resolves
2217 to the created object with server-populated fields (C<resourceVersion>, etc.).
2218  
2219 An object that is no Kubernetes resource -- an L<IO::K8s::List>, a nested
2220 type such as a C<PodSpec> -- or anything that is not an IO::K8s object fails
2221 the L<Future> before a request is sent.
2222  
2223 Arguments:
2224  
2225 =over 4
2226  
2227 =item C<$object> - IO::K8s object instance (e.g., C<IO::K8s::Api::Core::V1::Pod>)
2228  
2229 =back
2230  
2231 =head2 update
2232  
2233 my $future = $kube->update($modified_pod);
2234 my $updated = $future->get;
2235  
2236 Update an existing resource. The object must have C<metadata.name> (and
2237 C<metadata.namespace> if namespaced). Returns a L<Future> that resolves to
2238 the updated object.
2239  
2240 A missing C<metadata> or C<metadata.name> croaks synchronously, and so does
2241 an object that is no Kubernetes resource -- an L<IO::K8s::List>, a nested
2242 type such as a C<PodSpec> -- or anything that is not an IO::K8s object.
2243  
2244 Arguments:
2245  
2246 =over 4
2247  
2248 =item C<$object> - Modified IO::K8s object with updated fields
2249  
2250 =back
2251  
2252 =head2 update_status
2253  
2254 my $future = $kube->update_status($node);
2255 my $updated = $future->get;
2256  
2257 Replace a resource's B<status> through the C</status> subresource. The whole
2258 object is sent, as with L</update>, but the server only stores the C<status>
2259 it carries and leaves C<spec> and C<metadata> untouched. Returns a L<Future>
2260 that resolves to the updated object.
2261  
2262 Needs a current C<resourceVersion> and fails with a 409 conflict if the
2263 object changed on the server in the meantime. A missing C<metadata> or
2264 C<metadata.name>, or an object that is no Kubernetes resource, croaks
2265 synchronously, as with L</update>. Prefer
2266 L</patch_status> when you are setting individual status fields.
2267  
2268 Arguments:
2269  
2270 =over 4
2271  
2272 =item C<$object> - IO::K8s object with C<metadata.name> (and C<namespace> if
2273 namespaced) and the desired C<status>
2274  
2275 =back
2276  
2277 =head2 patch
2278  
2279 # By class and name
2280 my $future = $kube->patch('Pod', 'nginx',
2281 namespace => 'default',
2282 patch => { metadata => { labels => { env => 'prod' } } },
2283 type => 'strategic', # or 'merge', 'json'
2284 );
2285  
2286 # Or by object
2287 my $future = $kube->patch($pod_object,
2288 patch => { spec => { replicas => 3 } },
2289 );
2290  
2291 Patch an existing resource. Returns a L<Future> that resolves to the patched
2292 object. Bad arguments -- among them an object that is no Kubernetes
2293 resource, such as an L<IO::K8s::List> or a nested C<PodSpec>, a plain
2294 reference such as a manifest hashref in place of the resource name, an odd
2295 list of options after the object or the name (C<Invalid arguments to
2296 patch()>), and an option not listed below (in the object form C<name> and
2297 C<namespace> come from the object and are refused as options too) -- fail
2298 the L<Future> before a request is sent.
2299  
2300 Arguments:
2301  
2302 =over 4
2303  
2304 =item C<$class_or_object> - Resource class name or IO::K8s object
2305  
2306 =item C<name> - Resource name (required unless passing object)
2307  
2308 =item C<namespace> - Namespace (if namespaced)
2309  
2310 =item C<patch> - HashRef of changes to apply (required)
2311  
2312 =item C<type> - Patch type: C<'strategic'> (default), C<'merge'>, or C<'json'>
2313  
2314 =back
2315  
2316 =head2 patch_status
2317  
2318 # By class and name
2319 my $future = $kube->patch_status('OCPNode', 'cp-1',
2320 namespace => 'ocp',
2321 patch => { status => { phase => 'Ready' } },
2322 );
2323  
2324 # Or by object
2325 my $future = $kube->patch_status($node,
2326 patch => { status => { phase => 'Ready' } },
2327 );
2328  
2329 Partially update a resource's B<status> through the C</status> subresource.
2330 Once a CustomResourceDefinition declares C<subresources: { status: {} }>, the
2331 API server drops the C<status> stanza from every write to the main endpoint
2332 and still answers 2xx -- so a C<status> written via L</patch> or L</update>
2333 is silently discarded. This method writes to C</status> instead.
2334  
2335 Takes the same call forms and arguments as L</patch> (object, or class plus
2336 name in either the shorthand or fully-keyed form), and refuses any other
2337 option, and an odd list of options, the same way. The patch document is
2338 sent unchanged and carries its own C<status> key.
2339  
2340 The default patch type is C<merge>, not C<strategic> as in L</patch>: custom
2341 resources reject strategic merge patch with a 415, and C<merge> works for
2342 built-in kinds too. Pass C<type =E<gt> 'strategic'> explicitly to patch the
2343 status of a built-in resource when you need array merge semantics.
2344  
2345 Returns a L<Future> that resolves to the patched object; bad arguments, an
2346 unknown patch C<type>, or a server error fail the Future, as with L</patch>.
2347  
2348 Arguments:
2349  
2350 =over 4
2351  
2352 =item C<$class_or_object> - Resource class name or IO::K8s object
2353  
2354 =item C<name> - Resource name (required unless passing object)
2355  
2356 =item C<namespace> - Namespace (if namespaced)
2357  
2358 =item C<patch> - HashRef with a C<status> key (or ArrayRef of operations when
2359 C<type> is C<json>)
2360  
2361 =item C<type> - Patch type: C<'merge'> (default), C<'strategic'>, or C<'json'>
2362  
2363 =back
2364  
2365 =head2 delete
2366  
2367 # By class and name
2368 my $future = $kube->delete('Pod', 'nginx', namespace => 'default');
2369 $future->get;
2370  
2371 # Or by object
2372 my $future = $kube->delete($pod_object);
2373 $future->get;
2374  
2375 # A Job together with its Pods
2376 $kube->delete('Job', 'nightly', namespace => 'default',
2377 propagationPolicy => 'Background')->get;
2378 $kube->delete($job, propagationPolicy => 'Foreground')->get;
2379  
2380 Delete a resource. Returns a L<Future> that resolves to C<1> on success.
2381 In the object form, an object without C<metadata.name>, one that is no
2382 Kubernetes resource (an L<IO::K8s::List>, a nested C<PodSpec>), or a
2383 reference that is not an IO::K8s object fails the L<Future>. In the class
2384 form, a reference in place of the name -- C<delete('Pod', $manifest)> --
2385 fails it as C<resource name must be a string, got a HASH reference>, before
2386 it is stringified into the request path.
2387  
2388 C<propagationPolicy> is sent as a query parameter and decides what happens
2389 to the objects the deleted one owns: C<Background> deletes them after it,
2390 C<Foreground> before it, C<Orphan> leaves them. Without it the API server
2391 applies the resource's own default -- for a C<Job> that orphans its Pods.
2392 Any other value, and any option C<delete> does not know (a misspelt
2393 C<propagationPolicy> would otherwise be dropped silently), fails the
2394 L<Future> before a request is sent, worded as L<Kubernetes::REST> words it:
2395 C<Unknown propagationPolicy 'x' for delete() (use: Background, Foreground,
2396 Orphan)>, or C<Unknown argument 'x' to delete() (allowed: ...)> naming the
2397 first unknown option.
2398  
2399 Arguments:
2400  
2401 =over 4
2402  
2403 =item C<$class_or_object> - Resource class name or IO::K8s object
2404  
2405 =item C<$name> - Resource name (required unless passing object)
2406  
2407 =item C<namespace> - Namespace (if namespaced; not in the object form, which
2408 takes it from the object)
2409  
2410 =item C<propagationPolicy> - C<'Background'>, C<'Foreground'> or C<'Orphan'>;
2411 optional
2412  
2413 =back
2414  
2415 =head2 ensure
2416  
2417 my $future = $kube->ensure($pod);
2418 my $obj = $future->get;
2419  
2420 # or from a plain hashref (treated as a Kubernetes manifest):
2421 my $future = $kube->ensure({
2422 apiVersion => 'v1',
2423 kind => 'Secret',
2424 metadata => { name => 'foo', namespace => 'default' },
2425 stringData => { password => 'hunter2' },
2426 });
2427  
2428 Idempotent create-or-update. GETs the object by kind/name/namespace: if it is
2429 missing, creates it; if it exists, updates it at the server's
2430 C<resourceVersion>, which this method writes back into the object passed in.
2431 Returns a L<Future> that resolves to the resulting IO::K8s object.
2432  
2433 Accepts a typed IO::K8s object or a plain hashref; a hashref must carry a
2434 C<kind> field and uses manifest-style camelCase keys (C<stringData>, not
2435 C<string_data>).
2436  
2437 A hashref's C<apiVersion>, when present, selects the class: an
2438 C<autoscaling/v1> HorizontalPodAutoscaler stays C<autoscaling/v1> and goes to
2439 that endpoint, although the bare Kind resolves to C<autoscaling/v2>. A hashref
2440 without C<apiVersion> (or with an empty one) resolves by its Kind alone, as
2441 L</expand_class> does.
2442  
2443 Handles the create/update race: a 409 on update (something else changed the
2444 object between GET and PUT) refetches once and retries the update; a 409 on
2445 create (something else created it between GET and POST) refetches it and
2446 handles it like an object that existed from the start -- updated, with the
2447 same one retry, or, for the two special cases below, returned unchanged or
2448 deleted and recreated.
2449  
2450 Two kinds get special handling because their spec is immutable after
2451 creation: an existing core C<v1> C<PersistentVolumeClaim> is left unchanged,
2452 and an existing C<batch/v1> C<Job> is left unchanged while it is active or has
2453 succeeded, and deleted and recreated otherwise -- deleted with
2454 C<propagationPolicy> C<Background>, so its Pods go with it instead of being
2455 orphaned. Both are recognised by
2456 apiVersion and Kind together -- the object's C<api_version> and C<kind> --
2457 never by the class name. A custom resource that reuses one of these Kind
2458 names in its own group is ensured like any other object, and so is a C<Job>
2459 under any apiVersion other than C<batch/v1>.
2460  
2461 Errors that are known before any request is made -- a hashref without
2462 C<kind>, a value that is neither an object nor a hashref, an object that is
2463 no Kubernetes resource (an L<IO::K8s::List>, a nested type such as a
2464 C<PodSpec>), an object missing C<metadata>/C<metadata.name>, an unknown
2465 C<kind>, a C<kind> whose class does
2466 not load or is no resource class (see L</expand_class>), or an C<apiVersion>
2467 that resolves to no known class (the message names both the Kind and the
2468 C<apiVersion>) -- croak synchronously, as with L</update>. Anything that
2469 goes wrong during the request flow itself fails the Future instead.
2470  
2471 Arguments:
2472  
2473 =over 4
2474  
2475 =item C<$object> - IO::K8s object or hashref manifest (must have C<kind> if a
2476 hashref)
2477  
2478 =back
2479  
2480 =head2 ensure_all
2481  
2482 my $future = $kube->ensure_all(@objects);
2483 my @results = $future->get;
2484  
2485 Batch form of L</ensure>. Applies each object in order, one at a time --
2486 object N+1 is only started once object N has resolved, so a later object may
2487 depend on an earlier one (a Namespace before what lives in it). Returns a
2488 L<Future> that resolves to the list of results in input order.
2489  
2490 If any object fails -- including a croak from L</ensure>, which becomes a
2491 failure here -- the Future fails and no later object is started.
2492 C<ensure_all> itself never croaks synchronously.
2493  
2494 Arguments:
2495  
2496 =over 4
2497  
2498 =item C<@objects> - IO::K8s objects or hashref manifests, as accepted by
2499 L</ensure>
2500  
2501 =back
2502  
2503 =head2 ensure_only
2504  
2505 my $future = $kube->ensure_only(
2506 label => 'app.kubernetes.io/component=queen',
2507 objects => \@objects,
2508 kinds => [qw(Role RoleBinding ClusterRoleBinding)],
2509 namespaces => ['default', 'kube-system', undef],
2510 );
2511 my @applied = $future->get;
2512  
2513 Like L</ensure_all>, but also deletes anything matching the label selector in
2514 the given kinds and namespaces that is not present in C<objects>. Use this
2515 for resources where stale objects must not survive (e.g. RBAC). Croaks
2516 synchronously if C<label> is missing, if C<propagationPolicy> is none of
2517 the values L</delete> accepts, on any option not listed below -- a
2518 C<namespace> meant as C<namespaces> would otherwise prune at cluster scope
2519 only -- and on an odd list of options (C<Invalid arguments to
2520 ensure_only()>), whose last key would otherwise be taken as given without a
2521 value: a stray C<objects> would prune everything carrying the label.
2522  
2523 Hashrefs in C<objects> are resolved as in L</ensure>, all of them before the
2524 first request: one without C<kind> or with an C<apiVersion> no class serves
2525 croaks, and nothing is applied or deleted.
2526  
2527 Applies C<objects> via L</ensure_all>, then for each kind in C<kinds> and
2528 each namespace in C<namespaces>, lists resources of that kind carrying the
2529 label and deletes any that do not match one of the just-applied objects by
2530 API group, Kind, namespace and name. Group and Kind are each object's own --
2531 the group from its C<api_version>, the Kind from its C<kind> -- so a
2532 qualified C<'group/version/Kind'> entry in C<kinds> still recognises the
2533 objects it lists rather than deleting them. The same Kind name in another
2534 group is another resource: with Istio's C<networking.istio.io> Gateway in
2535 C<objects>, a labelled Gateway API C<gateway.networking.k8s.io> Gateway of
2536 the same name and namespace is deleted. The version is not compared: an
2537 object applied as C<autoscaling/v1> is kept when the listing goes through
2538 C<autoscaling/v2>. A C<namespaces> entry of C<undef> scans cluster-scoped
2539 resources; if C<namespaces> is omitted, only cluster-scoped resources are
2540 scanned.
2541  
2542 Stale objects are deleted with C<propagationPolicy> C<Background> unless
2543 C<propagationPolicy> says otherwise, so a pruned Job or Deployment takes its
2544 Pods with it; the API server's own default would leave a Job's Pods behind.
2545  
2546 Pruning goes on past a failure, and says so. When a C<kinds> entry cannot be
2547 listed in one namespace -- the API server rejects the request, the request
2548 fails without a response, or the entry resolves to no usable class -- that
2549 combination is skipped with a warning (C<carp>) naming the entry, the
2550 namespace (or cluster scope) and the reason; anything stale there survives
2551 this run. A 404 is silent: the cluster does not serve that Kind, so there is
2552 nothing to prune. A delete that fails warns with the Kind, name, namespace
2553 and reason, and the next object is tried; a 404 there means the object is
2554 already gone and is silent too. A C<$SIG{__WARN__}> handler that dies while
2555 the prune runs fails the returned L<Future> with the warning instead.
2556  
2557 Returns a L<Future> that resolves to the list of applied objects (from
2558 L</ensure_all>), whether or not the pruning was complete.
2559  
2560 Arguments:
2561  
2562 =over 4
2563  
2564 =item C<label> - Label selector matching stale objects to delete (required)
2565  
2566 =item C<objects> - ArrayRef of objects/hashrefs to apply, as for L</ensure_all>
2567  
2568 =item C<kinds> - ArrayRef of resource kinds to scan for stale objects
2569  
2570 =item C<namespaces> - ArrayRef of namespaces to scan, C<undef> for
2571 cluster-scoped; defaults to cluster-scoped only
2572  
2573 =item C<propagationPolicy> - How stale objects are deleted: C<'Background'>
2574 (default), C<'Foreground'> or C<'Orphan'>, as for L</delete>
2575  
2576 =back
2577  
2578 =head2 log
2579  
2580 # One-shot mode (Future resolves to full text)
2581 my $text = $kube->log('Pod', 'my-pod',
2582 namespace => 'default',
2583 tailLines => 100,
2584 )->get;
2585  
2586 # Streaming mode (Future resolves when stream ends)
2587 $kube->log('Pod', 'my-pod',
2588 namespace => 'default',
2589 follow => 1,
2590 on_line => sub {
2591 my ($event) = @_; # Kubernetes::REST::LogEvent
2592 say $event->line;
2593 },
2594 )->get;
2595  
2596 Retrieve logs from a pod.
2597  
2598 Without C<on_line>, returns a L<Future> that resolves to the full log text.
2599  
2600 With C<on_line>, opens a streaming request and invokes the callback once per
2601 line with L<Kubernetes::REST::LogEvent> objects. The returned L<Future>
2602 resolves when the stream ends.
2603  
2604 Besides C<name>, C<namespace> and C<on_line> it takes the options
2605 C<container>, C<follow>, C<tailLines>, C<sinceSeconds>, C<sinceTime>,
2606 C<timestamps>, C<previous> and C<limitBytes>, sent as query parameters. Any
2607 other -- C<tail_lines> would otherwise fetch the whole log -- fails the
2608 L<Future> before a request is sent.
2609  
2610 =head2 port_forward
2611  
2612 my $f = $kube->port_forward('Pod', 'my-pod',
2613 namespace => 'default',
2614 ports => [8080, 8443],
2615 on_frame => sub { my ($channel, $payload) = @_; ... },
2616 );
2617 my $session = $f->get;
2618  
2619 Create an async pod port-forward session request.
2620  
2621 Returns a L<Future> that resolves to the duplex session object returned by the
2622 transport backend. The default transport returns a
2623 L<Net::Async::Kubernetes::PortForwardSession> object.
2624  
2625 The session helper supports C<write_channel>, C<write_stdin>, C<resize>, and
2626 C<close>.
2627  
2628 C<on_open> receives the created session object.
2629  
2630 C<on_frame> receives C<($channel, $payload)> where the first byte of each
2631 binary websocket frame is decoded as Kubernetes channel id.
2632  
2633 It takes C<name>, C<namespace>, C<ports> (one port or an arrayref of them,
2634 required), C<subprotocol> (default C<v4.channel.k8s.io>) and the callbacks
2635 C<on_open>, C<on_frame>, C<on_close> and C<on_error>. Any other option -- a
2636 misspelt C<onFrame> would otherwise be dropped, a C<subresource> would even
2637 land in the request path -- fails the L<Future> before a request is sent,
2638 naming the options C<port_forward> takes.
2639  
2640 =head2 exec
2641  
2642 my $f = $kube->exec('Pod', 'my-pod',
2643 namespace => 'default',
2644 command => ['sh', '-c', 'id'],
2645 on_frame => sub { my ($channel, $payload) = @_; ... },
2646 );
2647 my $session = $f->get;
2648  
2649 Create an async pod exec session request.
2650  
2651 Returns a L<Future> that resolves to the duplex session object returned by the
2652 transport backend. The default transport returns a
2653 L<Net::Async::Kubernetes::PortForwardSession> object.
2654  
2655 The session helper supports C<write_channel>, C<write_stdin>, C<resize>, and
2656 C<close>.
2657  
2658 C<on_open> receives the created session object.
2659  
2660 C<on_frame> receives C<($channel, $payload)> where the first byte of each
2661 binary websocket frame is decoded as Kubernetes channel id.
2662  
2663 Besides C<name>, C<namespace>, C<command> (a string or an arrayref,
2664 required), C<subprotocol> and the callbacks it takes C<container>,
2665 C<stdin>, C<stdout>, C<stderr> and C<tty>, sent as query parameters
2666 (C<stdout> and C<stderr> default to true, the others to false). Any other
2667 option -- a misspelt C<container> would otherwise run the command in the
2668 pod's default container -- fails the L<Future> before a request is sent,
2669 naming the options C<exec> takes.
2670  
2671 =head2 attach
2672  
2673 my $f = $kube->attach('Pod', 'my-pod',
2674 namespace => 'default',
2675 container => 'app',
2676 stdin => 1,
2677 stdout => 1,
2678 stderr => 1,
2679 tty => 0,
2680 on_frame => sub { my ($channel, $payload) = @_; ... },
2681 );
2682 my $session = $f->get;
2683  
2684 Create an async pod attach session request.
2685  
2686 Returns a L<Future> that resolves to the duplex session object returned by the
2687 transport backend. The default transport returns a
2688 L<Net::Async::Kubernetes::PortForwardSession> object.
2689  
2690 The session helper supports C<write_channel>, C<write_stdin>, C<resize>, and
2691 C<close>.
2692  
2693 C<on_open> receives the created session object.
2694  
2695 C<on_frame> receives C<($channel, $payload)> where the first byte of each
2696 binary websocket frame is decoded as Kubernetes channel id.
2697  
2698 It takes C<name>, C<namespace>, C<container>, C<stdin>, C<stdout>,
2699 C<stderr>, C<tty>, C<subprotocol> and the callbacks, as L</exec> does, but
2700 no C<command>. Any other option, C<command> among them, fails the L<Future>
2701 before a request is sent, naming the options C<attach> takes.
2702  
2703 =head2 cp_to_pod
2704  
2705 my $f = $kube->cp_to_pod('Pod', 'my-pod',
2706 namespace => 'default',
2707 container => 'app',
2708 local => '/tmp/local.txt',
2709 remote => '/tmp/remote.txt',
2710 );
2711 my $result = $f->get;
2712  
2713 Copy a single local file into a pod. Reads the entire local file into memory,
2714 then runs C<sh -c 'head -c "$1" > "$2"'> inside the pod via C<exec()> and
2715 streams the bytes over stdin.
2716  
2717 This is a single-file copy, not a tar-based transfer: there is no recursive
2718 directory copy, and the whole file is held in memory, so it is not suitable
2719 for very large files.
2720  
2721 Returns a L<Future> resolving to a hashref containing C<local>, C<remote>,
2722 C<bytes>, C<stderr>, and C<status>.
2723  
2724 It takes C<name>, C<namespace>, C<container>, C<local>, C<remote> and
2725 C<chunk_size> (bytes per stdin write, default 65536). Any other option fails
2726 the L<Future> before a request is sent, naming the options C<cp_to_pod>
2727 takes.
2728  
2729 =head2 cp_from_pod
2730  
2731 my $f = $kube->cp_from_pod('Pod', 'my-pod',
2732 namespace => 'default',
2733 container => 'app',
2734 remote => '/tmp/remote.txt',
2735 local => '/tmp/local.txt',
2736 );
2737 my $result = $f->get;
2738  
2739 Copy a single file out of a pod. Runs C<cat $remote> inside the pod via
2740 C<exec()>, buffers the entire stdout stream in memory, then writes it to the
2741 local file.
2742  
2743 This is a single-file copy, not a tar-based transfer: there is no recursive
2744 directory copy, and the whole file is held in memory, so it is not suitable
2745 for very large files.
2746  
2747 Returns a L<Future> resolving to a hashref containing C<local>, C<remote>,
2748 C<bytes>, C<stderr>, and C<status>.
2749  
2750 It takes C<name>, C<namespace>, C<container>, C<remote> and C<local>. Any
2751 other option -- C<chunk_size> belongs to L</cp_to_pod> only -- fails the
2752 L<Future> before a request is sent, naming the options C<cp_from_pod>
2753 takes.
2754  
2755 =head2 watcher
2756  
2757 my $watcher = $kube->watcher('Pod',
2758 namespace => 'default',
2759 label_selector => 'app=web',
2760 on_added => sub { my ($pod) = @_; ... },
2761 on_modified => sub { my ($pod) = @_; ... },
2762 on_deleted => sub { my ($pod) = @_; ... },
2763 );
2764  
2765 Create and register a L<Net::Async::Kubernetes::Watcher> for the specified
2766 resource type. The watcher is added as a child notifier and will start
2767 automatically when the parent is added to a loop.
2768  
2769 Returns the watcher object. An odd list of parameters croaks, as
2770 C<Invalid arguments to watcher()>, before the watcher is created -- as an
2771 unknown parameter does.
2772  
2773 Arguments:
2774  
2775 =over 4
2776  
2777 =item C<$resource> - Resource type to watch (e.g., C<'Pod'>, C<'Deployment'>)
2778  
2779 =item C<%args> - Watcher parameters (C<namespace>, C<label_selector>, callbacks, etc.)
2780  
2781 =back
2782  
2783 See L<Net::Async::Kubernetes::Watcher> for all available parameters.
2784  
2785 =head2 controller
2786  
2787 my $controller = $kube->controller(
2788 on_reconcile => sub {
2789 my ($ctx) = @_;
2790 ...
2791 },
2792 );
2793  
2794 Create and register a L<Net::Async::Kubernetes::Controller> runtime bound to
2795 this client. The controller is added as a child notifier and can register
2796 resource watches, queue reconcile work, and patch object status.
2797  
2798 Returns the controller object. An odd list of parameters croaks, as
2799 C<Invalid arguments to controller()>, before the controller is created.
2800  
2801 =head1 ERRORS
2802  
2803 The C<Future>-returning methods report every failure through the returned
2804 L<Future>:
2805  
2806 =over 4
2807  
2808 =item * Bad arguments -- an unknown resource, an object that is no
2809 Kubernetes resource, a missing name, an unknown option -- fail it with a
2810 message before any request is sent. L</expand_class>, L</update>,
2811 L</update_status>, L</ensure> and a watcher that starts croak instead, as
2812 each of them documents.
2813  
2814 =item * A response the API server refuses (status 400 and up) fails it
2815 following L<Future>'s convention for failure details:
2816 C<< ->fail($error, 'http', $response) >>. C<$error> is exactly what
2817 L<Kubernetes::REST/check_response> throws, a L<Kubernetes::REST::APIError>
2818 object, which stringifies to the message
2819 (C<Kubernetes API error (get Pod): 404 ...>).
2820 C<$response> is the L<Kubernetes::REST::HTTPResponse>, whose C<status>
2821 tells a C<404> from a C<409> without parsing the message:
2822  
2823 $kube->delete('Pod', 'web', namespace => 'default')->catch(http => sub {
2824 my ($error, $category, $response) = @_;
2825 return Future->done if $response->status == 404; # already gone
2826 return Future->fail(@_);
2827 })->get;
2828  
2829 L</ensure> and L</ensure_all> pass the failure of the request that failed
2830 on in the same form.
2831  
2832 =item * A request that gets no response at all (connection refused, a TLS
2833 error) fails it the way the HTTP transport reports it.
2834  
2835 =back
2836  
2837 A L<Net::Async::Kubernetes::Watcher> reports failures to its C<on_error>
2838 callback instead, with the HTTP status in the C<code> of the C<Status> it
2839 passes.
2840  
2841 =head1 SEE ALSO
2842  
2843 L<Net::Async::Kubernetes::Watcher>, L<Net::Async::Kubernetes::Controller>,
2844 L<Net::Async::Kubernetes::PortForwardSession>, L<Kubernetes::REST>,
2845 L<IO::Async>, L<IO::K8s>, L<Net::Async::WebSocket::Client>
2846  
2847 =head1 SUPPORT
2848  
2849 =head2 Issues
2850  
2851 Please report bugs and feature requests on GitHub at
2852 L<https://github.com/Getty/p5-net-async-kubernetes/issues>.
2853  
2854 =head2 IRC
2855  
2856 Join C<#kubernetes> on C<irc.perl.org> or message Getty directly.
2857  
2858 =head1 CONTRIBUTING
2859  
2860 Contributions are welcome! Please fork the repository and submit a pull request.
2861  
2862 =head1 AUTHOR
2863  
2864 Torsten Raudssus <torsten@raudssus.de>
2865  
2866 =head1 COPYRIGHT AND LICENSE
2867  
2868 This software is copyright (c) 2026 by Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>.
2869  
2870 This is free software; you can redistribute it and/or modify it under
2871 the same terms as the Perl 5 programming language system itself.
2872  
2873 =cut
« Kubernetes/Watcher.pm ↑ Summary Kubernetes/PortForwardSession.pm »