| 1 | package IO::K8s::APIObject; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Base class for top-level Kubernetes API objects | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 155 | our $VERSION = '1.112'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 155 | ▶ | use v5.10; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 155 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 155 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 155 | ▶ | use IO::K8s::Resource (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 155 | ▶ | use Import::Into; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 155 | ▶ | use Package::Stash; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 155 | ▶ | use Moo::Role (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 155 | ▶ | 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 | 155 | my @KNOWN_PARAMS = qw( api_version resource_plural subresources ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 155 | my %KNOWN_PARAM = map { $_ => 1 } @KNOWN_PARAMS; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1497 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=13 SCAR=25.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 1497 | my ($class, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1497 | 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 | 1497 | ▶ | croak $caller.': odd number of import arguments for '.__PACKAGE__ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | .' ('.scalar(@args).'); expected name => value pairs' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | if @args % 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 1495 | 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 | 1495 | for my $param (sort keys %params) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | croak $caller.": unknown import parameter '".$param."' for ".__PACKAGE__ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | .' (known: '.join(', ', @KNOWN_PARAMS).')' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 2217 | ▶ | unless $KNOWN_PARAM{$param}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 1490 | for my $param (grep { exists $params{$_} } qw( api_version resource_plural )) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 1600 | my $value = $params{$param}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 1600 | ▶ | croak $caller.": import parameter '".$param."' for ".__PACKAGE__ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%6/6
defined $value and not ref $value and length $value
defined $value and not ref $value
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 1484 | 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 | 1484 | ▶ | if (defined(my $api_ver = $params{api_version})) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 799 | 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 | 4943 | ▶ | $stash->add_symbol('&api_version', sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 4943 | ▶ | croak 'api_version is fixed for this class and cannot be set' if @_ > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 4942 | $api_ver; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 799 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 1484 | ▶ | if (defined(my $plural = $params{resource_plural})) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 792 | 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 | 594 | ▶ | $stash->add_symbol('&resource_plural', sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 594 | ▶ | croak 'resource_plural is fixed for this class and cannot be set' if @_ > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 593 | $plural; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 792 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 1484 | ▶ | if exists $params{subresources}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 1473 | 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 | 1473 | 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 | 155 | 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 | 631 | ▶ | sub _checked_subresources { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_checked_subresources: called CC=20 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 631 | my ($class, $subresources) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 631 | my $where = $class.':'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 631 | ▶ | croak $where.' subresources must be a hashref of status and/or scale, got ' | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%1/2
ref $subresources or "a plain scalar"
MC/DC0%0/1
ref $subresources or "a plain scalar"
ref $subresources
Truth table50%1/2
A
A= ref $subresources
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | .(defined $subresources ? ref $subresources || 'a plain scalar' : 'undef') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | unless ref $subresources eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 629 | for my $key (sort keys %$subresources) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 544 | ▶ | croak $where." unknown subresource '".$key."' (known: scale, status)" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key eq "status" or $key eq "scale"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | unless $key eq 'status' || $key eq 'scale'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 628 | my %copy; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 628 | ▶ | if (exists $subresources->{status}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 513 | my $status = $subresources->{status}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 513 | ▶ | croak $where." subresource 'status' must be an empty hashref" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $status eq "HASH" and not %$status
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | unless ref $status eq 'HASH' && !%$status; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 510 | $copy{status} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 625 | ▶ | if (exists $subresources->{scale}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 30 | my $scale = $subresources->{scale}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 30 | ▶ | croak $where." subresource 'scale' must be a hashref" unless ref $scale eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 29 | for my $key (sort keys %$scale) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | croak $where." unknown key '".$key."' in subresource 'scale' (known: " | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | .join(', ', sort keys %SCALE_KEY).')' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 78 | ▶ | unless $SCALE_KEY{$key}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 28 | for my $key (sort grep { $SCALE_KEY{$_} eq 'required' } keys %SCALE_KEY) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 55 | ▶ | croak $where." subresource 'scale' needs '".$key."'" unless exists $scale->{$key}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 26 | for my $key (sort keys %$scale) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 72 | my $path = $scale->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 72 | ▶ | croak $where." '".$key."' in subresource 'scale' must be a non-empty string" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $path and not ref $path and length $path
defined $path and not ref $path
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | unless defined $path && !ref $path && length $path; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 24 | $copy{scale} = { %$scale }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 619 | 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 | 619 | ▶ | sub _install_subresources { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_install_subresources: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 619 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 155 | return { map { $_ => { %{ $subresources->{$_} } } } keys %$subresources }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 619 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 619 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 155 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||