blib/lib/Kubernetes/REST/CLI/Crd2Class.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 173 CC 10 SCAR 24.8
CC
10
Cov
0%
run
30.0
_crds_from_cluster
12.0
_namespace_for
12.0
CRAP 11.9
1 package Kubernetes::REST::CLI::Crd2Class;
2 our $VERSION = '1.109';
3 # ABSTRACT: Generate IO::K8s classes from a CustomResourceDefinition
4 use Moo;
5 with 'Kubernetes::REST::CLI::Role::Connection';
6 use MooX::Options;
7 use Carp qw( croak );
8 use Path::Tiny qw( path );
9 use IO::K8s::CRD ();
10 use IO::K8s::CRD::Emitter ();
11  
12  
13 option file => (
14 is => 'ro',
15 format => 's',
16 short => 'f',
17 doc => 'Path to a CustomResourceDefinition YAML/JSON file',
18 );
19  
20  
21 option namespace => (
22 is => 'ro',
23 format => 's',
24 short => 'n',
25 doc => 'Base package for the generated classes (default: derived from the CRD group)',
26 );
27  
28  
29 option output_dir => (
30 is => 'ro',
31 format => 's',
32 short => 'd',
33 doc => 'Write the .pm files under this directory instead of printing them',
34 );
35  
36  
37 option from_cluster => (
38 is => 'ro',
39 format => 's',
40 doc => 'Generate from a Kind whose CRD a live cluster already serves',
41 );
42  
43  
44  
45 sub run {
46 my ($self) = @_;
47  
48 my $from_cluster = $self->from_cluster;
49 my $file = $self->file;
50  
51 my $crds;
52 if (defined $from_cluster && length $from_cluster) {
53 $crds = $self->_crds_from_cluster($from_cluster);
54 }
55 elsif (defined $file && length $file) {
56 croak "kube_crd2class: no such file: $file\n" unless -f $file;
57 $crds = IO::K8s::CRD->load($file);
58 }
59 else {
60 croak "kube_crd2class: no input given; pass --file/-f <crd.yaml> "
61 . "or --from-cluster <Kind>\n";
62 }
63  
64 binmode STDOUT, ':encoding(UTF-8)';
65  
66 my $files = $self->_render_crds($crds);
67  
68 if (defined $self->output_dir && length $self->output_dir) {
69 $self->_write_files($files);
70 }
71 else {
72 $self->_print_files($files);
73 }
74 return 0;
75 }
76  
77 # The CRD hashref(s) for one Kind served by the cluster, in the shape
78 # IO::K8s::CRD->load returns (so _render_crds drives the same pipeline as -f):
79 # list the cluster's CustomResourceDefinitions through the shared api, keep the
80 # one whose spec.names.kind matches, and normalize it through ->load - which
81 # takes a CustomResourceDefinition object straight, via its TO_JSON. A Kind
82 # served by no CRD, or by several (same kind in different groups), dies clearly.
83 sub _crds_from_cluster {
84 my ($self, $kind) = @_;
85  
86 my $list = $self->api->list('CustomResourceDefinition');
87 my @matches = grep {
88 my $names = $_->spec && $_->spec->names;
89 $names && defined $names->kind && $names->kind eq $kind;
90 } @{ $list->items };
91  
92 croak "kube_crd2class: no CustomResourceDefinition on the cluster serves "
93 . "kind '$kind'\n" unless @matches;
94 croak "kube_crd2class: kind '$kind' is served by more than one "
95 . "CustomResourceDefinition (" . join(', ', map { $_->metadata->name } @matches)
96 . "); nothing here can pick between them\n" if @matches > 1;
97  
98 return IO::K8s::CRD->load($matches[0]);
99 }
100  
101 # An arrayref of [ relative_path, api_version, source ] blocks for every
102 # served version of every loaded CRD, in manifest order - the whole
103 # schema-to-DSL job delegated to IO::K8s (load/served_versions/generate/render).
104 sub _render_crds {
105 my ($self, $crds) = @_;
106 my @out;
107 for my $crd (@$crds) {
108 my $ns = $self->_namespace_for($crd);
109 my $versions = IO::K8s::CRD->served_versions($crd);
110 # reuse_core on (D5) - a nested schema shaped like a shipped core
111 # class is typed as that class rather than a per-CRD copy.
112 my $classes = IO::K8s::CRD->generate($crd, $ns, reuse_core => 1);
113 for my $v (@$versions) {
114 my $root = $classes->{ $v->{api_version} } or next;
115 my $emitter = IO::K8s::CRD::Emitter->new(
116 base => $ns . '::' . ucfirst($v->{name}),
117 );
118 my $rendered = $emitter->render($root);
119 push @out, map { [ $_, $v->{api_version}, $rendered->{$_} ] }
120 sort keys %$rendered;
121 }
122 }
123 return \@out;
124 }
125  
126 # The base package for one CRD: --namespace when given, otherwise
127 # IO::K8s::<CamelCase leading DNS label of the group>.
128 sub _namespace_for {
129 my ($self, $crd) = @_;
130 return $self->namespace if defined $self->namespace && length $self->namespace;
131 my $group = $crd->{spec}{group} // '';
132 my ($label) = split /\./, $group;
133 $label = 'Crd' unless defined $label && length $label;
134 my $camel = join '', map { ucfirst } split /-/, $label;
135 return 'IO::K8s::' . $camel;
136 }
137  
138 sub _print_files {
139 my ($self, $files) = @_;
140 my @blocks = @$files;
141 for my $i (0 .. $#blocks) {
142 my ($path, $api_version, $source) = @{ $blocks[$i] };
143 print "\n" if $i;
144 print '# ', ('=' x 74), "\n";
145 print "# $path ($api_version)\n";
146 print '# ', ('=' x 74), "\n";
147 print $source;
148 }
149 return;
150 }
151  
152 sub _write_files {
153 my ($self, $files) = @_;
154 my $dir = path($self->output_dir);
155 for my $block (@$files) {
156 my ($path, undef, $source) = @$block;
157 my $target = $dir->child($path);
158 $target->parent->mkpath;
159 $target->spew_utf8($source);
160 print STDERR "wrote $target\n";
161 }
162 return;
163 }
164  
165 1;
166  
167 __END__
168  
169 =pod
170  
171 =encoding UTF-8
172  
173 =head1 NAME
174  
175 Kubernetes::REST::CLI::Crd2Class - Generate IO::K8s classes from a CustomResourceDefinition
176  
177 =head1 VERSION
178  
179 version 1.109
180  
181 =head1 SYNOPSIS
182  
183 use Kubernetes::REST::CLI::Crd2Class;
184  
185 my $cli = Kubernetes::REST::CLI::Crd2Class->new_with_options;
186 exit $cli->run;
187  
188 =head1 DESCRIPTION
189  
190 L<MooX::Options>-based class that powers the L<kube_crd2class> CLI tool: the
191 D15 wrapper around the D10 source emitter. It reads a
192 C<CustomResourceDefinition> from a file and prints, for every B<served>
193 version, the checked-in, documented L<IO::K8s> class source that models it -
194 the C<k8s> DSL block plus a POD skeleton - so a cluster's CRDs can become
195 typed, hand-maintained Perl classes without writing them by hand (the
196 kopium / C<cdk8s import> path).
197  
198 The schema-to-DSL work is entirely L<IO::K8s>'s: L<IO::K8s::CRD/load>,
199 L<IO::K8s::CRD/served_versions> and L<IO::K8s::CRD/generate> build the
200 classes and L<IO::K8s::CRD::Emitter/render> renders them. This class is only
201 the CLI hull: file in, emitter, source out.
202  
203 The C<-f> path never talks to a cluster: it builds the L<api|/api> lazily
204 through L<Kubernetes::REST::CLI::Role::Connection>, so reading a file reads no
205 kubeconfig. The C</from_cluster> path uses that same C<api> to list the
206 cluster's C<CustomResourceDefinition>s, picks the one whose C<spec.names.kind>
207 matches the given Kind, and feeds its manifest through the very same emitter
208 pipeline as C<-f> - so the two inputs produce identical source for the same
209 CRD.
210  
211 =head2 file
212  
213 Path to a file holding one or more C<CustomResourceDefinition> manifests
214 (YAML, possibly multi-document, or JSON). Every served version of every CRD
215 in the file is turned into class source.
216  
217 Short option: C<-f>
218  
219 =head2 namespace
220  
221 The base package the generated classes are rendered under, without the
222 version segment: C<IO::K8s::MyProvider> yields C<IO::K8s::MyProvider::V1::Kind>
223 for a served C<v1>. When omitted a default is derived from the CRD group's
224 leading DNS label (C<cert-manager.io> becomes C<IO::K8s::CertManager>). Per
225 served version the version name is appended, C<< ucfirst >>-ed the way the
226 D6 provider namespaces are (C<v1alpha1> becomes C<V1alpha1>).
227  
228 Short option: C<-n>
229  
230 =head2 output_dir
231  
232 When set, each rendered class is written to
233 F<E<lt>output_dirE<gt>/E<lt>relative/pathE<gt>.pm> (intermediate directories
234 created) instead of being printed to STDOUT, and the path written is logged to
235 STDERR. Without it every class is printed to STDOUT, each under a banner
236 naming its relative path and the C<apiVersion> it models.
237  
238 Short option: C<-d>
239  
240 =head2 from_cluster
241  
242 A Kubernetes B<Kind> (for example C<CronTab>) that a live cluster already
243 serves through a C<CustomResourceDefinition>. The cluster's CRDs are listed
244 through L</api> and the one whose C<spec.names.kind> matches is turned into
245 class source for every served version, exactly as L</file> would from that
246 CRD's manifest. Dies clearly when no CRD - or more than one - serves the Kind.
247  
248 Reaching the cluster needs a kubeconfig, so this path honours C<--kubeconfig>
249 and C<--context> from L<Kubernetes::REST::CLI::Role::Connection>; the L</file>
250 path reads neither.
251  
252 =head2 run
253  
254 my $exit_code = $cli->run;
255  
256 Entry point called by C<bin/kube_crd2class>. Obtains one or more CRD manifests
257 - from L</file>, or from the cluster by Kind when L</from_cluster> is given -
258 and for every served version of every CRD renders the L<IO::K8s> class source
259 through L<IO::K8s::CRD::Emitter>, printing it to STDOUT or writing it under
260 L</output_dir> when that is set. Returns 0.
261  
262 Dies with usage guidance when neither input is supplied, when the L</file> does
263 not exist, or when L</from_cluster> names a Kind the cluster does not serve
264 through exactly one C<CustomResourceDefinition>.
265  
266 =head1 SEE ALSO
267  
268 =over
269  
270 =item * L<kube_crd2class> - The CLI tool built on this class
271  
272 =item * L<IO::K8s::CRD> - Loads a CRD and generates one class per served version
273  
274 =item * L<IO::K8s::CRD::Emitter> - Renders those classes as house-style source
275  
276 =item * L<Kubernetes::REST> - The client whose CRD support this generates classes for
277  
278 =back
279  
280 =head1 SUPPORT
281  
282 =head2 Issues
283  
284 Please report bugs and feature requests on GitHub at
285 L<https://github.com/pplu/kubernetes-rest/issues>.
286  
287 =head2 IRC
288  
289 Join C<#kubernetes> on C<irc.perl.org> or message Getty directly.
290  
291 =head1 CONTRIBUTING
292  
293 Contributions are welcome! Please fork the repository and submit a pull request.
294  
295 =head1 AUTHORS
296  
297 =over 4
298  
299 =item *
300  
301 Torsten Raudssus <getty@cpan.org>
302  
303 =item *
304  
305 Jose Luis Martinez Torres <jlmartin@cpan.org>
306  
307 =back
308  
309 =head1 COPYRIGHT AND LICENSE
310  
311 This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>.
312  
313 This is free software, licensed under:
314  
315 The Apache License, Version 2.0, January 2004
316  
317 =cut