maint/crd-drift-check.pl

filter: Statementstmt 61.5% 427 / 694 Branchbran 39.1% 119 / 304 Conditioncond 29.7% 58 / 195 MC/DCmcdc 10.0% 11 / 110 Subroutinesub 72.5% 37 / 51 Podpod n/a totaltotal 48.1% 652 / 1354 CC 262 SCAR 41.8
CC
262
Cov
51%
render_check_report
420.0
_structural_signature
210.0
check_for
132.0
CRAP 65.4
« maint/crd-schema-audit.pl ↑ Summary blib/lib/IO/K8s/Role/Resource.pm »
1 #!/usr/bin/env perl
2 # Upstream CRD spec drift checker for the IO::K8s CRD providers.
3 #
4 # The CRD analog of maint/spec-drift-check.pl. Where that tool diffs the
5 # core Kubernetes swagger.json against the shipped IO::K8s::Api::* classes,
6 # this one diffs each CRD provider's upstream CustomResourceDefinition
7 # manifests -- at the exact upstream_version the provider pins -- against
8 # what that provider actually ships: its resource_map (Kind coverage) and,
9 # where a class models its spec as a typed inline struct rather than an
10 # opaque hash, the class's k8s() attribute registry (field coverage).
11 #
12 # The six providers are IO::K8s::{Cilium,GatewayAPI,AgentSandbox,Traefik,
13 # CertManager,K3s}. Each one grew a `sub crd_sources` alongside its existing
14 # `sub upstream_version`: a data-only hook returning the upstream CRD-manifest
15 # URLs (base + files) for its pinned version, plus a `status`. A provider
16 # whose upstream publishes no machine-readable openAPIV3Schema (K3s) returns
17 # status => 'unresolved' and is reported as such rather than diffed.
18 #
19 # This is a report generator. It never writes to lib/, never touches the
20 # karr board, and never modifies the exceptions file -- deciding what a
21 # reported gap is worth is a human/agent call, not the script's.
22 #
23 # Usage:
24 # maint/crd-drift-check.pl [--provider NAME]... [options]
25 # maint/crd-drift-check.pl --provider Cilium --dir path/to/crd/yamls
26 #
27 # Examples:
28 # maint/crd-drift-check.pl
29 # Drift report for all six providers (cached CRDs under spec/crd/).
30 #
31 # maint/crd-drift-check.pl --provider GatewayAPI --verbose
32 # One provider, including entries suppressed by the exceptions file.
33 #
34 # Network access: fetches each provider's CRD manifests over HTTPS via
35 # HTTP::Tiny (raw.githubusercontent.com and GitHub release assets), the
36 # same discipline as spec-drift-check.pl. Downloaded manifests are cached
37 # under --cache-dir (default: spec/crd/<Provider>/<upstream_version>/, already
38 # gitignored) so repeat runs against the same pin don't re-fetch. The cache is
39 # keyed on the provider's upstream_version, so bumping a pin re-fetches
40 # automatically; --no-cache forces a re-download regardless.
41 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 11 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 11 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 11 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 11 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 11 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 11 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 11 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 11 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # File::Spec->rel2abs makes a path absolute but, on Unix, never collapses a
53 # '..' segment already in it (that's deliberate upstream: collapsing one
54 # blindly can change what a path means across a symlink). $DIST_ROOT itself
55 # is built from FindBin::Bin + '..', so left at rel2abs alone it would read
56 # as ".../maint/.." forever -- harmless for open()/-d, which the OS resolves
57 # fine, but fatal for the --suggest-dir "not inside lib/" guard below, which
58 # is a plain string-prefix test: a literal '..' anywhere in either side
59 # defeats it silently instead of refusing. _canon_abs collapses '.'/'..'
60 # lexically after rel2abs, with no filesystem lookup -- unlike Cwd::abs_path,
61 # it works on a --suggest-dir that doesn't exist yet.
62 11 ▶sub _canon_abs {
Subroutine _canon_abs: called CC=6 SCAR=18.0
63 11 my ($path) = @_;
64 11 my ($vol, $dirs) = File::Spec->splitpath(File::Spec->rel2abs($path), 1);
65 11 my @out;
66 11 for my $seg (File::Spec->splitdir($dirs)) {
67 88 ▶ next if $seg eq '' || $seg eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $seg eq "" or $seg eq "." 11 77 88
Condition66%2/3
$seg eq "" or $seg eq "."
l!l&&r!l&&!r
11077
MC/DC50%1/2
$seg eq "" or $seg eq "."
$seg eq "" $seg eq "."
Truth table66%2/3
A or B
A= $seg eq ""B= $seg eq "."
ABresult
1X1
011
000
68 77 ▶ if ($seg eq '..') { pop @out if @out; next; }
Branch: 2 branches
BranchTrueFalseTotal
if ($seg eq "..") 11 66 77
if @out 11 0 11
69 66 push @out, $seg;
70 }
71 11 return File::Spec->catdir($vol, File::Spec->rootdir, @out);
72 }
73  
74 # _canon_abs is lexical only -- it never touches the filesystem, so a
75 # symlink component (a --suggest-dir that is itself a symlink into lib/, or
76 # has one anywhere in its existing prefix) sails straight through it
77 # unresolved, and the "not inside lib/" guard below is a plain string-prefix
78 # test on that unresolved string. Cwd::realpath resolves symlinks but
79 # returns undef for a path that doesn't fully exist -- and a --suggest-dir
80 # legitimately doesn't exist yet, that's the option's whole point. So walk
81 # up from the full (lexically collapsed) path until an existing prefix is
82 # found, realpath just that prefix, and reattach whatever tail doesn't
83 # exist untouched -- a path component that isn't there cannot itself be a
84 # symlink, so the lexical form is already correct for it.
85 0 ▶sub _resolve_path {
Subroutine _resolve_path: not called CC=5 SCAR=34.0
86 0 my ($path) = @_;
87 # A failed realpath() during the walk below leaves $! set (ENOENT) as a
88 # side effect of the underlying stat -- localize it so a caller's own
89 # die (e.g. the --suggest-dir guard) doesn't inherit that as its exit
90 # status instead of the usual 255.
91 0 local $!;
92 0 my ($vol, $dirs) = File::Spec->splitpath(_canon_abs($path), 1);
93 0 my @segs = grep { length } File::Spec->splitdir($dirs);
94 0 my @tail;
95 0 while (1) {
96 0 ▶ my $candidate = @segs
Branch: 1 branch
BranchTrueFalseTotal
@segs ? : 0 0 0
97 ? File::Spec->catdir($vol, File::Spec->rootdir, @segs)
98 : File::Spec->catdir($vol, File::Spec->rootdir);
99 0 my $real = realpath($candidate);
100 0 ▶ if (defined $real) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $real) 0 0 0
101 0 ▶ return @tail ? File::Spec->catdir($real, @tail) : $real;
Branch: 1 branch
BranchTrueFalseTotal
@tail ? : 0 0 0
102 }
103 0 ▶ last unless @segs;
Branch: 1 branch
BranchTrueFalseTotal
unless @segs 0 0 0
104 0 unshift @tail, pop @segs;
105 }
106 # Not even the root resolved (shouldn't happen on a real filesystem) --
107 # fall back to the lexical form rather than die.
108 0 return _canon_abs($path);
109 }
110  
111 11 my $DIST_ROOT = _canon_abs(File::Spec->catdir($FindBin::Bin, '..'));
112 11 my $UA_STRING = 'io-k8s-p5-crd-drift-check (+https://github.com/pplu/io-k8s-p5)';
113  
114 # The provider modules this tool knows how to check, in report order. Each
115 # is a Moo class composing IO::K8s::Role::ResourceMap with an upstream_version
116 # and (as of k82) a crd_sources method.
117 11 my @ALL_PROVIDERS = qw(Cilium GatewayAPI AgentSandbox Traefik CertManager K3s
118 PrometheusOperator VolumeSnapshot ExternalSecrets);
119  
120 # ---------------------------------------------------------------------------
121 # CLI
122 # ---------------------------------------------------------------------------
123  
124 0 ▶sub usage {
Subroutine usage: not called CC=2 SCAR=17.9
125 0 my ($exit_code) = @_;
126 0 print <<"USAGE";
127 Usage:
128 $0 [--provider NAME]... [options]
129  
130 Options:
131 --provider NAME Provider to check (repeatable). One of:
132 @ALL_PROVIDERS
133 Default: all of them.
134 --dir PATH Local directory of CRD YAML manifests to diff instead
135 of fetching (implies a single --provider).
136 --spec PATH Alias for --dir.
137 --exceptions PATH Exceptions file (default: maint/crd-drift-exceptions.yaml)
138 --lib PATH lib/ directory to load providers from (default: DIST/lib)
139 --cache-dir PATH Downloaded-manifest cache root (default: DIST/spec/crd)
140 --no-cache Force re-download even if a cached copy exists
141 --verbose Also list items suppressed by the exceptions file
142 --format text|json Report format (default: text)
143 --output PATH Also write the report to this file
144 --suggest After the report, print the class source the emitter
145 renders for every OPAQUE SPEC and MISSING FIELD Kind
146 (never touches lib/). With --format json this goes to
147 stderr instead of stdout, so stdout stays a single
148 parseable JSON document.
149 --suggest-dir PATH Write those files under PATH instead of printing
150 (PATH must not be inside the distribution's lib/).
151 The "wrote N file(s)" confirmation follows the same
152 stdout/stderr rule as --suggest above.
153 --names FILE YAML map of generated-class path (relative to the
154 Kind, e.g. "Middleware::Spec::RateLimit") to the
155 package name to use (e.g. "RateLimit") -- the
156 upstream Go type names (D6).
157 --render Like --suggest, but for EVERY served GVK of the
158 provider (not only reported gaps) -- the full D5
159 render, always through the provider's overlay (see
160 --overlay) when one exists. Same stdout/stderr rule.
161 --render-dir PATH Write the --render output under PATH instead of
162 printing (PATH must not be inside lib/).
163 --check Render every served GVK to memory and compare each
164 rendered file against the checked-in
165 lib/IO/K8s/<Provider>/<Version>/<File>.pm: MATCH,
166 DIFFERS (a short hand-rolled diff), COSMETIC (a
167 DIFFERS whose file is listed in
168 ignore_cosmetic_differs AND whose k8s declarations
169 still match the render after whitespace normalisation
170 -- POD/ABSTRACT/whitespace only; suppressed, listed
171 under --verbose), ACCEPTED DIVERGENCE (a DIFFERS whose
172 file is listed in accept_structural_divergence -- a
173 genuine, documented, file-exact k8s-declaration
174 difference the maintainer chose to keep, NOT gated on
175 the cosmetic signature guard; suppressed, listed with
176 its diff under --verbose), MISSING IN LIB, or NOT
177 RENDERED (a file under the provider directory the
178 render does not produce -- see ignore_unrendered in the
179 exceptions file for a kept back-compat track). Exits 1
180 if anything but a MATCH, a suppressed COSMETIC or a
181 suppressed ACCEPTED DIVERGENCE remains.
182 --overlay FILE Overlay YAML for --render/--check (default:
183 maint/crd-render/<Provider>.yaml when it exists;
184 requires exactly one --provider).
185 --help This message
186  
187 For each provider, fetches its upstream CustomResourceDefinition manifests at
188 the pinned upstream_version, parses each openAPIV3Schema, and reports missing
189 Kinds, missing/extra top-level spec fields, opaque specs (spec not modeled
190 field-by-field), and stale/perl-only GVKs (shipped with no upstream CRD).
191  
192 This is a report only -- it never creates karr tickets or edits lib/.
193 USAGE
194 0 ▶ exit($exit_code // 0);
Condition0%0/2
$exit_code // 0
l!l
00
MC/DC0%0/1
$exit_code // 0
$exit_code
Truth table0%0/2
A
A= $exit_code
Aresult
11
00
195 }
196  
197 11 ▶sub parse_args {
Subroutine parse_args: called CC=16 SCAR=35.0
198 11 my %opt = (
199 exceptions => File::Spec->catfile($FindBin::Bin, 'crd-drift-exceptions.yaml'),
200 lib => File::Spec->catdir($DIST_ROOT, 'lib'),
201 'cache-dir' => File::Spec->catdir($DIST_ROOT, 'spec', 'crd'),
202 format => 'text',
203 provider => [],
204 );
205 GetOptions(\%opt,
206 'provider=s@', 'dir=s', 'spec=s',
207 'exceptions=s', 'lib=s', 'cache-dir=s', 'no-cache',
208 'verbose', 'output=s', 'format=s', 'help|h',
209 'suggest' => \$opt{suggest}, 'suggest-dir=s' => \$opt{suggest_dir}, 'names=s' => \$opt{names},
210 'render' => \$opt{render}, 'render-dir=s' => \$opt{render_dir},
211 'check' => \$opt{check}, 'overlay=s' => \$opt{overlay},
212 11 ▶ ) or usage(1);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%opt, "provider=s\@", "dir=s", "spec=s", "exceptions=s", "lib=s", "cache-dir=s", "no-cache", "verbose", "output=s", "format=s", "help|h", "suggest", \$opt{"suggest"}, "suggest-dir=s", \$opt{"suggest_dir"}, "names=s", \$opt{"names"}, "render", \$opt{"render"}, "render-dir=s", \$opt{"render_dir"}, "check", \$opt{"check"}, "overlay=s", \$opt{"overlay"}) or usage(1) 11 0 11
213 11 ▶ usage(0) if $opt{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} 0 11 11
214 11 ▶ if ($opt{format} !~ /^(text|json)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"format"} =~ /^(text|json)$/) 0 11 11
215 0 die "crd-drift-check: --format must be 'text' or 'json'\n";
216 }
217 11 ▶ $opt{dir} //= $opt{spec};
Condition33%1/3
$opt{"dir"} //= $opt{"spec"}
l!l&&r!l&&!r
1100
MC/DC0%0/2
$opt{"dir"} //= $opt{"spec"}
$opt{"dir"} $opt{"spec"}
Truth table33%1/3
A //= B
A= $opt{"dir"}B= $opt{"spec"}
ABresult
1X1
011
000
218 11 my @providers = @{ $opt{provider} };
219 11 ▶ @providers = @ALL_PROVIDERS unless @providers;
Branch: 1 branch
BranchTrueFalseTotal
unless @providers 0 11 11
220 11 my %known = map { $_ => 1 } @ALL_PROVIDERS;
221 11 for my $p (@providers) {
222 die "crd-drift-check: unknown provider '$p' (known: @ALL_PROVIDERS)\n"
223 11 ▶ unless $known{$p};
Branch: 1 branch
BranchTrueFalseTotal
unless $known{$p} 0 11 11
224 }
225 11 ▶ if (defined $opt{dir} && @providers != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"dir"} and @providers != 1) 0 11 11
Condition33%1/3
defined $opt{"dir"} and @providers != 1
!ll&&!rl&&r
0110
MC/DC0%0/2
defined $opt{"dir"} and @providers != 1
defined $opt{"dir"} @providers != 1
Truth table33%1/3
A and B
A= defined $opt{"dir"}B= @providers != 1
ABresult
0X0
100
111
226 0 die "crd-drift-check: --dir/--spec requires exactly one --provider\n";
227 }
228 11 ▶ if (defined $opt{overlay} && @providers != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"overlay"} and @providers != 1) 0 11 11
Condition66%2/3
defined $opt{"overlay"} and @providers != 1
!ll&&!rl&&r
740
MC/DC0%0/2
defined $opt{"overlay"} and @providers != 1
defined $opt{"overlay"} @providers != 1
Truth table66%2/3
A and B
A= defined $opt{"overlay"}B= @providers != 1
ABresult
0X0
100
111
229 0 die "crd-drift-check: --overlay requires exactly one --provider\n";
230 }
231 11 ▶ if (defined $opt{overlay} && !-f $opt{overlay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"overlay"} and not -f $opt{"overlay"}) 0 11 11
Condition66%2/3
defined $opt{"overlay"} and not -f $opt{"overlay"}
!ll&&!rl&&r
740
MC/DC0%0/2
defined $opt{"overlay"} and not -f $opt{"overlay"}
defined $opt{"overlay"} not -f $opt{"overlay"}
Truth table66%2/3
A and not(B)
A= defined $opt{"overlay"}B= not -f $opt{"overlay"}
ABresult
0X0
100
111
232 0 die "crd-drift-check: --overlay file not found: $opt{overlay}\n";
233 }
234 11 ▶ if ($opt{suggest_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"suggest_dir"}) 0 11 11
235 0 $opt{suggest_dir} = _canon_abs($opt{suggest_dir});
236 0 _die_if_inside_lib($opt{suggest_dir}, $opt{lib}, '--suggest-dir');
237 }
238 11 ▶ if ($opt{render_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"render_dir"}) 0 11 11
239 0 $opt{render_dir} = _canon_abs($opt{render_dir});
240 0 _die_if_inside_lib($opt{render_dir}, $opt{lib}, '--render-dir');
241 }
242 11 $opt{provider} = \@providers;
243 11 return \%opt;
244 }
245  
246 # Shared by --suggest-dir and --render-dir: the write target stays the
247 # lexical form (either may not exist yet); the guard compares
248 # symlink-resolved forms so a symlink into lib/ can't sneak past a
249 # lexical-only check. See _resolve_path's own comment above for why.
250 0 ▶sub _die_if_inside_lib {
Subroutine _die_if_inside_lib: not called CC=3 SCAR=24.8
251 0 my ($path, $lib, $flag) = @_;
252 0 my $real = _resolve_path($path);
253 0 my $lib_real = _resolve_path($lib);
254 0 ▶ die "crd-drift-check: $flag must not point inside lib/\n"
Branch: 1 branch
BranchTrueFalseTotal
if $real eq $lib_real or index($real, "$lib_real/") == 0 0 0 0
Condition0%0/3
$real eq $lib_real or index($real, "$lib_real/") == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$real eq $lib_real or index($real, "$lib_real/") == 0
$real eq $lib_real index($real, "$lib_real/") == 0
Truth table0%0/3
A or B
A= $real eq $lib_realB= index($real, "$lib_real/") == 0
ABresult
1X1
011
000
255 if $real eq $lib_real || index($real, "$lib_real/") == 0;
256 }
257  
258 # ---------------------------------------------------------------------------
259 # Provider + registry loading (mirrors spec-drift-check.pl's lib loading)
260 # ---------------------------------------------------------------------------
261  
262 11 ▶sub load_lib {
Subroutine load_lib: called CC=3 SCAR=11.2
263 11 my ($lib_dir) = @_;
264 11 ▶ die "crd-drift-check: lib dir not found: $lib_dir\n" unless -d $lib_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $lib_dir 0 11 11
265 11 ▶ unshift @INC, $lib_dir unless grep { $_ eq $lib_dir } @INC;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $lib_dir;} @INC 11 0 11
266 11 require IO::K8s;
267 11 return;
268 }
269  
270 # A resource_map value -> fully-qualified Perl class. Mirrors
271 # IO::K8s::Role::ResourceMap's rule: a '+' prefix is used verbatim, anything
272 # else is relative to IO::K8s::.
273 108 ▶sub qualify_class {
Subroutine qualify_class: called CC=2 SCAR=7.1
274 108 my ($path) = @_;
275 108 ▶ return substr($path, 1) if $path =~ /^\+/;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /^\+/ 0 108 108
276 108 return "IO::K8s::$path";
277 }
278  
279 # ---------------------------------------------------------------------------
280 # CRD manifest fetch + cache
281 # ---------------------------------------------------------------------------
282  
283 0 ▶sub http_get {
Subroutine http_get: not called CC=4 SCAR=30.0
284 0 my ($url) = @_;
285 0 require HTTP::Tiny;
286 0 require Encode;
287 0 my $ua = HTTP::Tiny->new(agent => $UA_STRING, timeout => 90);
288 0 my $res = $ua->get($url);
289 die sprintf("crd-drift-check: GET %s failed: %s %s\n",
290 $url, $res->{status} // '?', $res->{reason} // '?')
291 0 ▶ unless $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"success"} 0 0 0
Condition0%0/4
$res->{"status"} // "?"
l!l
00
$res->{"reason"} // "?"
l!l
00
MC/DC0%0/1
$res->{"status"} // "?"
$res->{"status"}
MC/DC0%0/1
$res->{"reason"} // "?"
$res->{"reason"}
Truth table0%0/2
A
A= $res->{"status"}
Aresult
11
00
Truth table0%0/2
A
A= $res->{"reason"}
Aresult
11
00
292 # HTTP::Tiny hands back raw response bytes, not a decoded character
293 # string. Decoding once here -- before the caller both caches it (via
294 # the '>:encoding(UTF-8)' write in load_manifests) and parses it --
295 # means those bytes are only ever UTF-8-encoded once: writing an
296 # UNdecoded byte string through an ':encoding(UTF-8)' filehandle
297 # re-encodes each byte as if it were a Latin-1 codepoint, doubling the
298 # encoding for any non-ASCII character (a manifest's 'µs' pattern, say)
299 # and corrupting it on disk (k94 review).
300 0 return Encode::decode('UTF-8', $res->{content});
301 }
302  
303 # The byte shape a double-encoded character leaves behind, derived from
304 # UTF-8's own structure rather than listed case by case. Misreading a
305 # character's UTF-8 bytes as Latin-1 codepoints and re-encoding them maps
306 # every CONTINUATION byte (\x80-\xBF) to "\xC2" plus itself, and the LEAD
307 # byte to "\xC3" plus a byte that still says how long the original was:
308 #
309 # original lead doubled lead trailing "\xC2"+cont pairs
310 # 2-byte U+0080+ C2-DF C3 82-9F 1
311 # 3-byte U+0800+ E0-EF C3 A0-AF 2
312 # 4-byte U+10000+ F0-F4 C3 B0-B4 3
313 #
314 # so a doubled run is one C3-led byte from one of those ranges followed by
315 # exactly as many pairs as its row implies. Two boundaries are deliberate:
316 #
317 # * The 2-byte row stays narrowed to C2/C3 -- the Latin-1 Supplement, the
318 # 'µ'/'ü'/'é' a CRD manifest realistically carries. Widening it to the
319 # full C2-DF would make the signature match plain, correct text: 'Ä«'
320 # is C3 84 C2 AB, and since the repair below succeeds on it, it would
321 # silently become 'ī'. A doubled 'Â'/'Ã' run has no such reading.
322 # * Nothing above the 4-byte row: UTF-8 has no longer sequence, and F5-FF
323 # is not a lead byte at all.
324 #
325 # Before k114 only the 2-byte row existed, so every character from U+0800 up
326 # -- an en dash, a curly quote, anything typographic an upstream description
327 # realistically carries -- went unrepaired. Cilium's CiliumPodIPPool
328 # description reached the emitter as "\xC3\xA2\xC2\x80\xC2\x93" and --check
329 # then reported a permanent false difference against a perfectly good
330 # checked-in class, which is the way a drift report stops being read.
331 11 my $DOUBLE_ENCODED_RUN = qr{
332 \xC3[\x82\x83] (?:\xC2[\x80-\xBF])
333 | \xC3[\xA0-\xAF] (?:\xC2[\x80-\xBF]){2}
334 | \xC3[\xB0-\xB4] (?:\xC2[\x80-\xBF]){3}
335 }x;
336  
337 30 ▶sub _slurp {
Subroutine _slurp: called CC=4 SCAR=16.5
338 30 my ($path) = @_;
339 30 ▶ open my $fh, '<:raw', $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or die "crd-drift-check: cannot read ${path}: $!\n" 30 0 30
340 or die "crd-drift-check: cannot read $path: $!\n";
341 30 local $/;
342 30 my $bytes = <$fh>;
343 30 close $fh;
344 30 require Encode;
345 30 my $content = Encode::decode('UTF-8', $bytes);
346  
347 # A cache file written before the http_get fix above holds bytes that
348 # were UTF-8-encoded twice: e.g. 'µ' (correctly, bytes C2 B5) written
349 # through ':encoding(UTF-8)' without ever having been decoded first
350 # becomes, on disk, the UTF-8 encoding of the two CHARACTERS U+00C2 and
351 # U+00B5, i.e. C3 82 C2 B5. An earlier version of this check matched
352 # any "\xC3" followed by a continuation byte -- but that signature is
353 # NOT unique to double-encoded content: a single, correctly-encoded
354 # Latin-1-Supplement character (C3 A9 = 'e-acute', C3 BC = 'u-umlaut',
355 # ...) is EXACTLY that shape, since every codepoint from U+00C0-U+00FF
356 # has UTF-8 lead byte C3 too. That false positive silently mangled a
357 # legitimately cached 'Grüße'/'Délai' into U+FFFD replacement
358 # characters on read alone (round-2 review finding).
359 #
360 # $DOUBLE_ENCODED_RUN above is that tighter signature: a C3-led byte in
361 # one of the three doubled-lead ranges plus the exact number of "C2" +
362 # continuation pairs its row implies -- four bytes for a doubled 'µ',
363 # six for a doubled en dash, eight for a doubled emoji, never two. A
364 # lone 'ü'/'é' never produces such a run on its own; it would need a
365 # literal "C2" + continuation byte immediately afterward too, and for
366 # the longer rows two or three of them in a row.
367 #
368 # Even with that tighter signature, only ACCEPT the repair if the
369 # WHOLE text survives the reinterpret-as-Latin-1-then-decode-as-UTF-8
370 # round trip strictly. FB_CROAK on both encode() and decode(): encode()
371 # alone would otherwise silently substitute '?' for any codepoint above
372 # U+00FF (a real emoji elsewhere in an otherwise-unrelated part of the
373 # same cached file, say), which decode() would then just as silently
374 # accept -- masking real data loss instead of tripping this fallback.
375 # LEAVE_SRC on both: CHECK => FB_CROAK makes encode()/decode() consume
376 # (empty out) their source argument as they convert it, which would
377 # destroy $content -- the exact value this needs to fall back to --
378 # even along the success path. A file that fails this check keeps its
379 # own, already correctly single-decoded $content.
380 30 ▶ if ($bytes =~ $DOUBLE_ENCODED_RUN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes =~ /$DOUBLE_ENCODED_RUN/x) 0 30 30
381 0 my $check = Encode::FB_CROAK() | Encode::LEAVE_SRC();
382 0 my $repaired = eval {
383 0 Encode::decode('UTF-8', Encode::encode('iso-8859-1', $content, $check), $check);
384 };
385 0 ▶ $content = $repaired if defined $repaired;
Branch: 1 branch
BranchTrueFalseTotal
if defined $repaired 0 0 0
386 }
387 30 return $content;
388 }
389  
390 # A `files` entry ('v2/foo.yaml') maps to one cache file with path
391 # separators flattened to '_' ('v2_foo.yaml'). Nothing here is committed:
392 # the cache is per-version local scratch under spec/crd/<Provider>/<version>/
393 # (fully gitignored), keyed on the provider's upstream_version so a pin bump
394 # re-fetches automatically.
395 0 ▶sub cache_name_for {
Subroutine cache_name_for: not called CC=1 SCAR=6.9
396 0 my ($file) = @_;
397 0 (my $name = $file) =~ s{/}{_}g;
398 0 return $name;
399 }
400  
401 # Returns a list of [source_label, yaml_text] for a provider, either from a
402 # local --dir or from crd_sources (fetching + caching as needed).
403 11 ▶sub load_manifests {
Subroutine load_manifests: called CC=11 SCAR=38.7
404 11 my ($opt, $provider, $sources, $version) = @_;
405 11 my @out;
406 11 ▶ if (defined $opt->{dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{"dir"}) 11 0 11
407 opendir my $dh, $opt->{dir}
408 11 ▶ or die "crd-drift-check: cannot open --dir $opt->{dir}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $opt->{"dir"} or die qq[crd-drift-check: cannot open --dir $opt->{"dir"}: $!\n] 11 0 11
409 11 my @files = sort grep { /\.ya?ml$/ } readdir $dh;
410 11 closedir $dh;
411 11 ▶ die "crd-drift-check: no *.yaml manifests in $opt->{dir}\n" unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 11 11
412 11 for my $f (@files) {
413 19 my $path = File::Spec->catfile($opt->{dir}, $f);
414 19 push @out, [$f, _slurp($path)];
415 }
416 11 return @out;
417 }
418  
419 # Cache is keyed on upstream_version so a pin bump caches separately and a
420 # stale older-version cache never serves the wrong manifests.
421 0 ▶ (my $safe_version = defined $version ? $version : '(unknown)')
Branch: 1 branch
BranchTrueFalseTotal
defined $version ? : 0 0 0
422 =~ s/[^A-Za-z0-9._-]/_/g;
423 0 my $cache_dir = File::Spec->catdir($opt->{'cache-dir'}, $provider, $safe_version);
424 0 ▶ make_path($cache_dir) unless -d $cache_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $cache_dir 0 0 0
425 0 for my $file (@{ $sources->{files} }) {
426 0 my $cache_file = File::Spec->catfile($cache_dir, cache_name_for($file));
427 0 ▶ if (!$opt->{'no-cache'} && -f $cache_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opt->{"no-cache"} and -f $cache_file) 0 0 0
Condition0%0/3
not $opt->{"no-cache"} and -f $cache_file
!ll&&!rl&&r
000
MC/DC0%0/2
not $opt->{"no-cache"} and -f $cache_file
not $opt->{"no-cache"} -f $cache_file
Truth table0%0/3
not(A) and B
A= not $opt->{"no-cache"}B= -f $cache_file
ABresult
0X0
100
111
428 0 push @out, [$file, _slurp($cache_file)];
429 0 next;
430 }
431 0 my $url = $sources->{base} . '/' . $file;
432 0 print STDERR "crd-drift-check: downloading $url ...\n";
433 0 my $content = http_get($url);
434 0 ▶ open my $fh, '>:encoding(UTF-8)', $cache_file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(UTF-8)", $cache_file or die "crd-drift-check: cannot write ${cache_file}: $!\n" 0 0 0
435 or die "crd-drift-check: cannot write $cache_file: $!\n";
436 0 print $fh $content;
437 0 close $fh;
438 0 push @out, [$file, $content];
439 }
440 0 return @out;
441 }
442  
443 # ---------------------------------------------------------------------------
444 # CRD manifest parsing
445 #
446 # From each CustomResourceDefinition document: spec.group, spec.names.kind,
447 # and per served version its name + the spec-object property set from
448 # openAPIV3Schema.properties.spec.properties. GVK = "group/version/Kind".
449 # The whole document rides along too (doc), so --suggest can hand a
450 # reported GVK's manifest straight to IO::K8s::CRD->generate without
451 # re-fetching or re-parsing it. Keep the root schema as well: a clearly
452 # closed root without `spec` is distinct from an opaque spec schema.
453 # ---------------------------------------------------------------------------
454  
455 11 ▶sub parse_crds {
Subroutine parse_crds: called CC=18 SCAR=29.6
456 11 my (@manifests) = @_;
457 11 require YAML::PP;
458 11 my $yp = YAML::PP->new(boolean => 'JSON::PP');
459 11 my %by_gvk; # "group/version/Kind" -> { kind, group, version, doc, spec_props => {name=>1}, has_spec_schema }
460 11 for my $m (@manifests) {
461 19 my ($label, $text) = @$m;
462 19 my @docs = eval { $yp->load_string($text) };
463 19 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 19 19
464 0 warn "crd-drift-check: could not parse $label: $@";
465 0 next;
466 }
467 19 for my $doc (@docs) {
468 19 ▶ next unless ref $doc eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $doc eq "HASH" 0 19 19
469 19 ▶ next unless ($doc->{kind} // '') eq 'CustomResourceDefinition';
Branch: 1 branch
BranchTrueFalseTotal
unless ($doc->{"kind"} // "") eq "CustomResourceDefinition" 4 15 19
Condition100%2/2
$doc->{"kind"} // ""
l!l
154
MC/DC100%1/1
$doc->{"kind"} // ""
$doc->{"kind"}
Truth table100%2/2
A
A= $doc->{"kind"}
Aresult
11
00
470 15 ▶ my $spec = $doc->{spec} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $doc->{"spec"} or next 15 0 15
471 15 ▶ my $group = $spec->{group} // next;
Condition50%1/2
$spec->{"group"} // (next)
l!l
150
MC/DC0%0/1
$spec->{"group"} // (next)
$spec->{"group"}
Truth table50%1/2
A
A= $spec->{"group"}
Aresult
11
00
472 15 ▶ my $kind = $spec->{names}{kind} // next;
Condition50%1/2
$spec->{"names"}{"kind"} // (next)
l!l
150
MC/DC0%0/1
$spec->{"names"}{"kind"} // (next)
$spec->{"names"}{"kind"}
Truth table50%1/2
A
A= $spec->{"names"}{"kind"}
Aresult
11
00
473 15 ▶ for my $ver (@{ $spec->{versions} // [] }) {
Condition50%1/2
$spec->{"versions"} // []
l!l
150
MC/DC0%0/1
$spec->{"versions"} // []
$spec->{"versions"}
Truth table50%1/2
A
A= $spec->{"versions"}
Aresult
11
00
474 15 ▶ my $vname = $ver->{name} // next;
Condition50%1/2
$ver->{"name"} // (next)
l!l
150
MC/DC0%0/1
$ver->{"name"} // (next)
$ver->{"name"}
Truth table50%1/2
A
A= $ver->{"name"}
Aresult
11
00
475 15 my $gvk = "$group/$vname/$kind";
476 15 my $schema = $ver->{schema}{openAPIV3Schema};
477 my $spec_schema = $schema->{properties} && exists $schema->{properties}{spec}
478 15 ▶ ? $schema->{properties}{spec} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$schema->{'properties'} && exists $schema->{'properties'}{'spec'} ? : 10 5 15
Condition100%3/3
$schema->{"properties"} and exists $schema->{"properties"}{"spec"}
!ll&&!rl&&r
1410
MC/DC100%2/2
$schema->{"properties"} and exists $schema->{"properties"}{"spec"}
$schema->{"properties"} exists $schema->{"properties"}{"spec"}
Truth table100%3/3
A and B
A= $schema->{"properties"}B= exists $schema->{"properties"}{"spec"}
ABresult
0X0
100
111
479 15 ▶ my $spec_props = $spec_schema ? $spec_schema->{properties} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$spec_schema ? : 10 5 15
480 $by_gvk{$gvk} = {
481 kind => $kind,
482 group => $group,
483 version => $vname,
484 label => $label,
485 doc => $doc,
486 root_schema => $schema,
487 has_spec_schema => ($spec_props ? 1 : 0),
488 15 ▶ spec_props => { map { $_ => 1 } keys %{ $spec_props // {} } },
Branch: 1 branch
BranchTrueFalseTotal
$spec_props ? : 9 6 15
Condition100%2/2
$spec_props // {}
l!l
96
MC/DC100%1/1
$spec_props // {}
$spec_props
Truth table100%2/2
A
A= $spec_props
Aresult
11
00
489 };
490 }
491 }
492 }
493 11 return \%by_gvk;
494 }
495  
496 # A class without a registered `spec` can only be exempt from opaque-spec
497 # reporting when the upstream root is unambiguously a closed flat object.
498 # Keep every open, preserve-unknown, composite, or malformed shape visible:
499 # Tier 3 deliberately compares spec fields only, not arbitrary root fields.
500 4 ▶sub has_closed_flat_root_without_spec {
Subroutine has_closed_flat_root_without_spec: called CC=16 SCAR=28.9
501 4 my ($upstream) = @_;
502 4 my $schema = $upstream->{root_schema};
503 4 ▶ return unless ref $schema eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 4 4
504 4 ▶ return unless ($schema->{type} // '') eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema->{"type"} // "") eq "object" 0 4 4
Condition50%1/2
$schema->{"type"} // ""
l!l
40
MC/DC0%0/1
$schema->{"type"} // ""
$schema->{"type"}
Truth table50%1/2
A
A= $schema->{"type"}
Aresult
11
00
505 4 ▶ if (exists $schema->{'x-kubernetes-preserve-unknown-fields'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"x-kubernetes-preserve-unknown-fields"}) 2 2 4
506 2 my $preserve_unknown = $schema->{'x-kubernetes-preserve-unknown-fields'};
507 2 ▶ return unless JSON::PP::is_bool($preserve_unknown);
Branch: 1 branch
BranchTrueFalseTotal
unless JSON::PP::is_bool($preserve_unknown) 0 2 2
508 2 ▶ return if $preserve_unknown;
Branch: 1 branch
BranchTrueFalseTotal
if $preserve_unknown 1 1 2
509 }
510 3 ▶ if (exists $schema->{additionalProperties}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $schema->{"additionalProperties"}) 3 0 3
511 3 my $additional_properties = $schema->{additionalProperties};
512 3 ▶ return unless JSON::PP::is_bool($additional_properties) && !$additional_properties;
Branch: 1 branch
BranchTrueFalseTotal
unless JSON::PP::is_bool($additional_properties) and not $additional_properties 2 1 3
Condition100%3/3
JSON::PP::is_bool($additional_properties) and not $additional_properties
!ll&&!rl&&r
111
MC/DC100%2/2
JSON::PP::is_bool($additional_properties) and not $additional_properties
JSON::PP::is_bool($additional_properties) not $additional_properties
Truth table100%3/3
A and not(B)
A= JSON::PP::is_bool($additional_properties)B= not $additional_properties
ABresult
0X0
100
111
513 }
514 1 for my $keyword (qw( allOf anyOf oneOf not $ref )) {
515 5 ▶ return if exists $schema->{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
if exists $schema->{$keyword} 0 5 5
516 }
517  
518 1 my $properties = $schema->{properties};
519 1 ▶ return unless ref $properties eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $properties eq "HASH" 0 1 1
520 1 ▶ return if exists $properties->{spec};
Branch: 1 branch
BranchTrueFalseTotal
if exists $properties->{"spec"} 0 1 1
521 1 for my $property (values %$properties) {
522 3 ▶ return unless ref $property eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $property eq "HASH" 0 3 3
523 }
524 1 return 1;
525 }
526  
527 # ---------------------------------------------------------------------------
528 # Shipped-provider view: GVK -> class, from resource_map values.
529 #
530 # A mapped class's api_version() ("group/version") joined to its kind() is
531 # the exact GVK IO::K8s::add() would register it under -- the authoritative
532 # "what does this provider serve" set, independent of which resource_map
533 # keys (short or domain-qualified) happen to point at it.
534 # ---------------------------------------------------------------------------
535  
536 11 ▶sub shipped_view {
Subroutine shipped_view: called CC=6 SCAR=18.7
537 11 my ($provider) = @_;
538 11 my $pkg = "IO::K8s::$provider";
539 11 my $map = $pkg->resource_map;
540 11 my %gvk_class; # gvk -> class
541 my %classes; # class -> 1 (unique)
542 11 $classes{ qualify_class($_) } = 1 for values %$map;
543 11 for my $class (sort keys %classes) {
544 108 eval { require_class($class); 1 }
545 0 ▶ or do { warn "crd-drift-check: failed to load $class: $@"; next };
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require_class($class); 1 } } or do { warn "crd-drift-check: failed to load ${class}: $@"; next } 108 0 108
546 108 my $av = eval { $class->api_version };
547 108 my $kind = eval { $class->kind };
548 108 ▶ next unless defined $av && defined $kind;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $av and defined $kind 0 108 108
Condition33%1/3
defined $av and defined $kind
!ll&&!rl&&r
00108
MC/DC0%0/2
defined $av and defined $kind
defined $av defined $kind
Truth table33%1/3
A and B
A= defined $avB= defined $kind
ABresult
0X0
100
111
549 108 $gvk_class{"$av/$kind"} = $class;
550 }
551 11 return \%gvk_class;
552 }
553  
554 119 ▶sub require_class {
Subroutine require_class: called CC=1 SCAR=0.0
555 119 my ($class) = @_;
556 119 (my $rel = $class) =~ s{::}{/}g;
557 119 $rel .= '.pm';
558 119 require $rel;
559 119 return;
560 }
561  
562 # shipped_view() above only requires the resource_map's Kind-root classes.
563 # For a D5/D7-style provider, a Kind's `spec` is a NAMED nested class in its
564 # own file (e.g. IngressRoute's spec => '+IO::K8s::Traefik::V1alpha1::
565 # IngressRouteSpec') that resource_map never lists -- so without this,
566 # shipped_spec_fields() reads %IO::K8s::Resource::_attr_registry for a class
567 # that was never require()d, its registry entry doesn't exist, and every
568 # field is falsely reported MISSING (k105). Mirrors t/02_compile_all.t /
569 # spec-drift-check.pl's load_registry: walk + require every .pm, but scoped
570 # to this provider's own subtree so an unrelated provider's tree stays
571 # unloaded. A provider with no per-Kind subdirectory yet (nothing beyond the
572 # resource_map roots) is a silent no-op, not an error.
573 11 ▶sub require_provider_tree {
Subroutine require_provider_tree: called CC=4 SCAR=13.9
574 11 my ($lib_dir, $provider) = @_;
575 11 my $provider_dir = File::Spec->catdir($lib_dir, 'IO', 'K8s', $provider);
576 11 ▶ return unless -d $provider_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $provider_dir 0 11 11
577  
578 11 my @pm_paths;
579 find(
580 11 ▶ { wanted => sub { push @pm_paths, $File::Find::name if /\.pm$/ }, no_chdir => 1 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if /\.pm$/ 625 36 661
581 $provider_dir,
582 );
583 11 for my $path (sort @pm_paths) {
584 625 my $rel = File::Spec->abs2rel($path, $lib_dir);
585 625 $rel =~ s{\\}{/}g; # require() wants forward slashes regardless of OS
586 625 ▶ eval { require $rel; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $rel; 1 } } or warn "crd-drift-check: failed to load ${rel}: $@" 625 0 625
587 or warn "crd-drift-check: failed to load $rel: $@";
588 }
589 11 return;
590 }
591  
592 # The shipped `spec` field set for a class, or undef when spec is modeled
593 # opaquely (an Opaque map -- { Str => 1 } before k191 --, a free-form
594 # object, or simply absent).
595 # Two shapes expose upstream-comparable field names, both read off the
596 # nested class's own registry entry using each attribute's wire json_key:
597 #
598 # - an inline struct (is_inline_struct) -- the k93-era case, an anonymous
599 # `{ field => TypeSpec, ... }` the k8s DSL generated a struct for; or
600 # - (A3, D5/D7) a plain object reference (is_object, not an inline
601 # struct) whose class lives under IO::K8s::<Provider>:: -- the emitter
602 # output's own case, where `spec` is typed as a NAMED nested class
603 # ('Traefik::V1alpha1::MiddlewareSpec') rather than an anonymous one.
604 # Scoped to the provider's own namespace so a spec reusing a shipped
605 # CORE class (Meta::V1::LabelSelector, say -- D5's reuse_core) is not
606 # mistaken for that Kind's own field list.
607 15 ▶sub shipped_spec_fields {
Subroutine shipped_spec_fields: called CC=10 SCAR=25.4
608 15 my ($class, $provider) = @_;
609 15 my $reg = \%IO::K8s::Resource::_attr_registry;
610 15 ▶ my $info = $reg->{$class}{spec} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $info = $reg->{$class}{"spec"} or return undef 11 4 15
611 11 ▶ return undef unless $info->{is_object};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"is_object"} 0 11 11
612 11 ▶ my $inner = $info->{class} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $inner = $info->{"class"} or return undef 11 0 11
613 11 ▶ unless ($info->{is_inline_struct}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info->{"is_inline_struct"}) 11 0 11
614 return undef
615 11 ▶ unless defined $provider && index($inner, "IO::K8s::$provider\::") == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $provider and index($inner, "IO::K8s::${provider}::") == 0 0 11 11
Condition33%1/3
defined $provider and index($inner, "IO::K8s::${provider}::") == 0
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $provider and index($inner, "IO::K8s::${provider}::") == 0
defined $provider index($inner, "IO::K8s::${provider}::") == 0
Truth table33%1/3
A and B
A= defined $providerB= index($inner, "IO::K8s::${provider}::") == 0
ABresult
0X0
100
111
616 }
617 11 ▶ my $inner_attrs = $reg->{$inner} // {};
Condition50%1/2
$reg->{$inner} // {}
l!l
110
MC/DC0%0/1
$reg->{$inner} // {}
$reg->{$inner}
Truth table50%1/2
A
A= $reg->{$inner}
Aresult
11
00
618 11 my %fields;
619 11 for my $aname (keys %$inner_attrs) {
620 46 ▶ $fields{ $inner_attrs->{$aname}{json_key} // $aname } = 1;
Condition33%1/3
$inner_attrs->{$aname}{"json_key"} // $aname
l!l&&r!l&&!r
0460
MC/DC0%0/2
$inner_attrs->{$aname}{"json_key"} // $aname
$inner_attrs->{$aname}{"json_key"} $aname
Truth table33%1/3
A // B
A= $inner_attrs->{$aname}{"json_key"}B= $aname
ABresult
1X1
011
000
621 }
622 11 return \%fields;
623 }
624  
625 # ---------------------------------------------------------------------------
626 # Exceptions file
627 # ---------------------------------------------------------------------------
628  
629 11 ▶sub load_exceptions {
Subroutine load_exceptions: called CC=10 SCAR=25.2
630 11 my ($path) = @_;
631 11 ▶ die "crd-drift-check: exceptions file not found: $path\n" unless -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path 0 11 11
632 11 require YAML::PP;
633 11 my ($data) = YAML::PP->new->load_string(_slurp($path));
634 11 ▶ $data //= {};
Condition50%1/2
$data //= {}
l!l
110
MC/DC0%0/1
$data //= {}
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
635 11 ▶ $data->{ignore_missing_kinds} //= [];
Condition50%1/2
$data->{"ignore_missing_kinds"} //= []
l!l
110
MC/DC0%0/1
$data->{"ignore_missing_kinds"} //= []
$data->{"ignore_missing_kinds"}
Truth table50%1/2
A
A= $data->{"ignore_missing_kinds"}
Aresult
11
00
636 11 ▶ $data->{ignore_stale_kinds} //= [];
Condition50%1/2
$data->{"ignore_stale_kinds"} //= []
l!l
110
MC/DC0%0/1
$data->{"ignore_stale_kinds"} //= []
$data->{"ignore_stale_kinds"}
Truth table50%1/2
A
A= $data->{"ignore_stale_kinds"}
Aresult
11
00
637 11 ▶ $data->{ignore_missing_fields} //= [];
Condition50%1/2
$data->{"ignore_missing_fields"} //= []
l!l
110
MC/DC0%0/1
$data->{"ignore_missing_fields"} //= []
$data->{"ignore_missing_fields"}
Truth table50%1/2
A
A= $data->{"ignore_missing_fields"}
Aresult
11
00
638 11 ▶ $data->{ignore_extra_fields} //= [];
Condition50%1/2
$data->{"ignore_extra_fields"} //= []
l!l
110
MC/DC0%0/1
$data->{"ignore_extra_fields"} //= []
$data->{"ignore_extra_fields"}
Truth table50%1/2
A
A= $data->{"ignore_extra_fields"}
Aresult
11
00
639 11 ▶ $data->{ignore_unrendered} //= [];
Condition50%1/2
$data->{"ignore_unrendered"} //= []
l!l
110
MC/DC0%0/1
$data->{"ignore_unrendered"} //= []
$data->{"ignore_unrendered"}
Truth table50%1/2
A
A= $data->{"ignore_unrendered"}
Aresult
11
00
640 11 ▶ $data->{ignore_cosmetic_differs} //= [];
Condition50%1/2
$data->{"ignore_cosmetic_differs"} //= []
l!l
110
MC/DC0%0/1
$data->{"ignore_cosmetic_differs"} //= []
$data->{"ignore_cosmetic_differs"}
Truth table50%1/2
A
A= $data->{"ignore_cosmetic_differs"}
Aresult
11
00
641 11 ▶ $data->{accept_structural_divergence} //= [];
Condition50%1/2
$data->{"accept_structural_divergence"} //= []
l!l
110
MC/DC0%0/1
$data->{"accept_structural_divergence"} //= []
$data->{"accept_structural_divergence"}
Truth table50%1/2
A
A= $data->{"accept_structural_divergence"}
Aresult
11
00
642 11 return $data;
643 }
644  
645 # GVK-level exceptions match on provider + exact gvk (a bare `gvk` with no
646 # `provider` matches in any provider).
647 93 ▶sub gvk_excepted {
Subroutine gvk_excepted: called CC=7 SCAR=27.3
648 93 my ($provider, $gvk, $entries) = @_;
649 93 for my $e (@$entries) {
650 558 ▶ next unless ref $e eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $e eq "HASH" 0 558 558
651 558 ▶ next unless ($e->{gvk} // '') eq $gvk;
Branch: 1 branch
BranchTrueFalseTotal
unless ($e->{"gvk"} // "") eq $gvk 558 0 558
Condition50%1/2
$e->{"gvk"} // ""
l!l
5580
MC/DC0%0/1
$e->{"gvk"} // ""
$e->{"gvk"}
Truth table50%1/2
A
A= $e->{"gvk"}
Aresult
11
00
652 0 ▶ next if defined $e->{provider} && $e->{provider} ne $provider;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e->{"provider"} and $e->{"provider"} ne $provider 0 0 0
Condition0%0/3
defined $e->{"provider"} and $e->{"provider"} ne $provider
!ll&&!rl&&r
000
MC/DC0%0/2
defined $e->{"provider"} and $e->{"provider"} ne $provider
defined $e->{"provider"} $e->{"provider"} ne $provider
Truth table0%0/3
A and B
A= defined $e->{"provider"}B= $e->{"provider"} ne $provider
ABresult
0X0
100
111
653 0 return (1, $e->{reason});
654 }
655 93 return (0, undef);
656 }
657  
658 # Field-level exceptions match on provider + gvk + field.
659 48 ▶sub field_excepted {
Subroutine field_excepted: called CC=9 SCAR=41.3
660 48 my ($provider, $gvk, $field, $entries) = @_;
661 48 for my $e (@$entries) {
662 0 ▶ next unless ref $e eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $e eq "HASH" 0 0 0
663 0 ▶ next unless ($e->{gvk} // '') eq $gvk && ($e->{field} // '') eq $field;
Branch: 1 branch
BranchTrueFalseTotal
unless ($e->{"gvk"} // "") eq $gvk and ($e->{"field"} // "") eq $field 0 0 0
Condition0%0/7
($e->{"gvk"} // "") eq $gvk and ($e->{"field"} // "") eq $field
!ll&&!rl&&r
000
$e->{"gvk"} // ""
l!l
00
$e->{"field"} // ""
l!l
00
MC/DC0%0/2
($e->{"gvk"} // "") eq $gvk and ($e->{"field"} // "") eq $field
$e->{"gvk"} $e->{"field"}
Truth table0%0/3
A and B
A= $e->{"gvk"}B= $e->{"field"}
ABresult
0X0
100
111
664 0 ▶ next if defined $e->{provider} && $e->{provider} ne $provider;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e->{"provider"} and $e->{"provider"} ne $provider 0 0 0
Condition0%0/3
defined $e->{"provider"} and $e->{"provider"} ne $provider
!ll&&!rl&&r
000
MC/DC0%0/2
defined $e->{"provider"} and $e->{"provider"} ne $provider
defined $e->{"provider"} $e->{"provider"} ne $provider
Truth table0%0/3
A and B
A= defined $e->{"provider"}B= $e->{"provider"} ne $provider
ABresult
0X0
100
111
665 0 return (1, $e->{reason});
666 }
667 48 return (0, undef);
668 }
669  
670 # --check's NOT RENDERED exceptions match on provider + path, where `path`
671 # is the same lib/-relative string --check itself reports and render_gvk's
672 # own file keys already use ("IO/K8s/<Provider>/<Version>/<File>.pm") --
673 # one canonical form throughout, nothing to translate between.
674 0 ▶sub unrendered_excepted {
Subroutine unrendered_excepted: not called CC=7 SCAR=40.3
675 0 my ($provider, $path, $entries) = @_;
676 0 for my $e (@$entries) {
677 0 ▶ next unless ref $e eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $e eq "HASH" 0 0 0
678 0 ▶ next unless ($e->{path} // '') eq $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ($e->{"path"} // "") eq $path 0 0 0
Condition0%0/2
$e->{"path"} // ""
l!l
00
MC/DC0%0/1
$e->{"path"} // ""
$e->{"path"}
Truth table0%0/2
A
A= $e->{"path"}
Aresult
11
00
679 0 ▶ next if defined $e->{provider} && $e->{provider} ne $provider;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e->{"provider"} and $e->{"provider"} ne $provider 0 0 0
Condition0%0/3
defined $e->{"provider"} and $e->{"provider"} ne $provider
!ll&&!rl&&r
000
MC/DC0%0/2
defined $e->{"provider"} and $e->{"provider"} ne $provider
defined $e->{"provider"} $e->{"provider"} ne $provider
Truth table0%0/3
A and B
A= defined $e->{"provider"}B= $e->{"provider"} ne $provider
ABresult
0X0
100
111
680 0 return (1, $e->{reason});
681 }
682 0 return (0, undef);
683 }
684  
685 # --check's COSMETIC reclassification (k132) matches on provider + path, the
686 # same lib/-relative form ("IO/K8s/<Provider>/<Version>/<File>.pm") that
687 # ignore_unrendered uses and that --check itself reports. Being *listed* is
688 # necessary but never sufficient: check_for only suppresses a listed file
689 # when _structural_signature() confirms its k8s declarations still match the
690 # render (see the guard there).
691 0 ▶sub cosmetic_differ_excepted {
Subroutine cosmetic_differ_excepted: not called CC=7 SCAR=40.3
692 0 my ($provider, $path, $entries) = @_;
693 0 for my $e (@$entries) {
694 0 ▶ next unless ref $e eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $e eq "HASH" 0 0 0
695 0 ▶ next unless ($e->{path} // '') eq $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ($e->{"path"} // "") eq $path 0 0 0
Condition0%0/2
$e->{"path"} // ""
l!l
00
MC/DC0%0/1
$e->{"path"} // ""
$e->{"path"}
Truth table0%0/2
A
A= $e->{"path"}
Aresult
11
00
696 0 ▶ next if defined $e->{provider} && $e->{provider} ne $provider;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e->{"provider"} and $e->{"provider"} ne $provider 0 0 0
Condition0%0/3
defined $e->{"provider"} and $e->{"provider"} ne $provider
!ll&&!rl&&r
000
MC/DC0%0/2
defined $e->{"provider"} and $e->{"provider"} ne $provider
defined $e->{"provider"} $e->{"provider"} ne $provider
Truth table0%0/3
A and B
A= defined $e->{"provider"}B= $e->{"provider"} ne $provider
ABresult
0X0
100
111
697 0 return (1, $e->{reason});
698 }
699 0 return (0, undef);
700 }
701  
702 # --check's ACCEPTED DIVERGENCE reclassification (k133, Weg 1) matches on
703 # provider + path, the same lib/-relative form ("IO/K8s/<Provider>/<Version>/
704 # <File>.pm") the other --check exceptions use. Deliberately SEPARATE from
705 # ignore_cosmetic_differs and its _structural_signature guard: this category
706 # accepts a GENUINE structural difference between the render and lib -- the
707 # k55/k120 typed-empty-vs-opaque-hash UUIDSpec case, where lib names the empty
708 # struct '+IO::K8s::ExternalSecrets::V1alpha1::UUIDSpec' and the emitter types
709 # it opaquely as Opaque ({ Str => 1 } before k191) -- so it cannot go
710 # through the cosmetic guard,
711 # which by design refuses anything whose k8s declarations drift. The scope
712 # limit here is exactness, not a signature: only the EXACT provider+path pairs
713 # listed are reclassified, so an unlisted file's structural drift is never
714 # masked (and a listed file's diff is still printed under --verbose).
715 0 ▶sub accept_divergence_excepted {
Subroutine accept_divergence_excepted: not called CC=7 SCAR=40.3
716 0 my ($provider, $path, $entries) = @_;
717 0 for my $e (@$entries) {
718 0 ▶ next unless ref $e eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $e eq "HASH" 0 0 0
719 0 ▶ next unless ($e->{path} // '') eq $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ($e->{"path"} // "") eq $path 0 0 0
Condition0%0/2
$e->{"path"} // ""
l!l
00
MC/DC0%0/1
$e->{"path"} // ""
$e->{"path"}
Truth table0%0/2
A
A= $e->{"path"}
Aresult
11
00
720 0 ▶ next if defined $e->{provider} && $e->{provider} ne $provider;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e->{"provider"} and $e->{"provider"} ne $provider 0 0 0
Condition0%0/3
defined $e->{"provider"} and $e->{"provider"} ne $provider
!ll&&!rl&&r
000
MC/DC0%0/2
defined $e->{"provider"} and $e->{"provider"} ne $provider
defined $e->{"provider"} $e->{"provider"} ne $provider
Truth table0%0/3
A and B
A= defined $e->{"provider"}B= $e->{"provider"} ne $provider
ABresult
0X0
100
111
721 0 return (1, $e->{reason});
722 }
723 0 return (0, undef);
724 }
725  
726 # ---------------------------------------------------------------------------
727 # Per-provider drift
728 # ---------------------------------------------------------------------------
729  
730 11 ▶sub check_provider {
Subroutine check_provider: called CC=24 SCAR=37.0
731 11 my ($opt, $provider, $exceptions) = @_;
732 11 my $pkg = "IO::K8s::$provider";
733 11 require_class($pkg);
734 11 ▶ my $version = eval { $pkg->upstream_version } // '(unknown)';
Condition50%1/2
eval { do { $pkg->upstream_version } } // "(unknown)"
l!l
110
MC/DC0%0/1
eval { do { $pkg->upstream_version } } // "(unknown)"
eval { do { $pkg->upstream_version } }
Truth table50%1/2
A
A= eval { do { $pkg->upstream_version } }
Aresult
11
00
735 11 my $sources = eval { $pkg->crd_sources };
736 11 ▶ die "crd-drift-check: $pkg has no crd_sources method\n" unless ref $sources eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sources eq "HASH" 0 11 11
737  
738 # Populate the registry for every nested class under this provider (k105)
739 # before shipped_view()/shipped_spec_fields() read it -- not just the
740 # resource_map roots shipped_view() requires on its own.
741 11 require_provider_tree($opt->{lib}, $provider);
742  
743 11 my $shipped = shipped_view($provider);
744 my $result = {
745 provider => $provider,
746 upstream => $version,
747 status => $sources->{status} // 'ok',
748 shipped_gvks => scalar keys %$shipped,
749 missing_kind => [],
750 missing_field => [],
751 extra_field => [],
752 opaque_spec => [],
753 stale_gvk => [],
754 suppressed => [],
755 note => $sources->{note},
756 11 ▶ };
Condition50%1/2
$sources->{"status"} // "ok"
l!l
110
MC/DC0%0/1
$sources->{"status"} // "ok"
$sources->{"status"}
Truth table50%1/2
A
A= $sources->{"status"}
Aresult
11
00
757  
758 # Unresolved upstream source (K3s): report the shipped GVKs, diff nothing.
759 11 ▶ if (($sources->{status} // 'ok') ne 'ok') {
Branch: 1 branch
BranchTrueFalseTotal
if (($sources->{"status"} // "ok") ne "ok") 0 11 11
Condition50%1/2
$sources->{"status"} // "ok"
l!l
110
MC/DC0%0/1
$sources->{"status"} // "ok"
$sources->{"status"}
Truth table50%1/2
A
A= $sources->{"status"}
Aresult
11
00
760 0 $result->{stale_gvk} = [ map { [$_, $shipped->{$_}] } sort keys %$shipped ];
761 0 $result->{unresolved} = 1;
762 0 $result->{crd_count} = 0;
763 0 return $result;
764 }
765  
766 11 my @manifests = load_manifests($opt, $provider, $sources, $version);
767 11 my $upstream = parse_crds(@manifests);
768 11 $result->{crd_count} = scalar keys %$upstream;
769  
770 # Tier 1: upstream GVK with no shipped class.
771 11 for my $gvk (sort keys %$upstream) {
772 15 ▶ next if $shipped->{$gvk};
Branch: 1 branch
BranchTrueFalseTotal
if $shipped->{$gvk} 15 0 15
773 0 my ($ign, $reason) = gvk_excepted($provider, $gvk, $exceptions->{ignore_missing_kinds});
774 0 ▶ if ($ign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ign) 0 0 0
775 0 push @{ $result->{suppressed} }, ['ignore_missing_kinds', $gvk, $reason];
776 0 next;
777 }
778 0 push @{ $result->{missing_kind} }, [$gvk, $upstream->{$gvk}{kind}];
779 }
780  
781 # Info: shipped GVK with no upstream CRD (stale / perl-only).
782 11 for my $gvk (sort keys %$shipped) {
783 108 ▶ next if $upstream->{$gvk};
Branch: 1 branch
BranchTrueFalseTotal
if $upstream->{$gvk} 15 93 108
784 93 my ($ign, $reason) = gvk_excepted($provider, $gvk, $exceptions->{ignore_stale_kinds});
785 93 ▶ if ($ign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ign) 0 93 93
786 0 push @{ $result->{suppressed} }, ['ignore_stale_kinds', $gvk, $reason];
787 0 next;
788 }
789 93 push @{ $result->{stale_gvk} }, [$gvk, $shipped->{$gvk}];
790 }
791  
792 # Field-level: GVKs present in both.
793 11 for my $gvk (sort keys %$upstream) {
794 15 ▶ my $class = $shipped->{$gvk} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $class = $shipped->{$gvk} or next 15 0 15
795 15 my $u = $upstream->{$gvk};
796 15 my $shipped_fields = shipped_spec_fields($class, $provider);
797 15 my $has_shipped_spec = exists $IO::K8s::Resource::_attr_registry{$class}{spec};
798  
799 15 ▶ if (!defined $shipped_fields) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $shipped_fields) 4 11 15
800 4 ▶ next if !$has_shipped_spec && has_closed_flat_root_without_spec($u);
Branch: 1 branch
BranchTrueFalseTotal
if not $has_shipped_spec and has_closed_flat_root_without_spec($u) 1 3 4
Condition66%2/3
not $has_shipped_spec and has_closed_flat_root_without_spec($u)
!ll&&!rl&&r
031
MC/DC50%1/2
not $has_shipped_spec and has_closed_flat_root_without_spec($u)
not $has_shipped_spec has_closed_flat_root_without_spec($u)
Truth table66%2/3
not(A) and B
A= not $has_shipped_specB= has_closed_flat_root_without_spec($u)
ABresult
0X0
100
111
801 # No comparable spec fields: retain the uncertainty as info.
802 3 push @{ $result->{opaque_spec} },
803 3 [$gvk, $class, scalar keys %{ $u->{spec_props} }, $u->{has_spec_schema}];
804 3 next;
805 }
806 11 ▶ if (!$u->{has_spec_schema}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($u->{"has_spec_schema"}) 2 9 11
807 # class models spec fields but upstream has no spec schema to diff.
808 2 push @{ $result->{opaque_spec} }, [$gvk, $class, 0, 0];
809 2 next;
810 }
811  
812 9 for my $f (sort keys %{ $u->{spec_props} }) {
813 10 ▶ next if $shipped_fields->{$f};
Branch: 1 branch
BranchTrueFalseTotal
if $shipped_fields->{$f} 2 8 10
814 8 my ($ign, $reason) = field_excepted($provider, $gvk, $f, $exceptions->{ignore_missing_fields});
815 8 ▶ if ($ign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ign) 0 8 8
816 0 push @{ $result->{suppressed} }, ['ignore_missing_fields', "$gvk.$f", $reason];
817 0 next;
818 }
819 8 push @{ $result->{missing_field} }, [$gvk, $class, $f];
820 }
821 9 for my $f (sort keys %$shipped_fields) {
822 42 ▶ next if $u->{spec_props}{$f};
Branch: 1 branch
BranchTrueFalseTotal
if $u->{"spec_props"}{$f} 2 40 42
823 40 my ($ign, $reason) = field_excepted($provider, $gvk, $f, $exceptions->{ignore_extra_fields});
824 40 ▶ if ($ign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ign) 0 40 40
825 0 push @{ $result->{suppressed} }, ['ignore_extra_fields', "$gvk.$f", $reason];
826 0 next;
827 }
828 40 push @{ $result->{extra_field} }, [$gvk, $class, $f];
829 }
830 }
831 11 $result->{_upstream} = $upstream;
832 11 return $result;
833 }
834  
835 # ---------------------------------------------------------------------------
836 # Rendering
837 # ---------------------------------------------------------------------------
838  
839 4 ▶sub render_provider {
Subroutine render_provider: called CC=23 SCAR=38.3
840 4 my ($r, $verbose) = @_;
841 4 my @out;
842 4 push @out, sprintf('########## %s (upstream %s) ##########', $r->{provider}, $r->{upstream});
843  
844 4 ▶ if ($r->{unresolved}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"unresolved"}) 0 4 4
845 0 push @out, 'status: SOURCE UNRESOLVED -- no upstream openAPIV3Schema to diff';
846 0 ▶ push @out, "note: $r->{note}" if $r->{note};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"note"} 0 0 0
847 0 push @out, "lib: $r->{shipped_gvks} shipped GVK(s), not verified against upstream:";
848 0 push @out, " $_->[0] -> $_->[1]" for @{ $r->{stale_gvk} };
849 0 push @out, '';
850 0 return @out;
851 }
852  
853 4 push @out, sprintf('spec: %d upstream GVK(s) across the fetched CRD manifests', $r->{crd_count});
854 4 push @out, sprintf('lib: %d shipped GVK(s) from the resource_map', $r->{shipped_gvks});
855 4 push @out, '';
856 4 push @out, '--- SUMMARY ---';
857 4 push @out, sprintf(' Tier 1 MISSING KIND %4d upstream GVK, no shipped class', scalar @{ $r->{missing_kind} });
858 4 push @out, sprintf(' Tier 3 MISSING FIELD %4d upstream spec field a modeled class lacks', scalar @{ $r->{missing_field} });
859 4 push @out, sprintf(' Info EXTRA FIELD %4d modeled class declares a spec field upstream lacks', scalar @{ $r->{extra_field} });
860 4 push @out, sprintf(' Info OPAQUE SPEC %4d GVK whose spec is not modeled field-by-field', scalar @{ $r->{opaque_spec} });
861 4 push @out, sprintf(' Info STALE/PERL-ONLY %4d shipped GVK, no matching upstream CRD', scalar @{ $r->{stale_gvk} });
862 4 my %supp;
863 4 $supp{ $_->[0] }++ for @{ $r->{suppressed} };
864 push @out, sprintf(' Suppressed by exceptions: %d (%s)%s',
865 4 ▶ scalar @{ $r->{suppressed} },
Condition50%1/2
join(', ', map({"${_}: $supp{$_}";} sort(keys %supp))) || 'none'
l!l
04
MC/DC0%0/1
join(', ', map({"${_}: $supp{$_}";} sort(keys %supp))) || 'none'
join(', ', map({"${_}: $supp{$_}";} sort(keys %supp)))
Truth table50%1/2
A
A= join(', ', map({"${_}: $supp{$_}";} sort(keys %supp)))
Aresult
11
00
866 0 ▶ join(', ', map { "$_: $supp{$_}" } sort keys %supp) || 'none',
Branch: 1 branch
BranchTrueFalseTotal
@{$r->{'suppressed'};} && !$verbose ? : 0 4 4
Condition33%1/3
@{$r->{"suppressed"};} and not $verbose
!ll&&!rl&&r
400
MC/DC0%0/2
@{$r->{"suppressed"};} and not $verbose
@{$r->{"suppressed"};} not $verbose
Truth table33%1/3
A and not(B)
A= @{$r->{"suppressed"};}B= not $verbose
ABresult
0X0
100
111
867 4 (@{ $r->{suppressed} } && !$verbose) ? ' -- rerun with --verbose to list them' : '');
868 4 push @out, '';
869  
870 4 push @out, '--- Tier 1: MISSING KIND (upstream GVK, no shipped class) ---';
871 4 push @out, " $_->[0]" for @{ $r->{missing_kind} };
872 4 ▶ push @out, ' (none)' unless @{ $r->{missing_kind} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$r->{"missing_kind"};} 4 0 4
873 4 push @out, '';
874  
875 4 push @out, '--- Tier 3: MISSING FIELD (upstream spec.<field>, class lacks it) ---';
876 4 push @out, " $_->[0] spec.$_->[2] ($_->[1])" for @{ $r->{missing_field} };
877 4 ▶ push @out, ' (none)' unless @{ $r->{missing_field} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$r->{"missing_field"};} 0 4 4
878 4 push @out, '';
879  
880 4 push @out, '--- Info: EXTRA FIELD (class has spec.<field>, upstream lacks it) ---';
881 4 push @out, " $_->[0] spec.$_->[2] ($_->[1])" for @{ $r->{extra_field} };
882 4 ▶ push @out, ' (none)' unless @{ $r->{extra_field} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$r->{"extra_field"};} 0 4 4
883 4 push @out, '';
884  
885 4 push @out, '--- Info: OPAQUE SPEC (spec not modeled field-by-field) ---';
886 4 for my $e (@{ $r->{opaque_spec} }) {
887 0 my ($gvk, $class, $n, $has_schema) = @$e;
888 0 ▶ my $detail = $has_schema
Branch: 1 branch
BranchTrueFalseTotal
$has_schema ? : 0 0 0
889 ? "upstream schema has $n spec field(s), class models spec opaquely"
890 : 'upstream has no spec schema to compare';
891 0 push @out, " $gvk ($class): $detail";
892 }
893 4 ▶ push @out, ' (none)' unless @{ $r->{opaque_spec} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$r->{"opaque_spec"};} 4 0 4
894 4 push @out, '';
895  
896 4 push @out, '--- Info: STALE / PERL-ONLY (shipped GVK, no upstream CRD) ---';
897 4 push @out, " $_->[0] -> $_->[1]" for @{ $r->{stale_gvk} };
898 4 ▶ push @out, ' (none)' unless @{ $r->{stale_gvk} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$r->{"stale_gvk"};} 0 4 4
899  
900 0 ▶ if ($verbose && @{ $r->{suppressed} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose and @{$r->{"suppressed"};}) 0 4 4
Condition33%1/3
$verbose and @{$r->{"suppressed"};}
!ll&&!rl&&r
400
MC/DC0%0/2
$verbose and @{$r->{"suppressed"};}
$verbose @{$r->{"suppressed"};}
Truth table33%1/3
A and B
A= $verboseB= @{$r->{"suppressed"};}
ABresult
0X0
100
111
901 0 push @out, '';
902 0 push @out, '--- Suppressed by exceptions (--verbose) ---';
903 0 for my $s (@{ $r->{suppressed} }) {
904 0 my ($cat, $subject, $reason) = @$s;
905 0 ▶ push @out, sprintf(' [%s] %s%s', $cat, $subject,
Branch: 1 branch
BranchTrueFalseTotal
defined $reason ? : 0 0 0
906 (defined $reason ? " -- $reason" : ''));
907 }
908 }
909 4 push @out, '';
910 4 return @out;
911 }
912  
913 4 ▶sub render_report {
Subroutine render_report: called CC=2 SCAR=6.9
914 4 my ($results, $verbose) = @_;
915 4 my @out;
916 4 push @out, '=== IO::K8s crd-drift-check ===';
917 4 push @out, 'providers: ' . join(', ', map { $_->{provider} } @$results);
918 4 push @out, '';
919 4 push @out, render_provider($_, $verbose) for @$results;
920 4 return join("\n", @out) . "\n";
921 }
922  
923 # ---------------------------------------------------------------------------
924 # --suggest: the classes the emitter would write for a reported Kind.
925 #
926 # Report-only stays report-only: the source goes to stdout or to a directory
927 # the caller names, never into lib/. One throwaway AutoGen namespace per
928 # GVK keeps the generated classes apart from anything the providers loaded.
929 # ---------------------------------------------------------------------------
930  
931 11 ▶sub load_names {
Subroutine load_names: called CC=3 SCAR=18.1
932 11 my ($path) = @_;
933 11 ▶ return {} unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 11 0 11
934 0 require YAML::PP;
935 0 my $map = YAML::PP->new->load_file($path);
936 0 ▶ die "crd-drift-check: --names file must be a YAML map\n" unless ref $map eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $map eq "HASH" 0 0 0
937 0 return $map;
938 }
939  
940 # The per-Kind overlay slice for one provider (A2's shape: `base:`, `kinds:`
941 # with per-Kind `with`/`extra`/`names`), from --overlay when given or from
942 # maint/crd-render/<Provider>.yaml otherwise. A missing default file is the
943 # normal case until a provider's Phase B task writes one -- returns {}, not
944 # an error; a missing --overlay is already fatal in parse_args, so reaching
945 # here with one set means the file exists.
946 5 ▶sub load_overlay {
Subroutine load_overlay: called CC=5 SCAR=16.4
947 5 my ($opt, $provider) = @_;
948 my $path = defined $opt->{overlay}
949 ? $opt->{overlay}
950 5 ▶ : File::Spec->catfile($DIST_ROOT, 'maint', 'crd-render', "$provider.yaml");
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'overlay'} ? : 4 1 5
951 5 ▶ return {} unless -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path 0 5 5
952 5 require YAML::PP;
953 5 my $data = YAML::PP->new->load_file($path);
954 5 ▶ die "crd-drift-check: overlay file $path must be a YAML map\n" unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 5 5
955 5 ▶ $data->{kinds} //= {};
Condition50%1/2
$data->{"kinds"} //= {}
l!l
50
MC/DC0%0/1
$data->{"kinds"} //= {}
$data->{"kinds"}
Truth table50%1/2
A
A= $data->{"kinds"}
Aresult
11
00
956 5 return $data;
957 }
958  
959 # One served GVK, rendered through the emitter -- the shared core of
960 # --suggest (a reported gap only) and --render (every GVK): generate the
961 # AutoGen classes for $u->{doc} into a fresh throwaway namespace, then
962 # render $root with $overlay (the Kind's own `kinds.<Kind>` slice, already
963 # sliced out by the caller) and any --names D6 override.
964 8 ▶sub render_gvk {
Subroutine render_gvk: called CC=6 SCAR=19.5
965 8 my ($opt, $provider, $u, $overlay) = @_;
966 8 require IO::K8s::CRD;
967 8 require IO::K8s::CRD::Emitter;
968 8 state $ns_counter = 0;
969 8 my $ns = 'IO::K8s::_CRDRENDER_' . ++$ns_counter;
970 # reuse_core on (D5, and IO::K8s::CRD->generate's own default) -- a
971 # nested schema shaped exactly like a shipped core class is typed as
972 # that class rather than a per-provider copy. A provider overlay may name
973 # logical nested-class paths under `no_reuse_core` (k120) where that
974 # reuse is suppressed so the provider's own named type is generated
975 # instead (then given its Go name via `names`) -- PrometheusOperator's
976 # Argument, whose {name,value} shape would otherwise reuse
977 # Core::V1::HTTPHeader.
978 0 ▶ my %reuse_core_except = map { $_ => 1 } @{ $overlay->{no_reuse_core} // [] };
Condition50%1/2
$overlay->{"no_reuse_core"} // []
l!l
08
MC/DC0%0/1
$overlay->{"no_reuse_core"} // []
$overlay->{"no_reuse_core"}
Truth table50%1/2
A
A= $overlay->{"no_reuse_core"}
Aresult
11
00
979 8 my $classes = IO::K8s::CRD->generate($u->{doc}, $ns, reuse_core => 1,
980 reuse_core_except => \%reuse_core_except);
981 8 ▶ my $root = $classes->{"$u->{group}/$u->{version}"} or return {};
Branch: 1 branch
BranchTrueFalseTotal
my $root = $$classes{qq[$u->{"group"}/$u->{"version"}]} or return {} 8 0 8
982  
983 # A --names key is written relative to the Kind (the Kind's own
984 # generated class IS $root, e.g. '...::Middleware'), so a key of
985 # 'Middleware::Spec::RateLimit' names the nested class
986 # '$root::Spec::RateLimit' -- the leading 'Middleware::' names the
987 # Kind, it is not repeated inside $root. Strip it before joining.
988 8 ▶ my $names = $opt->{_names_map} // {};
Condition50%1/2
$opt->{"_names_map"} // {}
l!l
80
MC/DC0%0/1
$opt->{"_names_map"} // {}
$opt->{"_names_map"}
Truth table50%1/2
A
A= $opt->{"_names_map"}
Aresult
11
00
989 8 my %class_names;
990 0 for my $key (grep { $_ ne $u->{kind} } keys %$names) {
991 0 (my $suffix = $key) =~ s/^\Q$u->{kind}\E:://;
992 0 $class_names{"$root\::$suffix"} = $names->{$key};
993 }
994 # subresources: the shipped provider classes now declare their upstream
995 # subresources as the k158 import parameter, so the emitter default
996 # (subresources => 1) renders them and --check stays byte-identical.
997 my $emitter = IO::K8s::CRD::Emitter->new(
998 8 ▶ base => "IO::K8s::$provider\::" . ucfirst($u->{version}),
Condition50%1/2
$overlay // {}
l!l
80
MC/DC0%0/1
$overlay // {}
$overlay
Truth table50%1/2
A
A= $overlay
Aresult
11
00
999 names => \%class_names,
1000 overlay => $overlay // {},
1001 );
1002 8 return $emitter->render($root);
1003 }
1004  
1005 8 ▶sub _merge_rendered_files {
Subroutine _merge_rendered_files: called CC=4 SCAR=13.9
1006 8 my ($files, $functional, $origins, $rendered, $gvk) = @_;
1007 8 for my $path (sort keys %$rendered) {
1008 24 my $source = $rendered->{$path};
1009 24 my $signature = IO::K8s::CRD::Emitter->_functional_source($source);
1010 24 ▶ if (exists $files->{$path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $files->{$path}) 4 20 24
1011 die 'crd-drift-check: target ' . $path
1012 . ' has non-identical GVK sources ' . $origins->{$path}
1013 . ' and ' . $gvk . "\n"
1014 4 ▶ if $functional->{$path} ne $signature;
Branch: 1 branch
BranchTrueFalseTotal
if $functional->{$path} ne $signature 2 2 4
1015 }
1016 # Deliberately retain the last equivalent source, matching the
1017 # overwrite selection render_for/suggest_for made before collision
1018 # detection.
1019 22 $files->{$path} = $source;
1020 22 $functional->{$path} = $signature;
1021 22 $origins->{$path} = $gvk;
1022 }
1023 6 return;
1024 }
1025  
1026 3 ▶sub suggest_for {
Subroutine suggest_for: called CC=4 SCAR=13.9
1027 3 my ($opt, $result, $upstream) = @_;
1028 3 my @gvks = map { $_->[0] } @{ $result->{opaque_spec} }, @{ $result->{missing_field} };
1029 3 my %seen;
1030 my %files;
1031 3 my %functional;
1032 3 my %origins;
1033 3 my $overlay = load_overlay($opt, $result->{provider});
1034 3 for my $gvk (grep { !$seen{$_}++ } @gvks) {
1035 4 ▶ my $u = $upstream->{$gvk} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $u = $upstream->{$gvk} or next 4 0 4
1036 4 ▶ my $kind_overlay = $overlay->{kinds}{$u->{kind}} // {};
Condition50%1/2
$overlay->{"kinds"}{$u->{"kind"}} // {}
l!l
40
MC/DC0%0/1
$overlay->{"kinds"}{$u->{"kind"}} // {}
$overlay->{"kinds"}{$u->{"kind"}}
Truth table50%1/2
A
A= $overlay->{"kinds"}{$u->{"kind"}}
Aresult
11
00
1037 4 my $rendered = render_gvk($opt, $result->{provider}, $u, $kind_overlay);
1038 4 _merge_rendered_files(\%files, \%functional, \%origins, $rendered, $gvk);
1039 }
1040 2 return \%files;
1041 }
1042  
1043 # --render's own GVK set: every served GVK the manifests describe, not only
1044 # a reported gap.
1045 2 ▶sub render_for {
Subroutine render_for: called CC=3 SCAR=11.0
1046 2 my ($opt, $provider, $upstream) = @_;
1047 2 my $overlay = load_overlay($opt, $provider);
1048 2 my %files;
1049 my %functional;
1050 2 my %origins;
1051 2 for my $gvk (sort keys %$upstream) {
1052 4 my $u = $upstream->{$gvk};
1053 4 ▶ my $kind_overlay = $overlay->{kinds}{$u->{kind}} // {};
Condition50%1/2
$overlay->{"kinds"}{$u->{"kind"}} // {}
l!l
40
MC/DC0%0/1
$overlay->{"kinds"}{$u->{"kind"}} // {}
$overlay->{"kinds"}{$u->{"kind"}}
Truth table50%1/2
A
A= $overlay->{"kinds"}{$u->{"kind"}}
Aresult
11
00
1054 4 my $rendered = render_gvk($opt, $provider, $u, $kind_overlay);
1055 4 _merge_rendered_files(\%files, \%functional, \%origins, $rendered, $gvk);
1056 }
1057 1 return \%files;
1058 }
1059  
1060 2 ▶sub _report_out {
Subroutine _report_out: called CC=2 SCAR=7.2
1061 2 my ($opt) = @_;
1062 # --format json keeps stdout a parseable JSON document (see main below,
1063 # which prints the report there); everything --suggest/--render/--check
1064 # would otherwise print goes to stderr instead when that format is
1065 # active, and to stdout the same as ever otherwise. See usage().
1066 2 ▶ return $opt->{format} eq 'json' ? \*STDERR : \*STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'format'} eq 'json' ? : 0 2 2
1067 }
1068  
1069 3 ▶sub emit_files {
Subroutine emit_files: called CC=6 SCAR=26.0
1070 3 my ($opt, $files, $dir, $label) = @_;
1071 3 ▶ return unless %$files;
Branch: 1 branch
BranchTrueFalseTotal
unless %$files 1 2 3
1072 2 my $out = _report_out($opt);
1073 2 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 0 2 2
1074 0 require File::Path;
1075 0 require File::Basename;
1076 0 for my $rel (sort keys %$files) {
1077 0 my $path = "$dir/$rel";
1078 0 File::Path::make_path(File::Basename::dirname($path));
1079 0 ▶ open my $fh, '>:encoding(UTF-8)', $path or die "crd-drift-check: cannot write $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(UTF-8)", $path or die "crd-drift-check: cannot write ${path}: $!\n" 0 0 0
1080 0 print $fh $files->{$rel};
1081 0 close $fh;
1082 }
1083 0 print $out "\n--- $label: wrote " . scalar(keys %$files) . " file(s) under $dir\n";
1084 0 return;
1085 }
1086 2 for my $rel (sort keys %$files) {
1087 10 print $out "\n#### $rel\n", $files->{$rel};
1088 }
1089 }
1090  
1091 # ---------------------------------------------------------------------------
1092 # --check: rendered output vs. the checked-in lib/ tree.
1093 # ---------------------------------------------------------------------------
1094  
1095 # A hand-rolled "first differing line and context" report -- not a real
1096 # diff algorithm, just the common prefix/suffix trimmed off both sides so
1097 # the interesting middle is what's left, formatted unified-ish ('-'/'+')
1098 # and capped to 40 lines. Sufficient for a maint loop; Text::Diff is not a
1099 # dependency of this distribution and this doesn't need one.
1100 0 ▶sub _diff_lines {
Subroutine _diff_lines: not called CC=10 SCAR=47.0
1101 0 my ($have, $want) = @_;
1102 0 my @a = split /\n/, $have, -1;
1103 0 my @b = split /\n/, $want, -1;
1104 0 my $pre = 0;
1105 0 ▶ $pre++ while $pre < @a && $pre < @b && $a[$pre] eq $b[$pre];
Branch: 1 branch
BranchTrueFalseTotal
$pre < @a and $pre < @b and $a[$pre] eq $b[$pre] and do { ++$pre } 0 0 0
Condition0%0/6
$pre < @a and $pre < @b and $a[$pre] eq $b[$pre]
!ll&&!rl&&r
000
$pre < @a and $pre < @b
!ll&&!rl&&r
000
MC/DC0%0/3
$pre < @a and $pre < @b and $a[$pre] eq $b[$pre]
$pre < @a $pre < @b $a[$pre] eq $b[$pre]
Truth table0%0/4
A and B and C
A= $pre < @aB= $pre < @bC= $a[$pre] eq $b[$pre]
ABCresult
0XX0
10X0
1100
1111
1106 0 my $suf = 0;
1107 0 ▶ $suf++ while $suf < (@a - $pre) && $suf < (@b - $pre)
Branch: 1 branch
BranchTrueFalseTotal
$suf < @a - $pre and $suf < @b - $pre and $a[$#a - $suf] eq $b[$#b - $suf] and do { ++$suf } 0 0 0
Condition0%0/6
$suf < @a - $pre and $suf < @b - $pre and $a[$#a - $suf] eq $b[$#b - $suf]
!ll&&!rl&&r
000
$suf < @a - $pre and $suf < @b - $pre
!ll&&!rl&&r
000
MC/DC0%0/3
$suf < @a - $pre and $suf < @b - $pre and $a[$#a - $suf] eq $b[$#b - $suf]
$suf < @a - $pre $suf < @b - $pre $a[$#a - $suf] eq $b[$#b - $suf]
Truth table0%0/4
A and B and C
A= $suf < @a - $preB= $suf < @b - $preC= $a[$#a - $suf] eq $b[$#b - $suf]
ABCresult
0XX0
10X0
1100
1111
1108 && $a[$#a - $suf] eq $b[$#b - $suf];
1109 0 my @out = (sprintf('@@ first difference at line %d @@', $pre + 1));
1110 0 push @out, "-$_" for @a[$pre .. $#a - $suf];
1111 0 push @out, "+$_" for @b[$pre .. $#b - $suf];
1112 0 my $total = @out;
1113 0 ▶ if ($total > 40) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total > 40) 0 0 0
1114 0 @out = @out[0 .. 39];
1115 0 push @out, sprintf('... (%d more line(s) omitted)', $total - 40);
1116 }
1117 0 return @out;
1118 }
1119  
1120 # The guard behind ignore_cosmetic_differs (k132). A file may be suppressed
1121 # as a cosmetic differ ONLY when its STRUCTURAL content is byte-identical
1122 # between the rendered source and the checked-in lib source after whitespace
1123 # is normalised away -- so that a listed exception can never hide a real
1124 # change (house rule: a red test is a claim before it is a failure). The
1125 # structural content is exactly the statements the k8s DSL and the identity
1126 # import carry: every `k8s <name> => <type>[, <opts>];` declaration
1127 # (name/type/required/enum) plus the `use IO::K8s::APIObject|Resource ...;`
1128 # and any `with '...';` (api_version/scope/roles). Everything else in the
1129 # file -- the `# ABSTRACT:` line, POD (=attr/=description/=seealso ...),
1130 # ordinary comments, blank lines, =>-alignment and where the k8s lines sit
1131 # relative to their POD -- is cosmetic and never enters the signature.
1132 #
1133 # This is textual, not a Perl parse, and that is sufficient here: both sides
1134 # are emitter-shaped source whose only load-bearing lines are those three
1135 # statement kinds, each terminated by `;`. The normalisation collapses every
1136 # whitespace run to one space and drops the spaces just inside brackets, so a
1137 # qw() enum wrapped across several lines in lib compares equal to the same
1138 # enum on one line from the emitter, while a changed member, type or
1139 # required-ness does not.
1140 0 ▶sub _structural_signature {
Subroutine _structural_signature: not called CC=14 SCAR=53.5
1141 0 my ($src) = @_;
1142 0 my @code;
1143 0 my $in_pod = 0;
1144 0 for my $line (split /\n/, $src, -1) {
1145 0 ▶ if (!$in_pod && $line =~ /^=\w/) { $in_pod = 1; next; }
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_pod and $line =~ /^=\w/) 0 0 0
Condition0%0/3
not $in_pod and $line =~ /^=\w/
!ll&&!rl&&r
000
MC/DC0%0/2
not $in_pod and $line =~ /^=\w/
not $in_pod $line =~ /^=\w/
Truth table0%0/3
not(A) and B
A= not $in_podB= $line =~ /^=\w/
ABresult
0X0
100
111
1146 0 ▶ if ($in_pod) { $in_pod = 0 if $line =~ /^=cut\b/; next; }
Branch: 2 branches
BranchTrueFalseTotal
if ($in_pod) 0 0 0
if $line =~ /^=cut\b/ 0 0 0
1147 0 ▶ next if $line =~ /^\s*#/; # comment-only line (incl. # ABSTRACT:)
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*#/ 0 0 0
1148 0 ▶ next unless $line =~ /\S/; # blank
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 0 0
1149 0 push @code, $line;
1150 }
1151 0 my @stmts;
1152 my $buf;
1153 0 for my $line (@code) {
1154 0 ▶ if (!defined $buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $buf) { } 0 0 0
1155 0 ▶ next unless $line =~ /^\s*(?:use\s+IO::K8s::(?:APIObject|Resource)\b|with\b|k8s\b)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^\s*(?:use\s+IO::K8s::(?:APIObject|Resource)\b|with\b|k8s\b)/ 0 0 0
1156 0 $buf = $line;
1157 }
1158 else {
1159 0 $buf .= "\n" . $line;
1160 }
1161 0 ▶ if ($buf =~ /;\s*\z/) { push @stmts, $buf; undef $buf; }
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ /;\s*\z/) 0 0 0
1162 }
1163 0 ▶ push @stmts, $buf if defined $buf; # unterminated -- keep it, so it shows
Branch: 1 branch
BranchTrueFalseTotal
if defined $buf 0 0 0
1164 0 for my $stmt (@stmts) {
1165 0 $stmt =~ s/\s+/ /g; # collapse every whitespace run
1166 0 $stmt =~ s/([(\[{])\s+/$1/g; # no space just inside an opening bracket
1167 0 $stmt =~ s/\s+([)\]}])/$1/g; # ... nor just inside a closing one
1168 0 $stmt =~ s/\A\s+//;
1169 0 $stmt =~ s/\s+\z//;
1170 }
1171 0 return join("\n", @stmts);
1172 }
1173  
1174 # Renders every served GVK of $provider (already computed in $rendered, a
1175 # provider->file map from render_for) and diffs each file against
1176 # lib/IO/K8s/<Provider>/. Returns { provider, rows => [...], bad => 0|1 }.
1177 0 ▶sub check_for {
Subroutine check_for: not called CC=11 SCAR=48.8
1178 0 my ($opt, $provider, $rendered, $exceptions) = @_;
1179  
1180 0 my $provider_dir = File::Spec->catdir($opt->{lib}, 'IO', 'K8s', $provider);
1181 0 my %shipped;
1182 0 ▶ if (-d $provider_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $provider_dir) 0 0 0
1183 0 require File::Find;
1184 0 ▶ File::Find::find({ no_chdir => 1, wanted => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1185 0 ▶ return unless -f $File::Find::name && /\.pm\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $File::Find::name and /\.pm\z/ 0 0 0
Condition0%0/3
-f $File::Find::name and /\.pm\z/
!ll&&!rl&&r
000
MC/DC0%0/2
-f $File::Find::name and /\.pm\z/
-f $File::Find::name /\.pm\z/
Truth table0%0/3
A and B
A= -f $File::Find::nameB= /\.pm\z/
ABresult
0X0
100
111
1186 0 (my $rel = $File::Find::name) =~ s{^\Q$opt->{lib}\E/}{};
1187 0 $shipped{$rel} = 1;
1188 0 } }, $provider_dir);
1189 }
1190  
1191 0 my @rows;
1192 0 my $bad = 0;
1193 0 for my $rel (sort keys %$rendered) {
1194 0 delete $shipped{$rel};
1195 0 my $lib_path = File::Spec->catfile($opt->{lib}, split m{/}, $rel);
1196 0 ▶ if (!-f $lib_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $lib_path) 0 0 0
1197 0 push @rows, { status => 'MISSING IN LIB', path => $rel };
1198 0 $bad = 1;
1199 0 next;
1200 }
1201 0 my $have = _slurp($lib_path);
1202 0 my $want = $rendered->{$rel};
1203 0 ▶ if ($have eq $want) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have eq $want) 0 0 0
1204 0 push @rows, { status => 'MATCH', path => $rel };
1205 0 next;
1206 }
1207 0 my ($ign, $reason) = cosmetic_differ_excepted($provider, $rel, $exceptions->{ignore_cosmetic_differs});
1208 0 ▶ if ($ign && _structural_signature($have) eq _structural_signature($want)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ign and _structural_signature($have) eq _structural_signature($want)) 0 0 0
Condition0%0/3
$ign and _structural_signature($have) eq _structural_signature($want)
!ll&&!rl&&r
000
MC/DC0%0/2
$ign and _structural_signature($have) eq _structural_signature($want)
$ign _structural_signature($have) eq _structural_signature($want)
Truth table0%0/3
A and B
A= $ignB= _structural_signature($have) eq _structural_signature($want)
ABresult
0X0
100
111
1209 # Listed AND the k8s declarations still match the render: the
1210 # difference is POD/ABSTRACT/whitespace only. Suppress it -- not a
1211 # failing differ, does not set $bad.
1212 0 push @rows, { status => 'COSMETIC', path => $rel, excepted => 1, reason => $reason };
1213 0 next;
1214 }
1215 # A genuine structural difference the maintainer chose to keep as a
1216 # documented, file-exact divergence (k133 Weg 1) -- distinct from a
1217 # cosmetic differ: it is NOT gated on _structural_signature (the whole
1218 # point is that the k8s declarations DO differ). Not a failing differ;
1219 # does not set $bad. The diff rides along so --verbose shows exactly
1220 # what is being accepted, and matching stays strictly provider+path so
1221 # no other file's drift can hide behind it.
1222 my ($accept, $accept_reason) =
1223 0 accept_divergence_excepted($provider, $rel, $exceptions->{accept_structural_divergence});
1224 0 ▶ if ($accept) {
Branch: 1 branch
BranchTrueFalseTotal
if ($accept) 0 0 0
1225 0 push @rows, { status => 'ACCEPTED DIVERGENCE', path => $rel,
1226 excepted => 1, reason => $accept_reason,
1227 diff => [ _diff_lines($have, $want) ] };
1228 0 next;
1229 }
1230 # A real DIFFERS. If it was listed as cosmetic yet the structural
1231 # content deviates, the guard refused it: flag the stale/wrong entry
1232 # loudly rather than swallowing the change (k132).
1233 0 my $row = { status => 'DIFFERS', path => $rel, diff => [ _diff_lines($have, $want) ] };
1234 0 ▶ $row->{guard_tripped} = 1 if $ign;
Branch: 1 branch
BranchTrueFalseTotal
if $ign 0 0 0
1235 0 push @rows, $row;
1236 0 $bad = 1;
1237 }
1238 0 for my $rel (sort keys %shipped) {
1239 0 my ($ign, $reason) = unrendered_excepted($provider, $rel, $exceptions->{ignore_unrendered});
1240 0 push @rows, { status => 'NOT RENDERED', path => $rel, excepted => $ign, reason => $reason };
1241 0 ▶ $bad = 1 unless $ign;
Branch: 1 branch
BranchTrueFalseTotal
unless $ign 0 0 0
1242 }
1243 0 return { provider => $provider, rows => \@rows, bad => $bad };
1244 }
1245  
1246 0 ▶sub render_check_report {
Subroutine render_check_report: not called CC=20 SCAR=60.4
1247 0 my ($c, $verbose) = @_;
1248 0 my @out;
1249 0 push @out, sprintf('########## %s --check (rendered vs lib/IO/K8s/%s) ##########', $c->{provider}, $c->{provider});
1250 0 my %count;
1251 0 for my $row (@{ $c->{rows} }) {
1252 0 $count{ $row->{status} }++;
1253 # Cosmetic and accepted-divergence (both suppressed) rows are listed
1254 # only under --verbose -- the same discipline render_provider uses for
1255 # exception-suppressed drift (k132/k133), so a routine --check stays
1256 # structurally readable.
1257 next if ($row->{status} eq 'COSMETIC'
1258 0 ▶ || $row->{status} eq 'ACCEPTED DIVERGENCE') && !$verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"status"} eq "COSMETIC" || $row->{"status"} eq "ACCEPTED DIVERGENCE" and not $verbose 0 0 0
Condition0%0/6
$row->{"status"} eq "COSMETIC" || $row->{"status"} eq "ACCEPTED DIVERGENCE" and not $verbose
!ll&&!rl&&r
000
$row->{"status"} eq "COSMETIC" or $row->{"status"} eq "ACCEPTED DIVERGENCE"
l!l&&r!l&&!r
000
MC/DC0%0/3
$row->{"status"} eq "COSMETIC" || $row->{"status"} eq "ACCEPTED DIVERGENCE" and not $verbose
$row->{"status"} eq "COSMETIC" $row->{"status"} eq "ACCEPTED DIVERGENCE" not $verbose
Truth table0%0/5
A or B and not(C)
A= $row->{"status"} eq "COSMETIC"B= $row->{"status"} eq "ACCEPTED DIVERGENCE"C= not $verbose
ABCresult
00X0
1X00
0100
1X11
0111
1259 0 my $line = sprintf(' %-16s %s', $row->{status}, $row->{path});
1260 0 ▶ $line .= ' -- ' . $row->{reason} if $row->{excepted} && defined $row->{reason};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"excepted"} and defined $row->{"reason"} 0 0 0
Condition0%0/3
$row->{"excepted"} and defined $row->{"reason"}
!ll&&!rl&&r
000
MC/DC0%0/2
$row->{"excepted"} and defined $row->{"reason"}
$row->{"excepted"} defined $row->{"reason"}
Truth table0%0/3
A and B
A= $row->{"excepted"}B= defined $row->{"reason"}
ABresult
0X0
100
111
1261 $line .= ' -- LISTED cosmetic but structural content deviates; guard kept it as a real DIFFERS'
1262 0 ▶ if $row->{guard_tripped};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"guard_tripped"} 0 0 0
1263 0 push @out, $line;
1264 0 ▶ push @out, " $_" for @{ $row->{diff} // [] };
Condition0%0/2
$row->{"diff"} // []
l!l
00
MC/DC0%0/1
$row->{"diff"} // []
$row->{"diff"}
Truth table0%0/2
A
A= $row->{"diff"}
Aresult
11
00
1265 }
1266 0 ▶ my $cosmetic = $count{COSMETIC} // 0;
Condition0%0/2
$count{"COSMETIC"} // 0
l!l
00
MC/DC0%0/1
$count{"COSMETIC"} // 0
$count{"COSMETIC"}
Truth table0%0/2
A
A= $count{"COSMETIC"}
Aresult
11
00
1267 0 ▶ my $accepted = $count{'ACCEPTED DIVERGENCE'} // 0;
Condition0%0/2
$count{"ACCEPTED DIVERGENCE"} // 0
l!l
00
MC/DC0%0/1
$count{"ACCEPTED DIVERGENCE"} // 0
$count{"ACCEPTED DIVERGENCE"}
Truth table0%0/2
A
A= $count{"ACCEPTED DIVERGENCE"}
Aresult
11
00
1268 push @out, sprintf(
1269 '--- SUMMARY: %d match, %d differ, %d cosmetic (suppressed), %d accepted divergence (suppressed), %d missing in lib, %d not rendered ---',
1270 $count{MATCH} // 0, $count{DIFFERS} // 0, $cosmetic, $accepted,
1271 0 ▶ $count{'MISSING IN LIB'} // 0, $count{'NOT RENDERED'} // 0,
Condition0%0/8
$count{"MATCH"} // 0
l!l
00
$count{"DIFFERS"} // 0
l!l
00
$count{"MISSING IN LIB"} // 0
l!l
00
$count{"NOT RENDERED"} // 0
l!l
00
MC/DC0%0/1
$count{"MATCH"} // 0
$count{"MATCH"}
MC/DC0%0/1
$count{"DIFFERS"} // 0
$count{"DIFFERS"}
MC/DC0%0/1
$count{"MISSING IN LIB"} // 0
$count{"MISSING IN LIB"}
MC/DC0%0/1
$count{"NOT RENDERED"} // 0
$count{"NOT RENDERED"}
Truth table0%0/2
A
A= $count{"MATCH"}
Aresult
11
00
Truth table0%0/2
A
A= $count{"DIFFERS"}
Aresult
11
00
Truth table0%0/2
A
A= $count{"MISSING IN LIB"}
Aresult
11
00
Truth table0%0/2
A
A= $count{"NOT RENDERED"}
Aresult
11
00
1272 );
1273 0 ▶ push @out, ' (cosmetic differs suppressed by ignore_cosmetic_differs -- rerun with --verbose to list them)'
Branch: 1 branch
BranchTrueFalseTotal
if $cosmetic and not $verbose 0 0 0
Condition0%0/3
$cosmetic and not $verbose
!ll&&!rl&&r
000
MC/DC0%0/2
$cosmetic and not $verbose
$cosmetic not $verbose
Truth table0%0/3
A and not(B)
A= $cosmeticB= not $verbose
ABresult
0X0
100
111
1274 if $cosmetic && !$verbose;
1275 0 ▶ push @out, ' (accepted divergences suppressed by accept_structural_divergence -- rerun with --verbose to list them)'
Branch: 1 branch
BranchTrueFalseTotal
if $accepted and not $verbose 0 0 0
Condition0%0/3
$accepted and not $verbose
!ll&&!rl&&r
000
MC/DC0%0/2
$accepted and not $verbose
$accepted not $verbose
Truth table0%0/3
A and not(B)
A= $acceptedB= not $verbose
ABresult
0X0
100
111
1276 if $accepted && !$verbose;
1277 0 push @out, '';
1278 0 return join("\n", @out) . "\n";
1279 }
1280  
1281 0 ▶sub render_check_unresolved {
Subroutine render_check_unresolved: not called CC=1 SCAR=6.9
1282 0 my ($provider) = @_;
1283 0 return "########## $provider --check ##########\n"
1284 . " skipped: upstream source unresolved -- no openAPIV3Schema to render against\n\n";
1285 }
1286  
1287 # ---------------------------------------------------------------------------
1288 # main
1289 # ---------------------------------------------------------------------------
1290  
1291 11 my $opt = parse_args();
1292 11 load_lib($opt->{lib});
1293 11 my $exceptions = load_exceptions($opt->{exceptions});
1294 # Loaded once, up front: render_gvk reads it for every --suggest/--render
1295 # GVK regardless of which of those flags (or neither) is actually set.
1296 11 $opt->{_names_map} = load_names($opt->{names});
1297  
1298 11 my @results = map { check_provider($opt, $_, $exceptions) } @{ $opt->{provider} };
1299  
1300 # check_provider embeds the parsed upstream view as _upstream purely to get
1301 # it out to --suggest below. Pull it into its own map and strip it off
1302 # @results right away, unconditionally -- not only for --format json -- so
1303 # neither render_report nor the JSON encode ever has to know it was there,
1304 # and so --suggest still has it regardless of --format.
1305 11 my %upstream_by_provider;
1306 11 for my $r (@results) {
1307 11 ▶ $upstream_by_provider{ $r->{provider} } = delete $r->{_upstream} if $r->{_upstream};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"_upstream"} 11 0 11
1308 }
1309  
1310 11 my $report;
1311 11 ▶if ($opt->{format} eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'format'} eq 'json') { } 7 4 11
1312 7 $report = JSON::PP->new->canonical->pretty->encode({ providers => \@results });
1313 } else {
1314 4 $report = render_report(\@results, $opt->{verbose});
1315 }
1316  
1317 11 print $report;
1318 11 ▶if ($opt->{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"output"}) 0 11 11
1319 open my $fh, '>:encoding(UTF-8)', $opt->{output}
1320 0 ▶ or die "crd-drift-check: cannot write $opt->{output}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(UTF-8)", $opt->{"output"} or die qq[crd-drift-check: cannot write $opt->{"output"}: $!\n] 0 0 0
1321 0 print $fh $report;
1322 0 close $fh;
1323 }
1324  
1325 11 ▶if ($opt->{suggest} || $opt->{suggest_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"suggest"} or $opt->{"suggest_dir"}) 3 8 11
Condition66%2/3
$opt->{"suggest"} or $opt->{"suggest_dir"}
l!l&&r!l&&!r
308
MC/DC50%1/2
$opt->{"suggest"} or $opt->{"suggest_dir"}
$opt->{"suggest"} $opt->{"suggest_dir"}
Truth table66%2/3
A or B
A= $opt->{"suggest"}B= $opt->{"suggest_dir"}
ABresult
1X1
011
000
1326 3 my %files;
1327 3 for my $r (@results) {
1328 3 ▶ my $upstream = $upstream_by_provider{ $r->{provider} } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $upstream = $upstream_by_provider{$r->{"provider"}} or next 3 0 3
1329 3 my $f = suggest_for($opt, $r, $upstream);
1330 2 $files{$_} = $f->{$_} for keys %$f;
1331 }
1332 2 emit_files($opt, \%files, $opt->{suggest_dir}, 'suggest');
1333 }
1334  
1335 # --render and --check both need the full render (every served GVK, not
1336 # only a reported gap) -- computed once per provider here and reused by
1337 # whichever of the two flags is set, rather than rendering twice.
1338 10 my %rendered_by_provider;
1339 10 ▶if ($opt->{render} || $opt->{render_dir} || $opt->{check}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"render"} or $opt->{"render_dir"} or $opt->{"check"}) 2 8 10
Condition66%4/6
$opt->{"render"} or $opt->{"render_dir"} or $opt->{"check"}
l!l&&r!l&&!r
208
$opt->{"render"} or $opt->{"render_dir"}
l!l&&r!l&&!r
208
MC/DC33%1/3
$opt->{"render"} or $opt->{"render_dir"} or $opt->{"check"}
$opt->{"render"} $opt->{"render_dir"} $opt->{"check"}
Truth table50%2/4
A or B or C
A= $opt->{"render"}B= $opt->{"render_dir"}C= $opt->{"check"}
ABCresult
1XX1
01X1
0011
0000
1340 2 for my $r (@results) {
1341 2 ▶ my $upstream = $upstream_by_provider{ $r->{provider} } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $upstream = $upstream_by_provider{$r->{"provider"}} or next 2 0 2
1342 2 $rendered_by_provider{ $r->{provider} } = render_for($opt, $r->{provider}, $upstream);
1343 }
1344 }
1345  
1346 9 ▶if ($opt->{render} || $opt->{render_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"render"} or $opt->{"render_dir"}) 1 8 9
Condition66%2/3
$opt->{"render"} or $opt->{"render_dir"}
l!l&&r!l&&!r
108
MC/DC50%1/2
$opt->{"render"} or $opt->{"render_dir"}
$opt->{"render"} $opt->{"render_dir"}
Truth table66%2/3
A or B
A= $opt->{"render"}B= $opt->{"render_dir"}
ABresult
1X1
011
000
1347 1 my %files;
1348 1 for my $provider (keys %rendered_by_provider) {
1349 1 my $f = $rendered_by_provider{$provider};
1350 1 $files{$_} = $f->{$_} for keys %$f;
1351 }
1352 1 emit_files($opt, \%files, $opt->{render_dir}, 'render');
1353 }
1354  
1355 9 my $check_failed = 0;
1356 9 ▶if ($opt->{check}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"check"}) 0 9 9
1357 0 my $out = _report_out($opt);
1358 0 for my $r (@results) {
1359 0 ▶ if (!$upstream_by_provider{ $r->{provider} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($upstream_by_provider{$r->{"provider"}}) 0 0 0
1360 0 print $out render_check_unresolved($r->{provider});
1361 0 next;
1362 }
1363 0 my $c = check_for($opt, $r->{provider}, $rendered_by_provider{ $r->{provider} }, $exceptions);
1364 0 ▶ $check_failed = 1 if $c->{bad};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"bad"} 0 0 0
1365 0 print $out render_check_report($c, $opt->{verbose});
1366 }
1367 }
1368  
1369 9 ▶exit(1) if $check_failed;
Branch: 1 branch
BranchTrueFalseTotal
if $check_failed 0 9 9
« maint/crd-schema-audit.pl ↑ Summary blib/lib/IO/K8s/Role/Resource.pm »