| 1 | package Net::Async::Kubernetes::PortForwardListener; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Local TCP listeners forwarding to pod ports, like kubectl port-forward | ||||||||||||||||||||||||||||
| 3 | 54 | our $VERSION = '0.012'; | |||||||||||||||||||||||||||
| 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 Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 54 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 54 | ▶ | use Scalar::Util qw(refaddr); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | 6 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 12 | 6 | ▶ | croak "kube required" unless $args{kube}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | croak "mappings required" unless ref($args{mappings}) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 6 | ▶ | croak "forward_args required" unless ref($args{forward_args}) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 6 | ▶ | $args{host} //= '127.0.0.1'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"host"} //= "127.0.0.1"
MC/DC0%0/1
$args{"host"} //= "127.0.0.1"
$args{"host"}
Truth table50%1/2
A
A= $args{"host"}
| |||||||||||||||||||||||||||||
| 16 | 6 | $args{_listeners} = {}; | |||||||||||||||||||||||||||
| 17 | 6 | $args{_ports} = {}; | |||||||||||||||||||||||||||
| 18 | 6 | $args{_conns} = {}; | |||||||||||||||||||||||||||
| 19 | 6 | return bless \%args, $class; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 6 | ▶ | sub start { | ||||||||||||||||||||||||||
|
Subroutine
start: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 25 | 6 | my $loop = $self->{kube}->loop; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 6 | my @listening; | |||||||||||||||||||||||||||
| 28 | 6 | for my $local (sort { $a <=> $b } keys %{$self->{mappings}}) { | |||||||||||||||||||||||||||
| 29 | 7 | my $remote = $self->{mappings}{$local}; | |||||||||||||||||||||||||||
| 30 | push @listening, $loop->listen( | ||||||||||||||||||||||||||||
| 31 | host => $self->{host}, | ||||||||||||||||||||||||||||
| 32 | service => "$local", | ||||||||||||||||||||||||||||
| 33 | socktype => 'stream', | ||||||||||||||||||||||||||||
| 34 | 10 | ▶ | on_stream => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 10 | my ($stream) = @_; | |||||||||||||||||||||||||||
| 36 | 10 | $self->_accept($stream, $remote); | |||||||||||||||||||||||||||
| 37 | }, | ||||||||||||||||||||||||||||
| 38 | 7 | ▶ | )->on_done(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 7 | my ($listener) = @_; | |||||||||||||||||||||||||||
| 40 | 7 | $self->{_listeners}{$remote} = $listener; | |||||||||||||||||||||||||||
| 41 | 7 | $self->{_ports}{$remote} = $listener->read_handle->sockport; | |||||||||||||||||||||||||||
| 42 | 7 | }); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 6 | ▶ | return Future->needs_all(@listening)->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 6 | return Future->done($self); | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | })->else(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 48 | 0 | my ($error) = @_; | |||||||||||||||||||||||||||
| 49 | 0 | $self->stop; | |||||||||||||||||||||||||||
| 50 | 0 | return Future->fail('port_forward_listen: ' . $error); | |||||||||||||||||||||||||||
| 51 | 6 | }); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 12 | ▶ | sub port_for { | ||||||||||||||||||||||||||
|
Subroutine
port_for: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 12 | my ($self, $remote) = @_; | |||||||||||||||||||||||||||
| 56 | 12 | return $self->{_ports}{$remote}; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 1 | ▶ | sub host { $_[0]->{host} } | ||||||||||||||||||||||||||
|
Subroutine
host: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 4 | ▶ | sub connection_count { scalar keys %{$_[0]->{_conns}} } | ||||||||||||||||||||||||||
|
Subroutine
connection_count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 7 | ▶ | sub stop { | ||||||||||||||||||||||||||
|
Subroutine
stop: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 68 | 7 | $self->{stopped} = 1; | |||||||||||||||||||||||||||
| 69 | 7 | for my $remote (keys %{$self->{_listeners}}) { | |||||||||||||||||||||||||||
| 70 | 7 | my $listener = delete $self->{_listeners}{$remote}; | |||||||||||||||||||||||||||
| 71 | 7 | $listener->close; | |||||||||||||||||||||||||||
| 72 | 7 | $listener->remove_from_parent; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 7 | $self->_end($_) for values %{$self->{_conns}}; | |||||||||||||||||||||||||||
| 75 | 7 | return; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 3 | ▶ | sub _report { | ||||||||||||||||||||||||||
|
Subroutine
_report: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 80 | 3 | my ($self, $message) = @_; | |||||||||||||||||||||||||||
| 81 | 3 | ▶ | return unless ref($self->{on_error}) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 3 | my $ok = eval { $self->{on_error}->($message); 1 }; | |||||||||||||||||||||||||||
| 83 | 3 | ▶ | warn $@ unless $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 3 | return; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # One accepted TCP connection: its own port-forward session to $remote. | ||||||||||||||||||||||||||||
| 88 | 10 | ▶ | sub _accept { | ||||||||||||||||||||||||||
|
Subroutine
_accept: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 89 | 10 | my ($self, $stream, $remote) = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 10 | my $conn = { stream => $stream, remote => $remote, buffer => '' }; | |||||||||||||||||||||||||||
| 92 | 10 | $self->{_conns}{refaddr $conn} = $conn; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 10 | ▶ | if ($self->{stopped}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | $self->_end($conn); | |||||||||||||||||||||||||||
| 96 | 0 | return; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | $stream->configure( | ||||||||||||||||||||||||||||
| 100 | 9 | ▶ | on_read => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 101 | 9 | my (undef, $buffref) = @_; | |||||||||||||||||||||||||||
| 102 | 9 | my $data = $$buffref; | |||||||||||||||||||||||||||
| 103 | 9 | $$buffref = ''; | |||||||||||||||||||||||||||
| 104 | 9 | ▶ | $self->_from_tcp($conn, $data) if length $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 9 | return 0; | |||||||||||||||||||||||||||
| 106 | }, | ||||||||||||||||||||||||||||
| 107 | 1 | ▶ | on_read_eof => sub { $self->_end($conn) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | on_read_error => sub { $self->_end($conn, 'local read error: ' . ($_[1] // '')) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[1] // ""
MC/DC0%0/1
$_[1] // ""
$_[1]
Truth table0%0/2
A
A= $_[1]
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | on_write_error => sub { $self->_end($conn, 'local write error: ' . ($_[1] // '')) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[1] // ""
MC/DC0%0/1
$_[1] // ""
$_[1]
Truth table0%0/2
A
A= $_[1]
| |||||||||||||||||||||||||||||
| 110 | 10 | ▶ | on_closed => sub { $self->_end($conn) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 111 | 10 | ); | |||||||||||||||||||||||||||
| 112 | 10 | $self->{kube}->loop->add($stream); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 10 | my $label = 'port ' . $remote; | |||||||||||||||||||||||||||
| 115 | $conn->{future} = $self->{kube}->port_forward( | ||||||||||||||||||||||||||||
| 116 | 10 | @{$self->{forward_args}}, | |||||||||||||||||||||||||||
| 117 | ports => [$remote], | ||||||||||||||||||||||||||||
| 118 | 8 | ▶ | on_open => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 119 | 8 | my ($session) = @_; | |||||||||||||||||||||||||||
| 120 | # the connection ended while the session request was pending | ||||||||||||||||||||||||||||
| 121 | 8 | ▶ | if ($conn->{closed}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 1 | eval { $session->close(code => 1000); 1 }; | |||||||||||||||||||||||||||
| 123 | 1 | return; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 7 | $conn->{session} = $session; | |||||||||||||||||||||||||||
| 126 | 7 | my $buffered = delete $conn->{buffer}; | |||||||||||||||||||||||||||
| 127 | 7 | ▶ | $self->_from_tcp($conn, $buffered) if length $buffered; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | }, | ||||||||||||||||||||||||||||
| 129 | 7 | ▶ | on_data => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.1
| |||||||||||||||||||||||||||||
| 130 | 7 | my ($session, $port, $bytes) = @_; | |||||||||||||||||||||||||||
| 131 | 7 | ▶ | $conn->{stream}->write($bytes) if $conn->{stream} && !$conn->{closed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$conn->{"stream"} and not $conn->{"closed"}
MC/DC0%0/2
$conn->{"stream"} and not $conn->{"closed"}
$conn->{"stream"}
not $conn->{"closed"}
Truth table33%1/3
A and not(B)
A= $conn->{"stream"}B= not $conn->{"closed"}
| |||||||||||||||||||||||||||||
| 132 | }, | ||||||||||||||||||||||||||||
| 133 | 1 | ▶ | on_port_error => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 134 | 1 | my ($session, $port, $message) = @_; | |||||||||||||||||||||||||||
| 135 | 1 | $self->_end($conn, "$label: $message"); | |||||||||||||||||||||||||||
| 136 | }, | ||||||||||||||||||||||||||||
| 137 | 2 | ▶ | on_error => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 138 | 2 | my ($error) = @_; | |||||||||||||||||||||||||||
| 139 | 2 | $self->_end($conn, "$label: $error"); | |||||||||||||||||||||||||||
| 140 | }, | ||||||||||||||||||||||||||||
| 141 | 1 | ▶ | on_close => sub { $self->_end($conn) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 142 | 1 | ▶ | )->on_fail(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 143 | 1 | my ($error) = @_; | |||||||||||||||||||||||||||
| 144 | 1 | $self->_end($conn, "$label: $error"); | |||||||||||||||||||||||||||
| 145 | 10 | }); | |||||||||||||||||||||||||||
| 146 | 10 | return; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 8 | ▶ | sub _from_tcp { | ||||||||||||||||||||||||||
|
Subroutine
_from_tcp: called CC=4 SCAR=15.0
| |||||||||||||||||||||||||||||
| 150 | 8 | my ($self, $conn, $data) = @_; | |||||||||||||||||||||||||||
| 151 | 8 | ▶ | return if $conn->{closed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 8 | ▶ | unless ($conn->{session}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | $conn->{buffer} .= $data; | |||||||||||||||||||||||||||
| 154 | 0 | return; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | 8 | my $ok = eval { $conn->{session}->write_port($conn->{remote}, $data); 1 }; | |||||||||||||||||||||||||||
| 157 | 8 | ▶ | $self->_end($conn, 'port ' . $conn->{remote} . ': ' . $@) unless $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 8 | return; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Close both sides of a connection once; a $reason is an error to report. | ||||||||||||||||||||||||||||
| 162 | 21 | ▶ | sub _end { | ||||||||||||||||||||||||||
|
Subroutine
_end: called CC=9 SCAR=22.7
| |||||||||||||||||||||||||||||
| 163 | 21 | my ($self, $conn, $reason) = @_; | |||||||||||||||||||||||||||
| 164 | 21 | ▶ | return if $conn->{closed}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 10 | delete $self->{_conns}{refaddr $conn}; | |||||||||||||||||||||||||||
| 166 | 10 | ▶ | $self->_report($reason) if defined $reason; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 10 | my $session = delete $conn->{session}; | |||||||||||||||||||||||||||
| 169 | 10 | my $stream = delete $conn->{stream}; | |||||||||||||||||||||||||||
| 170 | 10 | my $future = delete $conn->{future}; | |||||||||||||||||||||||||||
| 171 | 10 | delete $conn->{buffer}; | |||||||||||||||||||||||||||
| 172 | 10 | ▶ | if ($session) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 173 | 7 | eval { $session->close(code => 1000); 1 }; | |||||||||||||||||||||||||||
| 174 | ▶ | } elsif ($future && !$future->is_ready) { | |||||||||||||||||||||||||||
|
Condition33%1/3
$future and not $future->is_ready
MC/DC0%0/2
$future and not $future->is_ready
$future
not $future->is_ready
Truth table33%1/3
A and not(B)
A= $futureB= not $future->is_ready
| |||||||||||||||||||||||||||||
| 175 | # Cancelled on the next loop iteration, never in place: _end also | ||||||||||||||||||||||||||||
| 176 | # runs inside the request's own callbacks (a failed connect reports | ||||||||||||||||||||||||||||
| 177 | # through on_error before the request future fails), and the | ||||||||||||||||||||||||||||
| 178 | # deferred closure keeps the request alive until those have unwound. | ||||||||||||||||||||||||||||
| 179 | # Freeing a sequence future from its own callback crashes Future::XS. | ||||||||||||||||||||||||||||
| 180 | 3 | ▶ | if (my $loop = $self->{kube}->loop) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 3 | ▶ | $loop->later(sub { $future->cancel unless $future->is_ready }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | } else { | ||||||||||||||||||||||||||||
| 183 | 0 | $future->cancel; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | 10 | ▶ | eval { $stream->close_when_empty; 1 } if $stream && $stream->loop; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$stream and $stream->loop
MC/DC0%0/2
$stream and $stream->loop
$stream
$stream->loop
Truth table33%1/3
A and B
A= $streamB= $stream->loop
| |||||||||||||||||||||||||||||
| 187 | 10 | return; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 54 | 1; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | __END__ | ||||||||||||||||||||||||||||