blib/lib/Kubernetes/REST/V0Group.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 176 CC 29 SCAR 46.4
CC
29
Cov
0%
_warn_deprecated
210.0
_dispatch
72.0
AUTOLOAD
20.0
CRAP 103.3
1 package Kubernetes::REST::V0Group;
2 our $VERSION = '1.108';
3 # ABSTRACT: Base class for backwards-compatible v0 API group wrappers
4 use Moo;
5 use Carp qw(croak carp);
6  
7 has api => (is => 'ro', required => 1);
8 has group => (is => 'ro', required => 1);
9 has version => (is => 'ro', default => sub { 'v1' });
10  
11 # ============================================================================
12 # BACKWARDS COMPATIBILITY LAYER (v0 API → v1 API)
13 #
14 # The original Kubernetes::REST (v0.01/v0.02, by JLMARTIN) used method names
15 # like $api->Core->ListNamespacedPod(...) with dedicated Call classes in
16 # lib/Kubernetes/REST/Call/v1/Core/ListNamespacedPod.pm (978 classes total).
17 #
18 # Our v1 rewrite simplified this to $api->list('Pod', namespace => ...).
19 #
20 # AUTOLOAD catches the old method names (e.g. "ListNamespacedPod"), parses
21 # them into action + resource, and dispatches to the new API. Each call
22 # emits a deprecation warning showing the new equivalent call.
23 #
24 # Subclasses (Kubernetes::REST::Core, ::Apps, ::Batch, etc.) only set the
25 # 'group' attribute. AUTOLOAD + _parse_method + _dispatch handle the rest.
26 #
27 # Pattern: {Action}{Namespaced?}{Resource}{ForAllNamespaces?}
28 # Actions: List, Read, Create, Replace, Patch, Delete, Watch
29 # Example: ListNamespacedPod → list('IO::K8s::Api::Core::V1::Pod', ...)
30 #
31 # The old Call classes no longer ship here at all - their names are tombstoned
32 # in Kubernetes-REST-Deprecated. This layer is the only thing left keeping the
33 # v0 method names alive, and can go once no downstream code uses them.
34 # ============================================================================
35  
36 our $AUTOLOAD;
37  
38 sub AUTOLOAD {
39 my ($self, @args) = @_;
40 my $method = $AUTOLOAD;
41 $method =~ s/.*:://;
42  
43 return if $method eq 'DESTROY';
44  
45 # Parse method name: ListNamespacedPod, ReadNamespacedPod, CreateNamespacedPod, etc.
46 my ($action, $namespaced, $resource) = _parse_method($method);
47  
48 unless ($action && $resource) {
49 croak "Unknown method: $method";
50 }
51  
52 # Build class name
53 my $class = $self->_build_class($resource);
54  
55 # Convert args to hash if needed
56 my %params = @args == 1 && ref($args[0]) eq 'HASH' ? %{$args[0]} : @args;
57  
58 # Show deprecation warning
59 $self->_warn_deprecated($method, $action, $class, \%params);
60  
61 # Call new API
62 return $self->_dispatch($action, $class, \%params);
63 }
64  
65 sub _parse_method {
66 my ($method) = @_;
67  
68 # Patterns: List/Read/Create/Replace/Patch/Delete/Watch + Namespaced? + Resource + ForAllNamespaces?
69 if ($method =~ /^(List|Read|Create|Replace|Patch|Delete|Watch)(Namespaced)?(\w+?)(ForAllNamespaces|Status)?$/) {
70 my ($action, $namespaced, $resource, $suffix) = ($1, $2, $3, $4);
71 $namespaced = 0 if $suffix && $suffix eq 'ForAllNamespaces';
72 return (lc($action), $namespaced ? 1 : 0, $resource);
73 }
74  
75 return (undef, undef, undef);
76 }
77  
78 # v0 group name -> IO::K8s group path. Only entries that actually rename
79 # something belong here; anything else falls through to the group name as
80 # written, which is already the IO::K8s spelling for every shipped group.
81 my %GROUP_MAP = (
82 RbacAuthorization => 'Rbac',
83 );
84  
85 sub _build_class {
86 my ($self, $resource) = @_;
87 my $group = $self->group;
88 my $version = ucfirst(lc($self->version));
89  
90 my $io_group = $GROUP_MAP{$group} // $group;
91  
92 # Not every group lives under IO::K8s::Api:: - apiextensions and
93 # apiregistration sit in their own staging namespaces. Kubernetes::REST
94 # owns that table, so ask it rather than reimplementing the exception
95 # here; the two copies drifting apart is exactly what broke this before.
96 return 'IO::K8s::' . $self->api->_io_k8s_namespace_for_group_path($io_group)
97 . "::${version}::${resource}";
98 }
99  
100 sub _warn_deprecated {
101 my ($self, $method, $action, $class, $params) = @_;
102  
103 return if $ENV{HIDE_KUBERNETES_REST_V0_API_WARNING};
104  
105 my $group = $self->group;
106 my $new_call;
107  
108 if ($action eq 'list') {
109 my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : '';
110 $new_call = "\$api->list('$class'$ns)";
111 } elsif ($action eq 'read') {
112 my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : '';
113 $new_call = "\$api->get('$class', name => '$params->{name}'$ns)";
114 } elsif ($action eq 'create') {
115 $new_call = "\$api->create(\$object)";
116 } elsif ($action eq 'replace') {
117 $new_call = "\$api->update(\$object)";
118 } elsif ($action eq 'delete') {
119 my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : '';
120 $new_call = "\$api->delete('$class', name => '$params->{name}'$ns)";
121 } elsif ($action eq 'patch') {
122 my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : '';
123 $new_call = "\$api->patch('$class', name => '$params->{name}'$ns, patch => \\%patch)";
124 } elsif ($action eq 'watch') {
125 my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : '';
126 $new_call = "\$api->watch('$class'$ns, on_event => sub { ... })";
127 } else {
128 $new_call = "\$api->$action('$class', ...)";
129 }
130  
131 carp "Kubernetes::REST v0 API is deprecated: \$api->$group->$method(...) should be: $new_call";
132 }
133  
134 sub _dispatch {
135 my ($self, $action, $class, $params) = @_;
136 my $api = $self->api;
137  
138 if ($action eq 'list') {
139 return $api->list($class, %$params);
140 } elsif ($action eq 'read') {
141 return $api->get($class, %$params);
142 } elsif ($action eq 'create') {
143 # For create, we need the body object
144 my $body = $params->{body} // croak "create requires 'body' parameter";
145 return $api->create($body);
146 } elsif ($action eq 'replace') {
147 my $body = $params->{body} // croak "replace requires 'body' parameter";
148 return $api->update($body);
149 } elsif ($action eq 'delete') {
150 return $api->delete($class, %$params);
151 } elsif ($action eq 'patch') {
152 return $api->patch($class, %$params);
153 } elsif ($action eq 'watch') {
154 return $api->watch($class, %$params);
155 } else {
156 croak "Unknown action: $action";
157 }
158 }
159  
160 # Prevent AUTOLOAD from being called for can()
161 sub can {
162 my ($self, $method) = @_;
163 return $self->SUPER::can($method) if $self->SUPER::can($method);
164 # For v0 API methods, we always "can"
165 my ($action, $namespaced, $resource) = _parse_method($method);
166 return sub { $self->$method(@_) } if $action && $resource;
167 return undef;
168 }
169  
170 1;
171  
172 __END__
173  
174 =pod
175  
176 =encoding UTF-8
177  
178 =head1 NAME
179  
180 Kubernetes::REST::V0Group - Base class for backwards-compatible v0 API group wrappers
181  
182 =head1 VERSION
183  
184 version 1.108
185  
186 =head1 SYNOPSIS
187  
188 # This API is deprecated - use the new API instead
189 my $api = Kubernetes::REST->new(...);
190  
191 # Old way (deprecated):
192 my $pods = $api->Core->ListNamespacedPod(namespace => 'default');
193  
194 # New way:
195 my $pods = $api->list('Pod', namespace => 'default');
196  
197 =head1 DESCRIPTION
198  
199 This module provides backwards compatibility for the old v0 API that used method
200 names like C<ListNamespacedPod>, C<ReadNamespacedPod>, etc. It translates these
201 calls to the new simplified API.
202  
203 Every call through this layer emits a deprecation warning unless you set
204 C<$ENV{HIDE_KUBERNETES_REST_V0_API_WARNING}>.
205  
206 See L<Kubernetes::REST/"UPGRADING FROM 0.02"> for migration guide.
207  
208 =head1 NAME
209  
210 Kubernetes::REST::V0Group - Base class for backwards-compatible v0 API group wrappers
211  
212 =head1 METHODS
213  
214 This module uses C<AUTOLOAD> to intercept method calls like C<ListNamespacedPod>
215 and translates them to the new API. The following actions are supported:
216  
217 =over 4
218  
219 =item * List -> list()
220  
221 =item * Read -> get()
222  
223 =item * Create -> create()
224  
225 =item * Replace -> update()
226  
227 =item * Delete -> delete()
228  
229 =item * Patch -> patch()
230  
231 =item * Watch -> watch()
232  
233 =back
234  
235 =head1 SEE ALSO
236  
237 L<Kubernetes::REST>
238  
239 =head1 SUPPORT
240  
241 =head2 Issues
242  
243 Please report bugs and feature requests on GitHub at
244 L<https://github.com/pplu/kubernetes-rest/issues>.
245  
246 =head2 IRC
247  
248 Join C<#kubernetes> on C<irc.perl.org> or message Getty directly.
249  
250 =head1 CONTRIBUTING
251  
252 Contributions are welcome! Please fork the repository and submit a pull request.
253  
254 =head1 AUTHORS
255  
256 =over 4
257  
258 =item *
259  
260 Torsten Raudssus <getty@cpan.org>
261  
262 =item *
263  
264 Jose Luis Martinez Torres <jlmartin@cpan.org>
265  
266 =back
267  
268 =head1 COPYRIGHT AND LICENSE
269  
270 This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>.
271  
272 This is free software, licensed under:
273  
274 The Apache License, Version 2.0, January 2004
275  
276 =cut