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.002';
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__
« blib/lib/Kubernetes/Comb/CRD/CombSpec.pm ↑ Summary blib/lib/Kubernetes/Comb/Role/Static.pm »