| 1 | package Kubernetes::REST::CLI::Watch; | ||
| 2 | our $VERSION = '1.108'; | ||
| 3 | # ABSTRACT: CLI for watching Kubernetes resources | ||
| 4 | use Moo; | ||
| 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 | use MooX::Options protect_argv => 0; | ||
| 11 | use JSON::MaybeXS; | ||
| 12 | use POSIX qw(strftime); | ||
| 13 | |||
| 14 | with 'Kubernetes::REST::CLI::Role::Connection'; | ||
| 15 | |||
| 16 | |||
| 17 | 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 | default => sub { 'text' }, | ||
| 30 | doc => 'Output format: text, json, yaml', | ||
| 31 | ); | ||
| 32 | |||
| 33 | |||
| 34 | option timeout => ( | ||
| 35 | is => 'ro', | ||
| 36 | format => 'i', | ||
| 37 | short => 't', | ||
| 38 | default => sub { 300 }, | ||
| 39 | doc => 'Server-side timeout per watch cycle (seconds)', | ||
| 40 | ); | ||
| 41 | |||
| 42 | |||
| 43 | 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 | option label => ( | ||
| 52 | is => 'ro', | ||
| 53 | format => 's', | ||
| 54 | short => 'l', | ||
| 55 | doc => 'Label selector', | ||
| 56 | ); | ||
| 57 | |||
| 58 | |||
| 59 | option field => ( | ||
| 60 | is => 'ro', | ||
| 61 | format => 's', | ||
| 62 | short => 'f', | ||
| 63 | doc => 'Field selector', | ||
| 64 | ); | ||
| 65 | |||
| 66 | |||
| 67 | 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 | default => sub { 'datetime' }, | ||
| 80 | doc => 'Timestamp format: datetime, date, time, epoch, iso', | ||
| 81 | ); | ||
| 82 | |||
| 83 | |||
| 84 | has _json => ( | ||
| 85 | is => 'ro', | ||
| 86 | default => sub { JSON::MaybeXS->new->canonical->utf8 }, | ||
| 87 | ); | ||
| 88 | |||
| 89 | has _type_filter => ( | ||
| 90 | is => 'lazy', | ||
| 91 | default => sub { | ||
| 92 | my $self = shift; | ||
| 93 | return {} unless $self->event_type; | ||
| 94 | return { map { uc($_) => 1 } split /,/, $self->event_type }; | ||
| 95 | }, | ||
| 96 | ); | ||
| 97 | |||
| 98 | has _name_re => ( | ||
| 99 | is => 'lazy', | ||
| 100 | default => sub { | ||
| 101 | my $self = shift; | ||
| 102 | return undef unless $self->names; | ||
| 103 | my $re = eval { qr/${\$self->names}/ }; | ||
| 104 | die "Invalid --names regex '" . $self->names . "': $@\n" if $@; | ||
| 105 | return $re; | ||
| 106 | }, | ||
| 107 | ); | ||
| 108 | |||
| 109 | 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 | sub _timestamp { | ||
| 117 | my ($self) = @_; | ||
| 118 | my $fmt = $self->timestamp_format; | ||
| 119 | return time if $fmt eq 'epoch'; | ||
| 120 | my $strftime_fmt = $TS_FORMATS{$fmt} | ||
| 121 | // die "Unknown --timestamp-format '$fmt' (use: datetime, date, time, epoch, iso)\n"; | ||
| 122 | return strftime($strftime_fmt, localtime); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub run { | ||
| 126 | my ($self, $kind) = @_; | ||
| 127 | |||
| 128 | unless ($kind) { | ||
| 129 | die "Usage: kube_watch [options] <Kind>\n" | ||
| 130 | . "Run 'kube_watch --help' for options.\n"; | ||
| 131 | } | ||
| 132 | |||
| 133 | my $rv; | ||
| 134 | while (1) { | ||
| 135 | $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 | on_event => sub { $self->_handle_event(@_) }, | ||
| 143 | ); | ||
| 144 | }; | ||
| 145 | if ($@) { | ||
| 146 | if ($@ =~ /410 Gone/) { | ||
| 147 | warn "Watch expired, re-listing...\n"; | ||
| 148 | $rv = undef; | ||
| 149 | next; | ||
| 150 | } | ||
| 151 | die "Watch error: $@\n"; | ||
| 152 | } | ||
| 153 | # Normal timeout, restart watch | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | sub _handle_event { | ||
| 159 | my ($self, $event) = @_; | ||
| 160 | my $type = $event->type; | ||
| 161 | |||
| 162 | # Type filter | ||
| 163 | my $tf = $self->_type_filter; | ||
| 164 | if (%$tf && !$tf->{$type}) { | ||
| 165 | return; | ||
| 166 | } | ||
| 167 | |||
| 168 | # Name filter | ||
| 169 | my $name_re = $self->_name_re; | ||
| 170 | if ($name_re) { | ||
| 171 | my $name = eval { $event->object->metadata->name } | ||
| 172 | // $event->raw->{metadata}{name} // ''; | ||
| 173 | return unless $name =~ $name_re; | ||
| 174 | } | ||
| 175 | |||
| 176 | if ($self->output eq 'json') { | ||
| 177 | print $self->_json->encode({ | ||
| 178 | type => $type, | ||
| 179 | object => $event->raw, | ||
| 180 | }), "\n"; | ||
| 181 | } elsif ($self->output eq 'yaml') { | ||
| 182 | require YAML::XS; | ||
| 183 | print YAML::XS::Dump({ | ||
| 184 | type => $type, | ||
| 185 | object => $event->raw, | ||
| 186 | }); | ||
| 187 | print "---\n"; | ||
| 188 | } else { | ||
| 189 | $self->_print_text($event); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | sub _print_text { | ||
| 194 | my ($self, $event) = @_; | ||
| 195 | my $type = $event->type; | ||
| 196 | my $ts = $self->_timestamp; | ||
| 197 | my $obj = $event->object; | ||
| 198 | |||
| 199 | if ($type eq 'ERROR') { | ||
| 200 | my $msg = $event->raw->{message} // 'unknown error'; | ||
| 201 | my $code = $event->raw->{code} // '?'; | ||
| 202 | printf "%s %-10s ERROR(%s): %s\n", $ts, $type, $code, $msg; | ||
| 203 | return; | ||
| 204 | } | ||
| 205 | |||
| 206 | my $name = eval { $obj->metadata->name } // '?'; | ||
| 207 | my $ns = eval { $obj->metadata->namespace } // ''; | ||
| 208 | my $qualified = $ns ? "$ns/$name" : $name; | ||
| 209 | |||
| 210 | # Try to get a useful status hint | ||
| 211 | my $hint = ''; | ||
| 212 | if ($obj->can('status')) { | ||
| 213 | my $status = eval { $obj->status }; | ||
| 214 | if ($status) { | ||
| 215 | $hint = eval { $status->phase } // ''; | ||
| 216 | if (!$hint && $status->can('readyReplicas')) { | ||
| 217 | my $ready = eval { $status->readyReplicas } // 0; | ||
| 218 | my $desired = eval { $obj->spec->replicas } // '?'; | ||
| 219 | $hint = "${ready}/${desired} ready"; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | printf "%s %-10s %-50s %s\n", $ts, $type, $qualified, $hint; | ||
| 225 | } | ||
| 226 | |||
| 227 | 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 |