blib/lib/Kubernetes/REST/CLI/Cmd/Create.pm untested

filter: Statementstmt 0.0% 0 / 49 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 87 CC 10 SCAR 26.6
CC
10
Cov
0%
_parse_manifest
30.0
_read_input
12.0
execute
12.0
CRAP 14.2
1 package Kubernetes::REST::CLI::Cmd::Create;
2 our $VERSION = '1.108';
3 # ABSTRACT: The create command of kube_client
4 use Moo;
5 use MooX::Options;
6 use MooX::Cmd;
7 use Encode ();
8  
9  
10 option file => (
11 is => 'ro',
12 format => 's',
13 short => 'f',
14 doc => 'File to read (- for stdin)',
15 default => sub { '-' },
16 );
17  
18  
19 sub _source {
20 my ($self) = @_;
21 return $self->file eq '-' ? 'stdin' : $self->file;
22 }
23  
24 sub _read_input {
25 my ($self) = @_;
26  
27 my $input;
28 if ($self->file eq '-') {
29 local $/;
30 $input = <STDIN>;
31 } else {
32 open my $fh, '<', $self->file or die "Cannot open " . $self->file . ": $!";
33 local $/;
34 $input = <$fh>;
35 close $fh;
36 }
37  
38 return defined $input ? $input : '';
39 }
40  
41 sub _parse_manifest {
42 my ($self, $api, $input) = @_;
43  
44 die "Empty manifest read from " . $self->_source . "\n"
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 return $api->inflate($input) if $input =~ /\A\s*\{/;
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 $input .= "\n" unless $input =~ /\n/;
60 my $objects = $api->load_yaml(Encode::decode('UTF-8', $input));
61  
62 die "No Kubernetes manifest documents found in " . $self->_source . "\n"
63 unless @$objects;
64  
65 return @$objects;
66 }
67  
68 sub execute {
69 my ($self, $args, $chain) = @_;
70 my $root = $chain->[0];
71  
72 my @objects = $self->_parse_manifest($root->api, $self->_read_input);
73 my $multi = @objects > 1;
74  
75 for my $i (0 .. $#objects) {
76 my $obj = $objects[$i];
77 my $result = eval { $root->api->create($obj) };
78 if (my $error = $@) {
79 # A single-document manifest reports its own error unchanged; in a
80 # multi-document one the caller cannot tell which document failed.
81 die $error unless $multi;
82 die sprintf "Document %d of %d (%s) in %s failed: %s",
83 $i + 1, scalar @objects, $obj->kind, $self->_source, $error;
84 }
85 $root->format_output($result);
86 }
87  
88 return 0;
89 }
90  
91  
92 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.108
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