File Coverage

blib/lib/IO/K8s/Manifest.pm
Criterion Covered Total %
statement 200 643 31.1
branch 71 370 19.1
condition n/a
subroutine 24 68 35.2
pod 0 2 0.0
total 295 1083 27.2


line stmt bran cond sub pod time code
1             package IO::K8s::Manifest;
2             # ABSTRACT: Internal collector for loading .pk8s manifest files
3             our $VERSION = '1.008';
4 8     8   1342 use v5.10;
  8         36  
5 8     8   296 use strict;
  8         35  
  8         282  
6 8     8   53 use warnings;
  8         16  
  8         625  
7 8     8   55 use Moo;
  8         31  
  8         89  
8              
9             # Current collector during evaluation
10             our $_collector;
11              
12             # Items collected in this manifest
13             has '_items' => (is => 'ro', default => sub { [] });
14              
15             # Add resources to manifest
16             sub add {
17 20     20 0 71 my ($self, @objs) = @_;
18 20         41 push @{$self->_items}, @objs;
  20         127  
19 20         573 return $self;
20             }
21              
22             # Get all items
23             sub items {
24 7     7 0 21 my $self = shift;
25 7         16 return @{$self->_items};
  7         164  
26             }
27              
28             # Load .pk8s file - called from IO::K8s->load
29             sub _load_file {
30 7     7   21 my ($class, $file, $k8s) = @_;
31              
32             # Read file content
33 7 50       1237 open my $fh, '<', $file or die "Cannot open $file: $!";
34 7         31 my $content = do { local $/; <$fh> };
  7         46  
  7         265  
35 7         91 close $fh;
36              
37             # Create manifest collector
38 7         77 my $m = $class->new;
39              
40             {
41 7         19 local $_collector = $m;
  7         18  
42              
43             # Build the DSL code with functions for all resource types
44 7         25 my $dsl_code = _build_dsl_code($k8s);
45              
46             # Eval the file content with DSL available
47 7         102 my $pkg = "IO::K8s::Manifest::_LOADER_$$" . "_" . int(rand(100000));
48 7         588 my $eval_code = qq{
49             package $pkg;
50             use strict;
51             use warnings;
52             $dsl_code
53             $content
54             };
55              
56 7 100   10   1632 eval $eval_code;
  10 50   8   73  
  10 50   1   26  
  10 100   1   257  
  10 50   0   48  
  19 50   1   55  
  13 100   1   79589  
  3 50   0   10  
  3 50   1   33  
  3 100   0   352  
  3 50   0   28  
  1 50   0   3  
  1 0   0   3  
  1 0   0   3  
  1 0   0   4  
  4 100   0   12  
  2 50   0   7  
  1 50   0   2  
  1 0   1   5  
  1 0   1   125  
  1 100   2   6  
  1 50   1   2  
  1 50   0   4  
  1 0   0   4  
  1 0   0   4  
  4 0   0   14  
  1 100   0   10  
  1 50   1   3  
  1 50   0   30  
  1 0   2   152  
  1 0   0   5  
  1 0   1   2  
  1 0   0   4  
  1 0   1   4  
  1 0   0   4  
  4 0   0   10  
  2 0   0   8  
  1 0   0   3  
  1 0   0   7  
  1 0   0   3658  
  1 0   0   21  
  0 0   0   0  
  0 0   0   0  
  0 0   0   0  
  0 0   0   0  
  0 0   0   0  
  0 0   0   0  
  0 0   0   0  
  0 0   0   0  
  0 0   0   0  
  0 0   0   0  
  1 0   0   3  
  1 0   0   2  
  1 0   0   5  
  1 0   0   3  
  4 0   0   12  
  1 100   1   4  
  1 100   0   16  
  1 100   0   8  
  1 50   0   3839  
  1 100       20  
  0 50       0  
  0 100       0  
  0 50       0  
  1 50       4  
  1 100       3  
  1 50       4  
  1 50       5  
  4 0       12  
  1 0       4  
  1 0       29  
  1 0       12  
  1 0       140  
  1 0       4  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 100       0  
  1 50       3  
  1 50       14  
  1 0       3  
  1 0       5  
  4 0       13  
  2 100       8  
  1 100       3  
  1 50       9  
  1 0       3993  
  1 0       12  
  0 0       0  
  0 100       0  
  0 50       0  
  0 50       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 100       0  
  0 50       0  
  0 50       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 100       0  
  0 50       0  
  0 50       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0 0       0  
  0         0  
  0         0  
  1         4  
  1         4  
  1         5  
  1         4  
  4         15  
  2         7  
  1         3  
  2         14  
  2         128  
  2         10  
  1         5  
  4         15  
  2         8  
  1         4  
  1         13  
  1         137  
  1         6  
  1         3  
  1         4  
  1         3  
  2         7  
  5         17  
  2         8  
  2         7  
  5         21  
  3         131  
  2         8  
  1         11  
  1         122  
  1         5  
  1         2  
  1         2  
  1         4  
  1         3  
  4         12  
  2         6  
  1         3  
  1         6  
  1         3392  
  1         5  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  1         3  
  1         3  
  1         4  
  1         4  
  4         12  
  2         7  
  1         2  
  1         14  
  1         143  
  1         5  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  2         5  
  2         5  
  2         7  
  2         8  
  8         27  
  4         41  
  2         7  
  2         31  
  2         267  
  2         32  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  1         3  
  1         2  
  1         3  
  1         3  
  4         10  
  2         6  
  1         2  
  1         23  
  1         151  
  1         17  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  1         3  
  1         3  
  1         3  
  1         4  
  4         14  
  2         7  
  1         3  
  1         8  
  1         3402  
  1         28  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  1         3  
  1         2  
  1         3  
  1         3  
  4         11  
  2         7  
  1         2  
  1         21  
  1         3498  
  1         25  
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
  0            
57 7 50       55 die "Error loading $file: $@" if $@;
58             }
59              
60 7         42 return [ $m->items ];
61             }
62              
63             # Build DSL code with resource functions
64             sub _build_dsl_code {
65 7     7   20 my ($k8s) = @_;
66              
67 7         18 my $code = '';
68              
69             # Get all resource types from the k8s instance
70 7         203 my $map = $k8s->resource_map;
71              
72 7         200 for my $kind (keys %$map) {
73             # Skip domain-qualified names (contain /) - not valid Perl identifiers
74 518 100       1131 next if $kind =~ m{/};
75              
76 468         1585 $code .= qq{
77             sub $kind (&@) {
78             my \$block = shift;
79             my \$api_version = shift;
80             my \%args = \$block->();
81              
82             # Convenience: move name/namespace/labels/annotations to metadata
83             for my \$key (qw(name namespace labels annotations)) {
84             if (exists \$args{\$key}) {
85             \$args{metadata}{\$key} = delete \$args{\$key};
86             }
87             }
88              
89             my \$k8s = \$IO::K8s::Manifest::_k8s_instance;
90             my \$obj = \$api_version
91             ? \$k8s->new_object('$kind', \\\%args, \$api_version)
92             : \$k8s->new_object('$kind', \\\%args);
93              
94             \$IO::K8s::Manifest::_collector->add(\$obj)
95             if \$IO::K8s::Manifest::_collector;
96              
97             return \$obj;
98             }
99             };
100             }
101              
102 7         697 return $code;
103             }
104              
105             # K8s instance for DSL functions (set during load)
106             our $_k8s_instance;
107              
108             1;
109              
110             __END__