| 1 | package Kubernetes::REST::CLI::Crd2Class; | ||||||||||||||||||||||||||||||||||
| 2 | 1 | our $VERSION = '1.110'; | |||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Generate IO::K8s classes from a CustomResourceDefinition | ||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 1 | with 'Kubernetes::REST::CLI::Role::Connection'; | |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use MooX::Options; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Path::Tiny qw( path ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use IO::K8s::CRD (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use IO::K8s::CRD::Emitter (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 1 | option file => ( | |||||||||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 15 | format => 's', | ||||||||||||||||||||||||||||||||||
| 16 | short => 'f', | ||||||||||||||||||||||||||||||||||
| 17 | doc => 'Path to a CustomResourceDefinition YAML/JSON file', | ||||||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | 1 | 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 | 1 | 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 | 1 | 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 | 5 | ▶ | sub run { | ||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 46 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||
| 48 | 5 | my $from_cluster = $self->from_cluster; | |||||||||||||||||||||||||||||||||
| 49 | 5 | my $file = $self->file; | |||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | 5 | my $crds; | |||||||||||||||||||||||||||||||||
| 52 | 5 | ▶ | if (defined $from_cluster && length $from_cluster) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $from_cluster and length $from_cluster
MC/DC50%1/2
defined $from_cluster and length $from_cluster
defined $from_cluster
length $from_cluster
Truth table66%2/3
A and B
A= defined $from_clusterB= length $from_cluster
| |||||||||||||||||||||||||||||||||||
| 53 | 2 | $crds = $self->_crds_from_cluster($from_cluster); | |||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||
| 55 | ▶ | elsif (defined $file && length $file) { | |||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $file and length $file
MC/DC50%1/2
defined $file and length $file
defined $file
length $file
Truth table66%2/3
A and B
A= defined $fileB= length $file
| |||||||||||||||||||||||||||||||||||
| 56 | 2 | ▶ | croak "kube_crd2class: no such file: $file\n" unless -f $file; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 57 | 2 | $crds = IO::K8s::CRD->load($file); | |||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||
| 59 | else { | ||||||||||||||||||||||||||||||||||
| 60 | 1 | croak "kube_crd2class: no input given; pass --file/-f <crd.yaml> " | |||||||||||||||||||||||||||||||||
| 61 | . "or --from-cluster <Kind>\n"; | ||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||
| 64 | 3 | binmode STDOUT, ':encoding(UTF-8)'; | |||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | 3 | my $files = $self->_render_crds($crds); | |||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | 3 | ▶ | if (defined $self->output_dir && length $self->output_dir) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->output_dir and length $self->output_dir
MC/DC50%1/2
defined $self->output_dir and length $self->output_dir
defined $self->output_dir
length $self->output_dir
Truth table66%2/3
A and B
A= defined $self->output_dirB= length $self->output_dir
| |||||||||||||||||||||||||||||||||||
| 69 | 1 | $self->_write_files($files); | |||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||
| 71 | else { | ||||||||||||||||||||||||||||||||||
| 72 | 2 | $self->_print_files($files); | |||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||
| 74 | 3 | 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 | 3 | ▶ | sub _crds_from_cluster { | ||||||||||||||||||||||||||||||||
|
Subroutine
_crds_from_cluster: called CC=6 SCAR=19.5
| |||||||||||||||||||||||||||||||||||
| 84 | 3 | my ($self, $kind) = @_; | |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 3 | my $list = $self->api->list('CustomResourceDefinition'); | |||||||||||||||||||||||||||||||||
| 87 | my @matches = grep { | ||||||||||||||||||||||||||||||||||
| 88 | 3 | ▶ | my $names = $_->spec && $_->spec->names; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$_->spec && $_->spec->names
MC/DC0%0/2
$_->spec && $_->spec->names
$_->spec
$_->spec->names
Truth table33%1/3
A && B
A= $_->specB= $_->spec->names
| |||||||||||||||||||||||||||||||||||
| 89 | 3 | ▶ | $names && defined $names->kind && $names->kind eq $kind; | ||||||||||||||||||||||||||||||||
|
Condition50%3/6
$names && defined $names->kind && $names->kind eq $kind
$names and defined $names->kind
MC/DC33%1/3
$names && defined $names->kind && $names->kind eq $kind
$names
defined $names->kind
$names->kind eq $kind
Truth table50%2/4
A and B && C
A= $namesB= defined $names->kindC= $names->kind eq $kind
| |||||||||||||||||||||||||||||||||||
| 90 | 3 | } @{ $list->items }; | |||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | 3 | ▶ | croak "kube_crd2class: no CustomResourceDefinition on the cluster serves " | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 93 | . "kind '$kind'\n" unless @matches; | ||||||||||||||||||||||||||||||||||
| 94 | croak "kube_crd2class: kind '$kind' is served by more than one " | ||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | . "CustomResourceDefinition (" . join(', ', map { $_->metadata->name } @matches) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 96 | . "); nothing here can pick between them\n" if @matches > 1; | ||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | 2 | 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 | 7 | ▶ | sub _render_crds { | ||||||||||||||||||||||||||||||||
|
Subroutine
_render_crds: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 105 | 7 | my ($self, $crds) = @_; | |||||||||||||||||||||||||||||||||
| 106 | 7 | my @out; | |||||||||||||||||||||||||||||||||
| 107 | 7 | for my $crd (@$crds) { | |||||||||||||||||||||||||||||||||
| 108 | 7 | my $ns = $self->_namespace_for($crd); | |||||||||||||||||||||||||||||||||
| 109 | 7 | 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 | 7 | my $classes = IO::K8s::CRD->generate($crd, $ns, reuse_core => 1); | |||||||||||||||||||||||||||||||||
| 113 | 7 | for my $v (@$versions) { | |||||||||||||||||||||||||||||||||
| 114 | 14 | ▶ | my $root = $classes->{ $v->{api_version} } or next; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 115 | my $emitter = IO::K8s::CRD::Emitter->new( | ||||||||||||||||||||||||||||||||||
| 116 | 14 | base => $ns . '::' . ucfirst($v->{name}), | |||||||||||||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||||||||||||||
| 118 | 14 | my $rendered = $emitter->render($root); | |||||||||||||||||||||||||||||||||
| 119 | 14 | push @out, map { [ $_, $v->{api_version}, $rendered->{$_} ] } | |||||||||||||||||||||||||||||||||
| 120 | sort keys %$rendered; | ||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||
| 123 | 7 | 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 | 7 | ▶ | sub _namespace_for { | ||||||||||||||||||||||||||||||||
|
Subroutine
_namespace_for: called CC=6 SCAR=18.8
| |||||||||||||||||||||||||||||||||||
| 129 | 7 | my ($self, $crd) = @_; | |||||||||||||||||||||||||||||||||
| 130 | 7 | ▶ | return $self->namespace if defined $self->namespace && length $self->namespace; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->namespace and length $self->namespace
MC/DC50%1/2
defined $self->namespace and length $self->namespace
defined $self->namespace
length $self->namespace
Truth table66%2/3
A and B
A= defined $self->namespaceB= length $self->namespace
| |||||||||||||||||||||||||||||||||||
| 131 | 6 | ▶ | my $group = $crd->{spec}{group} // ''; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$crd->{"spec"}{"group"} // ""
MC/DC0%0/1
$crd->{"spec"}{"group"} // ""
$crd->{"spec"}{"group"}
Truth table50%1/2
A
A= $crd->{"spec"}{"group"}
| |||||||||||||||||||||||||||||||||||
| 132 | 6 | my ($label) = split /\./, $group; | |||||||||||||||||||||||||||||||||
| 133 | 6 | ▶ | $label = 'Crd' unless defined $label && length $label; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $label and length $label
MC/DC0%0/2
defined $label and length $label
defined $label
length $label
Truth table33%1/3
A and B
A= defined $labelB= length $label
| |||||||||||||||||||||||||||||||||||
| 134 | 6 | my $camel = join '', map { ucfirst } split /-/, $label; | |||||||||||||||||||||||||||||||||
| 135 | 6 | return 'IO::K8s::' . $camel; | |||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | 2 | ▶ | sub _print_files { | ||||||||||||||||||||||||||||||||
|
Subroutine
_print_files: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 139 | 2 | my ($self, $files) = @_; | |||||||||||||||||||||||||||||||||
| 140 | 2 | my @blocks = @$files; | |||||||||||||||||||||||||||||||||
| 141 | 2 | for my $i (0 .. $#blocks) { | |||||||||||||||||||||||||||||||||
| 142 | 8 | my ($path, $api_version, $source) = @{ $blocks[$i] }; | |||||||||||||||||||||||||||||||||
| 143 | 8 | ▶ | print "\n" if $i; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 144 | 8 | print '# ', ('=' x 74), "\n"; | |||||||||||||||||||||||||||||||||
| 145 | 8 | print "# $path ($api_version)\n"; | |||||||||||||||||||||||||||||||||
| 146 | 8 | print '# ', ('=' x 74), "\n"; | |||||||||||||||||||||||||||||||||
| 147 | 8 | print $source; | |||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||
| 149 | 2 | return; | |||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||
| 152 | 1 | ▶ | sub _write_files { | ||||||||||||||||||||||||||||||||
|
Subroutine
_write_files: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 153 | 1 | my ($self, $files) = @_; | |||||||||||||||||||||||||||||||||
| 154 | 1 | my $dir = path($self->output_dir); | |||||||||||||||||||||||||||||||||
| 155 | 1 | for my $block (@$files) { | |||||||||||||||||||||||||||||||||
| 156 | 4 | my ($path, undef, $source) = @$block; | |||||||||||||||||||||||||||||||||
| 157 | 4 | my $target = $dir->child($path); | |||||||||||||||||||||||||||||||||
| 158 | 4 | $target->parent->mkpath; | |||||||||||||||||||||||||||||||||
| 159 | 4 | $target->spew_utf8($source); | |||||||||||||||||||||||||||||||||
| 160 | 4 | print STDERR "wrote $target\n"; | |||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||
| 162 | 1 | return; | |||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||
| 165 | 1 | 1; | |||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||
| 167 | __END__ | ||||||||||||||||||||||||||||||||||