blib/lib/Kubernetes/Comb/Upstream/K8s.pm

filter: Statementstmt 100% 59 / 59 Branchbran 94.1% 32 / 34 Conditioncond 77.7% 14 / 18 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 16 / 16 Podpod 100% 3 / 3 totaltotal 92.1% 129 / 140 CC 25 SCAR 15.8
CC
25
Cov
95%
__ANON__
9.1
__ANON__
7.0
__ANON__
5.0
CRAP 4.8
« blib/lib/Kubernetes/Comb/CRD/CombSpec.pm ↑ Summary blib/lib/Kubernetes/Comb/Role/Static.pm »
1 package Kubernetes::Comb::Upstream::K8s;
2 # ABSTRACT: Borrow a Comb's service from its peer Comb in another kube context
3 2 our $VERSION = '0.001';
4  
5 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 with 'Kubernetes::Comb::Role::Upstream';
7  
8 2 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Types::Standard qw( Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Kubernetes::Comb::Endpoint;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 2 has context => ( is => 'ro', isa => Str, required => 1 );
15  
16  
17 2 has namespace => ( is => 'ro', isa => Str, predicate => 1 );
18  
19  
20 2 has name => ( is => 'ro', isa => Str, predicate => 1 );
21  
22  
23 14 ▶sub endpoint_class { 'Kubernetes::Comb::Endpoint' }
Subroutine endpoint_class: called CC=1 SCAR=0.0
Pod documented
24  
25  
26 31 ▶sub status {
Subroutine status: called CC=1 SCAR=0.0
Pod documented
27 31 my ( $self, $comb ) = @_;
28 31 ▶ return $self->_observe($comb)->then( sub {
Subroutine __ANON__: called CC=9 SCAR=22.1
29 31 my ( $seen ) = @_;
30 31 my %status = ( context => $self->context, via => [ $self->context ] );
31 return Future->done( { %status, reachable => 0, message => $seen->{error} } )
32 31 ▶ if defined $seen->{error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $seen->{"error"} 7 24 31
33 24 my $recorded = $seen->{peer}->status;
34 3 my @unreachable = map { $_->name }
35 24 grep { !defined $self->_address_of( $_, $seen->{same} ) } $self->_published($recorded);
36 24 ▶ my $upstream = $recorded ? $recorded->upstream : undef;
Branch: 1 branch
BranchTrueFalseTotal
$recorded ? : 22 2 24
37 return Future->done( {
38 %status,
39 reachable => 1,
40 ( $recorded && defined $recorded->phase ? ( phase => $recorded->phase ) : () ),
41 14 ▶ via => [ $self->context, $upstream ? @{ $upstream->via // [] } : () ],
Branch: 2 branches
BranchTrueFalseTotal
@unreachable ? : 3 21 24
$seen->{'same'} ? : 0 3 3
Condition50%1/2
$upstream->via // []
l!l
140
MC/DC0%0/1
$upstream->via // []
$upstream->via
Truth table50%1/2
A
A= $upstream->via
Aresult
11
00
42 ( @unreachable ? ( message => 'no address reachable from here for endpoint(s) '
43 .join( ', ', @unreachable ).': '.( $seen->{same}
44 ? $seen->{where}.' publishes none'
45 : 'context '.$self->context.' is another API server, and '.$seen->{where}
46 24 ▶ .' publishes no external address' ) ) : () )
Branch: 2 branches
BranchTrueFalseTotal
$recorded && defined $recorded->phase ? : 22 2 24
$upstream ? : 14 10 24
Condition66%2/3
$recorded and defined $recorded->phase
!ll&&!rl&&r
2022
MC/DC50%1/2
$recorded and defined $recorded->phase
$recorded defined $recorded->phase
Truth table66%2/3
A and B
A= $recordedB= defined $recorded->phase
ABresult
0X0
100
111
47 } );
48 31 } );
49 }
50  
51  
52 22 ▶sub endpoints {
Subroutine endpoints: called CC=1 SCAR=0.0
Pod documented
53 22 my ( $self, $comb ) = @_;
54 22 ▶ return $self->_observe($comb)->then( sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
55 22 my ( $seen ) = @_;
56 22 ▶ return Future->fail( $seen->{error} ) if defined $seen->{error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $seen->{"error"} 5 17 22
57 return Future->done( [ map {
58 15 my $address = $self->_address_of( $_, $seen->{same} );
59 15 ▶ defined $address
Branch: 3 branches
BranchTrueFalseTotal
defined $address ? : 14 1 15
defined $_->protocol ? : 12 2 14
defined $_->external ? : 9 5 14
60 ? $self->endpoint_class->new(
61 name => $_->name,
62 port => $_->port,
63 ( defined $_->protocol ? ( protocol => $_->protocol ) : () ),
64 cluster => $address,
65 ( defined $_->external ? ( external => $_->external ) : () )
66 )
67 : ();
68 17 } $self->_published( $seen->{peer}->status ) ] );
69 22 } );
70 }
71  
72  
73 41 ▶sub _published {
Subroutine _published: called CC=3 SCAR=11.0
74 41 my ( $self, $recorded ) = @_;
75 41 ▶ return $recorded ? @{ $recorded->endpoints // [] } : ();
Branch: 1 branch
BranchTrueFalseTotal
$recorded ? : 37 4 41
Condition100%2/2
$recorded->endpoints // []
l!l
361
MC/DC100%1/1
$recorded->endpoints // []
$recorded->endpoints
Truth table100%2/2
A
A= $recorded->endpoints
Aresult
11
00
76 }
77  
78 33 ▶sub _address_of {
Subroutine _address_of: called CC=3 SCAR=11.1
79 33 my ( $self, $endpoint, $same ) = @_;
80 33 ▶ return $same ? $endpoint->cluster // $endpoint->external : $endpoint->external;
Branch: 1 branch
BranchTrueFalseTotal
$same ? : 19 14 33
Condition66%2/3
$endpoint->cluster // $endpoint->external
l!l&&r!l&&!r
1810
MC/DC0%0/2
$endpoint->cluster // $endpoint->external
$endpoint->cluster $endpoint->external
Truth table66%2/3
A // B
A= $endpoint->clusterB= $endpoint->external
ABresult
1X1
011
000
81 }
82  
83 # Future of { peer, same, where } or { error }; never fails.
84 53 ▶sub _observe {
Subroutine _observe: called CC=1 SCAR=0.0
85 53 my ( $self, $comb ) = @_;
86 53 my $where;
87 53 ▶ return Future->call( sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
88 53 ▶ my $namespace = $self->has_namespace ? $self->namespace : $comb->namespace;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_namespace ? : 31 22 53
89 51 ▶ my $name = $self->has_name ? $self->name : $comb->name;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_name ? : 1 50 51
90 51 $where = 'Comb '.$namespace.'/'.$name.' in context '.$self->context;
91 51 my $client = $comb->k8s->for_context( $self->context );
92 51 ▶ my $same = $client->server_url eq $comb->k8s->server_url ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$client->server_url eq $comb->k8s->server_url ? : 18 28 46
93 46 ▶ return Future->done( { error => $where.' is this Comb itself' } )
Branch: 1 branch
BranchTrueFalseTotal
if $same and $namespace eq $comb->namespace and $name eq $comb->name 1 45 46
Condition83%5/6
$same and $namespace eq $comb->namespace and $name eq $comb->name
!ll&&!rl&&r
4501
$same and $namespace eq $comb->namespace
!ll&&!rl&&r
28171
MC/DC66%2/3
$same and $namespace eq $comb->namespace and $name eq $comb->name
$same $namespace eq $comb->namespace $name eq $comb->name
Truth table75%3/4
A and B and C
A= $sameB= $namespace eq $comb->namespaceC= $name eq $comb->name
ABCresult
0XX0
10X0
1100
1111
94 if $same && $namespace eq $comb->namespace && $name eq $comb->name;
95 41 ▶ return $client->get( '+'.$comb->crd_class, $name, namespace => $namespace )->then( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
96 41 Future->done( { peer => $_[0], same => $same, where => $where } );
97 45 } );
98 11 ▶ } )->else( sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
99 11 ▶ my $error = defined $_[0] ? ( ''.$_[0] ) =~ s/\s+\z//r : 'unknown error';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 11 0 11
100 11 ▶ Future->done( { error => 'reading '.( $where // 'the peer Comb' ).' failed: '.$error } );
Condition100%2/2
$where // "the peer Comb"
l!l
92
MC/DC100%1/1
$where // "the peer Comb"
$where
Truth table100%2/2
A
A= $where
Aresult
11
00
101 53 } );
102 }
103  
104  
105 2 1;
106  
107 __END__
108  
109 =pod
110  
111 =encoding UTF-8
112  
113 =head1 NAME
114  
115 Kubernetes::Comb::Upstream::K8s - Borrow a Comb's service from its peer Comb in another kube context
116  
117 =head1 VERSION
118  
119 version 0.001
120  
121 =head1 SYNOPSIS
122  
123 # the controlling code: every Comb borrows from its peer in dev
124 my $comb = Kubernetes::Comb->from_crd($cr,
125 upstream => sub { K8s => ( context => 'dev' ) }
126 );
127  
128 # the same in the custom resource
129 upstream:
130 class: Kubernetes::Comb::Upstream::K8s
131 context: dev
132 namespace: platform # default: the Comb's own
133 name: nats # default: the Comb's own
134  
135 =head1 DESCRIPTION
136  
137 The layering upstream: the service comes from the peer Comb -- the same
138 Comb, one layer up -- in another kube context. It reads the peer's C<Comb>
139 custom resource and nothing else, so C<get> on C<combs> in that namespace is
140 all the access it needs. The custom resource only names the context; the
141 credentials are those of the local kubeconfig.
142  
143 The client for the context comes from the Comb's own
144 (L<Kubernetes::Comb::Role::Client/for_context>), synchronous or asynchronous
145 alike. The peer is read as the Comb's L<Kubernetes::Comb/crd_class>.
146  
147 What cannot be read -- a context that is missing or broken, a custom
148 resource that is not there or not accessible -- is no error: L</status>
149 reports it unreachable, and the Comb goes C<Blocked> with the reason.
150  
151 Because every Comb publishes its already resolved endpoints in
152 C<status.endpoints>, the peer's addresses are the right ones however many
153 layers are behind it, and L</status> reports them in C<via>.
154  
155 =head2 context
156  
157 Required. The kube context the peer lives in, from the kubeconfig the Comb's
158 own client uses.
159  
160 =head2 namespace
161  
162 Namespace of the peer. Default: the Comb's own.
163  
164 =head2 name
165  
166 Name of the peer. Default: the Comb's own.
167  
168 =head2 endpoint_class
169  
170 The class L</endpoints> builds, L<Kubernetes::Comb::Endpoint>.
171  
172 =head2 status
173  
174 my $seen = $upstream->status($comb)->get;
175  
176 Future of C<< { reachable, phase, via, context, message } >>: C<phase> is the
177 peer's, C<via> this L</context> followed by the peer's own C<via>. Never
178 fails: whatever keeps the peer from being read makes it unreachable, with
179 the reason in C<message>. So does a peer that is the Comb itself. A
180 C<message> also names the peer's endpoints that have no address reachable
181 from here.
182  
183 =head2 endpoints
184  
185 my $endpoints = $upstream->endpoints($comb)->get;
186  
187 Future of the arrayref of the peer's published endpoints that have an
188 address reachable from here, as L<Kubernetes::Comb::Endpoint>. When both
189 contexts point at the same API server (L<Kubernetes::Comb::Role::Client/server_url>)
190 C<cluster> is the peer's C<cluster> address, else its C<external> one;
191 C<external> stays the peer's. Fails with the reason when the peer cannot be
192 read.
193  
194 =head1 SEE ALSO
195  
196 =over
197  
198 =item * L<Kubernetes::Comb::Role::Upstream>
199  
200 =item * L<Kubernetes::Comb::Upstream::Static>
201  
202 =item * L<Kubernetes::Comb/reconcile>
203  
204 =back
205  
206 =head1 SUPPORT
207  
208 =head2 Issues
209  
210 Please report bugs and feature requests on GitHub at
211 L<https://github.com/Getty/p5-kubernetes-comb/issues>.
212  
213 =head2 IRC
214  
215 Join C<#kubernetes> on C<irc.perl.org> or message Getty directly.
216  
217 =head1 CONTRIBUTING
218  
219 Contributions are welcome! Please fork the repository and submit a pull request.
220  
221 =head1 AUTHOR
222  
223 Torsten Raudssus <getty@cpan.org>
224  
225 =head1 COPYRIGHT AND LICENSE
226  
227 This software is copyright (c) 2026 by Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>.
228  
229 This is free software; you can redistribute it and/or modify it under
230 the same terms as the Perl 5 programming language system itself.
231  
232 =cut
« blib/lib/Kubernetes/Comb/CRD/CombSpec.pm ↑ Summary blib/lib/Kubernetes/Comb/Role/Static.pm »