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.009';
4 8     8   1292 use v5.10;
  8         30  
5 8     8   39 use strict;
  8         16  
  8         181  
6 8     8   34 use warnings;
  8         13  
  8         533  
7 8     8   38 use Moo;
  8         30  
  8         68  
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 59 my ($self, @objs) = @_;
18 20         37 push @{$self->_items}, @objs;
  20         87  
19 20         387 return $self;
20             }
21              
22             # Get all items
23             sub items {
24 7     7 0 17 my $self = shift;
25 7         17 return @{$self->_items};
  7         151  
26             }
27              
28             # Load .pk8s file - called from IO::K8s->load
29             sub _load_file {
30 7     7   22 my ($class, $file, $k8s) = @_;
31              
32             # Read file content
33 7 50       333 open my $fh, '<', $file or die "Cannot open $file: $!";
34 7         21 my $content = do { local $/; <$fh> };
  7         37  
  7         213  
35 7         92 close $fh;
36              
37             # Create manifest collector
38 7         65 my $m = $class->new;
39              
40             {
41 7         17 local $_collector = $m;
  7         17  
42              
43             # Build the DSL code with functions for all resource types
44 7         24 my $dsl_code = _build_dsl_code($k8s);
45              
46             # Eval the file content with DSL available
47 7         85 my $pkg = "IO::K8s::Manifest::_LOADER_$$" . "_" . int(rand(100000));
48 7         485 my $eval_code = qq{
49             package $pkg;
50             use strict;
51             use warnings;
52             $dsl_code
53             $content
54             };
55              
56 7 100   10   10369 eval $eval_code;
  10 50   8   85  
  10 50   1   23  
  10 100   1   301  
  10 50   0   49  
  19 50   1   48  
  13 100   1   62910  
  3 50   0   9  
  3 50   1   28  
  3 100   0   270  
  3 50   0   21  
  1 50   0   3  
  1 0   0   3  
  1 0   0   2  
  1 0   0   4  
  4 100   0   31  
  2 50   0   8  
  1 50   0   3  
  1 0   1   9  
  1 0   1   127  
  1 100   2   5  
  1 50   1   3  
  1 50   0   2  
  1 0   0   2  
  1 0   0   4  
  4 0   0   18  
  1 100   0   5  
  1 50   1   3  
  1 50   0   35  
  1 0   2   156  
  1 0   0   6  
  1 0   1   3  
  1 0   0   3  
  1 0   1   4  
  1 0   0   3  
  4 0   0   15  
  2 0   0   6  
  1 0   0   23  
  1 0   0   10  
  1 0   0   2395  
  1 0   0   12  
  0 0   0   0  
  0 0   0   0  
  0 0   0   0  
  0 0   0   0  
  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   4  
  1 0   0   4  
  4 0   0   12  
  1 100   1   4  
  1 100   0   2  
  1 100   0   8  
  1 50   0   3905  
  1 100       23  
  0 50       0  
  0 100       0  
  0 50       0  
  1 50       3  
  1 100       2  
  1 50       2  
  1 50       3  
  4 0       8  
  1 0       17  
  1 0       2  
  1 0       19  
  1 0       69  
  1 0       3  
  0 0       0  
  0 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       2  
  1 50       10  
  1 0       3  
  1 0       3  
  4 0       10  
  2 100       4  
  1 100       2  
  1 50       7  
  1 0       4204  
  1 0       7  
  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         2  
  1         2  
  1         2  
  1         4  
  4         11  
  2         4  
  1         22  
  2         29  
  2         112  
  2         9  
  1         5  
  4         14  
  2         8  
  1         3  
  1         13  
  1         154  
  1         6  
  1         2  
  1         2  
  1         4  
  2         7  
  5         15  
  2         8  
  2         10  
  5         17  
  3         135  
  2         11  
  1         12  
  1         88  
  1         3  
  1         2  
  1         2  
  1         3  
  1         3  
  4         9  
  2         5  
  1         3  
  1         5  
  1         2357  
  1         3  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  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         4  
  1         4  
  4         10  
  2         8  
  1         3  
  1         18  
  1         118  
  1         31  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  2         4  
  2         4  
  2         5  
  2         20  
  8         17  
  4         8  
  2         4  
  2         8  
  2         159  
  2         20  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  1         2  
  1         2  
  1         4  
  1         3  
  4         20  
  2         5  
  1         1  
  1         5  
  1         82  
  1         11  
  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         4  
  4         14  
  2         7  
  1         3  
  1         28  
  1         3418  
  1         31  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  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         2  
  1         3  
  1         4  
  4         13  
  2         7  
  1         2  
  1         8  
  1         3588  
  1         18  
  0            
  0            
  0            
  0            
  0            
  0            
  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       45 die "Error loading $file: $@" if $@;
58             }
59              
60 7         35 return [ $m->items ];
61             }
62              
63             # Build DSL code with resource functions
64             sub _build_dsl_code {
65 7     7   17 my ($k8s) = @_;
66              
67 7         14 my $code = '';
68              
69             # Get all resource types from the k8s instance
70 7         219 my $map = $k8s->resource_map;
71              
72 7         172 for my $kind (keys %$map) {
73             # Skip domain-qualified names (contain /) - not valid Perl identifiers
74 518 100       891 next if $kind =~ m{/};
75              
76 468         1244 $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         601 return $code;
103             }
104              
105             # K8s instance for DSL functions (set during load)
106             our $_k8s_instance;
107              
108             1;
109              
110             __END__