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/LWPIO.pm ↑ Summary lib/Kubernetes/REST/V0Group.pm »
1 package Kubernetes::REST::CLI::Watch;
2 3 our $VERSION = '1.110';
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 { do { $event->object->metadata->name } } // $event->raw->{"metadata"}{"name"}) // ""
l!l
20
eval { do { $event->object->metadata->name } } // $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 { do { $obj->metadata->name } } // "?"
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 { do { $obj->metadata->namespace } } // ""
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 { do { $status->phase } } // ""
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 { do { $status->readyReplicas } } // 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 { do { $obj->spec->replicas } } // "?"
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__
« lib/Kubernetes/REST/LWPIO.pm ↑ Summary lib/Kubernetes/REST/V0Group.pm »