lib/Kubernetes/REST/CLI/Watch.pm

filter: Statementstmt 84.7% 89 / 105 Branchbran 61.9% 26 / 42 Conditioncond 48.1% 13 / 27 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 93.3% 14 / 15 Podpod 100% 1 / 1 totaltotal 71.5% 146 / 204 CC 34 SCAR 28.2
CC
34
Cov
74%
run
48.3
_print_text
22.1
_handle_event
9.1
CRAP 16.9
« lib/Kubernetes/REST/V0Group.pm ↑ Summary blib/lib/Kubernetes/REST/CLI/Cmd/Create.pm »
1 package Kubernetes::REST::CLI::Watch;
2 3 our $VERSION = '1.108';
3 # ABSTRACT: CLI for watching Kubernetes resources
4 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # protect_argv => 0 so parsed options are removed from @ARGV, leaving the
6 # positional Kind as the first remaining element. bin/kube_watch reads the Kind
7 # with `shift @ARGV` after new_with_options; with the default protect_argv => 1
8 # the options stay in @ARGV and `kube_watch -n default Pod` would shift off '-n'
9 # instead of 'Pod'.
10 3 ▶use MooX::Options protect_argv => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 with 'Kubernetes::REST::CLI::Role::Connection';
15  
16  
17 3 option namespace => (
18 is => 'ro',
19 format => 's',
20 short => 'n',
21 doc => 'Namespace to watch',
22 );
23  
24  
25 option output => (
26 is => 'ro',
27 format => 's',
28 short => 'o',
29 31 ▶ default => sub { 'text' },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 3 doc => 'Output format: text, json, yaml',
31 );
32  
33  
34 option timeout => (
35 is => 'ro',
36 format => 'i',
37 short => 't',
38 34 ▶ default => sub { 300 },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 3 doc => 'Server-side timeout per watch cycle (seconds)',
40 );
41  
42  
43 3 option event_type => (
44 is => 'ro',
45 format => 's',
46 short => 'T',
47 doc => 'Only show these event types (comma-separated)',
48 );
49  
50  
51 3 option label => (
52 is => 'ro',
53 format => 's',
54 short => 'l',
55 doc => 'Label selector',
56 );
57  
58  
59 3 option field => (
60 is => 'ro',
61 format => 's',
62 short => 'f',
63 doc => 'Field selector',
64 );
65  
66  
67 3 option names => (
68 is => 'ro',
69 format => 's',
70 short => 'N',
71 doc => 'Filter by resource name (Perl regex)',
72 );
73  
74  
75 option timestamp_format => (
76 is => 'ro',
77 format => 's',
78 short => 'F',
79 24 ▶ default => sub { 'datetime' },
Subroutine __ANON__: called CC=1 SCAR=0.0
80 3 doc => 'Timestamp format: datetime, date, time, epoch, iso',
81 );
82  
83  
84 has _json => (
85 is => 'ro',
86 34 ▶ default => sub { JSON::MaybeXS->new->canonical->utf8 },
Subroutine __ANON__: called CC=1 SCAR=0.0
87 3 );
88  
89 has _type_filter => (
90 is => 'lazy',
91 10 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
92 10 my $self = shift;
93 10 ▶ return {} unless $self->event_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->event_type 6 4 10
94 4 return { map { uc($_) => 1 } split /,/, $self->event_type };
95 },
96 3 );
97  
98 has _name_re => (
99 is => 'lazy',
100 9 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
101 9 my $self = shift;
102 9 ▶ return undef unless $self->names;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->names 5 4 9
103 4 my $re = eval { qr/${\$self->names}/ };
104 4 ▶ die "Invalid --names regex '" . $self->names . "': $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 3 4
105 3 return $re;
106 },
107 3 );
108  
109 3 my %TS_FORMATS = (
110 datetime => '%Y-%m-%d %H:%M:%S',
111 date => '%Y-%m-%d',
112 time => '%H:%M:%S',
113 iso => '%Y-%m-%dT%H:%M:%S%z',
114 );
115  
116 13 ▶sub _timestamp {
Subroutine _timestamp: called CC=3 SCAR=11.0
117 13 my ($self) = @_;
118 13 my $fmt = $self->timestamp_format;
119 13 ▶ return time if $fmt eq 'epoch';
Branch: 1 branch
BranchTrueFalseTotal
if $fmt eq "epoch" 5 8 13
120 8 ▶ my $strftime_fmt = $TS_FORMATS{$fmt}
Condition100%2/2
$TS_FORMATS{$fmt} // die("Unknown --timestamp-format '${fmt}' (use: datetime, date, time, epoch, iso)\n")
l!l
71
MC/DC100%1/1
$TS_FORMATS{$fmt} // die("Unknown --timestamp-format '${fmt}' (use: datetime, date, time, epoch, iso)\n")
$TS_FORMATS{$fmt}
Truth table100%2/2
A
A= $TS_FORMATS{$fmt}
Aresult
11
00
121 // die "Unknown --timestamp-format '$fmt' (use: datetime, date, time, epoch, iso)\n";
122 7 return strftime($strftime_fmt, localtime);
123 }
124  
125 2 ▶sub run {
Subroutine run: called CC=8 SCAR=38.8
Pod documented
126 2 my ($self, $kind) = @_;
127  
128 2 ▶ unless ($kind) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($kind) 2 0 2
129 2 die "Usage: kube_watch [options] <Kind>\n"
130 . "Run 'kube_watch --help' for options.\n";
131 }
132  
133 0 my $rv;
134 0 while (1) {
135 0 $rv = eval {
136 $self->api->watch($kind,
137 ($self->namespace ? (namespace => $self->namespace) : ()),
138 ($rv ? (resourceVersion => $rv) : ()),
139 ($self->label ? (labelSelector => $self->label) : ()),
140 ($self->field ? (fieldSelector => $self->field) : ()),
141 timeout => $self->timeout,
142 0 ▶ on_event => sub { $self->_handle_event(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 ▶ );
Branch: 4 branches
BranchTrueFalseTotal
$self->namespace ? : 0 0 0
$rv ? : 0 0 0
$self->label ? : 0 0 0
$self->field ? : 0 0 0
144 };
145 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
146 0 ▶ if ($@ =~ /410 Gone/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /410 Gone/) 0 0 0
147 0 warn "Watch expired, re-listing...\n";
148 0 $rv = undef;
149 0 next;
150 }
151 0 die "Watch error: $@\n";
152 }
153 # Normal timeout, restart watch
154 }
155 }
156  
157  
158 7 ▶sub _handle_event {
Subroutine _handle_event: called CC=9 SCAR=22.0
159 7 my ($self, $event) = @_;
160 7 my $type = $event->type;
161  
162 # Type filter
163 7 my $tf = $self->_type_filter;
164 7 ▶ if (%$tf && !$tf->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$tf and not $tf->{$type}) 1 6 7
Condition100%3/3
%$tf and not $tf->{$type}
!ll&&!rl&&r
511
MC/DC100%2/2
%$tf and not $tf->{$type}
%$tf not $tf->{$type}
Truth table100%3/3
A and not(B)
A= %$tfB= not $tf->{$type}
ABresult
0X0
100
111
165 1 return;
166 }
167  
168 # Name filter
169 6 my $name_re = $self->_name_re;
170 6 ▶ if ($name_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name_re) 2 4 6
171 2 my $name = eval { $event->object->metadata->name }
172 2 ▶ // $event->raw->{metadata}{name} // '';
Condition40%2/5
(eval { // ""
l!l
20
eval { // $event->raw->{"metadata"}{"name"}
l!l&&r!l&&!r
200
MC/DC0%0/2
(eval { do { $event->object->metadata->name } } // $event->raw->{"metadata"}{"name"}) // ""
eval { do { $event->object->metadata->name } } $event->raw->{"metadata"}{"name"}
Truth table33%1/3
A // B
A= eval { do { $event->object->metadata->name } }B= $event->raw->{"metadata"}{"name"}
ABresult
1X1
011
000
173 2 ▶ return unless $name =~ $name_re;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$name_re/ 1 1 2
174 }
175  
176 5 ▶ if ($self->output eq 'json') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->output eq 'json') { } 1 4 5
elsif ($self->output eq 'yaml') { } 1 3 4
177 1 print $self->_json->encode({
178 type => $type,
179 object => $event->raw,
180 }), "\n";
181 } elsif ($self->output eq 'yaml') {
182 1 require YAML::XS;
183 1 print YAML::XS::Dump({
184 type => $type,
185 object => $event->raw,
186 });
187 1 print "---\n";
188 } else {
189 3 $self->_print_text($event);
190 }
191 }
192  
193 7 ▶sub _print_text {
Subroutine _print_text: called CC=14 SCAR=30.9
194 7 my ($self, $event) = @_;
195 7 my $type = $event->type;
196 7 my $ts = $self->_timestamp;
197 7 my $obj = $event->object;
198  
199 7 ▶ if ($type eq 'ERROR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "ERROR") 1 6 7
200 1 ▶ my $msg = $event->raw->{message} // 'unknown error';
Condition50%1/2
$event->raw->{"message"} // "unknown error"
l!l
10
MC/DC0%0/1
$event->raw->{"message"} // "unknown error"
$event->raw->{"message"}
Truth table50%1/2
A
A= $event->raw->{"message"}
Aresult
11
00
201 1 ▶ my $code = $event->raw->{code} // '?';
Condition50%1/2
$event->raw->{"code"} // "?"
l!l
10
MC/DC0%0/1
$event->raw->{"code"} // "?"
$event->raw->{"code"}
Truth table50%1/2
A
A= $event->raw->{"code"}
Aresult
11
00
202 1 printf "%s %-10s ERROR(%s): %s\n", $ts, $type, $code, $msg;
203 1 return;
204 }
205  
206 6 ▶ my $name = eval { $obj->metadata->name } // '?';
Condition50%1/2
eval { // "?"
l!l
60
MC/DC0%0/1
eval { do { $obj->metadata->name } } // "?"
eval { do { $obj->metadata->name } }
Truth table50%1/2
A
A= eval { do { $obj->metadata->name } }
Aresult
11
00
207 6 ▶ my $ns = eval { $obj->metadata->namespace } // '';
Condition50%1/2
eval { // ""
l!l
60
MC/DC0%0/1
eval { do { $obj->metadata->namespace } } // ""
eval { do { $obj->metadata->namespace } }
Truth table50%1/2
A
A= eval { do { $obj->metadata->namespace } }
Aresult
11
00
208 6 ▶ my $qualified = $ns ? "$ns/$name" : $name;
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 6 0 6
209  
210 # Try to get a useful status hint
211 6 my $hint = '';
212 6 ▶ if ($obj->can('status')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->can("status")) 6 0 6
213 6 my $status = eval { $obj->status };
214 6 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) 1 5 6
215 1 ▶ $hint = eval { $status->phase } // '';
Condition50%1/2
eval { // ""
l!l
10
MC/DC0%0/1
eval { do { $status->phase } } // ""
eval { do { $status->phase } }
Truth table50%1/2
A
A= eval { do { $status->phase } }
Aresult
11
00
216 1 ▶ if (!$hint && $status->can('readyReplicas')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hint and $status->can("readyReplicas")) 0 1 1
Condition33%1/3
not $hint and $status->can("readyReplicas")
!ll&&!rl&&r
100
MC/DC0%0/2
not $hint and $status->can("readyReplicas")
not $hint $status->can("readyReplicas")
Truth table33%1/3
not(A) and B
A= not $hintB= $status->can("readyReplicas")
ABresult
0X0
100
111
217 0 ▶ my $ready = eval { $status->readyReplicas } // 0;
Condition0%0/2
eval { // 0
l!l
00
MC/DC0%0/1
eval { do { $status->readyReplicas } } // 0
eval { do { $status->readyReplicas } }
Truth table0%0/2
A
A= eval { do { $status->readyReplicas } }
Aresult
11
00
218 0 ▶ my $desired = eval { $obj->spec->replicas } // '?';
Condition0%0/2
eval { // "?"
l!l
00
MC/DC0%0/1
eval { do { $obj->spec->replicas } } // "?"
eval { do { $obj->spec->replicas } }
Truth table0%0/2
A
A= eval { do { $obj->spec->replicas } }
Aresult
11
00
219 0 $hint = "${ready}/${desired} ready";
220 }
221 }
222 }
223  
224 6 printf "%s %-10s %-50s %s\n", $ts, $type, $qualified, $hint;
225 }
226  
227 3 1;
228  
229 __END__
230  
231 =pod
232  
233 =encoding UTF-8
234  
235 =head1 NAME
236  
237 Kubernetes::REST::CLI::Watch - CLI for watching Kubernetes resources
238  
239 =head1 VERSION
240  
241 version 1.108
242  
243 =head1 SYNOPSIS
244  
245 use Kubernetes::REST::CLI::Watch;
246  
247 my $watcher = Kubernetes::REST::CLI::Watch->new_with_options;
248 $watcher->run($ARGV[0]);
249  
250 =head1 DESCRIPTION
251  
252 L<MooX::Options>-based class that powers the L<kube_watch> CLI tool. Uses L<Kubernetes::REST::CLI::Role::Connection> for shared kubeconfig/auth handling.
253  
254 =head2 namespace
255  
256 Namespace to watch. Omit for cluster-scoped resources or to watch all namespaces.
257  
258 Short option: C<-n>
259  
260 =head2 output
261  
262 Output format: C<text> (default), C<json>, or C<yaml>.
263  
264 Short option: C<-o>
265  
266 =head2 timeout
267  
268 Server-side timeout per watch cycle in seconds. Default: 300.
269  
270 Short option: C<-t>
271  
272 =head2 event_type
273  
274 Comma-separated list of watch event types to show, e.g. C<ADDED,DELETED>.
275 Matched case-insensitively (values are uppercased before comparing) against
276 C<ADDED>, C<MODIFIED>, C<DELETED>, C<ERROR>, and C<BOOKMARK>. Omit to show all
277 event types.
278  
279 Short option: C<-T>
280  
281 =head2 label
282  
283 Kubernetes label selector passed through as C<labelSelector>, e.g.
284 C<app=web,env=prod>.
285  
286 Short option: C<-l>
287  
288 =head2 field
289  
290 Kubernetes field selector passed through as C<fieldSelector>, e.g.
291 C<status.phase=Running>.
292  
293 Short option: C<-f>
294  
295 =head2 names
296  
297 Perl regular expression. Only events for resources whose C<metadata.name>
298 matches are printed; all others are silently skipped. Applied client-side,
299 after the event has already been received from the server.
300  
301 Short option: C<-N>
302  
303 =head2 timestamp_format
304  
305 Timestamp format used in C<text> output. One of C<datetime> (C<%Y-%m-%d
306 %H:%M:%S>, the default), C<date>, C<time>, C<epoch> (seconds since the Unix
307 epoch), or C<iso>. Ignored for C<json>/C<yaml> output.
308  
309 Short option: C<-F>
310  
311 =head2 run
312  
313 $watcher->run($kind);
314  
315 Watches resources of C<$kind>, printing each event as it arrives (formatted
316 per C<--output>). Calls L<Kubernetes::REST/watch> in an infinite loop: a
317 normal server-side timeout restarts the watch from the last seen
318 C<resourceVersion>, and a C<410 Gone> (the C<resourceVersion> has expired)
319 warns and restarts a fresh watch from scratch. Any other error is re-raised as
320 C<"Watch error: $@\n"> - the original message wrapped with that prefix, not
321 rethrown unchanged. Dies with a usage message if C<$kind> is missing. This
322 method never returns under normal operation - it is the entry point called by
323 C<bin/kube_watch>.
324  
325 =head1 SEE ALSO
326  
327 =over
328  
329 =item * L<Kubernetes::REST/watch> - Watch API documentation
330  
331 =item * L<Kubernetes::REST::CLI::Role::Connection> - Shared CLI options
332  
333 =item * L<Kubernetes::REST::WatchEvent> - Watch event object
334  
335 =back
336  
337 =head1 SUPPORT
338  
339 =head2 Issues
340  
341 Please report bugs and feature requests on GitHub at
342 L<https://github.com/pplu/kubernetes-rest/issues>.
343  
344 =head2 IRC
345  
346 Join C<#kubernetes> on C<irc.perl.org> or message Getty directly.
347  
348 =head1 CONTRIBUTING
349  
350 Contributions are welcome! Please fork the repository and submit a pull request.
351  
352 =head1 AUTHORS
353  
354 =over 4
355  
356 =item *
357  
358 Torsten Raudssus <getty@cpan.org>
359  
360 =item *
361  
362 Jose Luis Martinez Torres <jlmartin@cpan.org>
363  
364 =back
365  
366 =head1 COPYRIGHT AND LICENSE
367  
368 This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>.
369  
370 This is free software, licensed under:
371  
372 The Apache License, Version 2.0, January 2004
373  
374 =cut
« lib/Kubernetes/REST/V0Group.pm ↑ Summary blib/lib/Kubernetes/REST/CLI/Cmd/Create.pm »