| 1 | package Kubernetes::REST::CLI; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: CLI base class for Kubernetes::REST command-line tools | ||||||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '1.109'; | |||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use MooX::Options; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use MooX::Cmd; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 2 | with 'Kubernetes::REST::CLI::Role::Connection'; | |||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | option namespace => ( | ||||||||||||||||||||||||||||||||
| 13 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 14 | format => 's', | ||||||||||||||||||||||||||||||||
| 15 | short => 'n', | ||||||||||||||||||||||||||||||||
| 16 | 9 | ▶ | default => sub { 'default' }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 2 | doc => 'Namespace for namespaced resources', | |||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | option output => ( | ||||||||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 23 | format => 's', | ||||||||||||||||||||||||||||||||
| 24 | short => 'o', | ||||||||||||||||||||||||||||||||
| 25 | 9 | ▶ | default => sub { 'json' }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 26 | 2 | doc => 'Output format: json, yaml, name', | |||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | has json => ( | ||||||||||||||||||||||||||||||||
| 31 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 32 | 9 | ▶ | default => sub { JSON::MaybeXS->new->pretty->canonical->utf8 }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 33 | 2 | ); | |||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 6 | ▶ | sub format_output { | ||||||||||||||||||||||||||||||
|
Subroutine
format_output: called CC=17 SCAR=51.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 37 | 6 | my ($self, $result) = @_; | |||||||||||||||||||||||||||||||
| 38 | 6 | ▶ | return unless defined $result; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 6 | my $format = $self->output; | |||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 6 | ▶ | if ($format eq 'json') { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 43 | 6 | ▶ | my $data = ref($result) && $result->can('TO_JSON') ? $result->TO_JSON : $result; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $result and $result->can("TO_JSON")
MC/DC0%0/2
ref $result and $result->can("TO_JSON")
ref $result
$result->can("TO_JSON")
Truth table33%1/3
A and B
A= ref $resultB= $result->can("TO_JSON")
| |||||||||||||||||||||||||||||||||
| 44 | 6 | print $self->json->encode($data); | |||||||||||||||||||||||||||||||
| 45 | } elsif ($format eq 'yaml') { | ||||||||||||||||||||||||||||||||
| 46 | 0 | require YAML::XS; | |||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my $data = ref($result) && $result->can('TO_JSON') ? $result->TO_JSON : $result; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $result and $result->can("TO_JSON")
MC/DC0%0/2
ref $result and $result->can("TO_JSON")
ref $result
$result->can("TO_JSON")
Truth table0%0/3
A and B
A= ref $resultB= $result->can("TO_JSON")
| |||||||||||||||||||||||||||||||||
| 48 | 0 | print YAML::XS::Dump($data); | |||||||||||||||||||||||||||||||
| 49 | } elsif ($format eq 'name') { | ||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if (ref($result) && $result->can('items')) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
ref $result and $result->can("items")
MC/DC0%0/2
ref $result and $result->can("items")
ref $result
$result->can("items")
Truth table0%0/3
A and B
A= ref $resultB= $result->can("items")
| |||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | for my $item (@{$result->items // []}) { | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$result->items // []
MC/DC0%0/1
$result->items // []
$result->items
Truth table0%0/2
A
A= $result->items
| |||||||||||||||||||||||||||||||||
| 52 | 0 | my $meta = $item->metadata; | |||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my $ns = $meta->namespace ? $meta->namespace . '/' : ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | 0 | print $ns, $meta->name, "\n"; | |||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | ▶ | } elsif (ref($result) && $result->can('metadata')) { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $result and $result->can("metadata")
MC/DC0%0/2
ref $result and $result->can("metadata")
ref $result
$result->can("metadata")
Truth table0%0/3
A and B
A= ref $resultB= $result->can("metadata")
| |||||||||||||||||||||||||||||||||
| 57 | 0 | my $meta = $result->metadata; | |||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $ns = $meta->namespace ? $meta->namespace . '/' : ''; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 59 | 0 | print $ns, $meta->name, "\n"; | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | } else { | ||||||||||||||||||||||||||||||||
| 62 | 0 | require Data::Dumper; | |||||||||||||||||||||||||||||||
| 63 | 0 | print Data::Dumper::Dumper($result); | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub execute { | ||||||||||||||||||||||||||||||
|
Subroutine
execute: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 69 | 0 | my ($self, $args, $chain) = @_; | |||||||||||||||||||||||||||||||
| 70 | 0 | print "Usage: kube_client <command> [options]\n\n"; | |||||||||||||||||||||||||||||||
| 71 | 0 | print "Commands:\n"; | |||||||||||||||||||||||||||||||
| 72 | 0 | print " get <Kind> [name] Get resource(s)\n"; | |||||||||||||||||||||||||||||||
| 73 | 0 | print " create -f <file> Create resource from file\n"; | |||||||||||||||||||||||||||||||
| 74 | 0 | print " delete <Kind> <name> Delete resource\n"; | |||||||||||||||||||||||||||||||
| 75 | 0 | print " raw <Group> <Method> Raw API call\n"; | |||||||||||||||||||||||||||||||
| 76 | 0 | print "\nRun 'kube_client --help' for options.\n"; | |||||||||||||||||||||||||||||||
| 77 | 0 | print "See also: kube_watch <Kind> for live event streaming.\n"; | |||||||||||||||||||||||||||||||
| 78 | 0 | return 0; | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 2 | 1; | |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | =pod | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | Kubernetes::REST::CLI - CLI base class for Kubernetes::REST command-line tools | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | version 1.109 | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | kube_client <command> [options] | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | # Get resources | ||||||||||||||||||||||||||||||||
| 104 | kube_client get Pod my-pod -n default | ||||||||||||||||||||||||||||||||
| 105 | kube_client get Deployment --output yaml | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | # Create from file | ||||||||||||||||||||||||||||||||
| 108 | kube_client create -f deployment.yaml | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | # Delete resources | ||||||||||||||||||||||||||||||||
| 111 | kube_client delete Pod my-pod -n default | ||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | Base class for the C<kube_client> command-line tool. Provides common functionality for managing Kubernetes resources from the command line. | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | This tool uses L<Kubernetes::REST::Kubeconfig> to connect to the cluster: C<--kubeconfig> if given, otherwise the C<KUBECONFIG> environment variable, otherwise C<~/.kube/config>. A C<:>-separated list of kubeconfig files is merged the way C<kubectl> merges it, see L<Kubernetes::REST::Kubeconfig/MERGING>. | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | =head1 COMMANDS | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | =head2 get | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | kube_client get <Kind> [name] [options] | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | Get a resource or list resources. | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | =over | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | =item B<get Pod> - List all pods in the namespace | ||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | =item B<get Pod my-pod> - Get a specific pod | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | =item B<--output json> (default) - JSON output | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | =item B<--output yaml> - YAML output | ||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | =item B<--output name> - Names only | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | =back | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | =head2 create | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | kube_client create -f <file> | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | Create resources from a YAML or JSON manifest. Use C<-f -> to read from stdin. | ||||||||||||||||||||||||||||||||
| 146 | The format is detected from the content, and multi-document YAML creates every | ||||||||||||||||||||||||||||||||
| 147 | C<--->-separated document in order. | ||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | =head2 delete | ||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||
| 151 | kube_client delete <Kind> <name> [options] | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | Delete a resource by name. | ||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | =head2 raw | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | kube_client raw <Group> <Method> [key=value ...] | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | Make a raw call through the v0 compatibility layer. | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | =head1 GLOBAL OPTIONS | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | =head2 namespace | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | Namespace for namespaced resources. Defaults to C<default>. | ||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | Short form: C<-n> | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | =head2 output | ||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||
| 171 | Output format. One of: C<json>, C<yaml>, C<name>. | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | Defaults to C<json>. | ||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | Short form: C<-o> | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | =head2 json | ||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | L<JSON::MaybeXS> encoder instance used for C<--output json>. Configured | ||||||||||||||||||||||||||||||||
| 182 | C<pretty>, C<canonical> (stable key ordering) and C<utf8> - the C<utf8> flag | ||||||||||||||||||||||||||||||||
| 183 | is what makes C<format_output> print UTF-8 bytes instead of characters, so | ||||||||||||||||||||||||||||||||
| 184 | non-ASCII values survive the way to the terminal instead of triggering "Wide | ||||||||||||||||||||||||||||||||
| 185 | character in print". | ||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||
| 189 | =head2 format_output | ||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | $cli->format_output($result); | ||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | Format and print the result according to the C<--output> option | ||||||||||||||||||||||||||||||||
| 194 | (C<json>, C<yaml>, or C<name>). Any other value falls back to a raw dump via | ||||||||||||||||||||||||||||||||
| 195 | L<Data::Dumper>, mainly useful for debugging. | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | =head2 execute | ||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | Default C<execute> method, invoked by L<MooX::Cmd> when C<kube_client> is run | ||||||||||||||||||||||||||||||||
| 200 | without a recognized subcommand. Prints usage information to STDOUT. | ||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | =over | ||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | =item * L<Kubernetes::REST> - Main API module | ||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | =item * L<Kubernetes::REST::CLI::Cmd::Get> - The get command | ||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | =item * L<Kubernetes::REST::CLI::Cmd::Create> - The create command | ||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | =item * L<Kubernetes::REST::CLI::Cmd::Delete> - The delete command | ||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||
| 214 | =item * L<Kubernetes::REST::CLI::Cmd::Raw> - The raw command | ||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||
| 216 | =item * L<Kubernetes::REST::CLI::Watch> - Watch command implementation | ||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||
| 218 | =item * L<Kubernetes::REST::CLI::Role::Connection> - Connection options | ||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||
| 220 | =back | ||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||
| 222 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||
| 224 | =head2 Issues | ||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||
| 227 | L<https://github.com/pplu/kubernetes-rest/issues>. | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | =head2 IRC | ||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | Join C<#kubernetes> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||
| 237 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | =over 4 | ||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||
| 241 | =item * | ||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||
| 245 | =item * | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | Jose Luis Martinez Torres <jlmartin@cpan.org> | ||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | =back | ||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||
| 251 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>. | ||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||
| 255 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||
| 257 | The Apache License, Version 2.0, January 2004 | ||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||
| 259 | =cut | ||||||||||||||||||||||||||||||||