Kubernetes/Controller.pm

filter: Statementstmt 92.4% 196 / 212 Branchbran 76.8% 83 / 108 Conditioncond 57.5% 38 / 66 MC/DCmcdc 39.4% 15 / 38 Subroutinesub 97.0% 33 / 34 Podpod 100% 12 / 12 totaltotal 80.2% 377 / 470 CC 85 SCAR 21.1
CC
85
Cov
82%
_retry_after
22.1
patch_status
14.1
_drain_queue
13.5
CRAP 8.2
« Kubernetes/PortForwardSession.pm ↑ Summary
1 package Net::Async::Kubernetes::Controller;
2 # ABSTRACT: Minimal controller runtime for Net::Async::Kubernetes
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(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 48 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 48 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 36 ▶sub configure {
Subroutine configure: called CC=12 SCAR=25.3
Pod documented
13 36 my ($self, %params) = @_;
14  
15 36 ▶ if (exists $params{kube}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params{'kube'}) { } 35 1 36
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 35 $self->{kube} = delete $params{kube};
20 35 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 36 ▶ if (exists $params{on_reconcile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"on_reconcile"}) 36 0 36
35 36 $self->{on_reconcile} = delete $params{on_reconcile};
36 }
37 36 ▶ if (exists $params{on_watch_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"on_watch_error"}) 3 33 36
38 3 $self->{on_watch_error} = delete $params{on_watch_error};
39 }
40 36 ▶ if (exists $params{retry_delay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"retry_delay"}) 1 35 36
41 1 $self->{retry_delay} = delete $params{retry_delay};
42 }
43  
44 36 ▶ $self->{watch_specs} ||= [];
Condition50%1/2
$self->{"watch_specs"} ||= []
l!l
036
MC/DC0%0/1
$self->{"watch_specs"} ||= []
$self->{"watch_specs"}
Truth table50%1/2
A
A= $self->{"watch_specs"}
Aresult
11
00
45 36 ▶ $self->{queue} ||= [];
Condition50%1/2
$self->{"queue"} ||= []
l!l
036
MC/DC0%0/1
$self->{"queue"} ||= []
$self->{"queue"}
Truth table50%1/2
A
A= $self->{"queue"}
Aresult
11
00
46 36 ▶ $self->{entries} ||= {};
Condition50%1/2
$self->{"entries"} ||= {}
l!l
036
MC/DC0%0/1
$self->{"entries"} ||= {}
$self->{"entries"}
Truth table50%1/2
A
A= $self->{"entries"}
Aresult
11
00
47  
48 36 $self->SUPER::configure(%params);
49 }
50  
51 273 ▶sub kube { $_[0]->{kube} }
Subroutine kube: called CC=1 SCAR=0.0
Pod documented
52 94 ▶sub on_reconcile { $_[0]->{on_reconcile} }
Subroutine on_reconcile: called CC=1 SCAR=0.0
Pod documented
53 18 ▶sub on_watch_error { $_[0]->{on_watch_error} }
Subroutine on_watch_error: called CC=1 SCAR=0.0
Pod documented
54 1 ▶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'} ? : 1 0 1
55  
56 32 ▶sub _add_to_loop {
Subroutine _add_to_loop: called CC=5 SCAR=17.0
57 32 my ($self, $loop) = @_;
58  
59 32 ▶ croak "kube is required" unless $self->kube;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->kube 0 32 32
60 32 ▶ croak "on_reconcile is required" unless $self->on_reconcile;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_reconcile 0 32 32
61  
62 32 ▶ if (!$self->kube->loop) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->kube->loop) { } 4 28 32
elsif ($self->kube->loop != $loop) { } 0 28 28
63 4 $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 32 $self->start;
69 }
70  
71 27 ▶sub _remove_from_loop {
Subroutine _remove_from_loop: called CC=1 SCAR=0.0
72 27 my ($self, $loop) = @_;
73 27 $self->stop;
74 }
75  
76 37 ▶sub start {
Subroutine start: called CC=3 SCAR=11.0
Pod documented
77 37 my ($self) = @_;
78 37 ▶ return if $self->{started};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"started"} 0 37 37
79 37 $self->{started} = 1;
80 37 $self->{stopped} = 0;
81  
82 37 for my $spec (@{ $self->{watch_specs} }) {
83 9 $self->_start_watch_spec($spec);
84 }
85 }
86  
87 45 ▶sub stop {
Subroutine stop: called CC=6 SCAR=18.0
Pod documented
88 45 my ($self) = @_;
89  
90 45 $self->{stopped} = 1;
91 45 $self->{started} = 0;
92  
93 45 ▶ for my $spec (@{ $self->{watch_specs} || [] }) {
Condition50%1/2
$self->{'watch_specs'} || []
l!l
450
MC/DC0%0/1
$self->{'watch_specs'} || []
$self->{'watch_specs'}
Truth table50%1/2
A
A= $self->{'watch_specs'}
Aresult
11
00
94 27 ▶ next unless my $watcher = delete $spec->{watcher};
Branch: 1 branch
BranchTrueFalseTotal
unless my $watcher = delete $spec->{"watcher"} 9 18 27
95 18 $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 18 $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 45 $self->{queue} = [];
112 45 ▶ for my $entry (values %{ $self->{entries} || {} }) {
Condition50%1/2
$self->{'entries'} || {}
l!l
450
MC/DC0%0/1
$self->{'entries'} || {}
$self->{'entries'}
Truth table50%1/2
A
A= $self->{'entries'}
Aresult
11
00
113 12 delete $entry->{retry_future};
114 12 $entry->{queued} = 0;
115 12 $entry->{dirty} = 0;
116 }
117 }
118  
119 15 ▶sub watch_resource {
Subroutine watch_resource: called CC=4 SCAR=13.9
Pod documented
120 15 my ($self, $resource, @args) = @_;
121  
122 15 ▶ croak "Invalid arguments to watch_resource()" if @args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 2 13 15
123 13 my $spec = {
124 resource => $resource,
125 @args,
126 };
127  
128 13 push @{ $self->{watch_specs} }, $spec;
129 13 ▶ $self->_start_watch_spec($spec) if $self->{started} && !$self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"started"} and not $self->{"stopped"} 9 4 13
Condition66%2/3
$self->{"started"} and not $self->{"stopped"}
!ll&&!rl&&r
409
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
130 13 return $spec->{watcher};
131 }
132  
133 18 ▶sub _start_watch_spec {
Subroutine _start_watch_spec: called CC=4 SCAR=13.9
134 18 my ($self, $spec) = @_;
135 18 ▶ return if $spec->{watcher};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"watcher"} 0 18 18
136  
137 18 my %watch_args = %$spec;
138 18 my $resource = delete $watch_args{resource};
139 18 delete @watch_args{qw(key_for)};
140  
141 # ERROR events carry a Status hashref, not a keyed resource object, so they
142 # cannot go through the reconcile workqueue. Report them to the controller
143 # hook instead - an on_error given per watch keeps precedence.
144 18 my $on_watch_error = $self->on_watch_error;
145 18 ▶ if ($on_watch_error && !$watch_args{on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_watch_error and not $watch_args{"on_error"}) 2 16 18
Condition100%3/3
$on_watch_error and not $watch_args{"on_error"}
!ll&&!rl&&r
1512
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
146 2 ▶ $watch_args{on_error} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
147 2 my ($error) = @_;
148 $on_watch_error->($error, {
149 controller => $self,
150 kube => $self->kube,
151 resource => $spec->{resource},
152 2 });
153 2 };
154 }
155  
156 $spec->{watcher} = $self->kube->watcher($resource,
157 %watch_args,
158 63 ▶ on_added => sub { $self->_enqueue_event($spec, 'ADDED', $_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
159 2 ▶ on_modified => sub { $self->_enqueue_event($spec, 'MODIFIED', $_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
160 0 ▶ on_deleted => sub { $self->_enqueue_event($spec, 'DELETED', $_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
161 18 );
162 }
163  
164 65 ▶sub _enqueue_event {
Subroutine _enqueue_event: called CC=6 SCAR=17.9
165 65 my ($self, $spec, $event_type, $object) = @_;
166 65 ▶ return if $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 0 65 65
167  
168 65 my $key = $self->_key_for($spec, $object);
169 65 ▶ my $entry = $self->{entries}{$key} ||= { key => $key };
Condition100%2/2
$self->{"entries"}{$key} ||= {"key", $key}
l!l
461
MC/DC100%1/1
$self->{"entries"}{$key} ||= {"key", $key}
$self->{"entries"}{$key}
Truth table100%2/2
A
A= $self->{"entries"}{$key}
Aresult
11
00
170  
171 $entry->{ctx} = {
172 controller => $self,
173 kube => $self->kube,
174 resource => $spec->{resource},
175 event_type => $event_type,
176 object => $object,
177 key => $key,
178 65 ▶ attempt => ($entry->{failures} // 0) + 1,
Condition100%2/2
$entry->{"failures"} // 0
l!l
164
MC/DC100%1/1
$entry->{"failures"} // 0
$entry->{"failures"}
Truth table100%2/2
A
A= $entry->{"failures"}
Aresult
11
00
179 };
180 # The ctx outlives the reconcile in {entries} whenever the key still has
181 # work pending, so neither ref may be strong: the client would restore the
182 # kube -> controller -> kube cycle weakened above, and the controller
183 # would close controller -> entries -> ctx -> controller on itself.
184 65 weaken($entry->{ctx}{kube});
185 65 weaken($entry->{ctx}{controller});
186  
187 65 ▶ if ($entry->{active}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"active"}) 1 64 65
188 1 $entry->{dirty} = 1;
189 1 return;
190 }
191  
192 64 ▶ return if $entry->{queued};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"queued"} 1 63 64
193 63 $entry->{queued} = 1;
194 63 push @{ $self->{queue} }, $key;
195 63 $self->_schedule_drain;
196 }
197  
198 125 ▶sub _schedule_drain {
Subroutine _schedule_drain: called CC=3 SCAR=11.0
199 125 my ($self) = @_;
200 125 ▶ return if $self->{drain_scheduled} || $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"drain_scheduled"} or $self->{"stopped"} 58 67 125
Condition100%3/3
$self->{"drain_scheduled"} or $self->{"stopped"}
l!l&&r!l&&!r
50867
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
201 67 $self->{drain_scheduled} = 1;
202 67 ▶ $self->loop->later(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
203 67 delete $self->{drain_scheduled};
204 67 $self->_drain_queue;
205 67 });
206 }
207  
208 67 ▶sub _drain_queue {
Subroutine _drain_queue: called CC=12 SCAR=26.0
209 67 my ($self) = @_;
210 67 ▶ return if $self->{stopped} || $self->{active_key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} or $self->{"active_key"} 1 66 67
Condition66%2/3
$self->{"stopped"} or $self->{"active_key"}
l!l&&r!l&&!r
1066
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
211  
212 66 my ($key, $entry);
213 66 ▶ while (defined(my $candidate = shift @{ $self->{queue} || [] })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $candidate = shift @{$self->{"queue"} or [];})) 63 4 67
Condition50%1/2
$self->{'queue'} || []
l!l
670
MC/DC0%0/1
$self->{'queue'} || []
$self->{'queue'}
Truth table50%1/2
A
A= $self->{'queue'}
Aresult
11
00
214 63 my $next = $self->{entries}{$candidate};
215 # A queued key whose entry or ctx is gone has nothing to reconcile.
216 # Skip it and take the next one: returning here would leave the rest
217 # of the queue sitting until an unrelated key schedules the next
218 # drain. Clear the flag so the key can be queued again later.
219 63 ▶ if (!$next || !$next->{ctx}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($next and $next->{"ctx"}) 1 62 63
Condition66%2/3
$next and $next->{"ctx"}
!ll&&!rl&&r
1062
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
220 1 ▶ $next->{queued} = 0 if $next;
Branch: 1 branch
BranchTrueFalseTotal
if $next 0 1 1
221 1 next;
222 }
223 62 ($key, $entry) = ($candidate, $next);
224 62 last;
225 }
226 66 ▶ return unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 4 62 66
227 62 my $ctx = $entry->{ctx};
228  
229 62 $entry->{queued} = 0;
230 62 $entry->{active} = 1;
231 62 $self->{active_key} = $key;
232  
233 62 my $ret = eval { $self->on_reconcile->($ctx) };
234 62 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 62 62
elsif (not builtin::blessed($ret) && $ret->isa('Future')) { } 57 5 62
235 0 $ret = Future->fail($@);
236 ▶ } elsif (!blessed($ret) || !$ret->isa('Future')) {
Condition66%2/3
builtin::blessed($ret) && $ret->isa('Future')
!ll&&!rl&&r
5705
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
237 57 $ret = Future->done($ret);
238 }
239  
240 62 ▶ $ret->on_ready(sub {
Subroutine __ANON__: called CC=6 SCAR=19.9
241 62 my ($f) = @_;
242 62 delete $self->{active_key};
243 62 $entry->{active} = 0;
244  
245 62 ▶ if ($f->is_done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->is_done) { } 58 4 62
246 58 delete $entry->{failures};
247 # Nothing enters the queue while stopped, here no more than
248 # anywhere else: the drain is off, so the key would sit in the
249 # queue stop() just emptied, and the ADDED a restart re-lists would
250 # return early on its queued flag without scheduling a drain.
251 58 ▶ if ($entry->{dirty} && !$self->{stopped}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($entry->{'dirty'} and not $self->{'stopped'}) { } 0 58 58
elsif (not $entry->{'retry_future'}) { } 58 0 58
Condition33%1/3
$entry->{"dirty"} and not $self->{"stopped"}
!ll&&!rl&&r
5800
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
252 0 $entry->{dirty} = 0;
253 0 $entry->{queued} = 1;
254 0 push @{ $self->{queue} }, $key;
255 }
256 elsif (!$entry->{retry_future}) {
257 # Clean reconcile: nothing queued, nothing dirty, no backoff
258 # left to remember. Keeping the entry would hold one hashref
259 # plus its ctx per key seen, for the lifetime of the process.
260 # A DELETED event needs no case of its own - its reconcile
261 # ends here like any other.
262 58 delete $self->{entries}{$key};
263 }
264 } else {
265 4 $entry->{dirty} = 0;
266 4 ▶ $entry->{failures} = ($entry->{failures} // 0) + 1;
Condition50%1/2
$entry->{"failures"} // 0
l!l
04
MC/DC0%0/1
$entry->{"failures"} // 0
$entry->{"failures"}
Truth table50%1/2
A
A= $entry->{"failures"}
Aresult
11
00
267 4 my ($error) = $f->failure;
268 4 $self->_schedule_retry($key, $entry->{ctx}, $entry->{failures}, $error);
269 }
270  
271 62 $self->_schedule_drain;
272 62 });
273 }
274  
275 4 ▶sub _schedule_retry {
Subroutine _schedule_retry: called CC=4 SCAR=15.8
276 4 my ($self, $key, $ctx, $attempt, $error) = @_;
277 4 ▶ return if $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 3 1 4
278  
279 1 ▶ my $entry = $self->{entries}{$key} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->{"entries"}{$key} or return 1 0 1
280 1 my $delay = $self->_retry_after($attempt, $ctx, $error);
281  
282 1 ▶ my $enqueue = sub {
Subroutine __ANON__: called CC=4 SCAR=19.4
283 1 ▶ return if $self->{stopped};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stopped"} 0 1 1
284 1 delete $entry->{retry_future};
285 1 ▶ return if $entry->{active} || $entry->{queued};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"active"} or $entry->{"queued"} 1 0 1
Condition33%1/3
$entry->{"active"} or $entry->{"queued"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$entry->{"active"} or $entry->{"queued"}
$entry->{"active"} $entry->{"queued"}
Truth table33%1/3
A or B
A= $entry->{"active"}B= $entry->{"queued"}
ABresult
1X1
011
000
286 0 $entry->{queued} = 1;
287 0 push @{ $self->{queue} }, $key;
288 0 $self->_schedule_drain;
289 1 };
290  
291 1 ▶ if (!$delay) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($delay) 1 0 1
292 1 $self->loop->later($enqueue);
293 1 return;
294 }
295  
296 0 $entry->{retry_future} = $self->loop->delay_future(after => $delay)->on_ready($enqueue);
297 }
298  
299 1 ▶sub _retry_after {
Subroutine _retry_after: called CC=6 SCAR=31.0
300 1 my ($self, $attempt, $ctx, $error) = @_;
301 1 my $delay = $self->retry_delay;
302  
303 1 ▶ return $delay->($attempt, $ctx, $error) if ref($delay) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $delay eq "CODE" 1 0 1
304 0 ▶ if (ref($delay) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $delay eq "ARRAY") 0 0 0
305 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
306 }
307 0 ▶ return $delay // 0;
Condition0%0/2
$delay // 0
l!l
00
MC/DC0%0/1
$delay // 0
$delay
Truth table0%0/2
A
A= $delay
Aresult
11
00
308 }
309  
310 65 ▶sub _key_for {
Subroutine _key_for: called CC=6 SCAR=20.9
311 65 my ($self, $spec, $object) = @_;
312  
313 65 ▶ if (my $cb = $spec->{key_for}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $spec->{"key_for"}) 0 65 65
314 0 return $cb->($object, $spec);
315 }
316  
317 65 my $meta = $object->metadata;
318 65 ▶ my $name = $meta ? $meta->name : undef;
Branch: 1 branch
BranchTrueFalseTotal
$meta ? : 65 0 65
319 65 ▶ my $namespace = $meta ? $meta->namespace : undef;
Branch: 1 branch
BranchTrueFalseTotal
$meta ? : 65 0 65
320  
321 65 ▶ return defined($namespace) && length($namespace)
Branch: 1 branch
BranchTrueFalseTotal
defined $namespace && length $namespace ? : 65 0 65
Condition33%1/3
defined $namespace and length $namespace
!ll&&!rl&&r
0065
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
322 ? "$namespace/$name"
323 : $name;
324 }
325  
326 9 ▶sub get_object {
Subroutine get_object: called CC=1 SCAR=0.0
Pod documented
327 9 my ($self, @args) = @_;
328 9 return $self->kube->get(@args);
329 }
330  
331 9 ▶sub list_objects {
Subroutine list_objects: called CC=1 SCAR=0.0
Pod documented
332 9 my ($self, @args) = @_;
333 9 return $self->kube->list(@args);
334 }
335  
336 # The controller keeps its own signature - a status => {...} argument instead
337 # of a patch document, merge as the default type - and builds the patch from
338 # it; resolving the target, the request and the response are the client's
339 # patch_status. Its own options are checked here, with the client's k63
340 # helper: the client only sees patch and type, so any other key (typ,
341 # statuss, namespace in the object form) would be dropped silently. Every
342 # other argument check (metadata, name, patch type, unknown resource) happens
343 # there and comes back as a failed Future too.
344 39 ▶sub patch_status {
Subroutine patch_status: called CC=14 SCAR=26.5
Pod documented
345 39 my ($self, $class_or_object, @rest_args) = @_;
346  
347 39 my (%args, @target, @allowed);
348 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
349 12 ▶ return Future->fail("Invalid arguments to patch_status()") if @rest_args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest_args % 2 2 10 12
350 10 %args = @rest_args;
351 10 @allowed = qw( status type );
352 # A class without a status attribute has nothing to fall back on; the
353 # missing status is then reported below like any other.
354 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
355 10 @target = ($class_or_object);
356 } else {
357 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
358 22 $args{name} = shift @rest_args;
359 22 ▶ return Future->fail("Invalid arguments to patch_status()") if @rest_args % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest_args % 2 1 21 22
360 21 %args = (%args, @rest_args);
361 } elsif (@rest_args % 2 == 0) {
362 4 %args = @rest_args;
363 } else {
364 1 return Future->fail("Invalid arguments to patch_status()");
365 }
366 25 @allowed = qw( name namespace status type );
367 25 @target = ($class_or_object, name => $args{name}, namespace => $args{namespace});
368 }
369 35 my $unknown = $self->kube->_unknown_argument_error('patch_status', \%args, @allowed);
370 35 ▶ return Future->fail($unknown) if defined $unknown;
Branch: 1 branch
BranchTrueFalseTotal
if defined $unknown 4 31 35
371 31 ▶ return Future->fail("status required for patch_status") unless defined $args{status};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"status"} 3 28 31
372  
373 return $self->kube->patch_status(@target,
374 patch => { status => $args{status} },
375 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
376 );
377 }
378  
379 6 ▶sub update_status {
Subroutine update_status: called CC=4 SCAR=13.9
Pod documented
380 6 my ($self, $object) = @_;
381  
382 # The client's update_status croaks on these; the controller's helpers
383 # report every error as a failed Future.
384 6 my ($class, $error) = $self->kube->_object_class('update_status', $object);
385 6 ▶ return Future->fail($error) unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 2 4 6
386 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
387 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
388  
389 2 return $self->kube->update_status($object);
390 }
391  
392 48 1;
393  
394 __END__
395  
396 =pod
397  
398 =encoding UTF-8
399  
400 =head1 NAME
401  
402 Net::Async::Kubernetes::Controller - Minimal controller runtime for Net::Async::Kubernetes
403  
404 =head1 VERSION
405  
406 version 0.009
407  
408 =head1 SYNOPSIS
409  
410 use IO::Async::Loop;
411 use Net::Async::Kubernetes;
412  
413 my $loop = IO::Async::Loop->new;
414 my $kube = Net::Async::Kubernetes->new(
415 kubeconfig => "$ENV{HOME}/.kube/config",
416 );
417 $loop->add($kube);
418  
419 my $controller = $kube->controller(
420 on_reconcile => sub {
421 my ($ctx) = @_;
422  
423 return $ctx->{controller}->patch_status($ctx->{object},
424 status => { phase => 'Running' },
425 );
426 },
427 );
428  
429 $controller->watch_resource('Pod', namespace => 'default');
430 $loop->run;
431  
432 =head1 DESCRIPTION
433  
434 L<Net::Async::Kubernetes::Controller> is a minimal controller runtime built on
435 top of L<Net::Async::Kubernetes> and its watcher support.
436  
437 It is intentionally small. The module focuses on the operational pieces most
438 controllers need:
439  
440 =over 4
441  
442 =item *
443  
444 watch registration
445  
446 =item *
447  
448 keyed queueing with deduplication
449  
450 =item *
451  
452 serialized reconcile dispatch
453  
454 =item *
455  
456 retry hooks with configurable delay policy
457  
458 =item *
459  
460 status subresource helpers
461  
462 =back
463  
464 It does not attempt to provide higher-level controller DSL sugar.
465  
466 =head2 configure
467  
468 Internal L<IO::Async::Notifier> configuration method.
469  
470 Accepted parameters:
471  
472 =over 4
473  
474 =item C<kube>
475  
476 Existing L<Net::Async::Kubernetes> instance to bind to. The reference is weak,
477 as in L<Net::Async::Kubernetes::Watcher>, so the caller has to keep the client
478 alive for as long as the controller is used.
479  
480 =item C<kubeconfig>, C<context>, C<server>, C<credentials>, C<resource_map>,
481 C<resource_map_from_cluster>
482  
483 Client construction parameters used when C<kube> is not supplied. A client
484 built this way is owned by the controller.
485  
486 =item C<on_reconcile>
487  
488 Required reconcile callback. Receives a hashref with C<controller>, C<kube>,
489 C<resource>, C<event_type>, C<object>, C<key>, and C<attempt>. C<controller>
490 and C<kube> are weak references. Both are valid for the whole reconcile,
491 including anything chained onto the C<Future> it returns, but a context kept
492 past that keeps neither object alive.
493  
494 =item C<on_watch_error>
495  
496 Optional callback for errors of a registered watch: C<ERROR> events, for
497 example a C<403> arriving mid-stream, and failed watch attempts -- a
498 transport error, the API server rejecting the request, or a stream that
499 closes at once without an event or right after an C<ERROR> event -- which the
500 watcher retries with backoff. Receives C<($error, $ctx)>, where C<$error> is
501 the C<Status> hashref the watcher reports (see
502 L<Net::Async::Kubernetes::Watcher/on_error>; a failed attempt has C<reason>
503 C<WatchFailed>) and C<$ctx> carries C<controller>, C<kube>, and C<resource>.
504 Errors are not reconcile objects, so they never enter the workqueue. An
505 C<on_error> passed to C<watch_resource> takes precedence for that watch.
506 Without either, the watcher C<warn>s about failed attempts.
507  
508 The watcher's reconnect settings (C<reconnect_delay>, C<max_reconnect_delay>,
509 C<reconnect_jitter>, C<max_retries>, C<min_watch_duration>) are passed to
510 C<watch_resource> with the other watcher arguments. A watch that gave up on C<max_retries> stays stopped; C<stop> and
511 C<start> on the controller build it anew.
512  
513 =item C<retry_delay>
514  
515 Optional retry policy. Accepts a fixed scalar delay, an arrayref of delays, or
516 a coderef receiving C<($attempt, $ctx, $error)>.
517  
518 =back
519  
520 =head2 kube
521  
522 Returns the bound L<Net::Async::Kubernetes> client.
523  
524 =head2 on_reconcile
525  
526 Returns the reconcile callback.
527  
528 =head2 on_watch_error
529  
530 Returns the watch error callback, if one is configured.
531  
532 =head2 retry_delay
533  
534 Returns the configured retry delay policy. Defaults to C<1>.
535  
536 =head2 start
537  
538 Starts all registered resource watches. Called automatically when the
539 controller is added to an event loop.
540  
541 =head2 stop
542  
543 Stops registered watches and prevents further queue processing. Each stopped
544 watch is also detached from the client it was registered on, so a watcher
545 handle a caller kept from C<watch_resource> does not survive its controller's
546 C<stop>: it is no longer attached to anything, calling C<start> on it directly
547 drives a notifier with no loop, and the controller's own C<start> builds a
548 fresh watcher for the spec regardless. Do not hold on to a watcher handle past
549 a C<stop>.
550  
551 Pending work is dropped with them: the workqueue is cleared and any retry timer
552 is cancelled, because a restart re-lists through fresh watches and delivers
553 everything still present again. A key's failure count survives, so a key that
554 was retrying picks up at its next attempt number rather than at attempt 1.
555  
556 =head2 watch_resource
557  
558 $controller->watch_resource('Pod',
559 namespace => 'default',
560 key_for => sub {
561 my ($object, $spec) = @_;
562 return $object->metadata->name;
563 },
564 );
565  
566 Registers a watched resource and returns the watcher instance once started.
567 An odd list of parameters croaks, as C<Invalid arguments to
568 watch_resource()>, before anything is registered. Repeated events for the
569 same reconcile key are coalesced into a single queued entry. A key's entry is
570 dropped once it reconciles cleanly, so the queue does not grow with the
571 number of objects seen; a key that is still queued, dirty or retrying keeps
572 its entry, and with it its C<attempt> count.
573  
574 =head2 get_object
575  
576 Thin wrapper around C<< $controller->kube->get(...) >>. Returns its
577 L<Future>, resolving to the inflated IO::K8s object.
578  
579 =head2 list_objects
580  
581 Thin wrapper around C<< $controller->kube->list(...) >>. Returns its
582 L<Future>, resolving to an L<IO::K8s::List>.
583  
584 =head2 patch_status
585  
586 $controller->patch_status('Pod', 'my-pod',
587 namespace => 'default',
588 status => { phase => 'Running' },
589 )->get;
590  
591 $controller->patch_status($object,
592 status => { phase => 'Running' },
593 )->get;
594  
595 Patch the C</status> subresource for an object. Accepts either a class/name
596 pair (the name positional or as C<name =E<gt> ...>) or an object instance,
597 plus a C<status> payload; in the object form, C<status> defaults to the
598 object's own C<status> when its class has one. The helper sends
599 C<{ status =E<gt> $status }> through L<Net::Async::Kubernetes/patch_status>,
600 with the patch type from C<type> (C<merge> by default, C<strategic> or
601 C<json>). Returns a L<Future> that resolves to the patched object.
602  
603 Bad arguments, a missing C<status> (including an object whose class has no
604 C<status> to fall back on), an unknown resource or patch type, and server
605 errors fail the returned L<Future> instead of dying. So does any option other
606 than C<name>, C<namespace>, C<status> and C<type> -- in the object form only
607 C<status> and C<type>, the object gives name and namespace -- before a
608 request is sent: C<Unknown argument 'typ' to patch_status() (allowed: name,
609 namespace, status, type)>. Such an option would otherwise be dropped when the
610 patch is built. An odd list of options after the object or the name fails it
611 as C<Invalid arguments to patch_status()> too, before a request is sent.
612  
613 =head2 update_status
614  
615 $controller->update_status($object)->get;
616  
617 Update the C</status> subresource for a full object instance, through
618 L<Net::Async::Kubernetes/update_status>. Returns a L<Future> that resolves to
619 the updated object. An object without C<metadata> or C<metadata.name>, or one
620 that is no Kubernetes resource (an L<IO::K8s::List>, a nested C<PodSpec>),
621 fails the L<Future> rather than croaking as the client method does.
622  
623 =head1 SEE ALSO
624  
625 =over 4
626  
627 =item *
628  
629 L<Net::Async::Kubernetes>
630  
631 =item *
632  
633 L<Net::Async::Kubernetes::Watcher>
634  
635 =item *
636  
637 L<IO::Async::Notifier>
638  
639 =back
640  
641 =head1 SUPPORT
642  
643 =head2 Issues
644  
645 Please report bugs and feature requests on GitHub at
646 L<https://github.com/Getty/p5-net-async-kubernetes/issues>.
647  
648 =head2 IRC
649  
650 Join C<#kubernetes> on C<irc.perl.org> or message Getty directly.
651  
652 =head1 CONTRIBUTING
653  
654 Contributions are welcome! Please fork the repository and submit a pull request.
655  
656 =head1 AUTHOR
657  
658 Torsten Raudssus <torsten@raudssus.de>
659  
660 =head1 COPYRIGHT AND LICENSE
661  
662 This software is copyright (c) 2026 by Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>.
663  
664 This is free software; you can redistribute it and/or modify it under
665 the same terms as the Perl 5 programming language system itself.
666  
667 =cut
« Kubernetes/PortForwardSession.pm ↑ Summary