lib/IO/K8s/Manifest.pm

filter: Statementstmt 100% 96 / 96 Branchbran 92.8% 13 / 14 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 32 / 32 Podpod 0.0% 0 / 2 totaltotal 97.9% 144 / 147 CC 18 SCAR 5.9
CC
18
Cov
99%
ConfigMap
5.0
ControllerRevision
5.0
__ANON__
4.0
CRAP 1.8
« lib/IO/K8s/ApiextensionsApiserver/Pkg/Apis/Apiextensions/V1/JSONSchemaPropsOrArray.pm ↑ Summary lib/IO/K8s/Cilium.pm »
1 package IO::K8s::Manifest;
2 # ABSTRACT: Internal collector for loading .pk8s manifest files
3 14 our $VERSION = '1.112';
4 16 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Carp treats IO::K8s as part of this module (k175), the way IO::K8s::CRD
12 # does (k165): the "Cannot open" IO::K8s->_slurp_utf8 raises for a manifest
13 # that cannot be read names the line that called IO::K8s->load, not the
14 # read in _load_file below. A caller in any other package -- the manifest
15 # calling var(), say -- still sees its own line.
16 14 our @CARP_NOT = ('IO::K8s');
17  
18 # Runs a manifest's source, returning $@ (k163). Defined here, above every
19 # lexical of this file, so the string eval sees none of them: it used to
20 # run inside _load_file, where a manifest could read -- and under
21 # `use strict` compile against -- $file, $k8s, $vars and the collector $m,
22 # and the `our $_collector` alias below. `our $VERSION` above stays in
23 # view: it has to come first in every module of the distribution, and a
24 # stray $VERSION in a manifest only reaches this package's version string.
25 # No `my` on purpose: the invocant is dropped and the source shifted off,
26 # so @_ is empty by the time the manifest runs.
27 54 ▶sub _eval_manifest {
Subroutine _eval_manifest: called CC=1 SCAR=0.0
28 54 shift;
29 54 ▶ eval shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
30 54 return $@;
31 }
32  
33 # Current collector during evaluation
34 14 our $_collector;
35  
36 # Items collected in this manifest
37 57 ▶has '_items' => (is => 'ro', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
38  
39 # Add resources to manifest
40 53 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod undocumented
41 56 my ($self, @objs) = @_;
42 56 push @{$self->_items}, @objs;
43 65 return $self;
44 }
45  
46 # Get all items
47 30 ▶sub items {
Subroutine items: called CC=1 SCAR=0.0
Pod undocumented
48 33 my $self = shift;
49 33 return @{$self->_items};
50 }
51  
52 # Load .pk8s file - called from IO::K8s->load
53 55 ▶sub _load_file {
Subroutine _load_file: called CC=3 SCAR=11.0
54 58 my ($class, $file, $k8s, $vars) = @_;
55  
56 # Read as UTF-8 (k160), the way load_yaml reads a file (k159): a
57 # non-ASCII literal in a manifest gives the same characters as the same
58 # value in YAML instead of its UTF-8 bytes. A manifest that says
59 # `use utf8;` itself gets the same characters -- the source handed to
60 # the eval below already is characters.
61 58 my $content = $k8s->_slurp_utf8($file);
62  
63 # Create manifest collector
64 54 my $m = $class->new;
65  
66 # Each load evaluates the file in a package of its own and removes that
67 # package again afterwards, also when the load fails (k160). It holds
68 # one DSL sub per known Kind, about 850 KB, and used to stay behind after
69 # every call, so a process reloading manifests in a loop grew without
70 # bound. Only the stash entry is deleted, not Symbol::delete_package:
71 # that one undefs every glob first, which would empty the subs and
72 # package variables a closure the manifest handed out still calls. With
73 # just the entry gone, whatever is still referenced -- such closures,
74 # the globs they use, an object blessed into the package -- lives on,
75 # and the rest is freed.
76 54 my $leaf = "_LOADER_$$" . "_" . int(rand(100000));
77 54 my $pkg = __PACKAGE__ . '::' . $leaf;
78  
79 54 my $ok = eval {
80 54 local $_collector = $m;
81  
82 54 $class->_install_var($pkg, $file, $vars);
83  
84 # Build the DSL code with functions for all resource types
85 54 my $dsl_code = _build_dsl_code($k8s);
86  
87 # Eval the file content with DSL available, its own line numbers
88 # restarting at 1 under the file's name (k163).
89 54 my $failure = $class->_eval_manifest(join "\n",
90 'package '.$pkg.';',
91 'use strict;',
92 'use warnings;',
93 $dsl_code,
94 $class->_line_directive($file),
95 $content);
96 54 ▶ die "Error loading $file: $failure" if $failure;
Branch: 1 branch
BranchTrueFalseTotal
if $failure 27 36 63
97 30 1;
98 };
99 54 my $error = $@;
100 54 delete $IO::K8s::Manifest::{ $leaf . '::' };
101 54 ▶ die $error unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 24 30 54
102  
103 30 return [ $m->items ];
104 }
105  
106 # The #line directive in front of a manifest's source, so that die, warn
107 # and compile errors in it name the file and the manifest's own line
108 # instead of "(eval 273) line 1848" behind the generated DSL subs (k163).
109 # The directive has no escaping: a name with a double quote or a line
110 # break cannot be written into it, and one outside printable ASCII would
111 # come out re-encoded, because the evaluated source is a character string.
112 # Such a name gets the line numbers alone, under the "(eval N)"
113 # pseudo-file; the "Error loading <file>:" prefix still names the file.
114 54 ▶sub _line_directive {
Subroutine _line_directive: called CC=2 SCAR=6.9
115 54 my ($class, $file) = @_;
116 54 ▶ return $file =~ /\A[\x20\x21\x23-\x7e]+\z/ ? '#line 1 "'.$file.'"' : '#line 1';
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /\A[\x20\x21\x23-\x7e]+\z/ ? : 48 6 54
117 }
118  
119 # var() for the manifest evaluated in $pkg (k160): var($name) returns the
120 # value passed as load($file, vars => { $name => ... }), var($name,
121 # $default) falls back to $default, and a name with neither dies naming the
122 # file. A closure over this load's own copy of the values, installed before
123 # the file compiles so that var(...) parses as a call -- and lower case, so
124 # it can never be taken for a Kind function. The values are never
125 # interpolated into code.
126 54 ▶sub _install_var {
Subroutine _install_var: called CC=2 SCAR=6.9
127 54 my ($class, $pkg, $file, $vars) = @_;
128 54 ▶ my %vars = %{ $vars // {} };
Condition100%2/2
$vars // {}
l!l
546
MC/DC100%1/1
$vars // {}
$vars
Truth table100%2/2
A
A= $vars
Aresult
11
00
129 23 ▶ Package::Stash->new($pkg)->add_symbol('&var', sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
130 22 my ($name, @default) = @_;
131 22 ▶ croak 'var() needs a name in '.$file unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 20 20
132 22 ▶ return $vars{$name} if exists $vars{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $vars{$name} 13 7 20
133 9 ▶ return $default[0] if @default;
Branch: 1 branch
BranchTrueFalseTotal
if @default 4 3 7
134 5 croak "var('".$name."'): no value passed to ".$file.' and no default given';
135 53 });
136 53 return;
137 }
138  
139 # The package the bodies of the generated Kind functions are compiled in,
140 # while the functions themselves are named in the loader package (k163).
141 # It trusts IO::K8s for Carp, so an error new_object croaks with for a Kind
142 # call -- a field of the wrong shape -- skips the generated function and is
143 # reported at the manifest line of the call, not at "(eval 273) line 1674".
144 # The functions themselves have to be named in the loader package, where
145 # the manifest calls them unqualified, and that package cannot be the one
146 # to trust IO::K8s: Carp never stops between two frames of one package, so
147 # it would skip the manifest's own frames too and land in this file.
148 14 my $DSL_BODY_PACKAGE = __PACKAGE__.'::_DSL';
149 14 Package::Stash->new($DSL_BODY_PACKAGE)->add_symbol('@CARP_NOT', ['IO::K8s']);
150  
151 # Build DSL code with resource functions
152 54 ▶sub _build_dsl_code {
Subroutine _build_dsl_code: called CC=3 SCAR=11.0
153 53 my ($k8s) = @_;
154  
155 53 my $code = '';
156  
157 # Get all resource types from the k8s instance
158 53 my $map = $k8s->resource_map;
159  
160 53 for my $kind (keys %$map) {
161 # Skip domain-qualified names (contain /) - not valid Perl identifiers
162 10526 ▶ next if $kind =~ m{/};
Branch: 1 branch
BranchTrueFalseTotal
if $kind =~ m[/] 6391 4133 10524
163  
164 4135 $code .= qq{
165 sub $kind (&@) {
166 package $DSL_BODY_PACKAGE;
167 my \$block = shift;
168 my \$api_version = shift;
169 my \%args = \$block->();
170  
171 # Convenience: move name/namespace/labels/annotations to metadata
172 for my \$key (qw(name namespace labels annotations)) {
173 if (exists \$args{\$key}) {
174 \$args{metadata}{\$key} = delete \$args{\$key};
175 }
176 }
177  
178 my \$k8s = \$IO::K8s::Manifest::_k8s_instance;
179 my \$obj = \$api_version
180 ? \$k8s->new_object('$kind', \\\%args, \$api_version)
181 : \$k8s->new_object('$kind', \\\%args);
182  
183 \$IO::K8s::Manifest::_collector->add(\$obj)
184 if \$IO::K8s::Manifest::_collector;
185  
186 return \$obj;
187 }
188 };
189 }
190  
191 53 return $code;
192 }
193  
194 # K8s instance for DSL functions (set during load)
195 14 our $_k8s_instance;
196  
197 14 1;
198  
199 __END__
« lib/IO/K8s/ApiextensionsApiserver/Pkg/Apis/Apiextensions/V1/JSONSchemaPropsOrArray.pm ↑ Summary lib/IO/K8s/Cilium.pm »