| 1 | package Kubernetes::REST::V0Group; | ||
| 2 | our $VERSION = '1.109'; | ||
| 3 | # ABSTRACT: Base class for backwards-compatible v0 API group wrappers | ||
| 4 | use Moo; | ||
| 5 | use Carp qw(croak carp); | ||
| 6 | use IO::K8s (); | ||
| 7 | |||
| 8 | has api => (is => 'ro', required => 1); | ||
| 9 | has group => (is => 'ro', required => 1); | ||
| 10 | has version => (is => 'ro', default => sub { 'v1' }); | ||
| 11 | |||
| 12 | # Kinds whose own name ends in Status - ComponentStatus is the only built-in | ||
| 13 | # one. _parse_method's non-greedy resource capture would otherwise read | ||
| 14 | # ListComponentStatus as the Kind Component plus a Status subresource suffix | ||
| 15 | # and die loading a non-existent IO::K8s::Api::Core::V1::Component. Built once | ||
| 16 | # from what the installed IO::K8s ships (bare Kind names, not the versioned | ||
| 17 | # aliases), so a future Status-named Kind is handled too (karr k66). | ||
| 18 | my %STATUS_KIND = map { $_ => 1 } | ||
| 19 | grep { /Status\z/ && !m{/} } | ||
| 20 | keys %{ IO::K8s->default_resource_map }; | ||
| 21 | |||
| 22 | # ============================================================================ | ||
| 23 | # BACKWARDS COMPATIBILITY LAYER (v0 API → v1 API) | ||
| 24 | # | ||
| 25 | # The original Kubernetes::REST (v0.01/v0.02, by JLMARTIN) used method names | ||
| 26 | # like $api->Core->ListNamespacedPod(...) with dedicated Call classes in | ||
| 27 | # lib/Kubernetes/REST/Call/v1/Core/ListNamespacedPod.pm (978 classes total). | ||
| 28 | # | ||
| 29 | # Our v1 rewrite simplified this to $api->list('Pod', namespace => ...). | ||
| 30 | # | ||
| 31 | # AUTOLOAD catches the old method names (e.g. "ListNamespacedPod"), parses | ||
| 32 | # them into action + resource, and dispatches to the new API. Each call | ||
| 33 | # emits a deprecation warning showing the new equivalent call. | ||
| 34 | # | ||
| 35 | # Subclasses (Kubernetes::REST::Core, ::Apps, ::Batch, etc.) only set the | ||
| 36 | # 'group' attribute. AUTOLOAD + _parse_method + _dispatch handle the rest. | ||
| 37 | # | ||
| 38 | # Pattern: {Action}{Namespaced?}{Resource}{ForAllNamespaces?} | ||
| 39 | # Actions: List, Read, Create, Replace, Patch, Delete, Watch | ||
| 40 | # Example: ListNamespacedPod → list('IO::K8s::Api::Core::V1::Pod', ...) | ||
| 41 | # | ||
| 42 | # The old Call classes no longer ship here at all - their names are tombstoned | ||
| 43 | # in Kubernetes-REST-Deprecated. This layer is the only thing left keeping the | ||
| 44 | # v0 method names alive, and can go once no downstream code uses them. | ||
| 45 | # ============================================================================ | ||
| 46 | |||
| 47 | our $AUTOLOAD; | ||
| 48 | |||
| 49 | # AUTOLOAD dispatches to list/get/update/... in Kubernetes::REST, so an | ||
| 50 | # APIError thrown there - or a plain croak - has this layer's frames between | ||
| 51 | # it and the caller. Trust Kubernetes::REST so Carp's bidirectional check | ||
| 52 | # walks past both packages and blames the code that made the v0 call, not | ||
| 53 | # V0Group.pm; the object model's own errors are unaffected (karr k67). | ||
| 54 | our @CARP_NOT = ('Kubernetes::REST'); | ||
| 55 | |||
| 56 | sub AUTOLOAD { | ||
| 57 | my ($self, @args) = @_; | ||
| 58 | my $method = $AUTOLOAD; | ||
| 59 | $method =~ s/.*:://; | ||
| 60 | |||
| 61 | return if $method eq 'DESTROY'; | ||
| 62 | |||
| 63 | # Parse method name: ListNamespacedPod, ReadNamespacedPod, CreateNamespacedPod, etc. | ||
| 64 | my ($action, $namespaced, $resource, $status) = _parse_method($method); | ||
| 65 | |||
| 66 | unless ($action && $resource) { | ||
| 67 | croak "Unknown method: $method"; | ||
| 68 | } | ||
| 69 | |||
| 70 | # Build class name | ||
| 71 | my $class = $self->_build_class($resource); | ||
| 72 | |||
| 73 | # Convert args to hash if needed | ||
| 74 | my %params = @args == 1 && ref($args[0]) eq 'HASH' ? %{$args[0]} : @args; | ||
| 75 | |||
| 76 | # Read*Status reads the status subresource, not the object itself (karr | ||
| 77 | # k62); get takes it since k58. Set here, the warning names it too. | ||
| 78 | $params{subresource} = 'status' if $status && $action eq 'read'; | ||
| 79 | |||
| 80 | # Show deprecation warning | ||
| 81 | $self->_warn_deprecated($method, $action, $class, \%params, $status); | ||
| 82 | |||
| 83 | # Call new API | ||
| 84 | return $self->_dispatch($action, $class, \%params, $status); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _parse_method { | ||
| 88 | my ($method) = @_; | ||
| 89 | |||
| 90 | # Patterns: List/Read/Create/Replace/Patch/Delete/Watch + Namespaced? + Resource + ForAllNamespaces|Status? | ||
| 91 | # The fourth value says whether the name ends in Status. | ||
| 92 | if ($method =~ /^(List|Read|Create|Replace|Patch|Delete|Watch)(Namespaced)?(\w+?)(ForAllNamespaces|Status)?$/) { | ||
| 93 | my ($action, $namespaced, $resource, $suffix) = ($1, $2, $3, $4); | ||
| 94 | |||
| 95 | # A Kind that itself ends in Status (ComponentStatus) is one whole | ||
| 96 | # resource, not a shorter Kind plus a Status subresource: the | ||
| 97 | # non-greedy capture split it, so put the suffix back (karr k66). | ||
| 98 | if ($suffix && $suffix eq 'Status' && $STATUS_KIND{$resource . $suffix}) { | ||
| 99 | $resource .= $suffix; | ||
| 100 | $suffix = undef; | ||
| 101 | } | ||
| 102 | |||
| 103 | $namespaced = 0 if $suffix && $suffix eq 'ForAllNamespaces'; | ||
| 104 | return (lc($action), $namespaced ? 1 : 0, $resource, | ||
| 105 | ($suffix && $suffix eq 'Status') ? 1 : 0); | ||
| 106 | } | ||
| 107 | |||
| 108 | return (undef, undef, undef); | ||
| 109 | } | ||
| 110 | |||
| 111 | # v0 group name -> IO::K8s group path. Only entries that actually rename | ||
| 112 | # something belong here; anything else falls through to the group name as | ||
| 113 | # written, which is already the IO::K8s spelling for every shipped group. | ||
| 114 | my %GROUP_MAP = ( | ||
| 115 | RbacAuthorization => 'Rbac', | ||
| 116 | ); | ||
| 117 | |||
| 118 | sub _build_class { | ||
| 119 | my ($self, $resource) = @_; | ||
| 120 | my $group = $self->group; | ||
| 121 | my $version = ucfirst(lc($self->version)); | ||
| 122 | |||
| 123 | my $io_group = $GROUP_MAP{$group} // $group; | ||
| 124 | |||
| 125 | # Not every group lives under IO::K8s::Api:: - apiextensions and | ||
| 126 | # apiregistration sit in their own staging namespaces. Kubernetes::REST | ||
| 127 | # owns that table, so ask it rather than reimplementing the exception | ||
| 128 | # here; the two copies drifting apart is exactly what broke this before. | ||
| 129 | return 'IO::K8s::' . $self->api->_io_k8s_namespace_for_group_path($io_group) | ||
| 130 | . "::${version}::${resource}"; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub _warn_deprecated { | ||
| 134 | my ($self, $method, $action, $class, $params, $status) = @_; | ||
| 135 | |||
| 136 | return if $ENV{HIDE_KUBERNETES_REST_V0_API_WARNING}; | ||
| 137 | |||
| 138 | my $group = $self->group; | ||
| 139 | my $new_call; | ||
| 140 | |||
| 141 | if ($action eq 'list') { | ||
| 142 | my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : ''; | ||
| 143 | $new_call = "\$api->list('$class'$ns)"; | ||
| 144 | } elsif ($action eq 'read') { | ||
| 145 | my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : ''; | ||
| 146 | my $sub = $params->{subresource} ? ", subresource => '$params->{subresource}'" : ''; | ||
| 147 | $new_call = "\$api->get('$class', name => '$params->{name}'$ns$sub)"; | ||
| 148 | } elsif ($action eq 'create') { | ||
| 149 | $new_call = "\$api->create(\$object)"; | ||
| 150 | } elsif ($action eq 'replace') { | ||
| 151 | $new_call = $status ? "\$api->update_status(\$object)" | ||
| 152 | : "\$api->update(\$object)"; | ||
| 153 | } elsif ($action eq 'delete') { | ||
| 154 | my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : ''; | ||
| 155 | $new_call = "\$api->delete('$class', name => '$params->{name}'$ns)"; | ||
| 156 | } elsif ($action eq 'patch') { | ||
| 157 | my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : ''; | ||
| 158 | my $name = $status ? 'patch_status' : 'patch'; | ||
| 159 | $new_call = "\$api->$name('$class', name => '$params->{name}'$ns, patch => \\%patch)"; | ||
| 160 | } elsif ($action eq 'watch') { | ||
| 161 | my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : ''; | ||
| 162 | $new_call = "\$api->watch('$class'$ns, on_event => sub { ... })"; | ||
| 163 | } else { | ||
| 164 | $new_call = "\$api->$action('$class', ...)"; | ||
| 165 | } | ||
| 166 | |||
| 167 | carp "Kubernetes::REST v0 API is deprecated: \$api->$group->$method(...) should be: $new_call"; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub _dispatch { | ||
| 171 | my ($self, $action, $class, $params, $status) = @_; | ||
| 172 | my $api = $self->api; | ||
| 173 | |||
| 174 | if ($action eq 'list') { | ||
| 175 | # list, get and watch croak on arguments they do not take, as delete | ||
| 176 | # does (below). The v0 parameters they have no use for (limit, | ||
| 177 | # pretty, watch, timeoutSeconds, ...) were ignored all along; they | ||
| 178 | # stay ignored rather than break v0 callers. A List* name goes with | ||
| 179 | # them: it made the request a GET of one object, read as an empty | ||
| 180 | # list (karr k58). | ||
| 181 | my %args = map { exists $params->{$_} ? ($_ => $params->{$_}) : () } | ||
| 182 | qw(namespace labelSelector fieldSelector); | ||
| 183 | return $api->list($class, %args); | ||
| 184 | } elsif ($action eq 'read') { | ||
| 185 | my %args = map { exists $params->{$_} ? ($_ => $params->{$_}) : () } | ||
| 186 | qw(name namespace subresource); | ||
| 187 | return $api->get($class, %args); | ||
| 188 | } elsif ($action eq 'create') { | ||
| 189 | # For create, we need the body object | ||
| 190 | my $body = $params->{body} // croak "create requires 'body' parameter"; | ||
| 191 | return $api->create($body); | ||
| 192 | } elsif ($action eq 'replace') { | ||
| 193 | my $body = $params->{body} // croak "replace requires 'body' parameter"; | ||
| 194 | # Replace*Status replaces through the /status subresource: a plain | ||
| 195 | # update writes the main endpoint, where the server drops the status | ||
| 196 | # and still answers 2xx, losing it silently (karr k65). | ||
| 197 | return $status ? $api->update_status($body) : $api->update($body); | ||
| 198 | } elsif ($action eq 'delete') { | ||
| 199 | # delete croaks on arguments it does not take. The v0 parameters it | ||
| 200 | # has no use for (body, gracePeriodSeconds, dryRun, ...) were ignored | ||
| 201 | # all along; they stay ignored rather than break v0 callers. | ||
| 202 | my %args = map { exists $params->{$_} ? ($_ => $params->{$_}) : () } | ||
| 203 | qw(name namespace propagationPolicy); | ||
| 204 | return $api->delete($class, %args); | ||
| 205 | } elsif ($action eq 'patch') { | ||
| 206 | # patch croaks on arguments it does not take (karr k61); the v0 | ||
| 207 | # parameters it has no use for (pretty, dryRun, fieldManager, ...) | ||
| 208 | # stay ignored, as with delete above. | ||
| 209 | my %args = map { exists $params->{$_} ? ($_ => $params->{$_}) : () } | ||
| 210 | qw(name namespace patch type); | ||
| 211 | # Patch*Status patches through /status, for the same reason as | ||
| 212 | # Replace*Status above (karr k65). | ||
| 213 | return $status ? $api->patch_status($class, %args) : $api->patch($class, %args); | ||
| 214 | } elsif ($action eq 'watch') { | ||
| 215 | my %args = map { exists $params->{$_} ? ($_ => $params->{$_}) : () } | ||
| 216 | qw(on_event timeout resourceVersion labelSelector fieldSelector namespace); | ||
| 217 | return $api->watch($class, %args); | ||
| 218 | } else { | ||
| 219 | croak "Unknown action: $action"; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | # Prevent AUTOLOAD from being called for can() | ||
| 224 | sub can { | ||
| 225 | my ($self, $method) = @_; | ||
| 226 | return $self->SUPER::can($method) if $self->SUPER::can($method); | ||
| 227 | # For v0 API methods, we always "can" | ||
| 228 | my ($action, $namespaced, $resource) = _parse_method($method); | ||
| 229 | return sub { $self->$method(@_) } if $action && $resource; | ||
| 230 | return undef; | ||
| 231 | } | ||
| 232 | |||
| 233 | 1; | ||
| 234 | |||
| 235 | __END__ | ||
| 236 | |||
| 237 | =pod | ||
| 238 | |||
| 239 | =encoding UTF-8 | ||
| 240 | |||
| 241 | =head1 NAME | ||
| 242 | |||
| 243 | Kubernetes::REST::V0Group - Base class for backwards-compatible v0 API group wrappers | ||
| 244 | |||
| 245 | =head1 VERSION | ||
| 246 | |||
| 247 | version 1.109 | ||
| 248 | |||
| 249 | =head1 SYNOPSIS | ||
| 250 | |||
| 251 | # This API is deprecated - use the new API instead | ||
| 252 | my $api = Kubernetes::REST->new(...); | ||
| 253 | |||
| 254 | # Old way (deprecated): | ||
| 255 | my $pods = $api->Core->ListNamespacedPod(namespace => 'default'); | ||
| 256 | |||
| 257 | # New way: | ||
| 258 | my $pods = $api->list('Pod', namespace => 'default'); | ||
| 259 | |||
| 260 | =head1 DESCRIPTION | ||
| 261 | |||
| 262 | This module provides backwards compatibility for the old v0 API that used method | ||
| 263 | names like C<ListNamespacedPod>, C<ReadNamespacedPod>, etc. It translates these | ||
| 264 | calls to the new simplified API. | ||
| 265 | |||
| 266 | Every call through this layer emits a deprecation warning unless you set | ||
| 267 | C<$ENV{HIDE_KUBERNETES_REST_V0_API_WARNING}>. | ||
| 268 | |||
| 269 | See L<Kubernetes::REST/"UPGRADING FROM 0.02"> for migration guide. | ||
| 270 | |||
| 271 | =head1 METHODS | ||
| 272 | |||
| 273 | This module uses C<AUTOLOAD> to intercept method calls like C<ListNamespacedPod> | ||
| 274 | and translates them to the new API. The following actions are supported: | ||
| 275 | |||
| 276 | =over 4 | ||
| 277 | |||
| 278 | =item * List -> list() | ||
| 279 | |||
| 280 | =item * Read -> get(); a name ending in C<Status> (C<ReadNamespacedPodStatus>) | ||
| 281 | reads the status subresource, C<< get(..., subresource => 'status') >> | ||
| 282 | |||
| 283 | =item * Create -> create() | ||
| 284 | |||
| 285 | =item * Replace -> update(); a name ending in C<Status> | ||
| 286 | (C<ReplaceNamespacedPodStatus>) replaces through the status subresource, | ||
| 287 | C<update_status()> | ||
| 288 | |||
| 289 | =item * Delete -> delete() | ||
| 290 | |||
| 291 | =item * Patch -> patch(); a name ending in C<Status> | ||
| 292 | (C<PatchNamespacedPodStatus>) patches through the status subresource, | ||
| 293 | C<patch_status()> | ||
| 294 | |||
| 295 | =item * Watch -> watch() | ||
| 296 | |||
| 297 | =back | ||
| 298 | |||
| 299 | List, Read, Watch, Delete and Patch pass on only the parameters the new | ||
| 300 | method takes - C<namespace>, C<labelSelector> and C<fieldSelector>; C<name>, | ||
| 301 | C<namespace> and C<subresource>; C<on_event>, C<timeout>, | ||
| 302 | C<resourceVersion>, C<labelSelector>, C<fieldSelector> and C<namespace>; | ||
| 303 | C<name>, C<namespace> and C<propagationPolicy>; C<name>, C<namespace>, | ||
| 304 | C<patch> and C<type> - and ignore the others, which the new methods croak | ||
| 305 | on. | ||
| 306 | |||
| 307 | The trailing C<Status> is only a subresource suffix when the Kind before it is | ||
| 308 | a real Kind. A Kind whose own name ends in C<Status> - C<ComponentStatus> is | ||
| 309 | the only built-in one - is kept whole: C<ListComponentStatus> and | ||
| 310 | C<ReadComponentStatus> resolve the C<ComponentStatus> Kind itself, not a | ||
| 311 | C<Status> subresource of a C<Component>. | ||
| 312 | |||
| 313 | =head1 SEE ALSO | ||
| 314 | |||
| 315 | L<Kubernetes::REST> | ||
| 316 | |||
| 317 | =head1 SUPPORT | ||
| 318 | |||
| 319 | =head2 Issues | ||
| 320 | |||
| 321 | Please report bugs and feature requests on GitHub at | ||
| 322 | L<https://github.com/pplu/kubernetes-rest/issues>. | ||
| 323 | |||
| 324 | =head2 IRC | ||
| 325 | |||
| 326 | Join C<#kubernetes> on C<irc.perl.org> or message Getty directly. | ||
| 327 | |||
| 328 | =head1 CONTRIBUTING | ||
| 329 | |||
| 330 | Contributions are welcome! Please fork the repository and submit a pull request. | ||
| 331 | |||
| 332 | =head1 AUTHORS | ||
| 333 | |||
| 334 | =over 4 | ||
| 335 | |||
| 336 | =item * | ||
| 337 | |||
| 338 | Torsten Raudssus <getty@cpan.org> | ||
| 339 | |||
| 340 | =item * | ||
| 341 | |||
| 342 | Jose Luis Martinez Torres <jlmartin@cpan.org> | ||
| 343 | |||
| 344 | =back | ||
| 345 | |||
| 346 | =head1 COPYRIGHT AND LICENSE | ||
| 347 | |||
| 348 | This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>. | ||
| 349 | |||
| 350 | This is free software, licensed under: | ||
| 351 | |||
| 352 | The Apache License, Version 2.0, January 2004 | ||
| 353 | |||
| 354 | =cut |