Kubernetes/PortForwardListener.pm

filter: Statementstmt 91.4% 117 / 128 Branchbran 61.1% 22 / 36 Conditioncond 26.6% 4 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 86.6% 26 / 30 Podpod 83.3% 5 / 6 totaltotal 77.6% 174 / 224 CC 28 SCAR 12.9
CC
28
Cov
80%
_end
8.4
__ANON__
6.0
__ANON__
6.0
CRAP 3.6
« Kubernetes/Controller.pm ↑ Summary
1 package Net::Async::Kubernetes::PortForwardListener;
2 # ABSTRACT: Local TCP listeners forwarding to pod ports, like kubectl port-forward
3 54 our $VERSION = '0.011';
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 5 ▶sub new {
Subroutine new: called CC=5 SCAR=16.7
Pod documented
11 5 my ($class, %args) = @_;
12 5 ▶ croak "kube required" unless $args{kube};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"kube"} 0 5 5
13 5 ▶ croak "mappings required" unless ref($args{mappings}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"mappings"} eq "HASH" 0 5 5
14 5 ▶ croak "forward_args required" unless ref($args{forward_args}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"forward_args"} eq "ARRAY" 0 5 5
15 5 ▶ $args{host} //= '127.0.0.1';
Condition50%1/2
$args{"host"} //= "127.0.0.1"
l!l
50
MC/DC0%0/1
$args{"host"} //= "127.0.0.1"
$args{"host"}
Truth table50%1/2
A
A= $args{"host"}
Aresult
11
00
16 5 $args{_listeners} = {};
17 5 $args{_ports} = {};
18 5 $args{_conns} = {};
19 5 return bless \%args, $class;
20 }
21  
22  
23 5 ▶sub start {
Subroutine start: called CC=2 SCAR=7.0
Pod undocumented
24 5 my ($self) = @_;
25 5 my $loop = $self->{kube}->loop;
26  
27 5 my @listening;
28 5 for my $local (sort { $a <=> $b } keys %{$self->{mappings}}) {
29 6 my $remote = $self->{mappings}{$local};
30 push @listening, $loop->listen(
31 host => $self->{host},
32 service => "$local",
33 socktype => 'stream',
34 8 ▶ on_stream => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
35 8 my ($stream) = @_;
36 8 $self->_accept($stream, $remote);
37 },
38 6 ▶ )->on_done(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 6 my ($listener) = @_;
40 6 $self->{_listeners}{$remote} = $listener;
41 6 $self->{_ports}{$remote} = $listener->read_handle->sockport;
42 6 });
43 }
44  
45 5 ▶ return Future->needs_all(@listening)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
46 5 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 5 });
52 }
53  
54 10 ▶sub port_for {
Subroutine port_for: called CC=1 SCAR=0.0
Pod documented
55 10 my ($self, $remote) = @_;
56 10 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 3 ▶sub connection_count { scalar keys %{$_[0]->{_conns}} }
Subroutine connection_count: called CC=1 SCAR=0.0
Pod documented
64  
65  
66 6 ▶sub stop {
Subroutine stop: called CC=3 SCAR=11.0
Pod documented
67 6 my ($self) = @_;
68 6 $self->{stopped} = 1;
69 6 for my $remote (keys %{$self->{_listeners}}) {
70 6 my $listener = delete $self->{_listeners}{$remote};
71 6 $listener->close;
72 6 $listener->remove_from_parent;
73 }
74 6 $self->_end($_) for values %{$self->{_conns}};
75 6 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
BranchTrueFalseTotal
unless ref $self->{"on_error"} eq "CODE" 0 3 3
82 3 my $ok = eval { $self->{on_error}->($message); 1 };
83 3 ▶ warn $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 3 3
84 3 return;
85 }
86  
87 # One accepted TCP connection: its own port-forward session to $remote.
88 8 ▶sub _accept {
Subroutine _accept: called CC=2 SCAR=7.5
89 8 my ($self, $stream, $remote) = @_;
90  
91 8 my $conn = { stream => $stream, remote => $remote, buffer => '' };
92 8 $self->{_conns}{refaddr $conn} = $conn;
93  
94 8 ▶ if ($self->{stopped}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stopped"}) 0 8 8
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
BranchTrueFalseTotal
if length $data 8 1 9
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] // ""
l!l
00
MC/DC0%0/1
$_[1] // ""
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
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] // ""
l!l
00
MC/DC0%0/1
$_[1] // ""
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
110 8 ▶ on_closed => sub { $self->_end($conn) },
Subroutine __ANON__: called CC=1 SCAR=0.0
111 8 );
112 8 $self->{kube}->loop->add($stream);
113  
114 8 my $label = 'port ' . $remote;
115 $conn->{future} = $self->{kube}->port_forward(
116 8 @{$self->{forward_args}},
117 ports => [$remote],
118 7 ▶ on_open => sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
119 7 my ($session) = @_;
120 7 ▶ return if $conn->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $conn->{"closed"} 0 7 7
121 7 $conn->{session} = $session;
122 7 my $buffered = delete $conn->{buffer};
123 7 ▶ $self->_from_tcp($conn, $buffered) if length $buffered;
Branch: 1 branch
BranchTrueFalseTotal
if length $buffered 0 7 7
124 },
125 7 ▶ on_data => sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
126 7 my ($session, $port, $bytes) = @_;
127 7 ▶ $conn->{stream}->write($bytes) if $conn->{stream} && !$conn->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $conn->{"stream"} and not $conn->{"closed"} 7 0 7
Condition33%1/3
$conn->{"stream"} and not $conn->{"closed"}
!ll&&!rl&&r
007
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"}
ABresult
0X0
100
111
128 },
129 1 ▶ on_port_error => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
130 1 my ($session, $port, $message) = @_;
131 1 $self->_end($conn, "$label: $message");
132 },
133 2 ▶ on_error => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
134 2 my ($error) = @_;
135 2 $self->_end($conn, "$label: $error");
136 },
137 1 ▶ on_close => sub { $self->_end($conn) },
Subroutine __ANON__: called CC=1 SCAR=0.0
138 0 ▶ )->on_fail(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 my ($error) = @_;
140 0 $self->_end($conn, "$label: $error");
141 8 });
142 8 return;
143 }
144  
145 8 ▶sub _from_tcp {
Subroutine _from_tcp: called CC=4 SCAR=15.0
146 8 my ($self, $conn, $data) = @_;
147 8 ▶ return if $conn->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $conn->{"closed"} 0 8 8
148 8 ▶ unless ($conn->{session}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conn->{"session"}) 0 8 8
149 0 $conn->{buffer} .= $data;
150 0 return;
151 }
152 8 my $ok = eval { $conn->{session}->write_port($conn->{remote}, $data); 1 };
153 8 ▶ $self->_end($conn, 'port ' . $conn->{remote} . ': ' . $@) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 8 8
154 8 return;
155 }
156  
157 # Close both sides of a connection once; a $reason is an error to report.
158 16 ▶sub _end {
Subroutine _end: called CC=8 SCAR=21.3
159 16 my ($self, $conn, $reason) = @_;
160 16 ▶ return if $conn->{closed}++;
Branch: 1 branch
BranchTrueFalseTotal
if $conn->{"closed"}++ 8 8 16
161 8 delete $self->{_conns}{refaddr $conn};
162 8 ▶ $self->_report($reason) if defined $reason;
Branch: 1 branch
BranchTrueFalseTotal
if defined $reason 3 5 8
163  
164 8 my $session = delete $conn->{session};
165 8 my $stream = delete $conn->{stream};
166 8 my $future = delete $conn->{future};
167 8 delete $conn->{buffer};
168 8 ▶ if ($session) {
Branch: 2 branches
BranchTrueFalseTotal
if ($session) { } 7 1 8
elsif ($future and not $future->is_ready) { } 1 0 1
169 7 eval { $session->close(code => 1000); 1 };
170 ▶ } elsif ($future && !$future->is_ready) {
Condition33%1/3
$future and not $future->is_ready
!ll&&!rl&&r
001
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
ABresult
0X0
100
111
171 1 $future->cancel;
172 }
173 8 ▶ eval { $stream->close_when_empty; 1 } if $stream && $stream->loop;
Branch: 1 branch
BranchTrueFalseTotal
if $stream and $stream->loop 8 0 8
Condition33%1/3
$stream and $stream->loop
!ll&&!rl&&r
008
MC/DC0%0/2
$stream and $stream->loop
$stream $stream->loop
Truth table33%1/3
A and B
A= $streamB= $stream->loop
ABresult
0X0
100
111
174 8 return;
175 }
176  
177 54 1;
178  
179 __END__
180  
181 =pod
182  
183 =encoding UTF-8
184  
185 =head1 NAME
186  
187 Net::Async::Kubernetes::PortForwardListener - Local TCP listeners forwarding to pod ports, like kubectl port-forward
188  
189 =head1 VERSION
190  
191 version 0.011
192  
193 =head1 SYNOPSIS
194  
195 my $listener = $kube->port_forward_listen('Pod', 'web',
196 namespace => 'default',
197 ports => { 0 => 80 },
198 on_error => sub { warn "forward: $_[0]\n" },
199 )->get;
200  
201 my $port = $listener->port_for(80);
202 # ... talk HTTP to 127.0.0.1:$port ...
203 $listener->stop;
204  
205 =head1 DESCRIPTION
206  
207 The handle L<Net::Async::Kubernetes/port_forward_listen> resolves with. It
208 owns the local listening sockets and, for every accepted TCP connection, one
209 port-forward session of its own (see L<Net::Async::Kubernetes/port_forward>)
210 for the remote port the local port maps to. Bytes read from the connection
211 are written to the session's data channel and bytes from the pod written back
212 to the connection. The connection and the session end together.
213  
214 =head2 new
215  
216 Built by L<Net::Async::Kubernetes/port_forward_listen>, which validates its
217 arguments; there is no reason to call this directly.
218  
219 =head2 port_for
220  
221 my $local = $listener->port_for(8080);
222  
223 The local TCP port that forwards to remote C<$remote>: the one given in
224 C<ports>, or the one the system picked for a local port of C<0>. C<undef> for
225 a remote port that is not forwarded.
226  
227 =head2 host
228  
229 The local address the listeners are bound to.
230  
231 =head2 connection_count
232  
233 The number of forwarded connections currently open.
234  
235 =head2 stop
236  
237 $listener->stop;
238  
239 Close the listening sockets and every open connection, together with its
240 port-forward session. Safe to call more than once. The listener cannot be
241 restarted; open a new one.
242  
243 =head1 SEE ALSO
244  
245 L<Net::Async::Kubernetes>, L<Net::Async::Kubernetes::PortForwardSession>,
246 L<IO::Async::Listener>
247  
248 =head1 SUPPORT
249  
250 =head2 Issues
251  
252 Please report bugs and feature requests on GitHub at
253 L<https://github.com/Getty/p5-net-async-kubernetes/issues>.
254  
255 =head2 IRC
256  
257 Join C<#kubernetes> on C<irc.perl.org> or message Getty directly.
258  
259 =head1 CONTRIBUTING
260  
261 Contributions are welcome! Please fork the repository and submit a pull request.
262  
263 =head1 AUTHOR
264  
265 Torsten Raudssus <torsten@raudssus.de>
266  
267 =head1 COPYRIGHT AND LICENSE
268  
269 This software is copyright (c) 2026 by Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>.
270  
271 This is free software; you can redistribute it and/or modify it under
272 the same terms as the Perl 5 programming language system itself.
273  
274 =cut
« Kubernetes/Controller.pm ↑ Summary