lib/Kubernetes/REST/V0Group.pm

filter: Statementstmt 91.9% 91 / 99 Branchbran 84.1% 69 / 82 Conditioncond 79.4% 27 / 34 MC/DCmcdc 66.6% 14 / 21 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 1 totaltotal 85.0% 211 / 248 CC 53 SCAR 26.5
CC
53
Cov
87%
_warn_deprecated
23.6
_dispatch
17.9
_parse_method
10.0
CRAP 14.2
« lib/Kubernetes/REST/CLI/Watch.pm ↑ Summary lib/Kubernetes/REST.pm »
1 package Kubernetes::REST::V0Group;
2 9 our $VERSION = '1.110';
3 # ABSTRACT: Base class for backwards-compatible v0 API group wrappers
4 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use IO::K8s ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 has api => (is => 'ro', required => 1);
9 9 has group => (is => 'ro', required => 1);
10 94 ▶has version => (is => 'ro', default => sub { 'v1' });
Subroutine __ANON__: called CC=1 SCAR=0.0
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 9 my %STATUS_KIND = map { $_ => 1 }
19 1836 ▶ grep { /Status\z/ && !m{/} }
Condition100%3/3
/Status\z/ && !m[/]
!ll&&!rl&&r
181899
MC/DC100%2/2
/Status\z/ && !m[/]
/Status\z/ !m[/]
Truth table100%3/3
A && not(B)
A= /Status\z/B= !m[/]
ABresult
0X0
100
111
20 9 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 9 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 9 our @CARP_NOT = ('Kubernetes::REST');
55  
56 150 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=8 SCAR=20.8
57 150 my ($self, @args) = @_;
58 150 my $method = $AUTOLOAD;
59 150 $method =~ s/.*:://;
60  
61 150 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 96 54 150
62  
63 # Parse method name: ListNamespacedPod, ReadNamespacedPod, CreateNamespacedPod, etc.
64 54 my ($action, $namespaced, $resource, $status) = _parse_method($method);
65  
66 54 ▶ unless ($action && $resource) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($action and $resource) 1 53 54
Condition66%2/3
$action and $resource
!ll&&!rl&&r
1053
MC/DC50%1/2
$action and $resource
$action $resource
Truth table66%2/3
A and B
A= $actionB= $resource
ABresult
0X0
100
111
67 1 croak "Unknown method: $method";
68 }
69  
70 # Build class name
71 53 my $class = $self->_build_class($resource);
72  
73 # Convert args to hash if needed
74 53 ▶ my %params = @args == 1 && ref($args[0]) eq 'HASH' ? %{$args[0]} : @args;
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 && ref $args[0] eq 'HASH' ? : 1 52 53
Condition66%2/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
5201
MC/DC50%1/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
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 53 ▶ $params{subresource} = 'status' if $status && $action eq 'read';
Branch: 1 branch
BranchTrueFalseTotal
if $status and $action eq "read" 5 48 53
Condition100%3/3
$status and $action eq "read"
!ll&&!rl&&r
4265
MC/DC100%2/2
$status and $action eq "read"
$status $action eq "read"
Truth table100%3/3
A and B
A= $statusB= $action eq "read"
ABresult
0X0
100
111
79  
80 # Show deprecation warning
81 53 $self->_warn_deprecated($method, $action, $class, \%params, $status);
82  
83 # Call new API
84 53 return $self->_dispatch($action, $class, \%params, $status);
85 }
86  
87 157 ▶sub _parse_method {
Subroutine _parse_method: called CC=10 SCAR=23.0
88 157 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 157 ▶ if ($method =~ /^(List|Read|Create|Replace|Patch|Delete|Watch)(Namespaced)?(\w+?)(ForAllNamespaces|Status)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(List|Read|Create|Replace|Patch|Delete|Watch)(Namespaced)?(\w+?)(ForAllNamespaces|Status)?$/) 59 98 157
93 59 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 59 ▶ if ($suffix && $suffix eq 'Status' && $STATUS_KIND{$resource . $suffix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($suffix and $suffix eq "Status" and $STATUS_KIND{$resource . $suffix}) 4 55 59
Condition100%6/6
$suffix and $suffix eq "Status" and $STATUS_KIND{$resource . $suffix}
!ll&&!rl&&r
42134
$suffix and $suffix eq "Status"
!ll&&!rl&&r
41117
MC/DC100%3/3
$suffix and $suffix eq "Status" and $STATUS_KIND{$resource . $suffix}
$suffix $suffix eq "Status" $STATUS_KIND{$resource . $suffix}
Truth table100%4/4
A and B and C
A= $suffixB= $suffix eq "Status"C= $STATUS_KIND{$resource . $suffix}
ABCresult
0XX0
10X0
1100
1111
99 4 $resource .= $suffix;
100 4 $suffix = undef;
101 }
102  
103 59 ▶ $namespaced = 0 if $suffix && $suffix eq 'ForAllNamespaces';
Branch: 1 branch
BranchTrueFalseTotal
if $suffix and $suffix eq "ForAllNamespaces" 1 58 59
Condition100%3/3
$suffix and $suffix eq "ForAllNamespaces"
!ll&&!rl&&r
45131
MC/DC100%2/2
$suffix and $suffix eq "ForAllNamespaces"
$suffix $suffix eq "ForAllNamespaces"
Truth table100%3/3
A and B
A= $suffixB= $suffix eq "ForAllNamespaces"
ABresult
0X0
100
111
104 59 ▶ return (lc($action), $namespaced ? 1 : 0, $resource,
Branch: 2 branches
BranchTrueFalseTotal
$namespaced ? : 34 25 59
$suffix && $suffix eq 'Status' ? : 13 46 59
Condition100%3/3
$suffix and $suffix eq "Status"
!ll&&!rl&&r
45113
MC/DC100%2/2
$suffix and $suffix eq "Status"
$suffix $suffix eq "Status"
Truth table100%3/3
A and B
A= $suffixB= $suffix eq "Status"
ABresult
0X0
100
111
105 ($suffix && $suffix eq 'Status') ? 1 : 0);
106 }
107  
108 98 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 9 my %GROUP_MAP = (
115 RbacAuthorization => 'Rbac',
116 );
117  
118 77 ▶sub _build_class {
Subroutine _build_class: called CC=2 SCAR=7.0
119 77 my ($self, $resource) = @_;
120 77 my $group = $self->group;
121 77 my $version = ucfirst(lc($self->version));
122  
123 77 ▶ my $io_group = $GROUP_MAP{$group} // $group;
Condition66%2/3
$GROUP_MAP{$group} // $group
l!l&&r!l&&!r
2750
MC/DC0%0/2
$GROUP_MAP{$group} // $group
$GROUP_MAP{$group} $group
Truth table66%2/3
A // B
A= $GROUP_MAP{$group}B= $group
ABresult
1X1
011
000
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 77 return 'IO::K8s::' . $self->api->_io_k8s_namespace_for_group_path($io_group)
130 . "::${version}::${resource}";
131 }
132  
133 53 ▶sub _warn_deprecated {
Subroutine _warn_deprecated: called CC=17 SCAR=31.6
134 53 my ($self, $method, $action, $class, $params, $status) = @_;
135  
136 53 ▶ return if $ENV{HIDE_KUBERNETES_REST_V0_API_WARNING};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HIDE_KUBERNETES_REST_V0_API_WARNING"} 46 7 53
137  
138 7 my $group = $self->group;
139 7 my $new_call;
140  
141 7 ▶ if ($action eq 'list') {
Branch: 7 branches
BranchTrueFalseTotal
if ($action eq 'list') { } 1 6 7
elsif ($action eq 'read') { } 3 3 6
elsif ($action eq 'create') { } 0 3 3
elsif ($action eq 'replace') { } 1 2 3
elsif ($action eq 'delete') { } 1 1 2
elsif ($action eq 'patch') { } 1 0 1
elsif ($action eq 'watch') { } 0 0 0
142 1 ▶ my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : '';
Branch: 1 branch
BranchTrueFalseTotal
$params->{'namespace'} ? : 0 1 1
143 1 $new_call = "\$api->list('$class'$ns)";
144 } elsif ($action eq 'read') {
145 3 ▶ my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : '';
Branch: 1 branch
BranchTrueFalseTotal
$params->{'namespace'} ? : 2 1 3
146 3 ▶ my $sub = $params->{subresource} ? ", subresource => '$params->{subresource}'" : '';
Branch: 1 branch
BranchTrueFalseTotal
$params->{'subresource'} ? : 1 2 3
147 3 $new_call = "\$api->get('$class', name => '$params->{name}'$ns$sub)";
148 } elsif ($action eq 'create') {
149 0 $new_call = "\$api->create(\$object)";
150 } elsif ($action eq 'replace') {
151 1 ▶ $new_call = $status ? "\$api->update_status(\$object)"
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 1 0 1
152 : "\$api->update(\$object)";
153 } elsif ($action eq 'delete') {
154 1 ▶ my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : '';
Branch: 1 branch
BranchTrueFalseTotal
$params->{'namespace'} ? : 0 1 1
155 1 $new_call = "\$api->delete('$class', name => '$params->{name}'$ns)";
156 } elsif ($action eq 'patch') {
157 1 ▶ my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : '';
Branch: 1 branch
BranchTrueFalseTotal
$params->{'namespace'} ? : 1 0 1
158 1 ▶ my $name = $status ? 'patch_status' : 'patch';
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 1 0 1
159 1 $new_call = "\$api->$name('$class', name => '$params->{name}'$ns, patch => \\%patch)";
160 } elsif ($action eq 'watch') {
161 0 ▶ my $ns = $params->{namespace} ? ", namespace => '$params->{namespace}'" : '';
Branch: 1 branch
BranchTrueFalseTotal
$params->{'namespace'} ? : 0 0 0
162 0 $new_call = "\$api->watch('$class'$ns, on_event => sub { ... })";
163 } else {
164 0 $new_call = "\$api->$action('$class', ...)";
165 }
166  
167 7 carp "Kubernetes::REST v0 API is deprecated: \$api->$group->$method(...) should be: $new_call";
168 }
169  
170 53 ▶sub _dispatch {
Subroutine _dispatch: called CC=17 SCAR=28.8
171 53 my ($self, $action, $class, $params, $status) = @_;
172 53 my $api = $self->api;
173  
174 53 ▶ if ($action eq 'list') {
Branch: 7 branches
BranchTrueFalseTotal
if ($action eq 'list') { } 27 26 53
elsif ($action eq 'read') { } 13 13 26
elsif ($action eq 'create') { } 0 13 13
elsif ($action eq 'replace') { } 4 9 13
elsif ($action eq 'delete') { } 3 6 9
elsif ($action eq 'patch') { } 5 1 6
elsif ($action eq 'watch') { } 1 0 1
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 27 ▶ my %args = map { exists $params->{$_} ? ($_ => $params->{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{$_} ? : 13 68 81
182 qw(namespace labelSelector fieldSelector);
183 27 return $api->list($class, %args);
184 } elsif ($action eq 'read') {
185 13 ▶ my %args = map { exists $params->{$_} ? ($_ => $params->{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{$_} ? : 27 12 39
186 qw(name namespace subresource);
187 13 return $api->get($class, %args);
188 } elsif ($action eq 'create') {
189 # For create, we need the body object
190 0 ▶ my $body = $params->{body} // croak "create requires 'body' parameter";
Condition0%0/2
$params->{"body"} // croak("create requires 'body' parameter")
l!l
00
MC/DC0%0/1
$params->{"body"} // croak("create requires 'body' parameter")
$params->{"body"}
Truth table0%0/2
A
A= $params->{"body"}
Aresult
11
00
191 0 return $api->create($body);
192 } elsif ($action eq 'replace') {
193 4 ▶ my $body = $params->{body} // croak "replace requires 'body' parameter";
Condition50%1/2
$params->{"body"} // croak("replace requires 'body' parameter")
l!l
40
MC/DC0%0/1
$params->{"body"} // croak("replace requires 'body' parameter")
$params->{"body"}
Truth table50%1/2
A
A= $params->{"body"}
Aresult
11
00
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 4 ▶ return $status ? $api->update_status($body) : $api->update($body);
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 3 1 4
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 3 ▶ my %args = map { exists $params->{$_} ? ($_ => $params->{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{$_} ? : 5 4 9
203 qw(name namespace propagationPolicy);
204 3 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 5 ▶ my %args = map { exists $params->{$_} ? ($_ => $params->{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{$_} ? : 15 5 20
210 qw(name namespace patch type);
211 # Patch*Status patches through /status, for the same reason as
212 # Replace*Status above (karr k65).
213 5 ▶ return $status ? $api->patch_status($class, %args) : $api->patch($class, %args);
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 3 2 5
214 } elsif ($action eq 'watch') {
215 1 ▶ my %args = map { exists $params->{$_} ? ($_ => $params->{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $params->{$_} ? : 2 4 6
216 qw(on_event timeout resourceVersion labelSelector fieldSelector namespace);
217 1 return $api->watch($class, %args);
218 } else {
219 0 croak "Unknown action: $action";
220 }
221 }
222  
223 # Prevent AUTOLOAD from being called for can()
224 211 ▶sub can {
Subroutine can: called CC=4 SCAR=14.0
Pod undocumented
225 211 my ($self, $method) = @_;
226 211 ▶ return $self->SUPER::can($method) if $self->SUPER::can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $self->SUPER::can($method) 112 99 211
227 # For v0 API methods, we always "can"
228 99 my ($action, $namespaced, $resource) = _parse_method($method);
229 0 ▶ return sub { $self->$method(@_) } if $action && $resource;
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if $action and $resource 2 97 99
Condition66%2/3
$action and $resource
!ll&&!rl&&r
9702
MC/DC50%1/2
$action and $resource
$action $resource
Truth table66%2/3
A and B
A= $actionB= $resource
ABresult
0X0
100
111
230 97 return undef;
231 }
232  
233 9 1;
234  
235 __END__
« lib/Kubernetes/REST/CLI/Watch.pm ↑ Summary lib/Kubernetes/REST.pm »