| 1 | package Kubernetes::REST::LWPIO; | ||||||||||||||||||||||||||||
| 2 | 63 | our $VERSION = '1.109'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: HTTP client using LWP::UserAgent | ||||||||||||||||||||||||||||
| 4 | 63 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 63 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 63 | ▶ | use Kubernetes::REST::HTTPResponse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 63 | ▶ | use Types::Standard qw/Bool/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 63 | with 'Kubernetes::REST::Role::IO'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 63 | has ssl_verify_server => (is => 'ro', isa => Bool, default => 1); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 63 | has ssl_cert_file => (is => 'ro'); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 63 | has ssl_cert_pem => (is => 'ro'); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 63 | has ssl_key_file => (is => 'ro'); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 63 | has ssl_key_pem => (is => 'ro'); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 63 | has ssl_ca_file => (is => 'ro'); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 63 | has ssl_ca_pem => (is => 'ro'); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 12 | ▶ | has timeout => (is => 'ro', default => sub { 310 }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 3 | ▶ | has ua => (is => 'ro', lazy => 1, default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=27.1
| |||||||||||||||||||||||||||||
| 37 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 38 | 3 | require IO::Socket::SSL::Utils; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 3 | my %ssl_opts; | |||||||||||||||||||||||||||
| 41 | 3 | $ssl_opts{ verify_hostname } = $self->ssl_verify_server; | |||||||||||||||||||||||||||
| 42 | 3 | ▶ | $ssl_opts{ SSL_verify_mode } = $self->ssl_verify_server ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # PEM data: convert to X509/EVP_PKEY objects via SSL_cert/SSL_key | ||||||||||||||||||||||||||||
| 45 | # PEM file paths: use SSL_cert_file/SSL_key_file as normal | ||||||||||||||||||||||||||||
| 46 | 3 | ▶ | if (defined $self->ssl_cert_pem) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 47 | 0 | $ssl_opts{ SSL_cert } = [ IO::Socket::SSL::Utils::PEM_string2cert($self->ssl_cert_pem) ]; | |||||||||||||||||||||||||||
| 48 | } elsif (defined $self->ssl_cert_file) { | ||||||||||||||||||||||||||||
| 49 | 0 | $ssl_opts{ SSL_cert_file } = $self->ssl_cert_file; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 3 | ▶ | if (defined $self->ssl_key_pem) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 53 | 0 | $ssl_opts{ SSL_key } = IO::Socket::SSL::Utils::PEM_string2key($self->ssl_key_pem); | |||||||||||||||||||||||||||
| 54 | } elsif (defined $self->ssl_key_file) { | ||||||||||||||||||||||||||||
| 55 | 0 | $ssl_opts{ SSL_key_file } = $self->ssl_key_file; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 3 | ▶ | if (defined $self->ssl_ca_pem) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 59 | 0 | $ssl_opts{ SSL_ca } = [ IO::Socket::SSL::Utils::PEM_string2cert($self->ssl_ca_pem) ]; | |||||||||||||||||||||||||||
| 60 | } elsif (defined $self->ssl_ca_file) { | ||||||||||||||||||||||||||||
| 61 | 0 | $ssl_opts{ SSL_ca_file } = $self->ssl_ca_file; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 3 | ▶ | return LWP::UserAgent->new( | ||||||||||||||||||||||||||
|
Condition50%1/2
$Kubernetes::REST::VERSION // "dev"
MC/DC0%0/1
$Kubernetes::REST::VERSION // "dev"
$Kubernetes::REST::VERSION
Truth table50%1/2
A
A= $Kubernetes::REST::VERSION
| |||||||||||||||||||||||||||||
| 65 | agent => 'Kubernetes::REST Perl Client ' . ($Kubernetes::REST::VERSION // 'dev'), | ||||||||||||||||||||||||||||
| 66 | timeout => $self->timeout, | ||||||||||||||||||||||||||||
| 67 | ssl_opts => \%ssl_opts, | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | 63 | }); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 5 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 5 | my ($self, $req) = @_; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | my $http_req = HTTP::Request->new( | ||||||||||||||||||||||||||||
| 77 | $req->method, | ||||||||||||||||||||||||||||
| 78 | $req->url, | ||||||||||||||||||||||||||||
| 79 | 5 | [ %{$req->headers} ], | |||||||||||||||||||||||||||
| 80 | $req->content, | ||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 5 | my $res = $self->ua->request($http_req); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 5 | return $self->_response($res); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | ▶ | sub call_streaming { | ||||||||||||||||||||||||||
|
Subroutine
call_streaming: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 1 | my ($self, $req, $data_callback) = @_; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | my $http_req = HTTP::Request->new( | ||||||||||||||||||||||||||||
| 93 | $req->method, | ||||||||||||||||||||||||||||
| 94 | $req->url, | ||||||||||||||||||||||||||||
| 95 | 1 | [ %{$req->headers} ], | |||||||||||||||||||||||||||
| 96 | ); | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 2 | ▶ | my $res = $self->ua->request($http_req, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | 2 | my ($chunk) = @_; | |||||||||||||||||||||||||||
| 100 | 2 | $data_callback->($chunk); | |||||||||||||||||||||||||||
| 101 | 1 | }); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | return $self->_response($res); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # charset => 'none' undoes Content-Encoding (gzip et al) but leaves the charset | ||||||||||||||||||||||||||||
| 107 | # alone, so the body stays bytes - which is what Kubernetes::REST and IO::K8s | ||||||||||||||||||||||||||||
| 108 | # expect, and what HTTPTinyIO hands back. Plain decoded_content() would decode | ||||||||||||||||||||||||||||
| 109 | # UTF-8 here and IO::K8s would then decode the characters a second time. | ||||||||||||||||||||||||||||
| 110 | 6 | ▶ | sub _response { | ||||||||||||||||||||||||||
|
Subroutine
_response: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 111 | 6 | my ($self, $res) = @_; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 6 | my $content = $res->decoded_content(charset => 'none'); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 6 | ▶ | return Kubernetes::REST::HTTPResponse->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $content and length $content
MC/DC50%1/2
defined $content and length $content
defined $content
length $content
Truth table66%2/3
A and B
A= defined $contentB= length $content
| |||||||||||||||||||||||||||||
| 116 | status => $res->code, | ||||||||||||||||||||||||||||
| 117 | (defined $content && length $content) ? ( content => $content ) : (), | ||||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 63 | 1; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =pod | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head1 NAME | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | Kubernetes::REST::LWPIO - HTTP client using LWP::UserAgent | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head1 VERSION | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | version 1.109 | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | use Kubernetes::REST::LWPIO; | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | my $io = Kubernetes::REST::LWPIO->new( | ||||||||||||||||||||||||||||
| 142 | ssl_verify_server => 1, | ||||||||||||||||||||||||||||
| 143 | ssl_ca_file => '/path/to/ca.crt', | ||||||||||||||||||||||||||||
| 144 | ); | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Access the LWP::UserAgent for debugging (e.g. with LWP::ConsoleLogger) | ||||||||||||||||||||||||||||
| 147 | use LWP::ConsoleLogger::Easy qw(debug_ua); | ||||||||||||||||||||||||||||
| 148 | debug_ua($io->ua); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | HTTP client implementation using L<LWP::UserAgent> for making Kubernetes API requests. This is the default IO backend for L<Kubernetes::REST>. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | The C<ua> attribute is exposed so that debugging tools like L<LWP::ConsoleLogger> can be attached to inspect HTTP traffic. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | Response bodies are returned as bytes, via C<< decoded_content(charset => 'none') >>: C<Content-Encoding> is undone, the charset is left to L<Kubernetes::REST>. See L<Kubernetes::REST::Role::IO/Encoding contract>. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | This backend does not implement C<call_duplex>: L<Kubernetes::REST> methods that | ||||||||||||||||||||||||||||
| 159 | need full-duplex transport (C<port_forward>, C<exec>, C<attach>) croak against | ||||||||||||||||||||||||||||
| 160 | it by design. Use L<Net::Async::Kubernetes> for those. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head2 ssl_verify_server | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Boolean. Whether to verify the server's SSL certificate. Defaults to true. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head2 ssl_cert_file | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Path to a client certificate file (PEM) for TLS client-certificate authentication. Ignored when C<ssl_cert_pem> is set. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head2 ssl_cert_pem | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Client certificate as a PEM string, for TLS client-certificate authentication. Takes precedence over C<ssl_cert_file>. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head2 ssl_key_file | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Path to the private key file (PEM) matching C<ssl_cert_file> or C<ssl_cert_pem>. Ignored when C<ssl_key_pem> is set. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head2 ssl_key_pem | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Private key as a PEM string, matching C<ssl_cert_file> or C<ssl_cert_pem>. Takes precedence over C<ssl_key_file>. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head2 ssl_ca_file | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Path to a CA certificate file (PEM) used to verify the server's certificate. Ignored when C<ssl_ca_pem> is set. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head2 ssl_ca_pem | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | CA certificate as a PEM string, used to verify the server's certificate. Takes precedence over C<ssl_ca_file>. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head2 timeout | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | Timeout in seconds for HTTP requests. Defaults to 310 (slightly more than the Kubernetes default watch timeout of 300s). | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head2 ua | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | The underlying L<LWP::UserAgent> instance. Access this to attach middleware such as L<LWP::ConsoleLogger> for HTTP debugging. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head2 call | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | my $response = $io->call($req); | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Execute an HTTP request. Receives a fully prepared L<Kubernetes::REST::HTTPRequest> (URL, headers, content all set). Returns a L<Kubernetes::REST::HTTPResponse>. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head2 call_streaming | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | my $response = $io->call_streaming($req, sub { my ($chunk) = @_; ... }); | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Execute an HTTP request with streaming response. The C<$data_callback> is called with each chunk of data as it arrives. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | Used internally by L<Kubernetes::REST/watch> and L<Kubernetes::REST/log> for the Watch and Pod Log APIs. | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =over | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =item * L<Kubernetes::REST> - Main API client | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =item * L<Kubernetes::REST::Role::IO> - IO interface role | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =item * L<Kubernetes::REST::HTTPTinyIO> - Alternative HTTP::Tiny backend | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =item * L<LWP::ConsoleLogger> - HTTP debugging tool | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =back | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =head2 Issues | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 231 | L<https://github.com/pplu/kubernetes-rest/issues>. | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =head2 IRC | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | Join C<#kubernetes> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =over 4 | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =item * | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =item * | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | Jose Luis Martinez Torres <jlmartin@cpan.org> | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =back | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>. | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | The Apache License, Version 2.0, January 2004 | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =cut | ||||||||||||||||||||||||||||