Kubernetes/Controller.pm

filter: Statementstmt 96.3% 211 / 219 Branchbran 82.1% 92 / 112 Conditioncond 61.7% 42 / 68 MC/DCmcdc 43.5% 17 / 39 Subroutinesub 97.0% 33 / 34 Podpod 100% 12 / 12 totaltotal 84.0% 407 / 484 CC 88 SCAR 20.1
CC
88
Cov
86%
patch_status
14.1
_drain_queue
13.2
configure
12.5
CRAP 7.4
« Kubernetes/PortForwardSession.pm ↑ Summary Kubernetes/PortForwardListener.pm »
1 package Net::Async::Kubernetes::Controller;
2 # ABSTRACT: Minimal controller runtime for Net::Async::Kubernetes
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 Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 54 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 ▶sub configure {
Subroutine configure: called CC=12 SCAR=25.3
Pod documented
13 40 my ($self, %params) = @_;
14  
15 40 ▶ if (exists $params{kube}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params{'kube'}) { } 39 1 40
elsif (not $self->{'kube'}) { } 1 0 1
16 # Borrowed client: weak, like Net::Async::Kubernetes::Watcher does.
17 # $kube->controller(...) add_child()s us, so a strong ref here would
18 # cycle kube -> child controller -> kube and never free either.
19 39 $self->{kube} = delete $params{kube};
20 39 weaken($self->{kube});
21 } elsif (!$self->{kube}) {
22 1 require Net::Async::Kubernetes;
23 1 my %kube_args;
24 1 for my $key (qw(
25 kubeconfig context server credentials resource_map
26 resource_map_from_cluster
27 )) {
28 6 ▶ $kube_args{$key} = delete $params{$key} if exists $params{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$key} 3 3 6
29 }
30 # Self-constructed client: strong, the controller is its only owner.
31 1 ▶ $self->{kube} = Net::Async::Kubernetes->new(%kube_args) if %kube_args;
Branch: 1 branch
BranchTrueFalseTotal
if %kube_args 1 0 1
32 }
33  
34 40 ▶ if (exists $params{on_reconcile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"on_reconcile"}) 40 0 40
35 40 $self->{on_reconcile} = delete $params{on_reconcile};
36 }
37 40 ▶ if (exists $params{on_watch_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"on_watch_error"}) 3 37 40
38 3 $self->{on_watch_error} = delete $params{on_watch_error};
39 }
40 40 ▶ if (exists $params{retry_delay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"retry_delay"}) 3 37 40
41 3 $self->{retry_delay} = delete $params{retry_delay};
42 }
43  
44 40 ▶ $self->{watch_specs} ||= [];
Condition50%1/2
$self->{"watch_specs"} ||= []
l!l
040
MC/DC0%0/1
$self->{"watch_specs"} ||= []
$self->{"watch_specs"}
Truth table50%1/2
A
A= $self->{"watch_specs"}
Aresult
11
00
45 40 ▶ $self->{queue} ||= [];
Condition50%1/2
$self->{"queue"} ||= []
l!l
040
MC/DC0%0/1
$self->{"queue"} ||= []
$self->{"queue"}
Truth table50%1/2
A
A= $self->{"queue"}
Aresult
11
00
46 40 ▶ $self->{entries} ||= {};
Condition50%1/2
$self->{"entries"} ||= {}
l!l
040
MC/DC0%0/1
$self->{"entries"} ||= {}
$self->{"entries"}
Truth table50%1/2
A
A= $self->{"entries"}
Aresult
11
00
47  
48 40 $self->SUPER::configure(%params);
49 }
50  
51 295 ▶sub kube { $_[0]->{kube} }
Subroutine kube: called CC=1 SCAR=0.0
Pod documented
52 105 ▶sub on_reconcile { $_[0]->{on_reconcile} }
Subroutine on_reconcile: called CC=1 SCAR=0.0
Pod documented
53 22 ▶sub on_watch_error { $_[0]->{on_watch_error} }
Subroutine on_watch_error: called CC=1 SCAR=0.0
Pod documented
54 4 ▶sub retry_delay { exists $_[0]->{retry_delay} ? $_[0]->{retry_delay} : 1 }
Subroutine retry_delay: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{'retry_delay'} ? : 4 0 4
55  
56 36 ▶sub _add_to_loop {
Subroutine _add_to_loop: called CC=5 SCAR=17.0
57 36 my ($self, $loop) = @_;
58  
59 36 ▶ croak "kube is required" unless $self->kube;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->kube 0 36 36
60 36 ▶ croak "on_reconcile is required" unless $self->on_reconcile;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_reconcile 0 36 36
61  
62 36 ▶ if (!$self->kube->loop) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->kube->loop) { } 6 30 36
elsif ($self->kube->loop != $loop) { } 0 30 30
63 6 $loop->add($self->kube);
64 } elsif ($self->kube->loop != $loop) {
65 0 croak "controller and kube must use the same IO::Async loop";
66 }
67  
68 36 $self->start;
69 }
70  
71 31 ▶sub _remove_from_loop {
Subroutine _remove_from_loop: called CC=1 SCAR=0.0
72 31 my ($self, $loop) = @_;
73 31 $self->stop;
74 }
75  
76 41 ▶sub start {
Subroutine start: called CC=3 SCAR=11.0
Pod documented
77 41 my ($self) = @_;
78 41 ▶ return if $self->{started};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"started"} 0 41 41
79 41 $self->{started} = 1;
80 41 $self->{stopped} = 0;
81  
82 41 for my $spec (@{ $self->{watch_specs} }) {
83 13 $self->_start_watch_spec($spec);
84 }
85 }
86  
87 52 ▶sub stop {
Subroutine stop: called CC=7 SCAR=19.5
Pod documented
88 52 my ($self) = @_;
89  
90 52 $self->{stopped} = 1;
91 52 $self->{started} = 0;
92  
93 52 ▶ for my $spec (@{ $self->{watch_specs} || [] }) {
Condition50%1/2
$self->{'watch_specs'} || []
l!l
520
MC/DC0%0/1
$self->{'watch_specs'} || []
$self->{'watch_specs'}
Truth table50%1/2
A
A= $self->{'watch_specs'}
Aresult
11
00
94 35 ▶ next unless my $watcher = delete $spec->{watcher};
Branch: 1 branch
BranchTrueFalseTotal
unless my $watcher = delete $spec->{"watcher"} 13 22 35
95 22 $watcher->stop;
96 # $kube->watcher() add_child()ed it and a restart builds a fresh one,
97 # so without the matching removal every stop/start cycle leaves another
98 # stopped watcher attached to the client.
99 22 $watcher->remove_from_parent;
100 }
101  
102 # A restart re-lists through fresh watches, so everything still alive
103 # arrives again as ADDED: what was pending is a stale snapshot, and a key
104 # left flagged queued makes that new event return early without ever
105 # scheduling a drain. Queue and flags go together - a key in the queue
106 # without its flag, or a flag without the queue entry, is a stall either
107 # way. A dirty flag is pending work as well - it records an event a
108 # reconcile in flight has not seen yet, and the re-list delivers that
109 # object again anyway. The entries themselves stay: they carry the failure
110 # counts.
111 52 $self->{queue} = [];
112 52 ▶ for my $entry (values %{ $self->{entries} || {} }) {
Condition50%1/2
$self->{'entries'} || {}
l!l
520
MC/DC0%0/1
$self->{'entries'} || {}
$self->{'entries'}
Truth table50%1/2
A
A= $self->{'entries'}
Aresult
11
00
113 # Cancel, not just forget: a timer left running would re-queue the
114 # pre-stop ctx after a quick restart, reconciling an object that may
115 # be gone and for which no event arrived.
116 14 ▶ if (my $retry = delete $entry->{retry_future}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $retry = delete $entry->{"retry_future"}) 1 13 14
117 1 $retry->cancel;
118 }
119 14 $entry->{queued} = 0;
120 14 $entry->{dirty} = 0;
121 }
122 }
123  
124 19 ▶sub watch_resource {
Subroutine watch_resource: called CC=4 SCAR=13.9
Pod documented
125 19 my ($self, $resource, @args) = @_;
126  
127 19 ▶ croak "Invalid arguments to watch_resource()" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 2 17 19
128 17 my $spec = {
129 resource => $resource,
130 @args,
131 };
132  
133 17 push @{ $self->{watch_specs} }, $spec;
134 17 ▶ $self->_start_watch_spec($spec) if $self->{started} && !$self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"started"} and not $self->{"stopped"} 9 8 17
Condition66%2/3
$self->{"started"} and not $self->{"stopped"}
!ll&&!rl&&r
809
MC/DC50%1/2
$self->{"started"} and not $self->{"stopped"}
$self->{"started"} not $self->{"stopped"}
Truth table66%2/3
A and not(B)
A= $self->{"started"}B= not $self->{"stopped"}
ABresult
0X0
100
111
135 17 return $spec->{watcher};
136 }
137  
138 22 ▶sub _start_watch_spec {
Subroutine _start_watch_spec: called CC=4 SCAR=13.9
139 22 my ($self, $spec) = @_;
140 22 ▶ return if $spec->{watcher};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"watcher"} 0 22 22
141  
142 22 my %watch_args = %$spec;
143 22 my $resource = delete $watch_args{resource};
144 22 delete @watch_args{qw(key_for)};
145  
146 # ERROR events carry a Status hashref, not a keyed resource object, so they
147 # cannot go through the reconcile workqueue. Report them to the controller
148 # hook instead - an on_error given per watch keeps precedence.
149 22 my $on_watch_error = $self->on_watch_error;
150 22 ▶ if ($on_watch_error && !$watch_args{on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_watch_error and not $watch_args{"on_error"}) 2 20 22
Condition100%3/3
$on_watch_error and not $watch_args{"on_error"}
!ll&&!rl&&r
1912
MC/DC100%2/2
$on_watch_error and not $watch_args{"on_error"}
$on_watch_error not $watch_args{"on_error"}
Truth table100%3/3
A and not(B)
A= $on_watch_errorB= not $watch_args{"on_error"}
ABresult
0X0
100
111
151 2 ▶ $watch_args{on_error} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
152 2 my ($error) = @_;
153 $on_watch_error->($error, {
154 controller => $self,
155 kube => $self->kube,
156 resource => $spec->{resource},
157 2 });
158 2 };
159 }
160  
161 $spec->{watcher} = $self->kube->watcher($resource,
162 %watch_args,
163 63 ▶ on_added => sub { $self->_enqueue_event($spec, 'ADDED', $_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
164 2 ▶ on_modified => sub { $self->_enqueue_event($spec, 'MODIFIED', $_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
165 0 ▶ on_deleted => sub { $self->_enqueue_event($spec, 'DELETED', $_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 22 );
167 }
168  
169 71 ▶sub _enqueue_event {
Subroutine _enqueue_event: called CC=6 SCAR=17.9
170 71 my ($self, $spec, $event_type, $object) = @_;
171 71 ▶ return if $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 0 71 71
172  
173 71 my $key = $self->_key_for($spec, $object);
174 71 ▶ my $entry = $self->{entries}{$key} ||= { key => $key };
Condition100%2/2
$self->{"entries"}{$key} ||= {"key", $key}
l!l
566
MC/DC100%1/1
$self->{"entries"}{$key} ||= {"key", $key}
$self->{"entries"}{$key}
Truth table100%2/2
A
A= $self->{"entries"}{$key}
Aresult
11
00
175  
176 $entry->{ctx} = {
177 controller => $self,
178 kube => $self->kube,
179 resource => $spec->{resource},
180 event_type => $event_type,
181 object => $object,
182 key => $key,
183 71 ▶ attempt => ($entry->{failures} // 0) + 1,
Condition100%2/2
$entry->{"failures"} // 0
l!l
170
MC/DC100%1/1
$entry->{"failures"} // 0
$entry->{"failures"}
Truth table100%2/2
A
A= $entry->{"failures"}
Aresult
11
00
184 };
185 # The ctx outlives the reconcile in {entries} whenever the key still has
186 # work pending, so neither ref may be strong: the client would restore the
187 # kube -> controller -> kube cycle weakened above, and the controller
188 # would close controller -> entries -> ctx -> controller on itself.
189 71 weaken($entry->{ctx}{kube});
190 71 weaken($entry->{ctx}{controller});
191  
192 71 ▶ if ($entry->{active}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"active"}) 1 70 71
193 1 $entry->{dirty} = 1;
194 1 return;
195 }
196  
197 70 ▶ return if $entry->{queued};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"queued"} 2 68 70
198 68 $entry->{queued} = 1;
199 68 push @{ $self->{queue} }, $key;
200 68 $self->_schedule_drain;
201 }
202  
203 139 ▶sub _schedule_drain {
Subroutine _schedule_drain: called CC=3 SCAR=11.0
204 139 my ($self) = @_;
205 139 ▶ return if $self->{drain_scheduled} || $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"drain_scheduled"} or $self->{"stopped"} 61 78 139
Condition100%3/3
$self->{"drain_scheduled"} or $self->{"stopped"}
l!l&&r!l&&!r
53878
MC/DC100%2/2
$self->{"drain_scheduled"} or $self->{"stopped"}
$self->{"drain_scheduled"} $self->{"stopped"}
Truth table100%3/3
A or B
A= $self->{"drain_scheduled"}B= $self->{"stopped"}
ABresult
1X1
011
000
206 78 $self->{drain_scheduled} = 1;
207 78 ▶ $self->loop->later(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
208 78 delete $self->{drain_scheduled};
209 78 $self->_drain_queue;
210 78 });
211 }
212  
213 78 ▶sub _drain_queue {
Subroutine _drain_queue: called CC=12 SCAR=25.8
214 78 my ($self) = @_;
215 78 ▶ return if $self->{stopped} || $self->{active_key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} or $self->{"active_key"} 1 77 78
Condition66%2/3
$self->{"stopped"} or $self->{"active_key"}
l!l&&r!l&&!r
1077
MC/DC50%1/2
$self->{"stopped"} or $self->{"active_key"}
$self->{"stopped"} $self->{"active_key"}
Truth table66%2/3
A or B
A= $self->{"stopped"}B= $self->{"active_key"}
ABresult
1X1
011
000
216  
217 77 my ($key, $entry);
218 77 ▶ while (defined(my $candidate = shift @{ $self->{queue} || [] })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $candidate = shift @{$self->{"queue"} or [];})) 70 8 78
Condition50%1/2
$self->{'queue'} || []
l!l
780
MC/DC0%0/1
$self->{'queue'} || []
$self->{'queue'}
Truth table50%1/2
A
A= $self->{'queue'}
Aresult
11
00
219 70 my $next = $self->{entries}{$candidate};
220 # A queued key whose entry or ctx is gone has nothing to reconcile.
221 # Skip it and take the next one: returning here would leave the rest
222 # of the queue sitting until an unrelated key schedules the next
223 # drain. Clear the flag so the key can be queued again later.
224 70 ▶ if (!$next || !$next->{ctx}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($next and $next->{"ctx"}) 1 69 70
Condition66%2/3
$next and $next->{"ctx"}
!ll&&!rl&&r
1069
MC/DC50%1/2
$next and $next->{"ctx"}
$next $next->{"ctx"}
Truth table66%2/3
A and B
A= $nextB= $next->{"ctx"}
ABresult
0X0
100
111
225 1 ▶ $next->{queued} = 0 if $next;
Branch: 1 branch
BranchTrueFalseTotal
if $next 0 1 1
226 1 next;
227 }
228 69 ($key, $entry) = ($candidate, $next);
229 69 last;
230 }
231 77 ▶ return unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 8 69 77
232 69 my $ctx = $entry->{ctx};
233  
234 69 $entry->{queued} = 0;
235 69 $entry->{active} = 1;
236 69 $self->{active_key} = $key;
237  
238 69 my $ret = eval { $self->on_reconcile->($ctx) };
239 69 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 69 69
elsif (not builtin::blessed($ret) && $ret->isa('Future')) { } 61 8 69
240 0 $ret = Future->fail($@);
241 ▶ } elsif (!blessed($ret) || !$ret->isa('Future')) {
Condition66%2/3
builtin::blessed($ret) && $ret->isa('Future')
!ll&&!rl&&r
6108
MC/DC50%1/2
builtin::blessed($ret) && $ret->isa('Future')
builtin::blessed($ret) $ret->isa('Future')
Truth table66%2/3
A && B
A= builtin::blessed($ret)B= $ret->isa('Future')
ABresult
0X0
100
111
242 61 $ret = Future->done($ret);
243 }
244  
245 69 ▶ $ret->on_ready(sub {
Subroutine __ANON__: called CC=6 SCAR=19.4
246 69 my ($f) = @_;
247 69 delete $self->{active_key};
248 69 $entry->{active} = 0;
249  
250 69 ▶ if ($f->is_done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->is_done) { } 62 7 69
251 62 delete $entry->{failures};
252 # Nothing enters the queue while stopped, here no more than
253 # anywhere else: the drain is off, so the key would sit in the
254 # queue stop() just emptied, and the ADDED a restart re-lists would
255 # return early on its queued flag without scheduling a drain.
256 62 ▶ if ($entry->{dirty} && !$self->{stopped}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($entry->{'dirty'} and not $self->{'stopped'}) { } 0 62 62
elsif (not $entry->{'retry_future'}) { } 62 0 62
Condition33%1/3
$entry->{"dirty"} and not $self->{"stopped"}
!ll&&!rl&&r
6200
MC/DC0%0/2
$entry->{"dirty"} and not $self->{"stopped"}
$entry->{"dirty"} not $self->{"stopped"}
Truth table33%1/3
A and not(B)
A= $entry->{"dirty"}B= not $self->{"stopped"}
ABresult
0X0
100
111
257 0 $entry->{dirty} = 0;
258 0 $entry->{queued} = 1;
259 0 push @{ $self->{queue} }, $key;
260 }
261 elsif (!$entry->{retry_future}) {
262 # Clean reconcile: nothing queued, nothing dirty, no backoff
263 # left to remember. Keeping the entry would hold one hashref
264 # plus its ctx per key seen, for the lifetime of the process.
265 # A DELETED event needs no case of its own - its reconcile
266 # ends here like any other.
267 62 delete $self->{entries}{$key};
268 }
269 } else {
270 7 $entry->{dirty} = 0;
271 7 ▶ $entry->{failures} = ($entry->{failures} // 0) + 1;
Condition100%2/2
$entry->{"failures"} // 0
l!l
16
MC/DC100%1/1
$entry->{"failures"} // 0
$entry->{"failures"}
Truth table100%2/2
A
A= $entry->{"failures"}
Aresult
11
00
272 7 my ($error) = $f->failure;
273 7 $self->_schedule_retry($key, $entry->{ctx}, $entry->{failures}, $error);
274 }
275  
276 69 $self->_schedule_drain;
277 69 });
278 }
279  
280 7 ▶sub _schedule_retry {
Subroutine _schedule_retry: called CC=4 SCAR=13.9
281 7 my ($self, $key, $ctx, $attempt, $error) = @_;
282 7 ▶ return if $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 3 4 7
283  
284 4 ▶ my $entry = $self->{entries}{$key} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->{"entries"}{$key} or return 4 0 4
285 4 my $delay = $self->_retry_after($attempt, $ctx, $error);
286  
287 4 ▶ my $enqueue = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
288 4 ▶ return if $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 1 3 4
289 3 delete $entry->{retry_future};
290 3 ▶ return if $entry->{active} || $entry->{queued};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"active"} or $entry->{"queued"} 1 2 3
Condition66%2/3
$entry->{"active"} or $entry->{"queued"}
l!l&&r!l&&!r
012
MC/DC50%1/2
$entry->{"active"} or $entry->{"queued"}
$entry->{"active"} $entry->{"queued"}
Truth table66%2/3
A or B
A= $entry->{"active"}B= $entry->{"queued"}
ABresult
1X1
011
000
291 # The retry reuses the ctx of the failed run, so the attempt number
292 # has to move on here; only a fresh event rebuilds it otherwise.
293 2 ▶ if (my $old = $entry->{ctx}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $old = $entry->{"ctx"}) 2 0 2
294 2 ▶ my $ctx = { %$old, attempt => ($entry->{failures} // 0) + 1 };
Condition50%1/2
$entry->{"failures"} // 0
l!l
20
MC/DC0%0/1
$entry->{"failures"} // 0
$entry->{"failures"}
Truth table50%1/2
A
A= $entry->{"failures"}
Aresult
11
00
295 2 weaken($ctx->{kube});
296 2 weaken($ctx->{controller});
297 2 $entry->{ctx} = $ctx;
298 }
299 2 $entry->{queued} = 1;
300 2 push @{ $self->{queue} }, $key;
301 2 $self->_schedule_drain;
302 4 };
303  
304 4 ▶ if (!$delay) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($delay) 3 1 4
305 3 $self->loop->later($enqueue);
306 3 return;
307 }
308  
309 1 $entry->{retry_future} = $self->loop->delay_future(after => $delay)->on_ready($enqueue);
310 }
311  
312 4 ▶sub _retry_after {
Subroutine _retry_after: called CC=6 SCAR=22.8
313 4 my ($self, $attempt, $ctx, $error) = @_;
314 4 my $delay = $self->retry_delay;
315  
316 4 ▶ return $delay->($attempt, $ctx, $error) if ref($delay) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $delay eq "CODE" 1 3 4
317 3 ▶ if (ref($delay) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $delay eq "ARRAY") 0 3 3
318 0 ▶ return $delay->[$attempt - 1] // $delay->[-1] // 0;
Condition0%0/5
($$delay[$attempt - 1] // $delay->[-1]) // 0
l!l
00
$$delay[$attempt - 1] // $delay->[-1]
l!l&&r!l&&!r
000
MC/DC0%0/2
($$delay[$attempt - 1] // $delay->[-1]) // 0
$$delay[$attempt - 1] $delay->[-1]
Truth table0%0/3
A // B
A= $$delay[$attempt - 1]B= $delay->[-1]
ABresult
1X1
011
000
319 }
320 3 ▶ return $delay // 0;
Condition50%1/2
$delay // 0
l!l
30
MC/DC0%0/1
$delay // 0
$delay
Truth table50%1/2
A
A= $delay
Aresult
11
00
321 }
322  
323 71 ▶sub _key_for {
Subroutine _key_for: called CC=6 SCAR=19.0
324 71 my ($self, $spec, $object) = @_;
325  
326 71 ▶ if (my $cb = $spec->{key_for}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $spec->{"key_for"}) 2 69 71
327 2 return $cb->($object, $spec);
328 }
329  
330 69 my $meta = $object->metadata;
331 69 ▶ my $name = $meta ? $meta->name : undef;
Branch: 1 branch
BranchTrueFalseTotal
$meta ? : 69 0 69
332 69 ▶ my $namespace = $meta ? $meta->namespace : undef;
Branch: 1 branch
BranchTrueFalseTotal
$meta ? : 69 0 69
333  
334 69 ▶ my $id = defined($namespace) && length($namespace)
Branch: 1 branch
BranchTrueFalseTotal
defined $namespace && length $namespace ? : 69 0 69
Condition33%1/3
defined $namespace and length $namespace
!ll&&!rl&&r
0069
MC/DC0%0/2
defined $namespace and length $namespace
defined $namespace length $namespace
Truth table33%1/3
A and B
A= defined $namespaceB= length $namespace
ABresult
0X0
100
111
335 ? "$namespace/$name"
336 : $name;
337 69 return "$spec->{resource}:$id";
338 }
339  
340 9 ▶sub get_object {
Subroutine get_object: called CC=1 SCAR=0.0
Pod documented
341 9 my ($self, @args) = @_;
342 9 return $self->kube->get(@args);
343 }
344  
345 9 ▶sub list_objects {
Subroutine list_objects: called CC=1 SCAR=0.0
Pod documented
346 9 my ($self, @args) = @_;
347 9 return $self->kube->list(@args);
348 }
349  
350 # The controller keeps its own signature - a status => {...} argument instead
351 # of a patch document, merge as the default type - and builds the patch from
352 # it; resolving the target, the request and the response are the client's
353 # patch_status. Its own options are checked here, with the client's k63
354 # helper: the client only sees patch and type, so any other key (typ,
355 # statuss, namespace in the object form) would be dropped silently. Every
356 # other argument check (metadata, name, patch type, unknown resource) happens
357 # there and comes back as a failed Future too.
358 39 ▶sub patch_status {
Subroutine patch_status: called CC=14 SCAR=26.5
Pod documented
359 39 my ($self, $class_or_object, @rest_args) = @_;
360  
361 39 my (%args, @target, @allowed);
362 39 ▶ if (ref($class_or_object) && blessed($class_or_object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class_or_object and builtin::blessed($class_or_object)) { } 12 27 39
Condition66%2/3
ref $class_or_object and builtin::blessed($class_or_object)
!ll&&!rl&&r
27012
MC/DC50%1/2
ref $class_or_object and builtin::blessed($class_or_object)
ref $class_or_object builtin::blessed($class_or_object)
Truth table66%2/3
A and B
A= ref $class_or_objectB= builtin::blessed($class_or_object)
ABresult
0X0
100
111
363 12 ▶ return Future->fail("Invalid arguments to patch_status()") if @rest_args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest_args % 2 2 10 12
364 10 %args = @rest_args;
365 10 @allowed = qw( status type );
366 # A class without a status attribute has nothing to fall back on; the
367 # missing status is then reported below like any other.
368 10 ▶ $args{status} //= $class_or_object->status if $class_or_object->can('status');
Branch: 1 branch
BranchTrueFalseTotal
if $class_or_object->can("status") 7 3 10
Condition100%3/3
$args{"status"} //= $class_or_object->status
l!l&&r!l&&!r
511
MC/DC100%2/2
$args{"status"} //= $class_or_object->status
$args{"status"} $class_or_object->status
Truth table100%3/3
A //= B
A= $args{"status"}B= $class_or_object->status
ABresult
1X1
011
000
369 10 @target = ($class_or_object);
370 } else {
371 27 ▶ if (@rest_args >= 1 && !ref($rest_args[0]) && $rest_args[0] !~ /^(name|namespace|status|type)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (@rest_args >= 1 and not ref $rest_args[0] and not $rest_args[0] =~ /^(name|namespace|status|type)$/) { } 22 5 27
elsif (@rest_args % 2 == 0) { } 4 1 5
Condition50%3/6
@rest_args >= 1 and not ref $rest_args[0] and not $rest_args[0] =~ /^(name|namespace|status|type)$/
!ll&&!rl&&r
0522
@rest_args >= 1 and not ref $rest_args[0]
!ll&&!rl&&r
0027
MC/DC33%1/3
@rest_args >= 1 and not ref $rest_args[0] and not $rest_args[0] =~ /^(name|namespace|status|type)$/
@rest_args >= 1 not ref $rest_args[0] not $rest_args[0] =~ /^(name|namespace|status|type)$/
Truth table50%2/4
A and not(B) and not(C)
A= @rest_args >= 1B= not ref $rest_args[0]C= not $rest_args[0] =~ /^(name|namespace|status|type)$/
ABCresult
0XX0
10X0
1100
1111
372 22 $args{name} = shift @rest_args;
373 22 ▶ return Future->fail("Invalid arguments to patch_status()") if @rest_args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest_args % 2 1 21 22
374 21 %args = (%args, @rest_args);
375 } elsif (@rest_args % 2 == 0) {
376 4 %args = @rest_args;
377 } else {
378 1 return Future->fail("Invalid arguments to patch_status()");
379 }
380 25 @allowed = qw( name namespace status type );
381 25 @target = ($class_or_object, name => $args{name}, namespace => $args{namespace});
382 }
383 35 my $unknown = $self->kube->_unknown_argument_error('patch_status', \%args, @allowed);
384 35 ▶ return Future->fail($unknown) if defined $unknown;
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown 4 31 35
385 31 ▶ return Future->fail("status required for patch_status") unless defined $args{status};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"status"} 3 28 31
386  
387 return $self->kube->patch_status(@target,
388 patch => { status => $args{status} },
389 28 ▶ type => $args{type} // 'merge',
Condition100%2/2
$args{"type"} // "merge"
l!l
622
MC/DC100%1/1
$args{"type"} // "merge"
$args{"type"}
Truth table100%2/2
A
A= $args{"type"}
Aresult
11
00
390 );
391 }
392  
393 6 ▶sub update_status {
Subroutine update_status: called CC=4 SCAR=13.9
Pod documented
394 6 my ($self, $object) = @_;
395  
396 # The client's update_status croaks on these; the controller's helpers
397 # report every error as a failed Future.
398 6 my ($class, $error) = $self->kube->_object_class('update_status', $object);
399 6 ▶ return Future->fail($error) unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 2 4 6
400 4 ▶ 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") 3 1 4
401 3 ▶ $metadata->name or return Future->fail("object must have metadata.name");
Branch: 1 branch
BranchTrueFalseTotal
$metadata->name or return "Future"->fail("object must have metadata.name") 2 1 3
402  
403 2 return $self->kube->update_status($object);
404 }
405  
406 54 1;
407  
408 __END__
« Kubernetes/PortForwardSession.pm ↑ Summary Kubernetes/PortForwardListener.pm »