Kubernetes/Watcher.pm

filter: Statementstmt 99.1% 224 / 226 Branchbran 90.9% 131 / 144 Conditioncond 87.9% 73 / 83 MC/DCmcdc 76.6% 36 / 47 Subroutinesub 100% 41 / 41 Podpod 100% 21 / 21 totaltotal 93.5% 526 / 562 CC 107 SCAR 24.6
CC
107
Cov
94%
_dispatch_event
28.0
configure
20.0
__ANON__
11.0
CRAP 11.7
↑ Summary Kubernetes.pm »
1 package Net::Async::Kubernetes::Watcher;
2 # ABSTRACT: Auto-reconnecting Kubernetes watch as IO::Async::Notifier
3 54 our $VERSION = '0.012';
4 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 54 ▶use parent 'IO::Async::Notifier';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 54 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 54 ▶use Scalar::Util qw(blessed looks_like_number weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 54 ▶use Kubernetes::REST::HTTPResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 122 ▶sub configure {
Subroutine configure: called CC=20 SCAR=30.0
Pod documented
13 122 my ($self, %params) = @_;
14  
15 122 ▶ if (exists $params{kube}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"kube"}) 104 18 122
16 104 $self->{kube} = delete $params{kube};
17 104 weaken($self->{kube});
18 }
19 # Checked here: anything else (a Controller-style arrayref of delays, a
20 # word) numifies to a delay nobody asked for and the retries go quiet.
21 122 for my $key (qw(reconnect_delay max_reconnect_delay min_watch_duration)) {
22 361 ▶ next unless exists $params{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{$key} 344 17 361
23 17 my $value = delete $params{$key};
24 17 ▶ croak "$key must be a non-negative number of seconds"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and &looks_like_number($value) and $value >= 0 7 10 17
Condition100%9/9
defined $value and not ref $value and &looks_like_number($value) and $value >= 0
!ll&&!rl&&r
5210
defined $value and not ref $value and &looks_like_number($value)
!ll&&!rl&&r
3212
defined $value and not ref $value
!ll&&!rl&&r
1214
MC/DC100%4/4
defined $value and not ref $value and &looks_like_number($value) and $value >= 0
defined $value not ref $value &looks_like_number($value) $value >= 0
Truth table100%5/5
A and not(B) and C and D
A= defined $valueB= not ref $valueC= &looks_like_number($value)D= $value >= 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
25 unless defined $value && !ref $value && looks_like_number($value) && $value >= 0;
26 10 $self->{$key} = $value;
27 }
28 115 ▶ if (exists $params{reconnect_jitter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"reconnect_jitter"}) 36 79 115
29 36 my $value = delete $params{reconnect_jitter};
30 36 ▶ croak "reconnect_jitter must be a number from 0 to 1"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and &looks_like_number($value) and $value >= 0 and $value <= 1 5 31 36
Condition100%12/12
defined $value and not ref $value and &looks_like_number($value) and $value >= 0 and $value <= 1
!ll&&!rl&&r
4131
defined $value and not ref $value and &looks_like_number($value) and $value >= 0
!ll&&!rl&&r
3132
defined $value and not ref $value and &looks_like_number($value)
!ll&&!rl&&r
2133
defined $value and not ref $value
!ll&&!rl&&r
1134
MC/DC100%5/5
defined $value and not ref $value and &looks_like_number($value) and $value >= 0 and $value <= 1
defined $value not ref $value &looks_like_number($value) $value >= 0 $value <= 1
Truth table100%6/6
A and not(B) and C and D and E
A= defined $valueB= not ref $valueC= &looks_like_number($value)D= $value >= 0E= $value <= 1
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
31 unless defined $value && !ref $value && looks_like_number($value)
32 && $value >= 0 && $value <= 1;
33 31 $self->{reconnect_jitter} = $value;
34 }
35 110 ▶ if (exists $params{max_retries}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"max_retries"}) 6 104 110
36 6 my $value = delete $params{max_retries};
37 6 ▶ croak "max_retries must be a non-negative integer, or undef for no limit"
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and ref $value || !($value =~ /\A[0-9]+\z/) 2 4 6
Condition66%4/6
defined $value and ref $value || !($value =~ /\A[0-9]+\z/)
!ll&&!rl&&r
042
ref $value or not $value =~ /\A[0-9]+\z/
l!l&&r!l&&!r
024
MC/DC33%1/3
defined $value and ref $value || !($value =~ /\A[0-9]+\z/)
defined $value ref $value not $value =~ /\A[0-9]+\z/
Truth table50%2/4
A and B or not(C)
A= defined $valueB= ref $valueC= not $value =~ /\A[0-9]+\z/
ABCresult
0XX0
1000
11X1
1011
38 if defined $value && (ref $value || $value !~ /\A[0-9]+\z/);
39 4 $self->{max_retries} = $value;
40 }
41 108 for my $key (qw(resource namespace timeout label_selector field_selector
42 names event_types
43 on_added on_modified on_deleted on_error on_event)) {
44 1296 ▶ if (exists $params{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{$key}) 416 880 1296
45 416 $self->{$key} = delete $params{$key};
46 }
47 }
48  
49 108 $self->SUPER::configure(%params);
50 }
51  
52  
53 # Accessors
54 948 ▶sub kube { $_[0]->{kube} }
Subroutine kube: called CC=1 SCAR=0.0
Pod documented
55  
56  
57 580 ▶sub resource { $_[0]->{resource} }
Subroutine resource: called CC=1 SCAR=0.0
Pod documented
58  
59  
60 365 ▶sub namespace { $_[0]->{namespace} }
Subroutine namespace: called CC=1 SCAR=0.0
Pod documented
61  
62  
63 186 ▶sub timeout { $_[0]->{timeout} // 300 }
Subroutine timeout: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]{"timeout"} // 300
l!l
3183
MC/DC100%1/1
$_[0]{"timeout"} // 300
$_[0]{"timeout"}
Truth table100%2/2
A
A= $_[0]{"timeout"}
Aresult
11
00
64  
65  
66 92 ▶sub reconnect_delay { $_[0]->{reconnect_delay} // 1 }
Subroutine reconnect_delay: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]{"reconnect_delay"} // 1
l!l
785
MC/DC100%1/1
$_[0]{"reconnect_delay"} // 1
$_[0]{"reconnect_delay"}
Truth table100%2/2
A
A= $_[0]{"reconnect_delay"}
Aresult
11
00
67  
68  
69 118 ▶sub max_reconnect_delay { $_[0]->{max_reconnect_delay} // 30 }
Subroutine max_reconnect_delay: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]{"max_reconnect_delay"} // 30
l!l
8110
MC/DC100%1/1
$_[0]{"max_reconnect_delay"} // 30
$_[0]{"max_reconnect_delay"}
Truth table100%2/2
A
A= $_[0]{"max_reconnect_delay"}
Aresult
11
00
70  
71  
72 93 ▶sub reconnect_jitter { $_[0]->{reconnect_jitter} // 0.2 }
Subroutine reconnect_jitter: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]{"reconnect_jitter"} // "0.2"
l!l
858
MC/DC100%1/1
$_[0]{"reconnect_jitter"} // "0.2"
$_[0]{"reconnect_jitter"}
Truth table100%2/2
A
A= $_[0]{"reconnect_jitter"}
Aresult
11
00
73  
74  
75 95 ▶sub max_retries { $_[0]->{max_retries} }
Subroutine max_retries: called CC=1 SCAR=0.0
Pod documented
76  
77  
78 17 ▶sub min_watch_duration { $_[0]->{min_watch_duration} // 1 }
Subroutine min_watch_duration: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]{"min_watch_duration"} // 1
l!l
710
MC/DC100%1/1
$_[0]{"min_watch_duration"} // 1
$_[0]{"min_watch_duration"}
Truth table100%2/2
A
A= $_[0]{"min_watch_duration"}
Aresult
11
00
79  
80  
81 187 ▶sub label_selector { $_[0]->{label_selector} }
Subroutine label_selector: called CC=1 SCAR=0.0
Pod documented
82  
83  
84 187 ▶sub field_selector { $_[0]->{field_selector} }
Subroutine field_selector: called CC=1 SCAR=0.0
Pod documented
85  
86  
87 129 ▶sub names { $_[0]->{names} }
Subroutine names: called CC=1 SCAR=0.0
Pod documented
88  
89  
90 147 ▶sub event_types { $_[0]->{event_types} }
Subroutine event_types: called CC=1 SCAR=0.0
Pod documented
91  
92  
93 324 ▶sub on_added { $_[0]->{on_added} }
Subroutine on_added: called CC=1 SCAR=0.0
Pod documented
94  
95  
96 115 ▶sub on_modified { $_[0]->{on_modified} }
Subroutine on_modified: called CC=1 SCAR=0.0
Pod documented
97  
98  
99 115 ▶sub on_deleted { $_[0]->{on_deleted} }
Subroutine on_deleted: called CC=1 SCAR=0.0
Pod documented
100  
101  
102 220 ▶sub on_error { $_[0]->{on_error} }
Subroutine on_error: called CC=1 SCAR=0.0
Pod documented
103  
104  
105 272 ▶sub on_event { $_[0]->{on_event} }
Subroutine on_event: called CC=1 SCAR=0.0
Pod documented
106  
107  
108 104 ▶sub _add_to_loop {
Subroutine _add_to_loop: called CC=3 SCAR=11.0
109 104 my ($self, $loop) = @_;
110 104 ▶ croak "kube is required" unless $self->{kube};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"kube"} 1 103 104
111 103 ▶ croak "resource is required" unless $self->{resource};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"resource"} 1 102 103
112 102 $self->start;
113 }
114  
115 24 ▶sub _remove_from_loop {
Subroutine _remove_from_loop: called CC=1 SCAR=0.0
116 24 my ($self, $loop) = @_;
117 24 $self->stop;
118 }
119  
120 104 ▶sub start {
Subroutine start: called CC=3 SCAR=11.0
Pod documented
121 104 my ($self) = @_;
122 # Waiting out a reconnect delay is running too: starting now as well
123 # would leave two watches once the pending reconnect fires.
124 104 ▶ return if $self->{_watching} || $self->{_retry_future};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_watching"} or $self->{"_retry_future"} 1 103 104
Condition66%2/3
$self->{"_watching"} or $self->{"_retry_future"}
l!l&&r!l&&!r
01103
MC/DC50%1/2
$self->{"_watching"} or $self->{"_retry_future"}
$self->{"_watching"} $self->{"_retry_future"}
Truth table66%2/3
A or B
A= $self->{"_watching"}B= $self->{"_retry_future"}
ABresult
1X1
011
000
125 103 $self->{_stopped} = 0;
126 103 $self->{_failures} = 0;
127 103 $self->_start_watch;
128 }
129  
130  
131 127 ▶sub stop {
Subroutine stop: called CC=5 SCAR=16.1
Pod documented
132 127 my ($self) = @_;
133 127 $self->{_stopped} = 1;
134 127 $self->{_watching} = 0;
135 127 ▶ if (my $retry = delete $self->{_retry_future}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $retry = delete $self->{"_retry_future"}) 25 102 127
136 25 $retry->cancel;
137 }
138 127 ▶ if (my $f = delete $self->{_watch_future}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = delete $self->{"_watch_future"}) 88 39 127
139 88 ▶ return if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 28 60 88
140 # Defer cancel to next loop iteration to avoid closing the HTTP
141 # connection from within its own on_read handler, which triggers
142 # Net::Async::HTTP's "Spurious on_read of connection while idle".
143 60 ▶ if (my $loop = $self->loop) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $loop = $self->loop) { } 60 0 60
144 50 ▶ $loop->later(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
145 50 ▶ $f->cancel if !$f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if not $f->is_ready 39 11 50
146 60 });
147 } else {
148 0 $f->cancel;
149 }
150 }
151 }
152  
153  
154 197 ▶sub _start_watch {
Subroutine _start_watch: called CC=9 SCAR=22.0
155 197 my ($self) = @_;
156 197 ▶ return if $self->{_stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_stopped"} 0 197 197
157 197 ▶ return unless $self->{kube};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"kube"} 0 197 197
158  
159 197 $self->{_watching} = 1;
160 197 $self->{_buffer} = '';
161  
162 197 my $rest = $self->kube->_rest;
163 197 my ($class, $error) = $self->kube->_resolve_class($self->resource);
164 197 ▶ croak $error unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 11 186 197
165 186 ▶ (my $path, $error) = $self->kube->_request_path($class, $self->resource,
Branch: 1 branch
BranchTrueFalseTotal
$self->namespace ? : 177 9 186
166 ($self->namespace ? (namespace => $self->namespace) : ()),
167 );
168 186 ▶ croak $error unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 2 184 186
169  
170 184 my %params = (
171 watch => 'true',
172 timeoutSeconds => $self->timeout,
173 # Without it the only resourceVersion a quiet watch ever holds is
174 # that of the last initial ADDED event, older than the state the
175 # server started the watch from, and the reconnect replays history
176 # from there (k73). With it the server sends BOOKMARK events (one
177 # after the initial events, then periodically) carrying the current
178 # one.
179 allowWatchBookmarks => 'true',
180 );
181 $params{resourceVersion} = $self->{_resource_version}
182 184 ▶ if defined $self->{_resource_version};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_resource_version"} 20 164 184
183 184 ▶ $params{labelSelector} = $self->label_selector
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->label_selector 2 182 184
184 if defined $self->label_selector;
185 184 ▶ $params{fieldSelector} = $self->field_selector
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->field_selector 2 182 184
186 if defined $self->field_selector;
187  
188 184 my $req = $rest->prepare_request('GET', $path, parameters => \%params);
189 # The resolved class, handed over exactly (see the client's _exact_class).
190 184 my $exact_class = $self->kube->_exact_class($class);
191  
192 # What tells a watch cycle that ran its course from a failed attempt
193 # when this stream ends: when it started, whether any event came, and
194 # the ERROR event it would end on.
195 184 $self->{_stream_started} = $self->_now;
196 184 $self->{_stream_events} = 0;
197 184 $self->{_stream_error} = undef;
198  
199 184 weaken(my $weak_self = $self);
200  
201 164 ▶ my $f = $self->kube->_do_streaming_request($req, sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
202 164 my ($chunk) = @_;
203 164 ▶ return unless $weak_self;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak_self 8 156 164
204  
205 156 my $buffer = $weak_self->{_buffer};
206 156 for my $result ($rest->process_watch_chunk($exact_class, \$buffer, $chunk)) {
207 156 $weak_self->{_buffer} = $buffer;
208  
209 156 ▶ if ($result->{resourceVersion}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"resourceVersion"}) 140 16 156
210 140 $weak_self->{_resource_version} = $result->{resourceVersion};
211 }
212  
213 156 my $event = $result->{event};
214 156 $weak_self->{_stream_events}++;
215  
216 # A BOOKMARK only carries the resourceVersion (taken above). It
217 # is an event for the attempt (a stream that got one ran its
218 # course) but not one for the consumer: not dispatched, to
219 # on_event neither, and no name filter or event_types decides.
220 156 ▶ if ($event->type eq 'BOOKMARK') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->type eq "BOOKMARK") 6 150 156
221 6 $weak_self->{_failures} = 0;
222 6 $weak_self->{_stream_error} = undef;
223 6 next;
224 }
225  
226 150 ▶ if ($result->{is_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{'is_error'}) { } 14 136 150
227 14 ▶ if ($result->{error_code} == 410) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"error_code"} == 410) 3 11 14
228 3 $weak_self->{_resource_version} = undef;
229 3 $weak_self->{_stream_error} = undef;
230 3 return;
231 }
232 # Not reset by an ERROR event: a server that sends one and
233 # closes, again and again, must meet a growing backoff.
234 11 $weak_self->{_stream_error} = $event->object;
235 } else {
236 # An event: this attempt got through, so a failure after it
237 # starts the backoff over.
238 136 $weak_self->{_failures} = 0;
239 136 $weak_self->{_stream_error} = undef;
240 }
241  
242 147 $weak_self->_dispatch_event($event);
243 }
244 153 $weak_self->{_buffer} = $buffer;
245 184 });
246  
247 56 ▶ $f->on_done(sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
248 56 my ($response) = @_;
249 56 ▶ return unless $weak_self;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak_self 3 53 56
250 53 ▶ return if $weak_self->{_stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $weak_self->{"_stopped"} 6 47 53
251 47 $weak_self->{_watching} = 0;
252 # A rejected request (401, 403, 5xx) resolves like any response. It
253 # is a failed attempt, not a watch cycle that ran its course.
254 47 ▶ if ($response->status >= 400) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->status >= 400) 9 38 47
255 # 410 Gone on the request itself (aggregated API servers,
256 # proxies) is the same expired resourceVersion as the ERROR
257 # event: forget it and start over at once, silently (k78). Only
258 # when there was a version to forget: a 410 to a request without
259 # one is a failed attempt like any other and meets the backoff
260 # instead of a tight loop. The failure count is kept - this
261 # restart is no success.
262 9 ▶ if ($response->status == 410 && defined $weak_self->{_resource_version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->status == 410 and defined $weak_self->{"_resource_version"}) 2 7 9
Condition100%3/3
$response->status == 410 and defined $weak_self->{"_resource_version"}
!ll&&!rl&&r
612
MC/DC100%2/2
$response->status == 410 and defined $weak_self->{"_resource_version"}
$response->status == 410 defined $weak_self->{"_resource_version"}
Truth table100%3/3
A and B
A= $response->status == 410B= defined $weak_self->{"_resource_version"}
ABresult
0X0
100
111
263 2 $weak_self->{_resource_version} = undef;
264 2 return $weak_self->_start_watch;
265 }
266 7 ▶ my $cause = eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $rest->check_response($response, 'watch ' . $weak_self->resource); 1 } } ? : 0 7 7
267 7 $rest->check_response($response, 'watch ' . $weak_self->resource);
268 0 1;
269 } ? 'HTTP ' . $response->status : $@;
270 7 return $weak_self->_watch_failed($cause, $response->status);
271 }
272 # So is a stream that ends on an ERROR event (other than 410 Gone,
273 # which lets the reconnect start over), or at once without any.
274 # Reconnecting at once would be a tight loop against the API server.
275 38 ▶ if (my $error = $weak_self->{_stream_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $weak_self->{"_stream_error"}) 3 35 38
276 return $weak_self->_watch_failed($weak_self->_error_event_cause($error),
277 3 ▶ ref $error eq 'HASH' ? $error->{code} : undef);
Branch: 1 branch
BranchTrueFalseTotal
ref $error eq 'HASH' ? : 3 0 3
278 }
279 35 my $elapsed = $weak_self->_now - $weak_self->{_stream_started};
280 35 ▶ if (!$weak_self->{_stream_events} && $elapsed < $weak_self->min_watch_duration) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $weak_self->{"_stream_events"} and $elapsed < $weak_self->min_watch_duration) 9 26 35
Condition100%3/3
not $weak_self->{"_stream_events"} and $elapsed < $weak_self->min_watch_duration
!ll&&!rl&&r
2069
MC/DC100%2/2
not $weak_self->{"_stream_events"} and $elapsed < $weak_self->min_watch_duration
not $weak_self->{"_stream_events"} $elapsed < $weak_self->min_watch_duration
Truth table100%3/3
not(A) and B
A= not $weak_self->{"_stream_events"}B= $elapsed < $weak_self->min_watch_duration
ABresult
0X0
100
111
281 9 return $weak_self->_watch_failed(
282 sprintf('stream closed after %.1fs without an event', $elapsed));
283 }
284 26 $weak_self->{_failures} = 0;
285 26 $weak_self->_start_watch;
286 184 });
287  
288 77 ▶ $f->on_fail(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
289 77 my ($error) = @_;
290 77 ▶ return unless $weak_self;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak_self 0 77 77
291 77 ▶ return if $weak_self->{_stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $weak_self->{"_stopped"} 2 75 77
292 75 $weak_self->{_watching} = 0;
293 75 $weak_self->_watch_failed($error);
294 184 });
295  
296 184 $self->{_watch_future} = $f;
297 }
298  
299 # The clock min_watch_duration is measured with: the loop's, which its
300 # timers use too. Tests replace it.
301 168 ▶sub _now { $_[0]->loop->time }
Subroutine _now: called CC=1 SCAR=0.0
302  
303 # The random share of reconnect_jitter a delay is shortened by, from 0 up to
304 # (not including) 1. Tests replace it.
305 27 ▶sub _random_fraction { rand() }
Subroutine _random_fraction: called CC=1 SCAR=0.0
306  
307 # The cause of a failure for a stream that ended on the ERROR event $error,
308 # the raw Status hashref: its code, reason and message.
309 3 ▶sub _error_event_cause {
Subroutine _error_event_cause: called CC=5 SCAR=17.8
310 3 my ($self, $error) = @_;
311 3 ▶ return 'stream closed after an ERROR event' unless ref $error eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $error eq "HASH" 0 3 3
312 3 ▶ my $what = join ' ', grep { defined && length } @{$error}{qw(code reason)};
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
006
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
313 return 'stream closed after an ERROR event'
314 . (length $what ? " ($what)" : '')
315 3 ▶ . (defined $error->{message} ? ': ' . $error->{message} : '');
Branch: 2 branches
BranchTrueFalseTotal
length $what ? : 3 0 3
defined $error->{'message'} ? : 3 0 3
316 }
317  
318 # The cause of a failed watch attempt as its report words it, for humans: a
319 # Kubernetes::REST::APIError as "HTTP <code> <reason>: <message>" from its
320 # accessors (the body when it holds no Status), anything else as its text
321 # without the Perl location a croak or die ends in.
322 94 ▶sub _cause_text {
Subroutine _cause_text: called CC=7 SCAR=19.7
323 94 my ($self, $cause) = @_;
324 94 ▶ return 'unknown error' unless defined $cause;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cause 0 94 94
325 94 ▶ if (blessed $cause && $cause->isa('Kubernetes::REST::APIError')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($cause) and $cause->isa("Kubernetes::REST::APIError")) 7 87 94
Condition66%2/3
builtin::blessed($cause) and $cause->isa("Kubernetes::REST::APIError")
!ll&&!rl&&r
8707
MC/DC50%1/2
builtin::blessed($cause) and $cause->isa("Kubernetes::REST::APIError")
builtin::blessed($cause) $cause->isa("Kubernetes::REST::APIError")
Truth table66%2/3
A and B
A= builtin::blessed($cause)B= $cause->isa("Kubernetes::REST::APIError")
ABresult
0X0
100
111
326 7 ▶ my $text = join ' ', 'HTTP', $cause->code, grep { defined && length } $cause->reason;
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
502
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
327 7 ▶ my $detail = $cause->message // $cause->body;
Condition66%2/3
$cause->message // $cause->body
l!l&&r!l&&!r
520
MC/DC0%0/2
$cause->message // $cause->body
$cause->message $cause->body
Truth table66%2/3
A // B
A= $cause->messageB= $cause->body
ABresult
1X1
011
000
328 7 $detail =~ s/\s+\z//;
329 7 ▶ return length $detail ? $text . ': ' . $detail : $text;
Branch: 1 branch
BranchTrueFalseTotal
length $detail ? : 6 1 7
330 }
331 87 my $text = "$cause";
332 87 $text =~ s/\s+\z//;
333 87 $text =~ s/ at \S+ line \d+\.\z//;
334 87 return $text;
335 }
336  
337 # A failed watch attempt: a request that failed outright, was rejected with
338 # HTTP status $code, or opened a stream that ended badly ($code then the
339 # ERROR event's, if any). Schedules the next attempt with exponential
340 # backoff - or, once max_retries consecutive failures have been retried,
341 # stops the watcher - and reports the failure either way: to on_error, else
342 # as a warning.
343 94 ▶sub _watch_failed {
Subroutine _watch_failed: called CC=10 SCAR=23.0
344 94 my ($self, $cause, $code) = @_;
345 94 my $failures = ++$self->{_failures};
346 94 my $max_retries = $self->max_retries;
347 94 $cause = $self->_cause_text($cause);
348  
349 94 my ($delay, $next);
350 94 ▶ if (defined $max_retries && $failures > $max_retries) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_retries and $failures > $max_retries) { } 3 91 94
Condition100%3/3
defined $max_retries and $failures > $max_retries
!ll&&!rl&&r
8653
MC/DC100%2/2
defined $max_retries and $failures > $max_retries
defined $max_retries $failures > $max_retries
Truth table100%3/3
A and B
A= defined $max_retriesB= $failures > $max_retries
ABresult
0X0
100
111
351 # Stopped before the report, so an on_error that restarts the watcher
352 # is not undone right after.
353 3 $self->stop;
354 3 ▶ $next = sprintf('giving up after %d %s',
Branch: 1 branch
BranchTrueFalseTotal
$max_retries == 1 ? : 1 2 3
355 $max_retries, $max_retries == 1 ? 'retry' : 'retries');
356 } else {
357 # The exponent is bounded so a long outage cannot overflow it to inf,
358 # which reconnect_delay => 0 would turn into a NaN delay.
359 91 my $exponent = $failures - 1;
360 91 ▶ $exponent = 64 if $exponent > 64;
Branch: 1 branch
BranchTrueFalseTotal
if $exponent > 64 0 91 91
361 91 $delay = $self->reconnect_delay * 2 ** $exponent;
362 91 ▶ $delay = $self->max_reconnect_delay if $delay > $self->max_reconnect_delay;
Branch: 1 branch
BranchTrueFalseTotal
if $delay > $self->max_reconnect_delay 26 65 91
363 # Shortened, never lengthened, so both delays stay upper bounds.
364 91 ▶ if (my $jitter = $self->reconnect_jitter) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $jitter = $self->reconnect_jitter) 48 43 91
365 48 $delay -= $delay * $jitter * $self->_random_fraction;
366 48 $delay = int($delay * 1000 + 0.5) / 1000;
367 }
368  
369 # Scheduled before the report, so an on_error that stops the watcher
370 # cancels it.
371 91 weaken(my $weak_self = $self);
372 66 ▶ $self->{_retry_future} = $self->loop->delay_future(after => $delay)->on_done(sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
373 66 ▶ return unless $weak_self;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak_self 0 66 66
374 66 delete $weak_self->{_retry_future};
375 66 $weak_self->_start_watch;
376 91 });
377 91 $next = 'retrying in ' . $delay . 's';
378 }
379  
380 94 ▶ my $status = {
Branch: 1 branch
BranchTrueFalseTotal
defined $delay ? : 91 3 94
Condition100%2/2
$code // 0
l!l
1084
MC/DC100%1/1
$code // 0
$code
Truth table100%2/2
A
A= $code
Aresult
11
00
381 kind => 'Status',
382 apiVersion => 'v1',
383 status => 'Failure',
384 reason => 'WatchFailed',
385 code => $code // 0,
386 message => 'watch ' . $self->resource . ' failed, ' . $next . ': ' . $cause,
387 details => {
388 kind => $self->resource,
389 (defined $delay ? (retryAfterSeconds => $delay) : ()),
390 },
391 };
392  
393 94 ▶ if (my $cb = $self->on_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->on_error) { } 88 6 94
394 88 $cb->($status);
395 } else {
396 6 warn $status->{message} . "\n";
397 }
398 }
399  
400 147 ▶sub _dispatch_event {
Subroutine _dispatch_event: called CC=28 SCAR=33.3
401 147 my ($self, $event) = @_;
402 147 my $type = $event->type;
403  
404 # Client-side event type filter
405 # Explicit event_types wins; otherwise auto-derive from callbacks
406 # (on_event is catch-all, so if set, all types pass)
407 147 ▶ if (my $types = $self->event_types) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $types = $self->event_types) { } 12 135 147
elsif (not $self->on_event) { } 104 31 135
408 12 my %allowed = map { uc($_) => 1 } @$types;
409 12 ▶ return unless $allowed{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed{$type} 4 8 12
410 } elsif (!$self->on_event) {
411 104 my %has;
412 104 ▶ $has{ADDED} = 1 if $self->on_added;
Branch: 1 branch
BranchTrueFalseTotal
if $self->on_added 104 0 104
413 104 ▶ $has{MODIFIED} = 1 if $self->on_modified;
Branch: 1 branch
BranchTrueFalseTotal
if $self->on_modified 66 38 104
414 104 ▶ $has{DELETED} = 1 if $self->on_deleted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->on_deleted 69 35 104
415 104 ▶ $has{ERROR} = 1 if $self->on_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->on_error 25 79 104
416 104 ▶ return unless !%has || $has{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless not %has or $has{$type} 3 101 104
Condition66%2/3
not %has or $has{$type}
l!l&&r!l&&!r
01013
MC/DC50%1/2
not %has or $has{$type}
not %has $has{$type}
Truth table66%2/3
not(A) or B
A= not %hasB= $has{$type}
ABresult
1X1
011
000
417 }
418  
419 # Client-side name filter (skip for ERROR events which have no metadata)
420 140 ▶ if ($type ne 'ERROR' && (my $names = $self->names)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type ne "ERROR" and my $names = $self->names) 11 129 140
Condition100%3/3
$type ne "ERROR" and my $names = $self->names
!ll&&!rl&&r
1111811
MC/DC100%2/2
$type ne "ERROR" and my $names = $self->names
$type ne "ERROR" my $names = $self->names
Truth table100%3/3
A and B
A= $type ne "ERROR"B= my $names = $self->names
ABresult
0X0
100
111
421 11 ▶ my $obj_name = eval { $event->object->metadata->name } // '';
Condition50%1/2
eval { do { $event->object->metadata->name } } // ""
l!l
110
MC/DC0%0/1
eval { do { $event->object->metadata->name } } // ""
eval { do { $event->object->metadata->name } }
Truth table50%1/2
A
A= eval { do { $event->object->metadata->name } }
Aresult
11
00
422 11 ▶ my @patterns = ref $names eq 'ARRAY' ? @$names : ($names);
Branch: 1 branch
BranchTrueFalseTotal
ref $names eq 'ARRAY' ? : 3 8 11
423 11 my $matched = 0;
424 11 for my $pat (@patterns) {
425 13 ▶ if (ref $pat eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pat eq 'Regexp') { } 11 2 13
426 11 ▶ $matched = 1, last if $obj_name =~ $pat;
Branch: 1 branch
BranchTrueFalseTotal
if $obj_name =~ /$pat/ 6 5 11
427 } else {
428 2 ▶ $matched = 1, last if $obj_name eq $pat;
Branch: 1 branch
BranchTrueFalseTotal
if $obj_name eq $pat 1 1 2
429 }
430 }
431 11 ▶ return unless $matched;
Branch: 1 branch
BranchTrueFalseTotal
unless $matched 4 7 11
432 }
433  
434 136 ▶ if (my $cb = $self->on_event) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->on_event) 37 99 136
435 37 $cb->($event);
436 }
437  
438 136 ▶ if ($type eq 'ADDED' && $self->on_added) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'ADDED' and $self->on_added) { } 105 31 136
elsif ($type eq 'MODIFIED' and $self->on_modified) { } 4 27 31
elsif ($type eq 'DELETED' and $self->on_deleted) { } 4 23 27
elsif ($type eq 'ERROR' and $self->on_error) { } 10 25 35
Condition100%3/3
$type eq "ADDED" and $self->on_added
!ll&&!rl&&r
229105
MC/DC100%2/2
$type eq "ADDED" and $self->on_added
$type eq "ADDED" $self->on_added
Truth table100%3/3
A and B
A= $type eq "ADDED"B= $self->on_added
ABresult
0X0
100
111
439 105 $self->on_added->($event->object);
440 ▶ } elsif ($type eq 'MODIFIED' && $self->on_modified) {
Condition100%3/3
$type eq "MODIFIED" and $self->on_modified
!ll&&!rl&&r
2614
MC/DC100%2/2
$type eq "MODIFIED" and $self->on_modified
$type eq "MODIFIED" $self->on_modified
Truth table100%3/3
A and B
A= $type eq "MODIFIED"B= $self->on_modified
ABresult
0X0
100
111
441 4 $self->on_modified->($event->object);
442 ▶ } elsif ($type eq 'DELETED' && $self->on_deleted) {
Condition100%3/3
$type eq "DELETED" and $self->on_deleted
!ll&&!rl&&r
2124
MC/DC100%2/2
$type eq "DELETED" and $self->on_deleted
$type eq "DELETED" $self->on_deleted
Truth table100%3/3
A and B
A= $type eq "DELETED"B= $self->on_deleted
ABresult
0X0
100
111
443 4 $self->on_deleted->($event->object);
444 ▶ } elsif ($type eq 'ERROR' && $self->on_error) {
Condition100%3/3
$type eq "ERROR" and $self->on_error
!ll&&!rl&&r
12110
MC/DC100%2/2
$type eq "ERROR" and $self->on_error
$type eq "ERROR" $self->on_error
Truth table100%3/3
A and B
A= $type eq "ERROR"B= $self->on_error
ABresult
0X0
100
111
445 10 $self->on_error->($event->object);
446 }
447 }
448  
449 54 1;
450  
451 __END__
↑ Summary Kubernetes.pm »