Kubernetes/PortForwardSession.pm

filter: Statementstmt 100% 47 / 47 Branchbran 59.3% 19 / 32 Conditioncond 38.1% 8 / 21 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 100% 10 / 10 Podpod 83.3% 5 / 6 totaltotal 70.8% 90 / 127 CC 24 SCAR 21.9
CC
24
Cov
74%
close
13.9
write_channel
10.1
resize
9.9
CRAP 8.9
« Kubernetes.pm ↑ Summary Kubernetes/Controller.pm »
1 package Net::Async::Kubernetes::PortForwardSession;
2 # ABSTRACT: Duplex websocket session for pod port-forward, exec and attach
3 48 our $VERSION = '0.009';
4 48 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 48 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 48 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
9 5 my ($class, %args) = @_;
10 5 ▶ croak "ws_client required" unless $args{ws_client};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"ws_client"} 0 5 5
11 5 return bless \%args, $class;
12 }
13  
14  
15 6 ▶sub ws_client { $_[0]->{ws_client} }
Subroutine ws_client: called CC=1 SCAR=0.0
Pod undocumented
16  
17 3 ▶sub write_channel {
Subroutine write_channel: called CC=7 SCAR=23.2
Pod documented
18 3 my ($self, $channel, $payload) = @_;
19  
20 3 ▶ croak "channel required for write_channel" unless defined $channel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $channel 0 3 3
21 3 ▶ croak "invalid channel '$channel' for write_channel"
Branch: 1 branch
BranchTrueFalseTotal
unless $channel =~ /^\d+$/ and $channel >= 0 and $channel <= 255 0 3 3
Condition33%2/6
$channel =~ /^\d+$/ and $channel >= 0 and $channel <= 255
!ll&&!rl&&r
003
$channel =~ /^\d+$/ and $channel >= 0
!ll&&!rl&&r
003
MC/DC0%0/3
$channel =~ /^\d+$/ and $channel >= 0 and $channel <= 255
$channel =~ /^\d+$/ $channel >= 0 $channel <= 255
Truth table25%1/4
A and B and C
A= $channel =~ /^\d+$/B= $channel >= 0C= $channel <= 255
ABCresult
0XX0
10X0
1100
1111
22 unless $channel =~ /^\d+$/ && $channel >= 0 && $channel <= 255;
23  
24 3 ▶ $payload = '' unless defined $payload;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $payload 0 3 3
25 3 ▶ croak "payload must be a plain string for write_channel" if ref($payload);
Branch: 1 branch
BranchTrueFalseTotal
if ref $payload 0 3 3
26  
27 3 return $self->ws_client->send_binary_frame(chr($channel) . $payload);
28 }
29  
30  
31 1 ▶sub write_stdin {
Subroutine write_stdin: called CC=1 SCAR=0.0
Pod documented
32 1 my ($self, $payload) = @_;
33 1 return $self->write_channel(0, $payload);
34 }
35  
36  
37 {
38 48 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 48 *write = \&write_channel;
40 48 *stdin = \&write_stdin;
41 }
42  
43 3 ▶sub resize {
Subroutine resize: called CC=9 SCAR=22.9
Pod documented
44 3 my ($self, %args) = @_;
45  
46 3 ▶ my $width = exists($args{width}) ? $args{width} : $args{cols};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'width'} ? : 3 0 3
47 3 ▶ my $height = exists($args{height}) ? $args{height} : $args{rows};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'height'} ? : 2 1 3
48  
49 3 ▶ croak "width required for resize" unless defined $width;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $width 0 3 3
50 3 ▶ croak "height required for resize" unless defined $height;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $height 1 2 3
51 2 ▶ croak "invalid width '$width' for resize"
Branch: 1 branch
BranchTrueFalseTotal
unless $width =~ /^\d+$/ and $width > 0 1 1 2
Condition66%2/3
$width =~ /^\d+$/ and $width > 0
!ll&&!rl&&r
011
MC/DC50%1/2
$width =~ /^\d+$/ and $width > 0
$width =~ /^\d+$/ $width > 0
Truth table66%2/3
A and B
A= $width =~ /^\d+$/B= $width > 0
ABresult
0X0
100
111
52 unless $width =~ /^\d+$/ && $width > 0;
53 1 ▶ croak "invalid height '$height' for resize"
Branch: 1 branch
BranchTrueFalseTotal
unless $height =~ /^\d+$/ and $height > 0 0 1 1
Condition33%1/3
$height =~ /^\d+$/ and $height > 0
!ll&&!rl&&r
001
MC/DC0%0/2
$height =~ /^\d+$/ and $height > 0
$height =~ /^\d+$/ $height > 0
Truth table33%1/3
A and B
A= $height =~ /^\d+$/B= $height > 0
ABresult
0X0
100
111
54 unless $height =~ /^\d+$/ && $height > 0;
55  
56 1 my $payload = sprintf('{"Width":%d,"Height":%d}', $width, $height);
57 1 return $self->write_channel(4, $payload);
58 }
59  
60  
61 1 ▶sub close {
Subroutine close: called CC=9 SCAR=26.3
Pod documented
62 1 my ($self, %args) = @_;
63 1 my $code = $args{code};
64 1 ▶ my $payload = exists $args{payload} ? $args{payload} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'payload'} ? : 1 0 1
65  
66 1 ▶ croak "payload must be a plain string for close" if ref($payload);
Branch: 1 branch
BranchTrueFalseTotal
if ref $payload 0 1 1
67 1 ▶ croak "invalid websocket close code '$code'"
Branch: 1 branch
BranchTrueFalseTotal
if defined $code and !($code =~ /^\d+$/) || $code < 1000 || $code > 4999 0 1 1
Condition33%3/9
defined $code and !($code =~ /^\d+$/) || $code < 1000 || $code > 4999
!ll&&!rl&&r
010
not $code =~ /^\d+$/ or $code < 1000 or $code > 4999
l!l&&r!l&&!r
001
not $code =~ /^\d+$/ or $code < 1000
l!l&&r!l&&!r
001
MC/DC0%0/4
defined $code and !($code =~ /^\d+$/) || $code < 1000 || $code > 4999
defined $code not $code =~ /^\d+$/ $code < 1000 $code > 4999
Truth table20%1/5
A and not(B) or C or D
A= defined $codeB= not $code =~ /^\d+$/C= $code < 1000D= $code > 4999
ABCDresult
0XXX0
10000
11XX1
101X1
10011
68 if defined($code) && ($code !~ /^\d+$/ || $code < 1000 || $code > 4999);
69  
70 1 ▶ my $close_payload = defined($code) ? pack('n', $code) . $payload : $payload;
Branch: 1 branch
BranchTrueFalseTotal
defined $code ? : 1 0 1
71 1 my $ret = $self->ws_client->send_close_frame($close_payload);
72 1 ▶ $self->ws_client->close_when_empty if $self->ws_client->can('close_when_empty');
Branch: 1 branch
BranchTrueFalseTotal
if $self->ws_client->can("close_when_empty") 1 0 1
73 1 return $ret;
74 }
75  
76  
77 48 1;
78  
79 __END__
80  
81 =pod
82  
83 =encoding UTF-8
84  
85 =head1 NAME
86  
87 Net::Async::Kubernetes::PortForwardSession - Duplex websocket session for pod port-forward, exec and attach
88  
89 =head1 VERSION
90  
91 version 0.009
92  
93 =head1 SYNOPSIS
94  
95 my $session = $kube->exec('Pod', 'my-pod',
96 namespace => 'default',
97 command => ['sh'],
98 tty => 1,
99 on_frame => sub {
100 my ($channel, $payload) = @_;
101 print $payload if $channel == 1; # stdout
102 },
103 )->get;
104  
105 $session->write_stdin("id\n");
106 $session->resize(width => 120, height => 40);
107 $session->close(code => 1000);
108  
109 =head1 DESCRIPTION
110  
111 A duplex session handle for the Kubernetes exec/attach/port-forward
112 websocket sub-protocol (C<v4.channel.k8s.io> by default). Every frame on the
113 wire carries a channel number as its first byte, identifying which logical
114 stream - stdin, stdout, stderr, error/status, or TTY resize - the rest of the
115 frame belongs to. C<write_channel> is the primitive that builds such a
116 frame; C<write_stdin> and C<resize> are convenience wrappers over channels 0
117 and 4 respectively.
118  
119 Callers do not normally construct this class themselves: it is what
120 L<Net::Async::Kubernetes/port_forward>, L<Net::Async::Kubernetes/exec>, and
121 L<Net::Async::Kubernetes/attach> resolve their returned C<Future> with, and
122 what they pass to an C<on_open> callback if one was given. Incoming frames
123 are not read through this object; they arrive via the C<on_frame> callback
124 passed to those methods, already decoded into C<($channel, $payload)> pairs.
125  
126 =head2 new
127  
128 my $session = Net::Async::Kubernetes::PortForwardSession->new(
129 ws_client => $ws_client,
130 );
131  
132 Wraps an already-connected websocket client (normally a
133 L<Net::Async::WebSocket::Client>) as a session. C<ws_client> is required.
134  
135 Callers do not normally build a session this way: the C<Future> returned by
136 L<Net::Async::Kubernetes/port_forward>, L<Net::Async::Kubernetes/exec>, and
137 L<Net::Async::Kubernetes/attach> resolves to one, already bound to the open
138 connection, and it is what those methods pass to an C<on_open> callback.
139  
140 =head2 write_channel
141  
142 $session->write_channel(0, "id\n");
143  
144 Send C<$payload> as a single binary websocket frame on the given Kubernetes
145 stream C<$channel>, with the channel number prepended as the frame's first
146 byte (C<chr($channel) . $payload>). C<$channel> is required and must be an
147 integer between 0 and 255; C<$payload> defaults to the empty string.
148  
149 C<write_channel> itself does not know what any channel number means, it only
150 frames whatever it is given, but the Kubernetes exec/attach/port-forward
151 sub-protocol fixes their meaning: 0 is stdin, 1 is stdout, 2 is stderr, 3
152 carries an error/status JSON document, and 4 carries TTY resize events (see
153 L</resize>). C<on_frame>, passed to L<Net::Async::Kubernetes/exec> and
154 friends, decodes incoming frames the same way in reverse.
155  
156 Returns the L<Future> from the underlying C<ws_client>'s
157 C<send_binary_frame> - an L<IO::Async::Stream> write-future that completes
158 with no value once the frame has been flushed to the connection.
159  
160 Aliased as C<write>.
161  
162 =head2 write_stdin
163  
164 $session->write_stdin("id\n");
165  
166 Shortcut for C<< $session->write_channel(0, $payload) >> - writes to channel
167 0, the stdin stream of an exec or attach session's remote process.
168  
169 Returns the L<Future> from the underlying C<write_channel> call.
170  
171 Aliased as C<stdin>.
172  
173 =head2 resize
174  
175 $session->resize(width => 120, height => 40);
176 $session->resize(cols => 120, rows => 40); # cols/rows are accepted too
177  
178 Send a TTY resize event on channel 4, as a C<{"Width":$width,"Height":$height}>
179 JSON payload. C<width> and C<height> (or their C<cols>/C<rows> aliases) are
180 required and must be positive integers. Only meaningful for a session opened
181 with C<tty =E<gt> 1>.
182  
183 Returns the L<Future> from the underlying C<write_channel> call.
184  
185 =head2 close
186  
187 $session->close(code => 1000, payload => 'bye');
188 $session->close; # close frame with no code
189  
190 Send a websocket close frame, ending the session. C<code>, if given, must be
191 a websocket close code between 1000 and 4999; it is packed as a 16-bit
192 big-endian prefix (C<pack('n', $code)>) ahead of C<payload>, which defaults
193 to the empty string. Once the close frame is sent, the underlying transport
194 is also asked to close the connection once its write buffer drains
195 (C<close_when_empty>), if the transport supports that method.
196  
197 Returns the L<Future> from the underlying C<ws_client>'s C<send_close_frame>
198 - an L<IO::Async::Stream> write-future that completes with no value once the
199 close frame has been flushed to the connection.
200  
201 =head1 SEE ALSO
202  
203 L<Net::Async::Kubernetes>, L<Net::Async::WebSocket::Client>,
204 L<IO::Async::Stream>
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-net-async-kubernetes/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 <torsten@raudssus.de>
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
« Kubernetes.pm ↑ Summary Kubernetes/Controller.pm »