blib/lib/Kubernetes/Comb/Client/Async.pm

filter: Statementstmt 87.7% 43 / 49 Branchbran 62.5% 5 / 8 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 79.1% 19 / 24 Podpod n/a totaltotal 76.9% 70 / 91 CC 7 SCAR 6.4
CC
7
Cov
81%
_build_loop
4.1
for_context
3.2
_build_kube
3.0
CRAP 1.9
« examples/lib/Example/Comb/Mailer.pm ↑ Summary examples/lib/Example/Comb/NATS.pm »
1 package Kubernetes::Comb::Client::Async;
2 # ABSTRACT: Asynchronous Comb client on Net::Async::Kubernetes
3 our $VERSION = '0.001';
4  
5 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 with 'Kubernetes::Comb::Role::Client';
7  
8 8 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use IO::K8s;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Module::Runtime qw( module_notional_filename require_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Types::Standard qw( InstanceOf Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Kubernetes::Comb::CRD;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Optional dependencies (recommends): loaded here, not with `use`, so the
17 # rest of the distribution never needs them, and a missing or too old one is
18 # named. Each is a module and, where one is needed, its minimum version --
19 # the only place in the code that states it.
20 for my $optional ( [ 'IO::Async::Loop' ], [ 'Net::Async::Kubernetes', '0.009' ] ) {
21 my ( $module, $minimum ) = @$optional;
22 croak __PACKAGE__.' needs '.$module.', an optional dependency of'
23 .' Kubernetes::Comb that is not available: '.$@
24 unless eval { require_module($module); 1 };
25 next unless defined $minimum;
26 next if eval { $module->VERSION($minimum); 1 };
27 my $found = $module->VERSION;
28 croak __PACKAGE__.' needs '.$module.' '.$minimum.' or newer, an optional'
29 .' dependency of Kubernetes::Comb, and found '
30 .( defined $found ? $found : 'one without a version' )
31 .' in '.$INC{ module_notional_filename($module) };
32 }
33  
34  
35 has kubeconfig => ( is => 'ro', isa => Str, predicate => 1 );
36  
37  
38 has context => ( is => 'ro', isa => Str, predicate => 1 );
39  
40  
41 has kube => (
42 is => 'lazy',
43 isa => InstanceOf['Net::Async::Kubernetes'],
44 predicate => '_has_kube'
45 );
46  
47 5 ▶sub _build_kube {
Subroutine _build_kube: called CC=3 SCAR=11.0
48 5 my ( $self ) = @_;
49 my $kube = Net::Async::Kubernetes->new(
50 ( $self->has_kubeconfig ? ( kubeconfig => $self->kubeconfig ) : () ),
51 ( $self->has_context ? ( context => $self->context ) : () ),
52 resource_map => {
53 5 %{ IO::K8s->default_resource_map },
54 5 ▶ %{ Kubernetes::Comb::CRD->new->resource_map }
Branch: 2 branches
BranchTrueFalseTotal
$self->has_kubeconfig ? : 5 0 5
$self->has_context ? : 2 3 5
55 }
56 );
57 4 $self->loop->add($kube);
58 4 return $kube;
59 }
60  
61  
62 has loop => ( is => 'lazy', isa => InstanceOf['IO::Async::Loop'] );
63  
64 1 ▶sub _build_loop {
Subroutine _build_loop: called CC=3 SCAR=14.2
65 1 my ( $self ) = @_;
66 1 ▶ return $self->kube->loop if $self->_has_kube && $self->kube->loop;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_kube and $self->kube->loop 1 0 1
Condition33%1/3
$self->_has_kube and $self->kube->loop
!ll&&!rl&&r
001
MC/DC0%0/2
$self->_has_kube and $self->kube->loop
$self->_has_kube $self->kube->loop
Truth table33%1/3
A and B
A= $self->_has_kubeB= $self->kube->loop
ABresult
0X0
100
111
67 0 return IO::Async::Loop->new;
68 }
69  
70  
71 2 ▶sub get { shift->_call( get => @_ ) }
Subroutine get: called CC=1 SCAR=0.0
72 0 ▶sub list { shift->_call( list => @_ ) }
Subroutine list: not called CC=1 SCAR=6.9
73 1 ▶sub ensure { shift->_call( ensure => @_ ) }
Subroutine ensure: called CC=1 SCAR=0.0
74 2 ▶sub delete { shift->_call( delete => @_ ) }
Subroutine delete: called CC=1 SCAR=0.0
75 1 ▶sub update { shift->_call( update => @_ ) }
Subroutine update: called CC=1 SCAR=0.0
76 0 ▶sub patch { shift->_call( patch => @_ ) }
Subroutine patch: not called CC=1 SCAR=6.9
77 0 ▶sub update_status { shift->_call( update_status => @_ ) }
Subroutine update_status: not called CC=1 SCAR=6.9
78 0 ▶sub patch_status { shift->_call( patch_status => @_ ) }
Subroutine patch_status: not called CC=1 SCAR=6.9
79 0 ▶sub log { shift->_call( log => @_ ) }
Subroutine log: not called CC=1 SCAR=6.9
80  
81 6 ▶sub _call {
Subroutine _call: called CC=1 SCAR=0.0
82 6 my ( $self, $method, @args ) = @_;
83 6 ▶ return Future->call( sub { $self->kube->$method(@args) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
84 }
85  
86  
87 3 ▶sub server_url { shift->kube->server->endpoint }
Subroutine server_url: called CC=1 SCAR=0.0
88  
89  
90 12 ▶has _for_context => ( is => 'ro', init_arg => undef, default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
91  
92 3 ▶sub for_context {
Subroutine for_context: called CC=3 SCAR=11.7
93 3 my ( $self, $context ) = @_;
94 3 ▶ return $self->_for_context->{$context} //= ref($self)->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->has_kubeconfig ? : 2 0 2
Condition66%2/3
$self->_for_context->{$context} //= ref($self)->new("loop", $self->loop, $self->has_kubeconfig ? ("kubeconfig", $self->kubeconfig) : (), "context", $context)
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->_for_context->{$context} //= ref($self)->new("loop", $self->loop, $self->has_kubeconfig ? ("kubeconfig", $self->kubeconfig) : (), "context", $context)
$self->_for_context->{$context} ref($self)->new("loop", $self->loop, $self->has_kubeconfig ? ("kubeconfig", $self->kubeconfig) : (), "context", $context)
Truth table66%2/3
A //= B
A= $self->_for_context->{$context}B= ref($self)->new("loop", $self->loop, $self->has_kubeconfig ? ("kubeconfig", $self->kubeconfig) : (), "context", $context)
ABresult
1X1
011
000
95 loop => $self->loop,
96 ( $self->has_kubeconfig ? ( kubeconfig => $self->kubeconfig ) : () ),
97 context => $context
98 );
99 }
100  
101  
102 1;
103  
104 __END__
105  
106 =pod
107  
108 =encoding UTF-8
109  
110 =head1 NAME
111  
112 Kubernetes::Comb::Client::Async - Asynchronous Comb client on Net::Async::Kubernetes
113  
114 =head1 VERSION
115  
116 version 0.001
117  
118 =head1 SYNOPSIS
119  
120 use IO::Async::Loop;
121 use Kubernetes::Comb::Client::Async;
122  
123 my $loop = IO::Async::Loop->new;
124 my $k8s = Kubernetes::Comb::Client::Async->new(loop => $loop, context => 'dev');
125 my $k8s = Kubernetes::Comb::Client::Async->new(kube => $kube); # your own client
126  
127 $k8s->list('Pod', namespace => 'platform')->then(sub {
128 my ( $list ) = @_;
129 ...
130 });
131  
132 =head1 DESCRIPTION
133  
134 The client of L<Kubernetes::Comb::Role::Client> for an L<IO::Async> program.
135 Every request goes through L<Net::Async::Kubernetes> and returns its
136 L<Future>; the loop runs the request. Where L<Net::Async::Kubernetes> croaks
137 instead of failing -- bad arguments to C<update>, C<update_status>, C<ensure>
138 -- the croak becomes a failed Future too.
139  
140 L<IO::Async> and L<Net::Async::Kubernetes> are optional dependencies of
141 Kubernetes::Comb. Loading this module without them dies with a message naming
142 the missing one. The client needs L<Net::Async::Kubernetes> 0.009 for
143 C<ensure>, C<update_status>, C<patch_status> and C<delete> with a
144 C<propagationPolicy>, and an older one is refused the same way, when this
145 module is loaded: the message names the version needed, the version found
146 and the file it was found in.
147  
148 =head2 kubeconfig
149  
150 Path of the kubeconfig to read. Defaults to what L<Net::Async::Kubernetes>
151 finds: C<$KUBECONFIG>, then F<~/.kube/config>, then the in-cluster service
152 account.
153  
154 =head2 context
155  
156 Kube context to use. Defaults to the kubeconfig's current context.
157  
158 =head2 kube
159  
160 The L<Net::Async::Kubernetes> instance. Built on first use from
161 L</kubeconfig> and L</context>, with the Comb custom resource in its resource
162 map, and added to L</loop>. A failure building it fails the Future of the
163 request that needed it. One you pass yourself must already be added to a
164 loop.
165  
166 =head2 loop
167  
168 The L<IO::Async::Loop>. Defaults to the loop a passed L</kube> is in, else
169 to C<< IO::Async::Loop->new >>, the process-wide loop.
170  
171 =head2 get
172  
173 =head2 list
174  
175 =head2 ensure
176  
177 =head2 delete
178  
179 =head2 update
180  
181 =head2 patch
182  
183 =head2 update_status
184  
185 =head2 patch_status
186  
187 =head2 log
188  
189 The request methods of L<Kubernetes::Comb::Role::Client>, each passing its
190 arguments to the L<Net::Async::Kubernetes> method of the same name and
191 returning its L<Future>.
192  
193 =head2 server_url
194  
195 The API server URL of L</kube>.
196  
197 =head2 for_context
198  
199 my $dev = $k8s->for_context('dev');
200  
201 The client on the same L</loop> for another context of the same
202 L</kubeconfig> (the default one when none was given), made on first use and
203 kept: asking again returns the same one, so the loop gets one more
204 L<Net::Async::Kubernetes> per context, not per call. Nothing is read until
205 its first request.
206  
207 =head1 SEE ALSO
208  
209 =over
210  
211 =item * L<Kubernetes::Comb::Role::Client>
212  
213 =item * L<Kubernetes::Comb::Client::Sync>
214  
215 =back
216  
217 =head1 SUPPORT
218  
219 =head2 Issues
220  
221 Please report bugs and feature requests on GitHub at
222 L<https://github.com/Getty/p5-kubernetes-comb/issues>.
223  
224 =head2 IRC
225  
226 Join C<#kubernetes> on C<irc.perl.org> or message Getty directly.
227  
228 =head1 CONTRIBUTING
229  
230 Contributions are welcome! Please fork the repository and submit a pull request.
231  
232 =head1 AUTHOR
233  
234 Torsten Raudssus <getty@cpan.org>
235  
236 =head1 COPYRIGHT AND LICENSE
237  
238 This software is copyright (c) 2026 by Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>.
239  
240 This is free software; you can redistribute it and/or modify it under
241 the same terms as the Perl 5 programming language system itself.
242  
243 =cut
« examples/lib/Example/Comb/Mailer.pm ↑ Summary examples/lib/Example/Comb/NATS.pm »