blib/lib/Kubernetes/Comb/CRD/CombSpec.pm

filter: Statementstmt 100% 30 / 30 Branchbran 100% 10 / 10 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 96.6% 57 / 59 CC 10 SCAR 16.9
CC
10
Cov
98%
FROM_STRUCT
8.0
__ANON__
3.0
BEGIN
1.0
CRAP 5.4
« blib/lib/Kubernetes/Comb/CRD/Comb.pm ↑ Summary blib/lib/Kubernetes/Comb/Upstream/K8s.pm »
1 package Kubernetes::Comb::CRD::CombSpec;
2 # ABSTRACT: Spec of the Comb custom resource
3 27 our $VERSION = '0.001';
4  
5 27 ▶use IO::K8s::Resource;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 27 ▶use IO::K8s::Types qw( Opaque );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 27 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 27 k8s class => Str, {
11 required => 1,
12 description => 'Perl class of the Comb; a stub is just another class'
13 };
14  
15  
16 27 k8s dependsOn => [Str], {
17 description => 'Combs this one needs, as "name" or "namespace/name"'
18 };
19  
20  
21 27 k8s config => Opaque, {
22 preserve_unknown => 1,
23 description => 'Free-form configuration for the class'
24 };
25  
26  
27 27 k8s enabled => Bool, {
28 description => 'Unset: automatic, false: off, true: on'
29 };
30  
31  
32 27 k8s upstream => Opaque, {
33 nullable => 1,
34 preserve_unknown => 1,
35 description => 'Where the Comb borrows its service from: class plus'
36 .' upstream-specific keys; null means local'
37 };
38  
39 27 has '+upstream' => ( predicate => 1 );
40  
41  
42 723 ▶sub FROM_STRUCT {
Subroutine FROM_STRUCT: called CC=8 SCAR=20.8
Pod documented
43 723 my ( $class, $struct ) = @_;
44 723 ▶ croak $class.'->FROM_STRUCT needs a hashref, got '.( ref $struct || 'a plain scalar' )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $struct eq "HASH" 1 722 723
Condition50%1/2
ref $struct || 'a plain scalar'
l!l
10
MC/DC0%0/1
ref $struct || 'a plain scalar'
ref $struct
Truth table50%1/2
A
A= ref $struct
Aresult
11
00
45 unless ref $struct eq 'HASH';
46 722 my %args;
47 722 for my $key ( keys %$struct ) {
48 1153 my $value = $struct->{$key};
49 1153 ▶ next unless defined $value || $key eq 'upstream';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value or $key eq "upstream" 2 1151 1153
Condition100%3/3
defined $value or $key eq "upstream"
l!l&&r!l&&!r
1095562
MC/DC100%2/2
defined $value or $key eq "upstream"
defined $value $key eq "upstream"
Truth table100%3/3
A or B
A= defined $valueB= $key eq "upstream"
ABresult
1X1
011
000
50 1151 ▶ $args{$key} = ref $value eq 'HASH' ? { %$value }
Branch: 2 branches
BranchTrueFalseTotal
ref $value eq 'HASH' ? : 216 935 1151
ref $value eq 'ARRAY' ? : 95 840 935
51 : ref $value eq 'ARRAY' ? [ @$value ]
52 : $value;
53 }
54 722 return $class->new(%args);
55 }
56  
57  
58 625 ▶around TO_JSON => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
59 625 my ( $orig, $self, @args ) = @_;
60 625 my $data = $self->$orig(@args);
61 625 ▶ $data->{upstream} = undef if $self->has_upstream && !defined $self->upstream;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_upstream and not defined $self->upstream 46 579 625
Condition100%3/3
$self->has_upstream and not defined $self->upstream
!ll&&!rl&&r
42315646
MC/DC100%2/2
$self->has_upstream and not defined $self->upstream
$self->has_upstream not defined $self->upstream
Truth table100%3/3
A and not(B)
A= $self->has_upstreamB= not defined $self->upstream
ABresult
0X0
100
111
62 625 return $data;
63 27 };
64  
65  
66 27 1;
67  
68 __END__
69  
70 =pod
71  
72 =encoding UTF-8
73  
74 =head1 NAME
75  
76 Kubernetes::Comb::CRD::CombSpec - Spec of the Comb custom resource
77  
78 =head1 VERSION
79  
80 version 0.001
81  
82 =head1 SYNOPSIS
83  
84 my $spec = Kubernetes::Comb::CRD::CombSpec->new(
85 class => 'MyApp::Comb::NATS',
86 dependsOn => [ 'db' ],
87 config => { cluster_size => 3 },
88 upstream => {
89 class => 'Kubernetes::Comb::Upstream::K8s',
90 context => 'dev'
91 }
92 );
93  
94 # explicit "local": the key exists, its value is null
95 my $local = Kubernetes::Comb::CRD::CombSpec->new(
96 class => 'MyApp::Comb::NATS',
97 upstream => undef
98 );
99 $local->has_upstream; # true
100 $local->upstream; # undef
101  
102 =head1 DESCRIPTION
103  
104 The C<spec> of a L<Kubernetes::Comb::CRD::Comb>, an L<IO::K8s> class like any
105 other with one addition: C<upstream> keeps the difference between a key that
106 is absent and a key that is present with C<null>. The upstream is resolved
107 from the first source that I<exists>, and an explicit C<upstream: null> in the
108 custom resource is the answer "local" that ends the lookup.
109  
110 L<IO::K8s> drops a JSON C<null> on the way in and never writes one on the way
111 out -- C<nullable> is a schema-only option there. So this class inflates
112 itself through C<FROM_STRUCT>, the hook L<IO::K8s/struct_to_object> documents
113 for classes the generic path would lose data of, and adds the C<null> back in
114 C<TO_JSON>. The schema marks C<upstream> C<nullable>, so the API server keeps
115 the C<null> too.
116  
117 =head2 class
118  
119 Required. The Perl class that implements the Comb. Pointing it at a stub class
120 selects the stub.
121  
122 =head2 dependsOn
123  
124 ArrayRef of the Combs this one depends on, each C<name> or C<namespace/name>.
125  
126 =head2 config
127  
128 Free-form hashref for the Comb class. Never credentials.
129  
130 =head2 enabled
131  
132 Tri-state: C<undef> is automatic, false switches the Comb off, true on.
133  
134 =head2 upstream
135  
136 Hashref naming the upstream: C<class>, always the fully qualified class name,
137 plus the keys that class takes -- for L<Kubernetes::Comb::Upstream::K8s>
138 C<context>, C<namespace> and C<name>. C<undef> while L</has_upstream> is true
139 is the explicit "local".
140  
141 =head2 has_upstream
142  
143 True when the spec carries an C<upstream> key at all, an explicit C<null>
144 included. This, not the truth of L</upstream>, says whether the custom
145 resource has a say in the upstream resolution.
146  
147 =head2 FROM_STRUCT
148  
149 my $spec = Kubernetes::Comb::CRD::CombSpec->FROM_STRUCT($hashref);
150  
151 Called by L<IO::K8s> whenever a C<CombSpec> is built from a plain structure.
152 Behaves like the generic inflation -- a C<null> field counts as absent,
153 containers are copied one level -- except that C<upstream: null> is kept.
154 Croaks on anything but a hashref.
155  
156 =head2 TO_JSON
157  
158 The inherited serialization, plus C<< upstream => undef >> (JSON C<null>) when
159 the spec holds the explicit "local".
160  
161 =head1 SEE ALSO
162  
163 =over
164  
165 =item * L<Kubernetes::Comb::CRD::Comb>
166  
167 =back
168  
169 =head1 SUPPORT
170  
171 =head2 Issues
172  
173 Please report bugs and feature requests on GitHub at
174 L<https://github.com/Getty/p5-kubernetes-comb/issues>.
175  
176 =head2 IRC
177  
178 Join C<#kubernetes> on C<irc.perl.org> or message Getty directly.
179  
180 =head1 CONTRIBUTING
181  
182 Contributions are welcome! Please fork the repository and submit a pull request.
183  
184 =head1 AUTHOR
185  
186 Torsten Raudssus <getty@cpan.org>
187  
188 =head1 COPYRIGHT AND LICENSE
189  
190 This software is copyright (c) 2026 by Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>.
191  
192 This is free software; you can redistribute it and/or modify it under
193 the same terms as the Perl 5 programming language system itself.
194  
195 =cut
« blib/lib/Kubernetes/Comb/CRD/Comb.pm ↑ Summary blib/lib/Kubernetes/Comb/Upstream/K8s.pm »