| 1 | package Kubernetes::REST::CLI::Cmd::Create; | ||||||||||||||||||||||||||||
| 2 | 1 | our $VERSION = '1.109'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: The create command of kube_client | ||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use MooX::Options; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use MooX::Cmd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Encode (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | option file => ( | ||||||||||||||||||||||||||||
| 11 | is => 'ro', | ||||||||||||||||||||||||||||
| 12 | format => 's', | ||||||||||||||||||||||||||||
| 13 | short => 'f', | ||||||||||||||||||||||||||||
| 14 | doc => 'File to read (- for stdin)', | ||||||||||||||||||||||||||||
| 15 | 9 | ▶ | default => sub { '-' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | sub _source { | ||||||||||||||||||||||||||
|
Subroutine
_source: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 20 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 21 | 5 | ▶ | return $self->file eq '-' ? 'stdin' : $self->file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 8 | ▶ | sub _read_input { | ||||||||||||||||||||||||||
|
Subroutine
_read_input: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 25 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 8 | my $input; | |||||||||||||||||||||||||||
| 28 | 8 | ▶ | if ($self->file eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 2 | local $/; | |||||||||||||||||||||||||||
| 30 | 2 | $input = <STDIN>; | |||||||||||||||||||||||||||
| 31 | } else { | ||||||||||||||||||||||||||||
| 32 | 6 | ▶ | open my $fh, '<', $self->file or die "Cannot open " . $self->file . ": $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 5 | local $/; | |||||||||||||||||||||||||||
| 34 | 5 | $input = <$fh>; | |||||||||||||||||||||||||||
| 35 | 5 | close $fh; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 7 | ▶ | return defined $input ? $input : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 19 | ▶ | sub _parse_manifest { | ||||||||||||||||||||||||||
|
Subroutine
_parse_manifest: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 42 | 19 | my ($self, $api, $input) = @_; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 19 | ▶ | die "Empty manifest read from " . $self->_source . "\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $input and $input =~ /\S/
MC/DC50%1/2
defined $input and $input =~ /\S/
defined $input
$input =~ /\S/
Truth table66%2/3
A and B
A= defined $inputB= $input =~ /\S/
| |||||||||||||||||||||||||||||
| 45 | unless defined $input && $input =~ /\S/; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # A JSON manifest is a JSON object, so after optional whitespace it starts | ||||||||||||||||||||||||||||
| 48 | # with a brace, while a YAML manifest starts with a key, a comment or '---'. | ||||||||||||||||||||||||||||
| 49 | # YAML is a superset of JSON and would parse both, but routing JSON to | ||||||||||||||||||||||||||||
| 50 | # inflate() leaves the path it already took completely untouched: the same | ||||||||||||||||||||||||||||
| 51 | # decoder, the same UTF-8 bytes straight in (that decoder is utf8 => 1, so | ||||||||||||||||||||||||||||
| 52 | # there is no decode step to get wrong) and JSON error messages, with a | ||||||||||||||||||||||||||||
| 53 | # character offset, for someone who wrote JSON. | ||||||||||||||||||||||||||||
| 54 | 16 | ▶ | return $api->inflate($input) if $input =~ /\A\s*\{/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # load_yaml() parses characters, not bytes - handing it the raw bytes turns | ||||||||||||||||||||||||||||
| 57 | # every non-ASCII value into mojibake. It also treats a newline-free | ||||||||||||||||||||||||||||
| 58 | # argument as a file name, which the trailing newline keeps us out of. | ||||||||||||||||||||||||||||
| 59 | 11 | ▶ | $input .= "\n" unless $input =~ /\n/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 11 | my $objects = $api->load_yaml(Encode::decode('UTF-8', $input)); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 10 | ▶ | die "No Kubernetes manifest documents found in " . $self->_source . "\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | unless @$objects; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 9 | return @$objects; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 5 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 5 | my ($self, $args, $chain) = @_; | |||||||||||||||||||||||||||
| 70 | 5 | my $root = $chain->[0]; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 5 | my @objects = $self->_parse_manifest($root->api, $self->_read_input); | |||||||||||||||||||||||||||
| 73 | 5 | my $multi = @objects > 1; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 5 | for my $i (0 .. $#objects) { | |||||||||||||||||||||||||||
| 76 | 8 | my $obj = $objects[$i]; | |||||||||||||||||||||||||||
| 77 | 8 | my $result = eval { $root->api->create($obj) }; | |||||||||||||||||||||||||||
| 78 | 8 | ▶ | if (my $error = $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | # A single-document manifest reports its own error unchanged; in a | ||||||||||||||||||||||||||||
| 80 | # multi-document one the caller cannot tell which document failed. | ||||||||||||||||||||||||||||
| 81 | 2 | ▶ | die $error unless $multi; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 1 | die sprintf "Document %d of %d (%s) in %s failed: %s", | |||||||||||||||||||||||||||
| 83 | $i + 1, scalar @objects, $obj->kind, $self->_source, $error; | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 6 | $root->format_output($result); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 3 | return 0; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | 1; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | __END__ | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =pod | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 NAME | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | Kubernetes::REST::CLI::Cmd::Create - The create command of kube_client | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 VERSION | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | version 1.109 | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | kube_client create -f <file> | ||||||||||||||||||||||||||||
| 111 | kube_client create -f - # read from stdin | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Implements the C<create> command of L<Kubernetes::REST::CLI>. Reads a YAML or | ||||||||||||||||||||||||||||
| 116 | JSON manifest, inflates it into typed L<IO::K8s> objects (the class of each | ||||||||||||||||||||||||||||
| 117 | document is auto-detected from its C<kind> field) and creates them on the | ||||||||||||||||||||||||||||
| 118 | cluster. L<MooX::Cmd> finds and loads this class, you do not use it directly. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | The format is detected from the content, not from the file name, so C<-f -> is | ||||||||||||||||||||||||||||
| 121 | covered as well as C<-f file.yaml>: a manifest starting with C<{> after | ||||||||||||||||||||||||||||
| 122 | optional whitespace goes to C<inflate> as before, anything else is parsed as | ||||||||||||||||||||||||||||
| 123 | YAML by C<load_yaml> - see L<Kubernetes::REST/k8s> for both methods' argument | ||||||||||||||||||||||||||||
| 124 | contracts. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Multi-document YAML (C<--->-separated) is supported and is the common case for | ||||||||||||||||||||||||||||
| 127 | Kubernetes manifests. Each document is created in the order it appears in the | ||||||||||||||||||||||||||||
| 128 | file - which is what makes a manifest listing a C<Namespace> before the objects | ||||||||||||||||||||||||||||
| 129 | inside it work - and each created object is printed through | ||||||||||||||||||||||||||||
| 130 | L<Kubernetes::REST::CLI/format_output>. With C<--output yaml> that is a | ||||||||||||||||||||||||||||
| 131 | C<--->-separated stream again; with the default C<--output json> it is one JSON | ||||||||||||||||||||||||||||
| 132 | document per created object. If a document fails, the documents before it have | ||||||||||||||||||||||||||||
| 133 | already been created and the error names the one that failed. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head2 file | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Path of the manifest to create, C<-> for stdin. Short form: C<-f>. Defaults to | ||||||||||||||||||||||||||||
| 138 | C<->. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head2 execute | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Runs the command. Called by L<MooX::Cmd> with the remaining arguments and the | ||||||||||||||||||||||||||||
| 143 | command chain, whose first element is the L<Kubernetes::REST::CLI> root object. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =over | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =item * L<Kubernetes::REST::CLI> - CLI base class | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =item * L<Kubernetes::REST/k8s> - Documents C<load_yaml>, the YAML reader this command parses manifests with | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =back | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head2 Issues | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 160 | L<https://github.com/pplu/kubernetes-rest/issues>. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head2 IRC | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Join C<#kubernetes> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =over 4 | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =item * | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =item * | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Jose Luis Martinez Torres <jlmartin@cpan.org> | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =back | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | The Apache License, Version 2.0, January 2004 | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =cut | ||||||||||||||||||||||||||||