| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$upstream->via // []
MC/DC0%0/1
$upstream->via // []
$upstream->via
Truth table50%1/2
A
A= $upstream->via
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$recorded and defined $recorded->phase
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | return Future->done( [ map { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 15 | my $address = $self->_address_of( $_, $seen->{same} ); | |||||||||||||||||||||||||||||||||||||||||
| 59 | 15 | ▶ | defined $address | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$recorded->endpoints // []
MC/DC100%1/1
$recorded->endpoints // []
$recorded->endpoints
Truth table100%2/2
A
A= $recorded->endpoints
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$endpoint->cluster // $endpoint->external
MC/DC0%0/2
$endpoint->cluster // $endpoint->external
$endpoint->cluster
$endpoint->external
Truth table66%2/3
A // B
A= $endpoint->clusterB= $endpoint->external
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 51 | ▶ | my $name = $self->has_name ? $self->name : $comb->name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 46 | ▶ | return Future->done( { error => $where.' is this Comb itself' } ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$same and $namespace eq $comb->namespace and $name eq $comb->name
$same and $namespace eq $comb->namespace
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 11 | ▶ | Future->done( { error => 'reading '.( $where // 'the peer Comb' ).' failed: '.$error } ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$where // "the peer Comb"
MC/DC100%1/1
$where // "the peer Comb"
$where
Truth table100%2/2
A
A= $where
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 53 | } ); | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||||||||||||||||