| 1 | package Net::Async::Kubernetes::Controller; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Minimal controller runtime for Net::Async::Kubernetes | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 54 | our $VERSION = '0.011'; | |||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 40 | ▶ | if (exists $params{on_reconcile}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 40 | $self->{on_reconcile} = delete $params{on_reconcile}; | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 40 | ▶ | if (exists $params{on_watch_error}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | $self->{on_watch_error} = delete $params{on_watch_error}; | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 40 | ▶ | if (exists $params{retry_delay}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 3 | $self->{retry_delay} = delete $params{retry_delay}; | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 40 | ▶ | $self->{watch_specs} ||= []; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"watch_specs"} ||= []
MC/DC0%0/1
$self->{"watch_specs"} ||= []
$self->{"watch_specs"}
Truth table50%1/2
A
A= $self->{"watch_specs"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 40 | ▶ | $self->{queue} ||= []; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"queue"} ||= []
MC/DC0%0/1
$self->{"queue"} ||= []
$self->{"queue"}
Truth table50%1/2
A
A= $self->{"queue"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 40 | ▶ | $self->{entries} ||= {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"entries"} ||= {}
MC/DC0%0/1
$self->{"entries"} ||= {}
$self->{"entries"}
Truth table50%1/2
A
A= $self->{"entries"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 36 | ▶ | croak "on_reconcile is required" unless $self->on_reconcile; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 36 | ▶ | if (!$self->kube->loop) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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'} || []
MC/DC0%0/1
$self->{'watch_specs'} || []
$self->{'watch_specs'}
Truth table50%1/2
A
A= $self->{'watch_specs'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 35 | ▶ | next unless my $watcher = delete $spec->{watcher}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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'} || {}
MC/DC0%0/1
$self->{'entries'} || {}
$self->{'entries'}
Truth table50%1/2
A
A= $self->{'entries'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$self->{"started"} and not $self->{"stopped"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$on_watch_error and not $watch_args{"on_error"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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}
MC/DC100%1/1
$self->{"entries"}{$key} ||= {"key", $key}
$self->{"entries"}{$key}
Truth table100%2/2
A
A= $self->{"entries"}{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$entry->{"failures"} // 0
$entry->{"failures"}
Truth table100%2/2
A
A= $entry->{"failures"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 1 | $entry->{dirty} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 194 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 70 | ▶ | return if $entry->{queued}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$self->{"drain_scheduled"} or $self->{"stopped"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$self->{"stopped"} or $self->{"active_key"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 77 | my ($key, $entry); | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | 77 | ▶ | while (defined(my $candidate = shift @{ $self->{queue} || [] })) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{'queue'} || []
MC/DC0%0/1
$self->{'queue'} || []
$self->{'queue'}
Truth table50%1/2
A
A= $self->{'queue'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$next and $next->{"ctx"}
MC/DC50%1/2
$next and $next->{"ctx"}
$next
$next->{"ctx"}
Truth table66%2/3
A and B
A= $nextB= $next->{"ctx"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 1 | ▶ | $next->{queued} = 0 if $next; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 69 | ($key, $entry) = ($candidate, $next); | |||||||||||||||||||||||||||||||||||||||||||||
| 229 | 69 | last; | |||||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 77 | ▶ | return unless $entry; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | $ret = Future->fail($@); | |||||||||||||||||||||||||||||||||||||||||||||
| 241 | ▶ | } elsif (!blessed($ret) || !$ret->isa('Future')) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($ret) && $ret->isa('Future')
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')
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$entry->{"dirty"} and not $self->{"stopped"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$entry->{"failures"} // 0
$entry->{"failures"}
Truth table100%2/2
A
A= $entry->{"failures"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 4 | ▶ | my $entry = $self->{entries}{$key} or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 3 | delete $entry->{retry_future}; | |||||||||||||||||||||||||||||||||||||||||||||
| 290 | 3 | ▶ | return if $entry->{active} || $entry->{queued}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$entry->{"active"} or $entry->{"queued"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 2 | ▶ | my $ctx = { %$old, attempt => ($entry->{failures} // 0) + 1 }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$entry->{"failures"} // 0
MC/DC0%0/1
$entry->{"failures"} // 0
$entry->{"failures"}
Truth table50%1/2
A
A= $entry->{"failures"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 3 | ▶ | if (ref($delay) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | ▶ | return $delay->[$attempt - 1] // $delay->[-1] // 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
($$delay[$attempt - 1] // $delay->[-1]) // 0
$$delay[$attempt - 1] // $delay->[-1]
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]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 3 | ▶ | return $delay // 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$delay // 0
MC/DC0%0/1
$delay // 0
$delay
Truth table50%1/2
A
A= $delay
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 69 | ▶ | my $namespace = $meta ? $meta->namespace : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 69 | ▶ | my $id = defined($namespace) && length($namespace) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $namespace and length $namespace
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
ref $class_or_object and builtin::blessed($class_or_object)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 12 | ▶ | return Future->fail("Invalid arguments to patch_status()") if @rest_args % 2; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$args{"status"} //= $class_or_object->status
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
@rest_args >= 1 and not ref $rest_args[0] and not $rest_args[0] =~ /^(name|namespace|status|type)$/
@rest_args >= 1 and not ref $rest_args[0]
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)$/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 22 | $args{name} = shift @rest_args; | |||||||||||||||||||||||||||||||||||||||||||||
| 373 | 22 | ▶ | return Future->fail("Invalid arguments to patch_status()") if @rest_args % 2; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 31 | ▶ | return Future->fail("status required for patch_status") unless defined $args{status}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC100%1/1
$args{"type"} // "merge"
$args{"type"}
Truth table100%2/2
A
A= $args{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 4 | ▶ | my $metadata = $object->metadata or return Future->fail("object must have metadata"); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 3 | ▶ | $metadata->name or return Future->fail("object must have metadata.name"); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 2 | return $self->kube->update_status($object); | |||||||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 54 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||
| 408 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||
| 410 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||
| 412 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||
| 414 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||
| 416 | Net::Async::Kubernetes::Controller - Minimal controller runtime for Net::Async::Kubernetes | ||||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||||
| 418 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||
| 420 | version 0.011 | ||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||
| 422 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||
| 424 | use IO::Async::Loop; | ||||||||||||||||||||||||||||||||||||||||||||||
| 425 | use Net::Async::Kubernetes; | ||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||
| 427 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||||||||||||||||||||||||||||
| 428 | my $kube = Net::Async::Kubernetes->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 429 | kubeconfig => "$ENV{HOME}/.kube/config", | ||||||||||||||||||||||||||||||||||||||||||||||
| 430 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 431 | $loop->add($kube); | ||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||
| 433 | my $controller = $kube->controller( | ||||||||||||||||||||||||||||||||||||||||||||||
| 434 | on_reconcile => sub { | ||||||||||||||||||||||||||||||||||||||||||||||
| 435 | my ($ctx) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||||
| 437 | return $ctx->{controller}->patch_status($ctx->{object}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 438 | status => { phase => 'Running' }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 439 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 440 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 441 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||||||
| 443 | $controller->watch_resource('Pod', namespace => 'default'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 444 | $loop->run; | ||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||
| 446 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||
| 448 | L<Net::Async::Kubernetes::Controller> is a minimal controller runtime built on | ||||||||||||||||||||||||||||||||||||||||||||||
| 449 | top of L<Net::Async::Kubernetes> and its watcher support. | ||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||
| 451 | It is intentionally small. The module focuses on the operational pieces most | ||||||||||||||||||||||||||||||||||||||||||||||
| 452 | controllers need: | ||||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||||
| 454 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||
| 456 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||
| 458 | watch registration | ||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||
| 460 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||||||
| 462 | keyed queueing with deduplication | ||||||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||||||
| 464 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||
| 466 | serialized reconcile dispatch | ||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||
| 468 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||
| 470 | retry hooks with configurable delay policy | ||||||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||||||
| 472 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||||||
| 474 | status subresource helpers | ||||||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||||||
| 476 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||||||
| 478 | It does not attempt to provide higher-level controller DSL sugar. | ||||||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||||||
| 480 | =head2 configure | ||||||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||||||
| 482 | Internal L<IO::Async::Notifier> configuration method. | ||||||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||||||
| 484 | Accepted parameters: | ||||||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||||||
| 486 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||
| 488 | =item C<kube> | ||||||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||||||
| 490 | Existing L<Net::Async::Kubernetes> instance to bind to. The reference is weak, | ||||||||||||||||||||||||||||||||||||||||||||||
| 491 | as in L<Net::Async::Kubernetes::Watcher>, so the caller has to keep the client | ||||||||||||||||||||||||||||||||||||||||||||||
| 492 | alive for as long as the controller is used. | ||||||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||||||
| 494 | =item C<kubeconfig>, C<context>, C<server>, C<credentials>, C<resource_map>, | ||||||||||||||||||||||||||||||||||||||||||||||
| 495 | C<resource_map_from_cluster> | ||||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||||
| 497 | Client construction parameters used when C<kube> is not supplied. A client | ||||||||||||||||||||||||||||||||||||||||||||||
| 498 | built this way is owned by the controller. | ||||||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||||||
| 500 | =item C<on_reconcile> | ||||||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||||||
| 502 | Required reconcile callback. Receives a hashref with C<controller>, C<kube>, | ||||||||||||||||||||||||||||||||||||||||||||||
| 503 | C<resource>, C<event_type>, C<object>, C<key>, and C<attempt>. C<controller> | ||||||||||||||||||||||||||||||||||||||||||||||
| 504 | and C<kube> are weak references. Both are valid for the whole reconcile, | ||||||||||||||||||||||||||||||||||||||||||||||
| 505 | including anything chained onto the C<Future> it returns, but a context kept | ||||||||||||||||||||||||||||||||||||||||||||||
| 506 | past that keeps neither object alive. | ||||||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||||||
| 508 | =item C<on_watch_error> | ||||||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||||||
| 510 | Optional callback for errors of a registered watch: C<ERROR> events, for | ||||||||||||||||||||||||||||||||||||||||||||||
| 511 | example a C<403> arriving mid-stream, and failed watch attempts -- a | ||||||||||||||||||||||||||||||||||||||||||||||
| 512 | transport error, the API server rejecting the request, or a stream that | ||||||||||||||||||||||||||||||||||||||||||||||
| 513 | closes at once without an event or right after an C<ERROR> event -- which the | ||||||||||||||||||||||||||||||||||||||||||||||
| 514 | watcher retries with backoff. Receives C<($error, $ctx)>, where C<$error> is | ||||||||||||||||||||||||||||||||||||||||||||||
| 515 | the C<Status> hashref the watcher reports (see | ||||||||||||||||||||||||||||||||||||||||||||||
| 516 | L<Net::Async::Kubernetes::Watcher/on_error>; a failed attempt has C<reason> | ||||||||||||||||||||||||||||||||||||||||||||||
| 517 | C<WatchFailed>) and C<$ctx> carries C<controller>, C<kube>, and C<resource>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 518 | Errors are not reconcile objects, so they never enter the workqueue. An | ||||||||||||||||||||||||||||||||||||||||||||||
| 519 | C<on_error> passed to C<watch_resource> takes precedence for that watch. | ||||||||||||||||||||||||||||||||||||||||||||||
| 520 | Without either, the watcher C<warn>s about failed attempts. | ||||||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||||||
| 522 | The watcher's reconnect settings (C<reconnect_delay>, C<max_reconnect_delay>, | ||||||||||||||||||||||||||||||||||||||||||||||
| 523 | C<reconnect_jitter>, C<max_retries>, C<min_watch_duration>) are passed to | ||||||||||||||||||||||||||||||||||||||||||||||
| 524 | C<watch_resource> with the other watcher arguments. A watch that gave up on C<max_retries> stays stopped; C<stop> and | ||||||||||||||||||||||||||||||||||||||||||||||
| 525 | C<start> on the controller build it anew. | ||||||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||||||
| 527 | =item C<retry_delay> | ||||||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||||||
| 529 | Optional retry policy. Accepts a fixed scalar delay, an arrayref of delays, or | ||||||||||||||||||||||||||||||||||||||||||||||
| 530 | a coderef receiving C<($attempt, $ctx, $error)>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||||||
| 532 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||||||
| 534 | =head2 kube | ||||||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||||||
| 536 | Returns the bound L<Net::Async::Kubernetes> client. | ||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||
| 538 | =head2 on_reconcile | ||||||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||||||
| 540 | Returns the reconcile callback. | ||||||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||||||
| 542 | =head2 on_watch_error | ||||||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||||||
| 544 | Returns the watch error callback, if one is configured. | ||||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||
| 546 | =head2 retry_delay | ||||||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||||||
| 548 | Returns the configured retry delay policy. Defaults to C<1>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||||||
| 550 | =head2 start | ||||||||||||||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||||||||||||||
| 552 | Starts all registered resource watches. Called automatically when the | ||||||||||||||||||||||||||||||||||||||||||||||
| 553 | controller is added to an event loop. | ||||||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||||||
| 555 | =head2 stop | ||||||||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||||||
| 557 | Stops registered watches and prevents further queue processing. Each stopped | ||||||||||||||||||||||||||||||||||||||||||||||
| 558 | watch is also detached from the client it was registered on, so a watcher | ||||||||||||||||||||||||||||||||||||||||||||||
| 559 | handle a caller kept from C<watch_resource> does not survive its controller's | ||||||||||||||||||||||||||||||||||||||||||||||
| 560 | C<stop>: it is no longer attached to anything, calling C<start> on it directly | ||||||||||||||||||||||||||||||||||||||||||||||
| 561 | drives a notifier with no loop, and the controller's own C<start> builds a | ||||||||||||||||||||||||||||||||||||||||||||||
| 562 | fresh watcher for the spec regardless. Do not hold on to a watcher handle past | ||||||||||||||||||||||||||||||||||||||||||||||
| 563 | a C<stop>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||||||
| 565 | Pending work is dropped with them: the workqueue is cleared and any retry timer | ||||||||||||||||||||||||||||||||||||||||||||||
| 566 | is cancelled, because a restart re-lists through fresh watches and delivers | ||||||||||||||||||||||||||||||||||||||||||||||
| 567 | everything still present again. A key's failure count survives, so a key that | ||||||||||||||||||||||||||||||||||||||||||||||
| 568 | was retrying picks up at its next attempt number rather than at attempt 1. | ||||||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||||||
| 570 | =head2 watch_resource | ||||||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||||||
| 572 | $controller->watch_resource('Pod', | ||||||||||||||||||||||||||||||||||||||||||||||
| 573 | namespace => 'default', | ||||||||||||||||||||||||||||||||||||||||||||||
| 574 | key_for => sub { | ||||||||||||||||||||||||||||||||||||||||||||||
| 575 | my ($object, $spec) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 576 | return $object->metadata->name; | ||||||||||||||||||||||||||||||||||||||||||||||
| 577 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 578 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||||||
| 580 | Registers a watched resource and returns the watcher instance once started. | ||||||||||||||||||||||||||||||||||||||||||||||
| 581 | An odd list of parameters croaks, as C<Invalid arguments to | ||||||||||||||||||||||||||||||||||||||||||||||
| 582 | watch_resource()>, before anything is registered. Repeated events for the | ||||||||||||||||||||||||||||||||||||||||||||||
| 583 | same reconcile key are coalesced into a single queued entry. | ||||||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||||||
| 585 | The default reconcile key is C<Resource:namespace/name> | ||||||||||||||||||||||||||||||||||||||||||||||
| 586 | (C<Resource:name> for a cluster-scoped object), with C<Resource> the name the | ||||||||||||||||||||||||||||||||||||||||||||||
| 587 | resource was registered under, e.g. C<Pod:default/web>. Objects of different | ||||||||||||||||||||||||||||||||||||||||||||||
| 588 | watched resources that share a namespace and name, such as | ||||||||||||||||||||||||||||||||||||||||||||||
| 589 | C<ConfigMap default/web> and C<Service default/web>, are therefore reconciled | ||||||||||||||||||||||||||||||||||||||||||||||
| 590 | on their own, each with its own event in the context. | ||||||||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||||||
| 592 | To coalesce them instead -- the controller-runtime pattern of mapping a | ||||||||||||||||||||||||||||||||||||||||||||||
| 593 | secondary resource onto the key of its primary one, with a reconcile that | ||||||||||||||||||||||||||||||||||||||||||||||
| 594 | re-reads the state it needs -- set C<key_for>, which is called as | ||||||||||||||||||||||||||||||||||||||||||||||
| 595 | C<< $key_for->($object, $spec) >> with C<< $spec->{resource} >> naming the | ||||||||||||||||||||||||||||||||||||||||||||||
| 596 | watched resource, and return the shared key: | ||||||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||||||
| 598 | key_for => sub { | ||||||||||||||||||||||||||||||||||||||||||||||
| 599 | my ($object, $spec) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 600 | return $object->metadata->namespace.'/'.$object->metadata->name; | ||||||||||||||||||||||||||||||||||||||||||||||
| 601 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||||||||||||||
| 603 | The context of a coalesced key carries only the last event (C<resource>, | ||||||||||||||||||||||||||||||||||||||||||||||
| 604 | C<event_type> and C<object>). | ||||||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||||||
| 606 | A key's entry is | ||||||||||||||||||||||||||||||||||||||||||||||
| 607 | dropped once it reconciles cleanly, so the queue does not grow with the | ||||||||||||||||||||||||||||||||||||||||||||||
| 608 | number of objects seen; a key that is still queued, dirty or retrying keeps | ||||||||||||||||||||||||||||||||||||||||||||||
| 609 | its entry, and with it its C<attempt> count. | ||||||||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||||||
| 611 | =head2 get_object | ||||||||||||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||||||||||||
| 613 | Thin wrapper around C<< $controller->kube->get(...) >>. Returns its | ||||||||||||||||||||||||||||||||||||||||||||||
| 614 | L<Future>, resolving to the inflated IO::K8s object. | ||||||||||||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||||||||||||
| 616 | =head2 list_objects | ||||||||||||||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||||||||||||||
| 618 | Thin wrapper around C<< $controller->kube->list(...) >>. Returns its | ||||||||||||||||||||||||||||||||||||||||||||||
| 619 | L<Future>, resolving to an L<IO::K8s::List>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||||||||
| 621 | =head2 patch_status | ||||||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||||||
| 623 | $controller->patch_status('Pod', 'my-pod', | ||||||||||||||||||||||||||||||||||||||||||||||
| 624 | namespace => 'default', | ||||||||||||||||||||||||||||||||||||||||||||||
| 625 | status => { phase => 'Running' }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 626 | )->get; | ||||||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||||||
| 628 | $controller->patch_status($object, | ||||||||||||||||||||||||||||||||||||||||||||||
| 629 | status => { phase => 'Running' }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 630 | )->get; | ||||||||||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||||||||||
| 632 | Patch the C</status> subresource for an object. Accepts either a class/name | ||||||||||||||||||||||||||||||||||||||||||||||
| 633 | pair (the name positional or as C<name =E<gt> ...>) or an object instance, | ||||||||||||||||||||||||||||||||||||||||||||||
| 634 | plus a C<status> payload; in the object form, C<status> defaults to the | ||||||||||||||||||||||||||||||||||||||||||||||
| 635 | object's own C<status> when its class has one. The helper sends | ||||||||||||||||||||||||||||||||||||||||||||||
| 636 | C<{ status =E<gt> $status }> through L<Net::Async::Kubernetes/patch_status>, | ||||||||||||||||||||||||||||||||||||||||||||||
| 637 | with the patch type from C<type> (C<merge> by default, C<strategic> or | ||||||||||||||||||||||||||||||||||||||||||||||
| 638 | C<json>). Returns a L<Future> that resolves to the patched object. | ||||||||||||||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||||||||||||||
| 640 | Bad arguments, a missing C<status> (including an object whose class has no | ||||||||||||||||||||||||||||||||||||||||||||||
| 641 | C<status> to fall back on), an unknown resource or patch type, and server | ||||||||||||||||||||||||||||||||||||||||||||||
| 642 | errors fail the returned L<Future> instead of dying. So does any option other | ||||||||||||||||||||||||||||||||||||||||||||||
| 643 | than C<name>, C<namespace>, C<status> and C<type> -- in the object form only | ||||||||||||||||||||||||||||||||||||||||||||||
| 644 | C<status> and C<type>, the object gives name and namespace -- before a | ||||||||||||||||||||||||||||||||||||||||||||||
| 645 | request is sent: C<Unknown argument 'typ' to patch_status() (allowed: name, | ||||||||||||||||||||||||||||||||||||||||||||||
| 646 | namespace, status, type)>. Such an option would otherwise be dropped when the | ||||||||||||||||||||||||||||||||||||||||||||||
| 647 | patch is built. An odd list of options after the object or the name fails it | ||||||||||||||||||||||||||||||||||||||||||||||
| 648 | as C<Invalid arguments to patch_status()> too, before a request is sent. | ||||||||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||||||
| 650 | =head2 update_status | ||||||||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||||||||
| 652 | $controller->update_status($object)->get; | ||||||||||||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||||||||||||
| 654 | Update the C</status> subresource for a full object instance, through | ||||||||||||||||||||||||||||||||||||||||||||||
| 655 | L<Net::Async::Kubernetes/update_status>. Returns a L<Future> that resolves to | ||||||||||||||||||||||||||||||||||||||||||||||
| 656 | the updated object. An object without C<metadata> or C<metadata.name>, or one | ||||||||||||||||||||||||||||||||||||||||||||||
| 657 | that is no Kubernetes resource (an L<IO::K8s::List>, a nested C<PodSpec>), | ||||||||||||||||||||||||||||||||||||||||||||||
| 658 | fails the L<Future> rather than croaking as the client method does. | ||||||||||||||||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||||||||||||||||
| 660 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||||||||||||
| 662 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||||||||||
| 664 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||||||||||||||||
| 666 | L<Net::Async::Kubernetes> | ||||||||||||||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||||||||||||||
| 668 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||||||||||||||||
| 670 | L<Net::Async::Kubernetes::Watcher> | ||||||||||||||||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||||||||||||||||
| 672 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||||||||||||||||
| 674 | L<IO::Async::Notifier> | ||||||||||||||||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||||||||||||||||
| 676 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||||||||||||
| 678 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||||||
| 680 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||||||||||||||||
| 682 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||||||
| 683 | L<https://github.com/Getty/p5-net-async-kubernetes/issues>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||||||||||||||||||||
| 685 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||||||||||||||||||||
| 687 | Join C<#kubernetes> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||||||||||||||
| 689 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||||||||||||||
| 691 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||||||||||||||||
| 693 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||||||||||||||||||||
| 695 | Torsten Raudssus <torsten@raudssus.de> | ||||||||||||||||||||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||||||||||||||||||||
| 697 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||||||
| 699 | This software is copyright (c) 2026 by Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||||||||||||||
| 701 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||
| 702 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||||||||||||||||||||
| 704 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||