blib/lib/IO/K8s/APIObject.pm

filter: Statementstmt 100% 83 / 83 Branchbran 100% 42 / 42 Conditioncond 90.0% 18 / 20 MC/DCmcdc 81.8% 9 / 11 Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 97.6% 166 / 170 CC 35 SCAR 25.1
CC
35
Cov
99%
_checked_subresources
20.0
import
13.0
__ANON__
2.0
CRAP 12.3
« blib/lib/IO/K8s/CRD.pm ↑ Summary blib/lib/IO/K8s/List.pm »
1 package IO::K8s::APIObject;
2 # ABSTRACT: Base class for top-level Kubernetes API objects
3 152 our $VERSION = '1.110';
4 152 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 152 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 152 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 152 ▶use IO::K8s::Resource ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 152 ▶use Import::Into;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 152 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 152 ▶use Moo::Role ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 152 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 # The import parameters a class may pass (see import below).
15 152 my @KNOWN_PARAMS = qw( api_version resource_plural subresources );
16 152 my %KNOWN_PARAM = map { $_ => 1 } @KNOWN_PARAMS;
17  
18 2760 ▶sub import {
Subroutine import: called CC=13 SCAR=25.6
19 2760 my ($class, @args) = @_;
20 2760 my $caller = caller;
21  
22 # Name => value pairs or nothing: an odd count (a value lost to an edit,
23 # `use IO::K8s::APIObject 'api_version';`) used to leave the lone name
24 # with undef behind Perl's "Odd number of elements" warning (k182).
25 2760 ▶ croak $caller.': odd number of import arguments for '.__PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 2 2758 2760
26 .' ('.scalar(@args).'); expected name => value pairs'
27 if @args % 2;
28 2758 my %params = @args;
29  
30 # A misspelt parameter (subresource, resource_plurals) was ignored and
31 # the class built as if it had never been written (k174). Checked
32 # before anything is set up, so a refused import leaves no half-built
33 # class behind; sort: several unknown keys name a deterministic one.
34 2758 for my $param (sort keys %params) {
35 croak $caller.": unknown import parameter '".$param."' for ".__PACKAGE__
36 .' (known: '.join(', ', @KNOWN_PARAMS).')'
37 3810 ▶ unless $KNOWN_PARAM{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless $KNOWN_PARAM{$param} 5 3805 3810
38 }
39  
40 # api_version and resource_plural, when given, are the class's identity:
41 # an empty or undef value used to fail the truth tests below and was
42 # skipped without a word, leaving the api_version derived from the
43 # package name or no resource_plural at all (k182). exists, not truth,
44 # as for subresources; checked before anything is set up as well.
45 2753 for my $param (grep { exists $params{$_} } qw( api_version resource_plural )) {
46 2748 my $value = $params{$param};
47 2748 ▶ croak $caller.": import parameter '".$param."' for ".__PACKAGE__
Branch: 3 branches
BranchTrueFalseTotal
unless defined $value and not ref $value and length $value 6 2742 2748
!defined($value) ? : 2 4 6
ref $value ? : 2 2 4
Condition100%6/6
defined $value and not ref $value and length $value
!ll&&!rl&&r
422742
defined $value and not ref $value
!ll&&!rl&&r
222744
MC/DC100%3/3
defined $value and not ref $value and length $value
defined $value not ref $value length $value
Truth table100%4/4
A and not(B) and C
A= defined $valueB= not ref $valueC= length $value
ABCresult
0XX0
10X0
1100
1111
48 .' must be a non-empty string, got '
49 .(!defined $value ? 'undef'
50 : ref $value ? 'a reference of type '.ref($value)
51 : 'an empty string')
52 unless defined $value && !ref $value && length $value;
53 }
54  
55 # First, do everything IO::K8s::Resource does
56 2747 IO::K8s::Resource->import::into($caller);
57  
58 # Install CRD overrides *before* applying the role
59 # This way the role sees these methods and doesn't install its defaults
60 2747 ▶ if (defined(my $api_ver = $params{api_version})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $api_ver = $params{"api_version"})) 1373 1374 2747
61 1373 my $stash = Package::Stash->new($caller);
62 # A fixed identity method, not a writable field: reject an argument
63 # rather than swallow it (k67).
64 4926 ▶ $stash->add_symbol('&api_version', sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
65 4926 ▶ croak 'api_version is fixed for this class and cannot be set' if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 1 4925 4926
66 4925 $api_ver;
67 1373 });
68 }
69 2747 ▶ if (defined(my $plural = $params{resource_plural})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $plural = $params{"resource_plural"})) 1366 1381 2747
70 1366 my $stash = Package::Stash->new($caller);
71 # A fixed identity method, not a writable field: reject an argument
72 # rather than swallow it (k70, same shape as k67).
73 590 ▶ $stash->add_symbol('&resource_plural', sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
74 590 ▶ croak 'resource_plural is fixed for this class and cannot be set' if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 1 589 590
75 589 $plural;
76 1366 });
77 }
78 # exists, not truth: an empty hashref is a declaration (no subresource
79 # served) and undef is a mistake the check below names (k158).
80 _install_subresources($caller, _checked_subresources($caller, $params{subresources}))
81 2747 ▶ if exists $params{subresources};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"subresources"} 1054 1693 2747
82  
83 # Apply the APIObject role (provides metadata, labels, conditions,
84 # owners -- and, since k103, IO::K8s::Role::SpecBuilder, which that role
85 # composes for every top-level Kind rather than only for CRDs)
86 2736 Moo::Role->apply_roles_to_package($caller, 'IO::K8s::Role::APIObject');
87  
88 # Register the role's metadata attribute for the registry readers, so
89 # _inflate_struct inflates metadata as ObjectMeta. Adopted, not declared:
90 # the role already created the attribute, and the public k8s refuses to
91 # register over an attribute it did not create itself (k144).
92 2736 IO::K8s::Resource->_k8s_adopt($caller, 'metadata', 'Meta::V1::ObjectMeta');
93 }
94  
95 # The subresources a CRD version may serve (k158), the shape of
96 # apiextensions/v1 CustomResourceSubresources: status is an empty object,
97 # scale names where the replica counts and the label selector live.
98 152 my %SCALE_KEY = (
99 specReplicasPath => 'required',
100 statusReplicasPath => 'required',
101 labelSelectorPath => 'optional',
102 );
103  
104 # A validated, private copy of a class's subresources declaration, or a
105 # croak naming $class and the offending key. Shared with IO::K8s::AutoGen,
106 # which checks a CRD version's subresources with it before it begins the
107 # class, so a malformed manifest builds nothing.
108 1076 ▶sub _checked_subresources {
Subroutine _checked_subresources: called CC=20 SCAR=30.0
109 1076 my ($class, $subresources) = @_;
110 1076 my $where = $class.':';
111 1076 ▶ croak $where.' subresources must be a hashref of status and/or scale, got '
Branch: 2 branches
BranchTrueFalseTotal
unless ref $subresources eq "HASH" 2 1074 1076
defined $subresources ? : 1 1 2
Condition50%1/2
ref $subresources or "a plain scalar"
l!l
10
MC/DC0%0/1
ref $subresources or "a plain scalar"
ref $subresources
Truth table50%1/2
A
A= ref $subresources
Aresult
11
00
112 .(defined $subresources ? ref $subresources || 'a plain scalar' : 'undef')
113 unless ref $subresources eq 'HASH';
114 1074 for my $key (sort keys %$subresources) {
115 924 ▶ croak $where." unknown subresource '".$key."' (known: scale, status)"
Branch: 1 branch
BranchTrueFalseTotal
unless $key eq "status" or $key eq "scale" 1 923 924
Condition100%3/3
$key eq "status" or $key eq "scale"
l!l&&r!l&&!r
878451
MC/DC100%2/2
$key eq "status" or $key eq "scale"
$key eq "status" $key eq "scale"
Truth table100%3/3
A or B
A= $key eq "status"B= $key eq "scale"
ABresult
1X1
011
000
116 unless $key eq 'status' || $key eq 'scale';
117 }
118 1073 my %copy;
119 1073 ▶ if (exists $subresources->{status}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $subresources->{"status"}) 878 195 1073
120 878 my $status = $subresources->{status};
121 878 ▶ croak $where." subresource 'status' must be an empty hashref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $status eq "HASH" and not %$status 3 875 878
Condition100%3/3
ref $status eq "HASH" and not %$status
!ll&&!rl&&r
12875
MC/DC100%2/2
ref $status eq "HASH" and not %$status
ref $status eq "HASH" not %$status
Truth table100%3/3
A and not(B)
A= ref $status eq "HASH"B= not %$status
ABresult
0X0
100
111
122 unless ref $status eq 'HASH' && !%$status;
123 875 $copy{status} = {};
124 }
125 1070 ▶ if (exists $subresources->{scale}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $subresources->{"scale"}) 45 1025 1070
126 45 my $scale = $subresources->{scale};
127 45 ▶ croak $where." subresource 'scale' must be a hashref" unless ref $scale eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $scale eq "HASH" 1 44 45
128 44 for my $key (sort keys %$scale) {
129 croak $where." unknown key '".$key."' in subresource 'scale' (known: "
130 .join(', ', sort keys %SCALE_KEY).')'
131 123 ▶ unless $SCALE_KEY{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $SCALE_KEY{$key} 1 122 123
132 }
133 43 for my $key (sort grep { $SCALE_KEY{$_} eq 'required' } keys %SCALE_KEY) {
134 85 ▶ croak $where." subresource 'scale' needs '".$key."'" unless exists $scale->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $scale->{$key} 2 83 85
135 }
136 41 for my $key (sort keys %$scale) {
137 117 my $path = $scale->{$key};
138 117 ▶ croak $where." '".$key."' in subresource 'scale' must be a non-empty string"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and not ref $path and length $path 2 115 117
Condition83%5/6
defined $path and not ref $path and length $path
!ll&&!rl&&r
11115
defined $path and not ref $path
!ll&&!rl&&r
01116
MC/DC66%2/3
defined $path and not ref $path and length $path
defined $path not ref $path length $path
Truth table75%3/4
A and not(B) and C
A= defined $pathB= not ref $pathC= length $path
ABCresult
0XX0
10X0
1100
1111
139 unless defined $path && !ref $path && length $path;
140 }
141 39 $copy{scale} = { %$scale };
142 }
143 1064 return \%copy;
144 }
145  
146 # Install the checked declaration as the fixed identity method
147 # subresources, the way api_version and resource_plural are installed: an
148 # argument croaks (k67), and every call hands out a fresh copy, so a
149 # caller editing the result cannot change what the class declares.
150 1064 ▶sub _install_subresources {
Subroutine _install_subresources: called CC=1 SCAR=0.0
151 1064 my ($class, $subresources) = @_;
152 157 ▶ Package::Stash->new($class)->add_symbol('&subresources', sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
153 157 ▶ croak 'subresources is fixed for this class and cannot be set' if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 2 155 157
154 155 return { map { $_ => { %{ $subresources->{$_} } } } keys %$subresources };
155 1064 });
156 1064 return;
157 }
158  
159 152 1;
160  
161 __END__
« blib/lib/IO/K8s/CRD.pm ↑ Summary blib/lib/IO/K8s/List.pm »