| 1 | package Kubernetes::REST::Role::IO; | ||
| 2 | our $VERSION = '1.109'; | ||
| 3 | # ABSTRACT: Interface role for HTTP backends | ||
| 4 | use Moo::Role; | ||
| 5 | |||
| 6 | |||
| 7 | requires 'call'; | ||
| 8 | |||
| 9 | |||
| 10 | requires 'call_streaming'; | ||
| 11 | |||
| 12 | |||
| 13 | sub supports_duplex { | ||
| 14 | my ($self) = @_; | ||
| 15 | return $self->can('call_duplex') ? 1 : 0; | ||
| 16 | } | ||
| 17 | |||
| 18 | |||
| 19 | 1; | ||
| 20 | |||
| 21 | __END__ | ||
| 22 | |||
| 23 | =pod | ||
| 24 | |||
| 25 | =encoding UTF-8 | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | Kubernetes::REST::Role::IO - Interface role for HTTP backends | ||
| 30 | |||
| 31 | =head1 VERSION | ||
| 32 | |||
| 33 | version 1.109 | ||
| 34 | |||
| 35 | =head1 SYNOPSIS | ||
| 36 | |||
| 37 | package My::AsyncIO; | ||
| 38 | use Moo; | ||
| 39 | with 'Kubernetes::REST::Role::IO'; | ||
| 40 | |||
| 41 | sub call { | ||
| 42 | my ($self, $req) = @_; | ||
| 43 | # Execute HTTP request, return Kubernetes::REST::HTTPResponse | ||
| 44 | ... | ||
| 45 | } | ||
| 46 | |||
| 47 | sub call_streaming { | ||
| 48 | my ($self, $req, $data_callback) = @_; | ||
| 49 | # Execute HTTP request with streaming callback | ||
| 50 | ... | ||
| 51 | } | ||
| 52 | |||
| 53 | # Optional: full-duplex transport (WebSocket/SPDY) | ||
| 54 | sub call_duplex { | ||
| 55 | my ($self, $req, %callbacks) = @_; | ||
| 56 | ... | ||
| 57 | } | ||
| 58 | |||
| 59 | =head1 DESCRIPTION | ||
| 60 | |||
| 61 | This role defines the interface that HTTP backends must implement. L<Kubernetes::REST> delegates all HTTP communication through this interface, making it possible to swap out the transport layer. | ||
| 62 | |||
| 63 | The default backend is L<Kubernetes::REST::LWPIO> (using L<LWP::UserAgent>). An alternative L<Kubernetes::REST::HTTPTinyIO> (using L<HTTP::Tiny>) is provided. To use an async event loop, implement this role with e.g. L<Net::Async::HTTP>. | ||
| 64 | |||
| 65 | This is not how L<Net::Async::Kubernetes>, this distribution's own async client, integrates: it does not consume this role or call C<call>/C<call_streaming> at all. Instead it drives L<Kubernetes::REST>'s request pipeline directly, through the published C<build_path>, C<prepare_request>, C<check_response>, C<inflate_object>, C<inflate_list>, C<process_watch_chunk> and C<process_log_chunk> methods, running its own HTTP transport underneath them. Two more published methods, C<prepare_discovery_requests> and C<absorb_discovery>, let such a client read the cluster's discovery through its own transport too, instead of the synchronous L</call> that name resolution otherwise uses for it (see L<Kubernetes::REST/BUILDING BLOCKS FOR ASYNC WRAPPERS>). | ||
| 66 | |||
| 67 | Both shipped backends are synchronous, request/response-only transports: neither | ||
| 68 | implements C<call_duplex> (see L</supports_duplex> below), so L<Kubernetes::REST> | ||
| 69 | methods that need full-duplex transport (C<port_forward>, C<exec>, C<attach>) | ||
| 70 | croak against them by design. A backend that wants to support those needs to | ||
| 71 | implement C<call_duplex($req, %callbacks)> itself, as sketched in the L</SYNOPSIS>. | ||
| 72 | |||
| 73 | =head2 Encoding contract | ||
| 74 | |||
| 75 | Request and response bodies are B<bytes>, never character strings - this holds | ||
| 76 | for C<call> and C<call_streaming> alike, whatever kind of request they carry. | ||
| 77 | |||
| 78 | A backend receives C<< $req->content >> already UTF-8 encoded (the core client's | ||
| 79 | JSON encoder runs with C<utf8 =E<gt> 1>) and must put it on the wire unchanged. | ||
| 80 | It must hand back C<< $res->content >> - and every streaming chunk - as the | ||
| 81 | bytes it received, undoing C<Content-Encoding> (gzip) but B<not> the charset. | ||
| 82 | |||
| 83 | Two different callers rely on this, for two different reasons: | ||
| 84 | |||
| 85 | =over | ||
| 86 | |||
| 87 | =item * | ||
| 88 | |||
| 89 | For C<get>/C<list>/C<watch>, L<Kubernetes::REST> decodes UTF-8 itself once the | ||
| 90 | JSON is parsed, together with L<IO::K8s>. A backend that decodes the charset | ||
| 91 | first causes silent double decoding (mojibake) on any non-ASCII value. | ||
| 92 | |||
| 93 | =item * | ||
| 94 | |||
| 95 | For C<log> (streamed via C<call_streaming> just like C<watch>), the bytes are | ||
| 96 | handed straight back to the caller undecoded, on purpose: container output is | ||
| 97 | not guaranteed to be UTF-8, or even text, so there is no safe charset for the | ||
| 98 | backend to assume. See L<Kubernetes::REST/log>. | ||
| 99 | |||
| 100 | =back | ||
| 101 | |||
| 102 | With L<LWP::UserAgent> that means C<< $res->decoded_content(charset => 'none') >> | ||
| 103 | rather than C<< $res->decoded_content >>. See also L<Kubernetes::REST/ENCODING> | ||
| 104 | for the full picture from the caller's side of this module. | ||
| 105 | |||
| 106 | =head2 call | ||
| 107 | |||
| 108 | my $response = $io->call($req); | ||
| 109 | |||
| 110 | Required. Execute an HTTP request. Receives a L<Kubernetes::REST::HTTPRequest> with C<method>, C<url>, C<headers>, and optionally C<content> already set. | ||
| 111 | |||
| 112 | Must return a L<Kubernetes::REST::HTTPResponse> with C<status> and C<content>, the latter as bytes - see L</Encoding contract>. | ||
| 113 | |||
| 114 | =head2 call_streaming | ||
| 115 | |||
| 116 | my $response = $io->call_streaming($req, $data_callback); | ||
| 117 | |||
| 118 | Required. Execute an HTTP request with streaming response. The C<$data_callback> is called with each chunk of data as it arrives: C<< $data_callback->($chunk) >>. Chunks are bytes - see L</Encoding contract>. | ||
| 119 | |||
| 120 | Must return a L<Kubernetes::REST::HTTPResponse> when the stream ends. | ||
| 121 | |||
| 122 | =head2 supports_duplex | ||
| 123 | |||
| 124 | if ($io->supports_duplex) { | ||
| 125 | ... | ||
| 126 | } | ||
| 127 | |||
| 128 | Optional capability probe for full-duplex protocols used by Kubernetes | ||
| 129 | subresources such as pod port-forward and exec/attach streams. | ||
| 130 | |||
| 131 | Returns true if the backend implements C<call_duplex>, false otherwise. | ||
| 132 | |||
| 133 | =head1 SEE ALSO | ||
| 134 | |||
| 135 | =over | ||
| 136 | |||
| 137 | =item * L<Kubernetes::REST> - Main API client | ||
| 138 | |||
| 139 | =item * L<Kubernetes::REST::LWPIO> - LWP::UserAgent backend (default) | ||
| 140 | |||
| 141 | =item * L<Kubernetes::REST::HTTPTinyIO> - HTTP::Tiny backend | ||
| 142 | |||
| 143 | =item * L<Kubernetes::REST::HTTPRequest> - Request object | ||
| 144 | |||
| 145 | =item * L<Kubernetes::REST::HTTPResponse> - Response object | ||
| 146 | |||
| 147 | =back | ||
| 148 | |||
| 149 | =head1 SUPPORT | ||
| 150 | |||
| 151 | =head2 Issues | ||
| 152 | |||
| 153 | Please report bugs and feature requests on GitHub at | ||
| 154 | L<https://github.com/pplu/kubernetes-rest/issues>. | ||
| 155 | |||
| 156 | =head2 IRC | ||
| 157 | |||
| 158 | Join C<#kubernetes> on C<irc.perl.org> or message Getty directly. | ||
| 159 | |||
| 160 | =head1 CONTRIBUTING | ||
| 161 | |||
| 162 | Contributions are welcome! Please fork the repository and submit a pull request. | ||
| 163 | |||
| 164 | =head1 AUTHORS | ||
| 165 | |||
| 166 | =over 4 | ||
| 167 | |||
| 168 | =item * | ||
| 169 | |||
| 170 | Torsten Raudssus <getty@cpan.org> | ||
| 171 | |||
| 172 | =item * | ||
| 173 | |||
| 174 | Jose Luis Martinez Torres <jlmartin@cpan.org> | ||
| 175 | |||
| 176 | =back | ||
| 177 | |||
| 178 | =head1 COPYRIGHT AND LICENSE | ||
| 179 | |||
| 180 | This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>. | ||
| 181 | |||
| 182 | This is free software, licensed under: | ||
| 183 | |||
| 184 | The Apache License, Version 2.0, January 2004 | ||
| 185 | |||
| 186 | =cut |