K8s.pm

filter: Statementstmt 93.0% 494 / 531 Branchbran 68.3% 235 / 344 Conditioncond 56.9% 127 / 223 MC/DCmcdc 35.2% 48 / 136 Subroutinesub 98.0% 50 / 51 Podpod 66.6% 2 / 3 totaltotal 74.2% 956 / 1288 CC 263 SCAR 36.8
CC
263
Cov
78%
_build_namespaces
142.1
_format_resource_summary
124.3
_discover_resource_plurals
33.1
CRAP 39.6
↑ Summary K8s/Permissions.pm »
1 package MCP::K8s;
2 # ABSTRACT: MCP Server for Kubernetes with RBAC-aware dynamic tools
3 11 our $VERSION = '0.004';
4 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use MCP::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 extends 'MCP::Server';
7 11 ▶use MCP::K8s::Permissions;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Kubernetes::REST;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Kubernetes::REST::Kubeconfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 # Map common resource short names to their plural form for RBAC checking.
17 # This is necessary because RBAC rules use plural resource names (e.g. "pods")
18 # while the Kubernetes::REST API accepts singular Kind names (e.g. "Pod").
19 11 my %RESOURCE_PLURALS = (
20 Pod => 'pods',
21 Service => 'services',
22 Deployment => 'deployments',
23 ReplicaSet => 'replicasets',
24 StatefulSet => 'statefulsets',
25 DaemonSet => 'daemonsets',
26 Job => 'jobs',
27 CronJob => 'cronjobs',
28 ConfigMap => 'configmaps',
29 Secret => 'secrets',
30 Namespace => 'namespaces',
31 Node => 'nodes',
32 PersistentVolume => 'persistentvolumes',
33 PersistentVolumeClaim => 'persistentvolumeclaims',
34 ServiceAccount => 'serviceaccounts',
35 Role => 'roles',
36 RoleBinding => 'rolebindings',
37 ClusterRole => 'clusterroles',
38 ClusterRoleBinding => 'clusterrolebindings',
39 Ingress => 'ingresses',
40 NetworkPolicy => 'networkpolicies',
41 HorizontalPodAutoscaler => 'horizontalpodautoscalers',
42 Event => 'events',
43 Endpoints => 'endpoints',
44 LimitRange => 'limitranges',
45 ResourceQuota => 'resourcequotas',
46 );
47  
48 has context_name => (
49 is => 'ro',
50 lazy => 1,
51 16 ▶ default => sub { $ENV{MCP_K8S_CONTEXT} },
Subroutine __ANON__: called CC=1 SCAR=0.0
52 11 predicate => 1,
53 );
54  
55  
56 11 has kubeconfig_path => (
57 is => 'ro',
58 predicate => 1,
59 );
60  
61  
62 has token => (
63 is => 'ro',
64 lazy => 1,
65 27 ▶ default => sub { $ENV{MCP_K8S_TOKEN} },
Subroutine __ANON__: called CC=1 SCAR=0.0
66 11 predicate => 1,
67 );
68  
69  
70 has server_endpoint => (
71 is => 'ro',
72 lazy => 1,
73 9 ▶ default => sub { $ENV{MCP_K8S_SERVER} },
Subroutine __ANON__: called CC=1 SCAR=0.0
74 11 predicate => 1,
75 );
76  
77  
78 11 has namespaces => (
79 is => 'ro',
80 lazy => 1,
81 builder => '_build_namespaces',
82 );
83  
84  
85 11 has api => (
86 is => 'ro',
87 lazy => 1,
88 builder => '_build_api',
89 );
90  
91  
92 11 has permissions => (
93 is => 'ro',
94 lazy => 1,
95 builder => '_build_permissions',
96 );
97  
98  
99 has _resource_plurals_cache => (
100 is => 'rw',
101 47 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
102 11 );
103  
104 # Kind => 1 (namespaced) / 0 (cluster-scoped), filled by the discovery
105 has _resource_scopes => (
106 is => 'rw',
107 47 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
108 11 );
109  
110 # Plural => 1 (namespaced) / 0 (cluster-scoped), filled by the discovery
111 has _plural_scopes => (
112 is => 'rw',
113 47 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
114 11 );
115  
116 # Whether the discovery ran at all - a failed discovery must not be retried
117 # on every lookup
118 11 has _discovery_done => (
119 is => 'rw',
120 default => 0,
121 );
122  
123 # Stores [tool_ref, base_desc, verb] tuples for lazy description updates
124 has _tool_desc_map => (
125 is => 'rw',
126 47 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
127 11 );
128  
129 11 has _descriptions_updated => (
130 is => 'rw',
131 default => 0,
132 );
133  
134 has json => (
135 is => 'ro',
136 lazy => 1,
137 6 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
138 6 JSON::MaybeXS->new(utf8 => 1, pretty => 1, canonical => 1, convert_blessed => 1);
139 },
140 11 );
141  
142  
143  
144 47 ▶sub server { $_[0] }
Subroutine server: called CC=1 SCAR=0.0
Pod documented
145  
146 47 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=14.9
Pod undocumented
147 47 my ($self) = @_;
148 47 ▶ $self->name('MCP-K8s') if $self->name eq 'PerlServer';
Branch: 1 branch
BranchTrueFalseTotal
if $self->name eq "PerlServer" 47 0 47
149 47 ▶ $self->version($MCP::K8s::VERSION || 'dev') if $self->version eq '1.0.0';
Branch: 1 branch
BranchTrueFalseTotal
if $self->version eq "1.0.0" 47 0 47
Condition50%1/2
$MCP::K8s::VERSION || 'dev'
l!l
470
MC/DC0%0/1
$MCP::K8s::VERSION || 'dev'
$MCP::K8s::VERSION
Truth table50%1/2
A
A= $MCP::K8s::VERSION
Aresult
11
00
150 47 $self->_register_tools;
151 }
152  
153 11 my $IN_CLUSTER_TOKEN_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/token';
154 11 my $IN_CLUSTER_CA_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt';
155 11 my $IN_CLUSTER_NS_PATH = '/var/run/secrets/kubernetes.io/serviceaccount/namespace';
156 11 my $IN_CLUSTER_DEFAULT_SERVER = 'https://kubernetes.default.svc.cluster.local';
157  
158 2 ▶sub _read_file {
Subroutine _read_file: called CC=3 SCAR=11.0
159 2 my ($self, $path) = @_;
160 2 ▶ open my $fh, '<', $path or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or return undef 1 1 2
161 1 my $content = do { local $/; <$fh> };
162 1 close $fh;
163 1 ▶ chomp $content if defined $content;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content 1 0 1
164 1 return $content;
165 }
166  
167 26 ▶sub _build_api {
Subroutine _build_api: called CC=16 SCAR=33.2
168 26 my ($self) = @_;
169  
170 # Materialize the env-backed attributes up-front. The lazy `default`
171 # subs read $ENV{MCP_K8S_TOKEN} / $ENV{MCP_K8S_SERVER}, but `has_token`
172 # and `has_server_endpoint` are still false until the reader is called
173 # once. Gating Tier 1 on the predicates silently skipped Tier 1 when the
174 # token came purely from the environment — see Issue #1.
175 26 my $token = $self->token;
176  
177 # Tier 1: Direct token from MCP_K8S_TOKEN
178 26 ▶ if (defined $token && length $token) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token and length $token) 7 19 26
Condition100%3/3
defined $token and length $token
!ll&&!rl&&r
1727
MC/DC100%2/2
defined $token and length $token
defined $token length $token
Truth table100%3/3
A and B
A= defined $tokenB= length $token
ABresult
0X0
100
111
179 7 my $endpoint = $self->server_endpoint;
180 7 ▶ $endpoint = $IN_CLUSTER_DEFAULT_SERVER
Branch: 1 branch
BranchTrueFalseTotal
unless defined $endpoint and length $endpoint 4 3 7
Condition100%3/3
defined $endpoint and length $endpoint
!ll&&!rl&&r
313
MC/DC100%2/2
defined $endpoint and length $endpoint
defined $endpoint length $endpoint
Truth table100%3/3
A and B
A= defined $endpointB= length $endpoint
ABresult
0X0
100
111
181 unless defined $endpoint && length $endpoint;
182 7 my %server = (endpoint => $endpoint);
183 7 ▶ $server{ssl_ca_file} = $IN_CLUSTER_CA_PATH if -f $IN_CLUSTER_CA_PATH;
Branch: 1 branch
BranchTrueFalseTotal
if -f $IN_CLUSTER_CA_PATH 0 7 7
184 7 return Kubernetes::REST->new(
185 server => \%server,
186 credentials => { token => $token },
187 );
188 }
189  
190 # Tier 2: In-cluster service account token
191 19 ▶ if (-f $IN_CLUSTER_TOKEN_PATH) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $IN_CLUSTER_TOKEN_PATH) 0 19 19
192 0 my $sa_token = $self->_read_file($IN_CLUSTER_TOKEN_PATH);
193 0 ▶ if (defined $sa_token && length $sa_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sa_token and length $sa_token) 0 0 0
Condition0%0/3
defined $sa_token and length $sa_token
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sa_token and length $sa_token
defined $sa_token length $sa_token
Truth table0%0/3
A and B
A= defined $sa_tokenB= length $sa_token
ABresult
0X0
100
111
194 0 my $endpoint = $self->server_endpoint;
195 0 ▶ $endpoint = $IN_CLUSTER_DEFAULT_SERVER
Branch: 1 branch
BranchTrueFalseTotal
unless defined $endpoint and length $endpoint 0 0 0
Condition0%0/3
defined $endpoint and length $endpoint
!ll&&!rl&&r
000
MC/DC0%0/2
defined $endpoint and length $endpoint
defined $endpoint length $endpoint
Truth table0%0/3
A and B
A= defined $endpointB= length $endpoint
ABresult
0X0
100
111
196 unless defined $endpoint && length $endpoint;
197 0 my %server = (endpoint => $endpoint);
198 0 ▶ $server{ssl_ca_file} = $IN_CLUSTER_CA_PATH if -f $IN_CLUSTER_CA_PATH;
Branch: 1 branch
BranchTrueFalseTotal
if -f $IN_CLUSTER_CA_PATH 0 0 0
199 0 return Kubernetes::REST->new(
200 server => \%server,
201 credentials => { token => $sa_token },
202 );
203 }
204 }
205  
206 # Tier 3: Kubeconfig. Same predicate-with-lazy-default trap as Tier 1:
207 # `has_context_name` stays false until the reader fires, so an env-only
208 # MCP_K8S_CONTEXT would be silently dropped. Pass the materialized value
209 # through, dropping empty/undef so Kubeconfig keeps its current-context
210 # fallback when nothing is set. kubeconfig_path has no env default, but
211 # the same gate applies: Kubeconfig's own kubeconfig_path default is a
212 # plain (non-lazy) default, so an explicit undef or '' would override
213 # its KUBECONFIG / ~/.kube/config lookup instead of leaving it alone.
214 19 my $context_name = $self->context_name;
215 19 my $kubeconfig_path = $self->kubeconfig_path;
216 19 my %kc_args;
217 19 ▶ $kc_args{context_name} = $context_name
Branch: 1 branch
BranchTrueFalseTotal
if defined $context_name and length $context_name 6 13 19
Condition100%3/3
defined $context_name and length $context_name
!ll&&!rl&&r
1126
MC/DC100%2/2
defined $context_name and length $context_name
defined $context_name length $context_name
Truth table100%3/3
A and B
A= defined $context_nameB= length $context_name
ABresult
0X0
100
111
218 if defined $context_name && length $context_name;
219 19 ▶ $kc_args{kubeconfig_path} = $kubeconfig_path
Branch: 1 branch
BranchTrueFalseTotal
if defined $kubeconfig_path and length $kubeconfig_path 3 16 19
Condition100%3/3
defined $kubeconfig_path and length $kubeconfig_path
!ll&&!rl&&r
1513
MC/DC100%2/2
defined $kubeconfig_path and length $kubeconfig_path
defined $kubeconfig_path length $kubeconfig_path
Truth table100%3/3
A and B
A= defined $kubeconfig_pathB= length $kubeconfig_path
ABresult
0X0
100
111
220 if defined $kubeconfig_path && length $kubeconfig_path;
221 19 my $kc = Kubernetes::REST::Kubeconfig->new(%kc_args);
222 19 return $kc->api;
223 }
224  
225 1 ▶sub _build_namespaces {
Subroutine _build_namespaces: called CC=14 SCAR=49.6
226 1 my ($self) = @_;
227  
228 # From environment variable
229 1 ▶ if (my $env = $ENV{MCP_K8S_NAMESPACES}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $env = $ENV{"MCP_K8S_NAMESPACES"}) 1 0 1
230 1 my @ns = grep { length } split /\s*,\s*/, $env =~ s/^\s+|\s+$//gr;
231 1 ▶ return \@ns if @ns;
Branch: 1 branch
BranchTrueFalseTotal
if @ns 1 0 1
232 }
233  
234 # In-cluster: read mounted namespace as default
235 0 ▶ if (-f $IN_CLUSTER_NS_PATH) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $IN_CLUSTER_NS_PATH) 0 0 0
236 0 my $ns = $self->_read_file($IN_CLUSTER_NS_PATH);
237 0 ▶ if (defined $ns && length $ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ns and length $ns) 0 0 0
Condition0%0/3
defined $ns and length $ns
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ns and length $ns
defined $ns length $ns
Truth table0%0/3
A and B
A= defined $nsB= length $ns
ABresult
0X0
100
111
238 # Try to discover more, but use in-cluster namespace as fallback
239 0 my $list = eval { $self->api->list('Namespace') };
240 0 ▶ if (!$@ && $list) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ and $list) 0 0 0
Condition0%0/3
not $@ and $list
!ll&&!rl&&r
000
MC/DC0%0/2
not $@ and $list
not $@ $list
Truth table0%0/3
not(A) and B
A= not $@B= $list
ABresult
0X0
100
111
241 0 ▶ my @ns = map { $_->metadata->name } @{ $list->items // [] };
Condition0%0/2
$list->items // []
l!l
00
MC/DC0%0/1
$list->items // []
$list->items
Truth table0%0/2
A
A= $list->items
Aresult
11
00
242 0 ▶ return \@ns if @ns;
Branch: 1 branch
BranchTrueFalseTotal
if @ns 0 0 0
243 }
244 0 return [$ns];
245 }
246 }
247  
248 # Auto-discover from cluster
249 0 my $list = eval { $self->api->list('Namespace') };
250 0 ▶ if ($@ || !$list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $list) 0 0 0
Condition0%0/3
$@ or not $list
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $list
$@ not $list
Truth table0%0/3
A or not(B)
A= $@B= not $list
ABresult
1X1
011
000
251 0 return ['default'];
252 }
253  
254 0 ▶ my @ns = map { $_->metadata->name } @{ $list->items // [] };
Condition0%0/2
$list->items // []
l!l
00
MC/DC0%0/1
$list->items // []
$list->items
Truth table0%0/2
A
A= $list->items
Aresult
11
00
255 0 ▶ return @ns ? \@ns : ['default'];
Branch: 1 branch
BranchTrueFalseTotal
@ns ? : 0 0 0
256 }
257  
258 9 ▶sub _build_permissions {
Subroutine _build_permissions: called CC=1 SCAR=0.0
259 9 my ($self) = @_;
260  
261 # The resolver tells the RBAC discovery which resources are cluster-scoped,
262 # so a namespaced resource never lands in the cluster-scope map. It holds a
263 # weak reference back to the server: a strong one would make the server and
264 # its Permissions object keep each other alive.
265 9 my $weak_self = $self;
266 9 weaken($weak_self);
267  
268 return MCP::K8s::Permissions->new(
269 api => $self->api,
270 namespaces => $self->namespaces,
271 161 ▶ scope_resolver => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
272 161 my ($plural) = @_;
273 161 ▶ return undef unless $weak_self;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak_self 0 161 161
274 161 return $weak_self->_scope_of_plural($plural);
275 },
276 9 );
277 }
278  
279 25 ▶sub _to_json {
Subroutine _to_json: called CC=1 SCAR=0.0
280 25 my ($self, $data) = @_;
281 25 return $self->json->encode($data);
282 }
283  
284 93 ▶sub _resource_plural {
Subroutine _resource_plural: called CC=9 SCAR=22.0
285 93 my ($self, $resource) = @_;
286  
287  
288 # Tier 1: Static map (fast path)
289 93 ▶ return $RESOURCE_PLURALS{$resource} if $RESOURCE_PLURALS{$resource};
Branch: 1 branch
BranchTrueFalseTotal
if $RESOURCE_PLURALS{$resource} 66 27 93
290  
291 # Tier 2: resource_plural() on the IO::K8s class the Kind resolves to.
292 # Built-in Kinds resolve from the client's built-in map without a request;
293 # an unknown Kind costs the client's aggregated discovery (GET /api, /apis),
294 # which the tool's own API call right after would trigger anyway. A Kind
295 # nothing ships resolves to a fabricated name or IO::K8s::Unstructured,
296 # neither of which can resource_plural, so it falls through to tier 3.
297 27 my $class = eval { $self->api->expand_class($resource) };
298 27 ▶ if ($class && $class->can('resource_plural')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class and $class->can("resource_plural")) 8 19 27
Condition100%3/3
$class and $class->can("resource_plural")
!ll&&!rl&&r
1188
MC/DC100%2/2
$class and $class->can("resource_plural")
$class $class->can("resource_plural")
Truth table100%3/3
A and B
A= $classB= $class->can("resource_plural")
ABresult
0X0
100
111
299 8 my $plural = eval { $class->resource_plural };
300 8 ▶ return $plural if defined $plural && length $plural;
Branch: 1 branch
BranchTrueFalseTotal
if defined $plural and length $plural 8 0 8
Condition33%1/3
defined $plural and length $plural
!ll&&!rl&&r
008
MC/DC0%0/2
defined $plural and length $plural
defined $plural length $plural
Truth table33%1/3
A and B
A= defined $pluralB= length $plural
ABresult
0X0
100
111
301 }
302  
303 # Tier 3: API server discovery (cached)
304 19 my $cache = $self->_resource_plurals_cache;
305 19 ▶ unless ($self->_discovery_done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_discovery_done) 3 16 19
306 3 $self->_discover_resource_plurals;
307 3 $cache = $self->_resource_plurals_cache;
308 }
309 19 ▶ return $cache->{$resource} if $cache->{$resource};
Branch: 1 branch
BranchTrueFalseTotal
if $$cache{$resource} 7 12 19
310  
311 # Tier 4: Heuristic fallback
312 12 my $plural = lc($resource);
313 12 ▶ $plural .= 's' unless $plural =~ /s$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $plural =~ /s$/ 11 1 12
314 12 $plural =~ s/([^aeiou])ys$/$1ies/;
315 12 return $plural;
316 }
317  
318 12 ▶sub _discover_resource_plurals {
Subroutine _discover_resource_plurals: called CC=25 SCAR=35.0
319 12 my ($self) = @_;
320  
321  
322 12 my %cache;
323 my %kind_scopes;
324 12 my %plural_scopes;
325  
326 # Query /api/v1 for core resources
327 12 my $core = eval {
328 12 my $resp = $self->api->_request('GET', '/api/v1');
329 4 ▶ return undef if !$resp || $resp->status >= 400;
Branch: 1 branch
BranchTrueFalseTotal
if not $resp or $resp->status >= 400 1 3 4
Condition66%2/3
not $resp or $resp->status >= 400
l!l&&r!l&&!r
013
MC/DC50%1/2
not $resp or $resp->status >= 400
not $resp $resp->status >= 400
Truth table66%2/3
not(A) or B
A= not $respB= $resp->status >= 400
ABresult
1X1
011
000
330 3 JSON::MaybeXS->new->decode($resp->content);
331 };
332 12 ▶ if ($core && $core->{resources}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($core and $$core{"resources"}) 3 9 12
Condition66%2/3
$core and $$core{"resources"}
!ll&&!rl&&r
903
MC/DC50%1/2
$core and $$core{"resources"}
$core $$core{"resources"}
Truth table66%2/3
A and B
A= $coreB= $$core{"resources"}
ABresult
0X0
100
111
333 3 for my $r (@{ $core->{resources} }) {
334 15 ▶ next if ($r->{name} // '') =~ m{/}; # skip subresources
Branch: 1 branch
BranchTrueFalseTotal
if ($$r{"name"} // "") =~ m[/] 3 12 15
Condition50%1/2
$$r{"name"} // ""
l!l
150
MC/DC0%0/1
$$r{"name"} // ""
$$r{"name"}
Truth table50%1/2
A
A= $$r{"name"}
Aresult
11
00
335 12 ▶ $cache{ $r->{kind} } = $r->{name} if $r->{kind} && $r->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $$r{"kind"} and $$r{"name"} 12 0 12
Condition33%1/3
$$r{"kind"} and $$r{"name"}
!ll&&!rl&&r
0012
MC/DC0%0/2
$$r{"kind"} and $$r{"name"}
$$r{"kind"} $$r{"name"}
Truth table33%1/3
A and B
A= $$r{"kind"}B= $$r{"name"}
ABresult
0X0
100
111
336 12 $self->_record_scope(\%kind_scopes, \%plural_scopes, $r);
337 }
338 }
339  
340 # Query /apis for API groups
341 12 my $groups = eval {
342 12 my $resp = $self->api->_request('GET', '/apis');
343 4 ▶ return undef if !$resp || $resp->status >= 400;
Branch: 1 branch
BranchTrueFalseTotal
if not $resp or $resp->status >= 400 1 3 4
Condition66%2/3
not $resp or $resp->status >= 400
l!l&&r!l&&!r
013
MC/DC50%1/2
not $resp or $resp->status >= 400
not $resp $resp->status >= 400
Truth table66%2/3
not(A) or B
A= not $respB= $resp->status >= 400
ABresult
1X1
011
000
344 3 JSON::MaybeXS->new->decode($resp->content);
345 };
346 12 ▶ if ($groups && $groups->{groups}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($groups and $$groups{"groups"}) 3 9 12
Condition66%2/3
$groups and $$groups{"groups"}
!ll&&!rl&&r
903
MC/DC50%1/2
$groups and $$groups{"groups"}
$groups $$groups{"groups"}
Truth table66%2/3
A and B
A= $groupsB= $$groups{"groups"}
ABresult
0X0
100
111
347 3 for my $group (@{ $groups->{groups} }) {
348 3 ▶ my $pv = $group->{preferredVersion}{groupVersion} // next;
Condition50%1/2
$$group{"preferredVersion"}{"groupVersion"} // (next)
l!l
30
MC/DC0%0/1
$$group{"preferredVersion"}{"groupVersion"} // (next)
$$group{"preferredVersion"}{"groupVersion"}
Truth table50%1/2
A
A= $$group{"preferredVersion"}{"groupVersion"}
Aresult
11
00
349 3 my $group_resources = eval {
350 3 my $resp = $self->api->_request('GET', "/apis/$pv");
351 3 ▶ return undef if !$resp || $resp->status >= 400;
Branch: 1 branch
BranchTrueFalseTotal
if not $resp or $resp->status >= 400 0 3 3
Condition33%1/3
not $resp or $resp->status >= 400
l!l&&r!l&&!r
003
MC/DC0%0/2
not $resp or $resp->status >= 400
not $resp $resp->status >= 400
Truth table33%1/3
not(A) or B
A= not $respB= $resp->status >= 400
ABresult
1X1
011
000
352 3 JSON::MaybeXS->new->decode($resp->content);
353 };
354 3 ▶ if ($group_resources && $group_resources->{resources}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_resources and $$group_resources{"resources"}) 3 0 3
Condition33%1/3
$group_resources and $$group_resources{"resources"}
!ll&&!rl&&r
003
MC/DC0%0/2
$group_resources and $$group_resources{"resources"}
$group_resources $$group_resources{"resources"}
Truth table33%1/3
A and B
A= $group_resourcesB= $$group_resources{"resources"}
ABresult
0X0
100
111
355 3 for my $r (@{ $group_resources->{resources} }) {
356 5 ▶ next if ($r->{name} // '') =~ m{/};
Branch: 1 branch
BranchTrueFalseTotal
if ($$r{"name"} // "") =~ m[/] 0 5 5
Condition50%1/2
$$r{"name"} // ""
l!l
50
MC/DC0%0/1
$$r{"name"} // ""
$$r{"name"}
Truth table50%1/2
A
A= $$r{"name"}
Aresult
11
00
357 5 ▶ $cache{ $r->{kind} } = $r->{name} if $r->{kind} && $r->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $$r{"kind"} and $$r{"name"} 5 0 5
Condition33%1/3
$$r{"kind"} and $$r{"name"}
!ll&&!rl&&r
005
MC/DC0%0/2
$$r{"kind"} and $$r{"name"}
$$r{"kind"} $$r{"name"}
Truth table33%1/3
A and B
A= $$r{"kind"}B= $$r{"name"}
ABresult
0X0
100
111
358 5 $self->_record_scope(\%kind_scopes, \%plural_scopes, $r);
359 }
360 }
361 }
362 }
363  
364 12 $self->_resource_plurals_cache(\%cache);
365 12 $self->_resource_scopes(\%kind_scopes);
366 12 $self->_plural_scopes(\%plural_scopes);
367 12 $self->_discovery_done(1);
368 }
369  
370 17 ▶sub _record_scope {
Subroutine _record_scope: called CC=8 SCAR=21.2
371 17 my ($self, $kind_scopes, $plural_scopes, $resource) = @_;
372  
373 17 my $namespaced = $resource->{namespaced};
374 17 ▶ return unless defined $namespaced;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $namespaced 0 17 17
375  
376 17 ▶ my $scope = $namespaced ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$namespaced ? : 14 3 17
377 17 ▶ $kind_scopes->{ $resource->{kind} } = $scope if $resource->{kind};
Branch: 1 branch
BranchTrueFalseTotal
if $$resource{"kind"} 17 0 17
378  
379 # A plural that is namespaced in one API group and cluster-scoped in
380 # another is ambiguous. The namespaced reading wins: it is the one that
381 # keeps the resource out of the cluster-scope map.
382 17 ▶ my $plural = $resource->{name} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $plural = $$resource{"name"} or return 17 0 17
383 17 ▶ $plural_scopes->{$plural} = 1 if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 14 3 17
384 17 ▶ $plural_scopes->{$plural} = 0 if !$scope && !exists $plural_scopes->{$plural};
Branch: 1 branch
BranchTrueFalseTotal
if not $scope || exists $$plural_scopes{$plural} 3 14 17
Condition66%2/3
$scope || exists $$plural_scopes{$plural}
l!l&&r!l&&!r
1403
MC/DC50%1/2
$scope || exists $$plural_scopes{$plural}
$scope exists $$plural_scopes{$plural}
Truth table66%2/3
A || B
A= $scopeB= exists $$plural_scopes{$plural}
ABresult
1X1
011
000
385 }
386  
387 41 ▶sub _resource_scope {
Subroutine _resource_scope: called CC=7 SCAR=23.2
388 41 my ($self, $resource) = @_;
389  
390  
391 # The IO::K8s class carries its scope - no request needed.
392 41 my $class = eval { $self->api->expand_class($resource) };
393 41 ▶ if ($class && $class->can('resource_plural')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class and $class->can("resource_plural")) 0 41 41
Condition33%1/3
$class and $class->can("resource_plural")
!ll&&!rl&&r
0410
MC/DC0%0/2
$class and $class->can("resource_plural")
$class $class->can("resource_plural")
Truth table33%1/3
A and B
A= $classB= $class->can("resource_plural")
ABresult
0X0
100
111
394 0 ▶ my $scope = eval { $class->does('IO::K8s::Role::Namespaced') ? 1 : 0 };
Branch: 1 branch
BranchTrueFalseTotal
$class->does('IO::K8s::Role::Namespaced') ? : 0 0 0
395 0 ▶ return $scope if defined $scope;
Branch: 1 branch
BranchTrueFalseTotal
if defined $scope 0 0 0
396 }
397  
398 # Fall back to the discovery cache (CRDs without a class).
399 41 ▶ unless ($self->_discovery_done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_discovery_done) 5 36 41
400 5 $self->_discover_resource_plurals;
401 }
402 41 my $cache = $self->_resource_scopes;
403 41 ▶ return $cache->{$resource} if exists $cache->{$resource};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$cache{$resource} 4 37 41
404  
405 37 return undef;
406 }
407  
408 161 ▶sub _scope_of_plural {
Subroutine _scope_of_plural: called CC=3 SCAR=11.0
409 161 my ($self, $plural) = @_;
410  
411  
412 161 ▶ unless ($self->_discovery_done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_discovery_done) 4 157 161
413 4 $self->_discover_resource_plurals;
414 }
415 161 my $cache = $self->_plural_scopes;
416 161 ▶ return $cache->{$plural} if exists $cache->{$plural};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$cache{$plural} 6 155 161
417  
418 155 return undef;
419 }
420  
421 51 ▶sub _resolve_namespace {
Subroutine _resolve_namespace: called CC=4 SCAR=13.9
422 51 my ($self, $args) = @_;
423  
424  
425 51 my $ns = $args->{namespace};
426 51 ▶ return $ns if defined $ns && length $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns and length $ns 42 9 51
Condition100%3/3
defined $ns and length $ns
!ll&&!rl&&r
8142
MC/DC100%2/2
defined $ns and length $ns
defined $ns length $ns
Truth table100%3/3
A and B
A= defined $nsB= length $ns
ABresult
0X0
100
111
427  
428 # Auto-fill if only one namespace accessible
429 9 my @allowed = $self->permissions->allowed_namespaces;
430 9 ▶ return $allowed[0] if @allowed == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @allowed == 1 5 4 9
431  
432 4 return undef;
433 }
434  
435 41 ▶sub _scope_context {
Subroutine _scope_context: called CC=9 SCAR=22.0
436 41 my ($self, $args, $resource) = @_;
437  
438  
439 41 my $scope = $self->_resource_scope($resource);
440 41 ▶ my $cluster = defined $scope && !$scope ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $scope && !$scope ? : 3 38 41
Condition100%3/3
defined $scope and not $scope
!ll&&!rl&&r
3713
MC/DC100%2/2
defined $scope and not $scope
defined $scope not $scope
Truth table100%3/3
A and not(B)
A= defined $scopeB= not $scope
ABresult
0X0
100
111
441 41 ▶ my $ns = $cluster ? undef : $self->_resolve_namespace($args);
Branch: 1 branch
BranchTrueFalseTotal
$cluster ? : 3 38 41
442  
443 41 my $error;
444 41 ▶ if (!$cluster && !defined $ns && defined $scope) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cluster || defined $ns and defined $scope) 1 40 41
Condition83%5/6
not $cluster || defined $ns and defined $scope
!ll&&!rl&&r
4001
$cluster || defined $ns
l!l&&r!l&&!r
3371
MC/DC66%2/3
not $cluster || defined $ns and defined $scope
$cluster defined $ns defined $scope
Truth table75%3/4
not(A || B) and C
A= $clusterB= defined $nsC= defined $scope
ABCresult
1XX0
01X0
0000
0011
445 1 $error = $self->_namespace_required_message($resource);
446 }
447  
448 return {
449 41 ▶ scope => $scope,
Branch: 1 branch
BranchTrueFalseTotal
$cluster ? : 3 38 41
Condition100%2/2
$ns // ""
l!l
371
MC/DC100%1/1
$ns // ""
$ns
Truth table100%2/2
A
A= $ns
Aresult
11
00
450 cluster => $cluster,
451 ns => $ns,
452 perm_ns => $cluster ? '' : ($ns // ''),
453 error => $error,
454 };
455 }
456  
457 1 ▶sub _namespace_required_message {
Subroutine _namespace_required_message: called CC=2 SCAR=7.0
458 1 my ($self, $resource) = @_;
459  
460 1 my @available = $self->permissions->allowed_namespaces;
461 1 ▶ my $hint = @available
Branch: 1 branch
BranchTrueFalseTotal
@available ? : 1 0 1
462 ? ' (available: '.join(', ', @available).')'
463 : '';
464  
465 1 return "namespace required: $resource is a namespaced resource, specify a namespace".$hint;
466 }
467  
468 13 ▶sub _is_conflict_error {
Subroutine _is_conflict_error: called CC=6 SCAR=17.9
469 13 my ($self, $err) = @_;
470  
471  
472 13 ▶ return 0 unless defined $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 1 12 13
473 12 ▶ return $err->is_conflict ? 1 : 0
Branch: 2 branches
BranchTrueFalseTotal
if builtin::blessed($err) and $err->isa("Kubernetes::REST::APIError") 2 10 12
$err->is_conflict ? : 1 1 2
Condition66%2/3
builtin::blessed($err) and $err->isa("Kubernetes::REST::APIError")
!ll&&!rl&&r
1002
MC/DC50%1/2
builtin::blessed($err) and $err->isa("Kubernetes::REST::APIError")
builtin::blessed($err) $err->isa("Kubernetes::REST::APIError")
Truth table66%2/3
A and B
A= builtin::blessed($err)B= $err->isa("Kubernetes::REST::APIError")
ABresult
0X0
100
111
474 if blessed($err) && $err->isa('Kubernetes::REST::APIError');
475 10 ▶ return "$err" =~ /409|AlreadyExists/i ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
"$err" =~ /409|AlreadyExists/i ? : 7 3 10
476 }
477  
478 22 ▶sub _format_resource_summary {
Subroutine _format_resource_summary: called CC=56 SCAR=48.2
479 22 my ($self, $obj) = @_;
480  
481  
482 22 my %summary;
483  
484 22 ▶ if ($obj->can('metadata') && $obj->metadata) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->can("metadata") and $obj->metadata) 22 0 22
Condition33%1/3
$obj->can("metadata") and $obj->metadata
!ll&&!rl&&r
0022
MC/DC0%0/2
$obj->can("metadata") and $obj->metadata
$obj->can("metadata") $obj->metadata
Truth table33%1/3
A and B
A= $obj->can("metadata")B= $obj->metadata
ABresult
0X0
100
111
485 22 my $meta = $obj->metadata;
486 22 ▶ $summary{name} = $meta->name if $meta->can('name');
Branch: 1 branch
BranchTrueFalseTotal
if $meta->can("name") 22 0 22
487 22 ▶ $summary{namespace} = $meta->namespace if $meta->can('namespace') && $meta->namespace;
Branch: 1 branch
BranchTrueFalseTotal
if $meta->can("namespace") and $meta->namespace 18 4 22
Condition100%3/3
$meta->can("namespace") and $meta->namespace
!ll&&!rl&&r
1318
MC/DC100%2/2
$meta->can("namespace") and $meta->namespace
$meta->can("namespace") $meta->namespace
Truth table100%3/3
A and B
A= $meta->can("namespace")B= $meta->namespace
ABresult
0X0
100
111
488 22 ▶ $summary{labels} = $meta->labels if $meta->can('labels') && $meta->labels;
Branch: 1 branch
BranchTrueFalseTotal
if $meta->can("labels") and $meta->labels 4 18 22
Condition100%3/3
$meta->can("labels") and $meta->labels
!ll&&!rl&&r
5134
MC/DC100%2/2
$meta->can("labels") and $meta->labels
$meta->can("labels") $meta->labels
Truth table100%3/3
A and B
A= $meta->can("labels")B= $meta->labels
ABresult
0X0
100
111
489 22 ▶ $summary{creationTimestamp} = $meta->creationTimestamp if $meta->can('creationTimestamp') && $meta->creationTimestamp;
Branch: 1 branch
BranchTrueFalseTotal
if $meta->can("creationTimestamp") and $meta->creationTimestamp 1 21 22
Condition100%3/3
$meta->can("creationTimestamp") and $meta->creationTimestamp
!ll&&!rl&&r
5161
MC/DC100%2/2
$meta->can("creationTimestamp") and $meta->creationTimestamp
$meta->can("creationTimestamp") $meta->creationTimestamp
Truth table100%3/3
A and B
A= $meta->can("creationTimestamp")B= $meta->creationTimestamp
ABresult
0X0
100
111
490 }
491  
492 22 ▶ if ($obj->can('kind') && $obj->kind) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->can("kind") and $obj->kind) 22 0 22
Condition33%1/3
$obj->can("kind") and $obj->kind
!ll&&!rl&&r
0022
MC/DC0%0/2
$obj->can("kind") and $obj->kind
$obj->can("kind") $obj->kind
Truth table33%1/3
A and B
A= $obj->can("kind")B= $obj->kind
ABresult
0X0
100
111
493 22 $summary{kind} = $obj->kind;
494 }
495  
496 # Events carry their payload at the top level (core/v1) or under
497 # note/regarding (events.k8s.io), not in spec/status.
498 22 ▶ if (($obj->can('kind') && ($obj->kind // '') eq 'Event')
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->can("kind") and ($obj->kind // "") eq "Event" or $obj->can("involvedObject") or $obj->can("regarding")) 5 17 22
Condition63%7/11
$obj->can("kind") and ($obj->kind // "") eq "Event" or $obj->can("involvedObject") or $obj->can("regarding")
l!l&&r!l&&!r
5017
$obj->can("kind") and ($obj->kind // "") eq "Event" or $obj->can("involvedObject")
l!l&&r!l&&!r
5017
$obj->can("kind") and ($obj->kind // "") eq "Event"
!ll&&!rl&&r
0175
$obj->kind // ""
l!l
220
MC/DC0%0/4
$obj->can("kind") and ($obj->kind // "") eq "Event" or $obj->can("involvedObject") or $obj->can("regarding")
$obj->can("kind") $obj->kind $obj->can("involvedObject") $obj->can("regarding")
Truth table14%1/7
A and B or C or D
A= $obj->can("kind")B= $obj->kindC= $obj->can("involvedObject")D= $obj->can("regarding")
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
499 || $obj->can('involvedObject') || $obj->can('regarding')) {
500 5 for my $f (qw( type reason message note count firstTimestamp lastTimestamp eventTime )) {
501 40 ▶ next unless $obj->can($f);
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->can($f) 33 7 40
502 7 my $v = $obj->$f;
503 7 ▶ $summary{$f} = $v if defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and $v ne "" 5 2 7
Condition66%2/3
defined $v and $v ne ""
!ll&&!rl&&r
205
MC/DC50%1/2
defined $v and $v ne ""
defined $v $v ne ""
Truth table66%2/3
A and B
A= defined $vB= $v ne ""
ABresult
0X0
100
111
504 }
505 5 for my $f (qw( involvedObject regarding )) {
506 10 ▶ next unless $obj->can($f) && $obj->$f;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->can($f) and $obj->$f 9 1 10
Condition66%2/3
$obj->can($f) and $obj->$f
!ll&&!rl&&r
901
MC/DC50%1/2
$obj->can($f) and $obj->$f
$obj->can($f) $obj->$f
Truth table66%2/3
A and B
A= $obj->can($f)B= $obj->$f
ABresult
0X0
100
111
507 1 my $ref = $obj->$f;
508 1 ▶ my %r = map { $ref->can($_) && defined $ref->$_ ? ( $_ => $ref->$_ ) : () } qw( kind name );
Branch: 1 branch
BranchTrueFalseTotal
$ref->can($_) && defined $ref->$_ ? : 2 0 2
Condition33%1/3
$ref->can($_) and defined $ref->$_
!ll&&!rl&&r
002
MC/DC0%0/2
$ref->can($_) and defined $ref->$_
$ref->can($_) defined $ref->$_
Truth table33%1/3
A and B
A= $ref->can($_)B= defined $ref->$_
ABresult
0X0
100
111
509 1 ▶ $summary{$f} = \%r if %r;
Branch: 1 branch
BranchTrueFalseTotal
if %r 1 0 1
510 }
511 }
512  
513 22 ▶ if ($obj->can('status') && $obj->status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->can("status") and $obj->status) 13 9 22
Condition100%3/3
$obj->can("status") and $obj->status
!ll&&!rl&&r
6313
MC/DC100%2/2
$obj->can("status") and $obj->status
$obj->can("status") $obj->status
Truth table100%3/3
A and B
A= $obj->can("status")B= $obj->status
ABresult
0X0
100
111
514 13 my $status = $obj->status;
515 13 ▶ $summary{phase} = $status->phase if $status->can('phase') && $status->phase;
Branch: 1 branch
BranchTrueFalseTotal
if $status->can("phase") and $status->phase 13 0 13
Condition33%1/3
$status->can("phase") and $status->phase
!ll&&!rl&&r
0013
MC/DC0%0/2
$status->can("phase") and $status->phase
$status->can("phase") $status->phase
Truth table33%1/3
A and B
A= $status->can("phase")B= $status->phase
ABresult
0X0
100
111
516 13 ▶ $summary{replicas} = $status->replicas if $status->can('replicas') && defined $status->replicas;
Branch: 1 branch
BranchTrueFalseTotal
if $status->can("replicas") and defined $status->replicas 0 13 13
Condition33%1/3
$status->can("replicas") and defined $status->replicas
!ll&&!rl&&r
1300
MC/DC0%0/2
$status->can("replicas") and defined $status->replicas
$status->can("replicas") defined $status->replicas
Truth table33%1/3
A and B
A= $status->can("replicas")B= defined $status->replicas
ABresult
0X0
100
111
517 13 ▶ $summary{readyReplicas} = $status->readyReplicas if $status->can('readyReplicas') && defined $status->readyReplicas;
Branch: 1 branch
BranchTrueFalseTotal
if $status->can("readyReplicas") and defined $status->readyReplicas 0 13 13
Condition33%1/3
$status->can("readyReplicas") and defined $status->readyReplicas
!ll&&!rl&&r
1300
MC/DC0%0/2
$status->can("readyReplicas") and defined $status->readyReplicas
$status->can("readyReplicas") defined $status->readyReplicas
Truth table33%1/3
A and B
A= $status->can("readyReplicas")B= defined $status->readyReplicas
ABresult
0X0
100
111
518 13 ▶ $summary{availableReplicas} = $status->availableReplicas if $status->can('availableReplicas') && defined $status->availableReplicas;
Branch: 1 branch
BranchTrueFalseTotal
if $status->can("availableReplicas") and defined $status->availableReplicas 0 13 13
Condition33%1/3
$status->can("availableReplicas") and defined $status->availableReplicas
!ll&&!rl&&r
1300
MC/DC0%0/2
$status->can("availableReplicas") and defined $status->availableReplicas
$status->can("availableReplicas") defined $status->availableReplicas
Truth table33%1/3
A and B
A= $status->can("availableReplicas")B= defined $status->availableReplicas
ABresult
0X0
100
111
519 13 ▶ $summary{conditions} = $status->conditions if $status->can('conditions') && $status->conditions;
Branch: 1 branch
BranchTrueFalseTotal
if $status->can("conditions") and $status->conditions 0 13 13
Condition33%1/3
$status->can("conditions") and $status->conditions
!ll&&!rl&&r
1300
MC/DC0%0/2
$status->can("conditions") and $status->conditions
$status->can("conditions") $status->conditions
Truth table33%1/3
A and B
A= $status->can("conditions")B= $status->conditions
ABresult
0X0
100
111
520 }
521  
522 22 ▶ if ($obj->can('spec') && $obj->spec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->can("spec") and $obj->spec) 2 20 22
Condition100%3/3
$obj->can("spec") and $obj->spec
!ll&&!rl&&r
8122
MC/DC100%2/2
$obj->can("spec") and $obj->spec
$obj->can("spec") $obj->spec
Truth table100%3/3
A and B
A= $obj->can("spec")B= $obj->spec
ABresult
0X0
100
111
523 2 my $spec = $obj->spec;
524 2 ▶ $summary{replicas} //= $spec->replicas if $spec->can('replicas') && defined $spec->replicas;
Branch: 1 branch
BranchTrueFalseTotal
if $spec->can("replicas") and defined $spec->replicas 0 2 2
Condition16%1/6
$spec->can("replicas") and defined $spec->replicas
!ll&&!rl&&r
200
$summary{"replicas"} //= $spec->replicas
l!l&&r!l&&!r
000
MC/DC0%0/2
$spec->can("replicas") and defined $spec->replicas
$spec->can("replicas") defined $spec->replicas
MC/DC0%0/2
$summary{"replicas"} //= $spec->replicas
$summary{"replicas"} $spec->replicas
Truth table33%1/3
A and B
A= $spec->can("replicas")B= defined $spec->replicas
ABresult
0X0
100
111
Truth table0%0/3
A //= B
A= $summary{"replicas"}B= $spec->replicas
ABresult
1X1
011
000
525 2 ▶ if ($spec->can('containers') && $spec->containers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->can("containers") and $spec->containers) 1 1 2
Condition66%2/3
$spec->can("containers") and $spec->containers
!ll&&!rl&&r
101
MC/DC50%1/2
$spec->can("containers") and $spec->containers
$spec->can("containers") $spec->containers
Truth table66%2/3
A and B
A= $spec->can("containers")B= $spec->containers
ABresult
0X0
100
111
526 1 $summary{containers} = [ map { $_->name } @{ $spec->containers } ];
527 }
528 2 ▶ if ($spec->can('type') && $spec->type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->can("type") and $spec->type) 1 1 2
Condition66%2/3
$spec->can("type") and $spec->type
!ll&&!rl&&r
101
MC/DC50%1/2
$spec->can("type") and $spec->type
$spec->can("type") $spec->type
Truth table66%2/3
A and B
A= $spec->can("type")B= $spec->type
ABresult
0X0
100
111
529 1 $summary{type} = $spec->type;
530 }
531 2 ▶ if ($spec->can('ports') && $spec->ports) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->can("ports") and $spec->ports) 1 1 2
Condition66%2/3
$spec->can("ports") and $spec->ports
!ll&&!rl&&r
101
MC/DC50%1/2
$spec->can("ports") and $spec->ports
$spec->can("ports") $spec->ports
Truth table66%2/3
A and B
A= $spec->can("ports")B= $spec->ports
ABresult
0X0
100
111
532 $summary{ports} = [ map {
533 2 my $p = $_;
534 2 my %port;
535 2 ▶ $port{port} = $p->port if $p->can('port') && defined $p->port;
Branch: 1 branch
BranchTrueFalseTotal
if $p->can("port") and defined $p->port 2 0 2
Condition33%1/3
$p->can("port") and defined $p->port
!ll&&!rl&&r
002
MC/DC0%0/2
$p->can("port") and defined $p->port
$p->can("port") defined $p->port
Truth table33%1/3
A and B
A= $p->can("port")B= defined $p->port
ABresult
0X0
100
111
536 2 ▶ $port{targetPort} = $p->targetPort if $p->can('targetPort') && defined $p->targetPort;
Branch: 1 branch
BranchTrueFalseTotal
if $p->can("targetPort") and defined $p->targetPort 2 0 2
Condition33%1/3
$p->can("targetPort") and defined $p->targetPort
!ll&&!rl&&r
002
MC/DC0%0/2
$p->can("targetPort") and defined $p->targetPort
$p->can("targetPort") defined $p->targetPort
Truth table33%1/3
A and B
A= $p->can("targetPort")B= defined $p->targetPort
ABresult
0X0
100
111
537 2 ▶ $port{protocol} = $p->protocol if $p->can('protocol') && $p->protocol;
Branch: 1 branch
BranchTrueFalseTotal
if $p->can("protocol") and $p->protocol 2 0 2
Condition33%1/3
$p->can("protocol") and $p->protocol
!ll&&!rl&&r
002
MC/DC0%0/2
$p->can("protocol") and $p->protocol
$p->can("protocol") $p->protocol
Truth table33%1/3
A and B
A= $p->can("protocol")B= $p->protocol
ABresult
0X0
100
111
538 2 \%port;
539 1 } @{ $spec->ports } ];
540 }
541 }
542  
543 22 return \%summary;
544 }
545  
546 10 ▶sub _format_list {
Subroutine _format_list: called CC=3 SCAR=11.0
547 10 my ($self, $items) = @_;
548  
549  
550 10 my @summaries;
551 10 ▶ for my $item (@{ $items // [] }) {
Condition100%2/2
$items // []
l!l
91
MC/DC100%1/1
$items // []
$items
Truth table100%2/2
A
A= $items
Aresult
11
00
552 15 push @summaries, $self->_format_resource_summary($item);
553 }
554 10 return \@summaries;
555 }
556  
557 16 ▶sub _available_resources_desc {
Subroutine _available_resources_desc: called CC=6 SCAR=19.2
558 16 my ($self, $verb) = @_;
559 16 my @parts;
560 16 for my $ns ($self->permissions->allowed_namespaces) {
561 16 my @resources = $self->permissions->allowed_resources($verb, $ns);
562 16 ▶ if (@resources) {
Branch: 1 branch
BranchTrueFalseTotal
if (@resources) 16 0 16
563 16 my @display = grep { $_ ne '*' } @resources;
564 16 ▶ if (grep { $_ eq '*' } @resources) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$_ eq '*';} @resources) { } 0 16 16
elsif (@display > 10) { } 0 16 16
565 0 push @parts, "$ns: all resources";
566 } elsif (@display > 10) {
567 0 push @parts, "$ns: " . join(', ', @display[0..9]) . ", ...";
568 } else {
569 16 push @parts, "$ns: " . join(', ', @display);
570 }
571 }
572 }
573 16 ▶ return join('; ', @parts) || 'none discovered';
Condition50%1/2
join('; ', @parts) || 'none discovered'
l!l
160
MC/DC0%0/1
join('; ', @parts) || 'none discovered'
join('; ', @parts)
Truth table50%1/2
A
A= join('; ', @parts)
Aresult
11
00
574 }
575  
576 2 ▶sub _update_tool_descriptions {
Subroutine _update_tool_descriptions: called CC=5 SCAR=16.2
577 2 my ($self) = @_;
578 2 ▶ return if $self->_descriptions_updated;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_descriptions_updated 0 2 2
579 2 $self->permissions->ensure_discovered;
580  
581 2 for my $entry (@{ $self->_tool_desc_map }) {
582 18 my ($tool, $base, $verb) = @$entry;
583 18 ▶ if ($verb eq '_logs') {
Branch: 1 branch
BranchTrueFalseTotal
if ($verb eq '_logs') { } 2 16 18
584 my @log_ns = grep {
585 2 $self->permissions->can_read_logs($_)
586 } $self->permissions->allowed_namespaces;
587 2 ▶ $tool->description($base . (join(', ', @log_ns) || 'none'));
Condition50%1/2
join(', ', @log_ns) || 'none'
l!l
20
MC/DC0%0/1
join(', ', @log_ns) || 'none'
join(', ', @log_ns)
Truth table50%1/2
A
A= join(', ', @log_ns)
Aresult
11
00
588 } else {
589 16 $tool->description($base . $self->_available_resources_desc($verb));
590 }
591 }
592 2 $self->_descriptions_updated(1);
593 }
594  
595 1 ▶sub _tools {
Subroutine _tools: called CC=1 SCAR=0.0
596 1 my ($self, $context) = @_;
597  
598  
599 1 $self->_update_tool_descriptions;
600 1 return $self->SUPER::_tools($context);
601 }
602  
603  
604 47 ▶sub _register_tools {
Subroutine _register_tools: called CC=1 SCAR=0.0
605 47 my ($self) = @_;
606  
607 47 my @desc_map;
608  
609 # ---- Tool 1: k8s_permissions ----
610 $self->tool(
611 name => 'k8s_permissions',
612 description => 'Show what this Kubernetes service account is allowed to do (RBAC permissions). Call this first to understand available capabilities.',
613 input_schema => {
614 type => 'object',
615 properties => {},
616 },
617 2 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
618 2 my ($tool, $args) = @_;
619 2 return $self->permissions->summary;
620 },
621 47 );
622  
623 # ---- Tool 2: k8s_list ----
624 47 my $list_desc = 'List Kubernetes resources.';
625 push @desc_map, [$self->tool(
626 name => 'k8s_list',
627 description => $list_desc,
628 input_schema => {
629 type => 'object',
630 properties => {
631 resource => {
632 type => 'string',
633 description => 'Resource type (e.g. Pod, Deployment, Service, ConfigMap)',
634 },
635 namespace => {
636 type => 'string',
637 description => 'Namespace (auto-detected if only one available)',
638 },
639 label_selector => {
640 type => 'string',
641 description => 'Label selector filter (e.g. app=web)',
642 },
643 field_selector => {
644 type => 'string',
645 description => 'Field selector filter (e.g. status.phase=Running)',
646 },
647 },
648 required => ['resource'],
649 },
650 7 ▶ code => sub {
Subroutine __ANON__: called CC=10 SCAR=23.4
651 7 my ($tool, $args) = @_;
652  
653 7 my $resource = $args->{resource};
654 7 my $ctx = $self->_scope_context($args, $resource);
655 7 ▶ return $ctx->{error} if $ctx->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $$ctx{"error"} 1 6 7
656 6 my $ns = $ctx->{ns};
657 6 my $plural = $self->_resource_plural($resource);
658  
659 6 ▶ unless ($self->permissions->can_do('list', $plural, $ctx->{perm_ns})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->permissions->can_do("list", $plural, $$ctx{"perm_ns"})) 1 5 6
660 1 ▶ return "Permission denied: cannot list $resource" . ($ns ? " in namespace $ns" : "");
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 1 0 1
661 }
662  
663 5 my %api_args;
664 5 ▶ $api_args{namespace} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 3 2 5
665 5 ▶ $api_args{labelSelector} = $args->{label_selector} if $args->{label_selector};
Branch: 1 branch
BranchTrueFalseTotal
if $$args{"label_selector"} 0 5 5
666 5 ▶ $api_args{fieldSelector} = $args->{field_selector} if $args->{field_selector};
Branch: 1 branch
BranchTrueFalseTotal
if $$args{"field_selector"} 0 5 5
667  
668 5 my $list = eval { $self->api->list($resource, %api_args) };
669 5 ▶ return "Failed to list $resource: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
670  
671 5 ▶ my $items = $list->items // [];
Condition50%1/2
$list->items // []
l!l
50
MC/DC0%0/1
$list->items // []
$list->items
Truth table50%1/2
A
A= $list->items
Aresult
11
00
672 5 ▶ return "No $resource resources found" unless @$items;
Branch: 1 branch
BranchTrueFalseTotal
unless @$items 0 5 5
673  
674 5 my $summaries = $self->_format_list($items);
675 5 return $self->_to_json({
676 count => scalar @$items,
677 items => $summaries,
678 });
679 },
680 47 ), 'List Kubernetes resources. Available: ', 'list'];
681  
682 # ---- Tool 3: k8s_get ----
683 47 my $get_desc = 'Get a single Kubernetes resource.';
684 push @desc_map, [$self->tool(
685 name => 'k8s_get',
686 description => $get_desc,
687 input_schema => {
688 type => 'object',
689 properties => {
690 resource => {
691 type => 'string',
692 description => 'Resource type (e.g. Pod, Deployment, Service)',
693 },
694 name => {
695 type => 'string',
696 description => 'Resource name',
697 },
698 namespace => {
699 type => 'string',
700 description => 'Namespace (auto-detected if only one available)',
701 },
702 output => {
703 type => 'string',
704 description => 'Output format: summary (default), json, yaml',
705 enum => ['summary', 'json', 'yaml'],
706 },
707 },
708 required => ['resource', 'name'],
709 },
710 9 ▶ code => sub {
Subroutine __ANON__: called CC=10 SCAR=23.1
711 9 my ($tool, $args) = @_;
712  
713 9 my $resource = $args->{resource};
714 9 my $name = $args->{name};
715 9 my $ctx = $self->_scope_context($args, $resource);
716 9 ▶ return $ctx->{error} if $ctx->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $$ctx{"error"} 0 9 9
717 9 my $ns = $ctx->{ns};
718 9 ▶ my $output = $args->{output} // 'summary';
Condition100%2/2
$$args{"output"} // "summary"
l!l
54
MC/DC100%1/1
$$args{"output"} // "summary"
$$args{"output"}
Truth table100%2/2
A
A= $$args{"output"}
Aresult
11
00
719 9 my $plural = $self->_resource_plural($resource);
720  
721 9 ▶ unless ($self->permissions->can_do('get', $plural, $ctx->{perm_ns})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->permissions->can_do("get", $plural, $$ctx{"perm_ns"})) 1 8 9
722 1 ▶ return "Permission denied: cannot get $resource" . ($ns ? " in namespace $ns" : "");
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 1 0 1
723 }
724  
725 8 my %api_args = (name => $name);
726 8 ▶ $api_args{namespace} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 7 1 8
727  
728 8 my $obj = eval { $self->api->get($resource, %api_args) };
729 8 ▶ return "Failed to get $resource/$name: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
730  
731 8 ▶ if ($output eq 'json') {
Branch: 2 branches
BranchTrueFalseTotal
if ($output eq 'json') { } 2 6 8
elsif ($output eq 'yaml') { } 3 3 6
732 2 return $self->_to_json($obj->TO_JSON);
733 } elsif ($output eq 'yaml') {
734 # IO::K8s::Role::APIObject::to_yaml, not a YAML dump of TO_JSON.
735 # TO_JSON hands out JSON::PP::Boolean objects for every boolean field,
736 # and a generic dumper serialises those as Perl internals —
737 # "hostNetwork: !!perl/scalar:JSON::PP::Boolean 1" instead of
738 # "hostNetwork: true", which kubectl apply rejects. to_yaml runs
739 # YAML::PP with the JSON schema and boolean => 'JSON::PP', which is
740 # what makes the output a manifest.
741 3 ▶ unless ($obj->can('to_yaml')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->can("to_yaml")) 1 2 3
742 1 return "Cannot render $resource/$name as YAML: " . ref($obj)
743 . " does not implement to_yaml (not a top-level Kubernetes API"
744 . " object). Retry with output 'json'.";
745 }
746 2 return $obj->to_yaml;
747 } else {
748 3 return $self->_to_json($self->_format_resource_summary($obj));
749 }
750 },
751 47 ), 'Get a single Kubernetes resource. Available: ', 'get'];
752  
753 # ---- Tool 4: k8s_create ----
754 47 my $create_desc = 'Create a Kubernetes resource.';
755 push @desc_map, [$self->tool(
756 name => 'k8s_create',
757 description => $create_desc,
758 input_schema => {
759 type => 'object',
760 properties => {
761 resource => {
762 type => 'string',
763 description => 'Resource type (e.g. Pod, Deployment, ConfigMap)',
764 },
765 namespace => {
766 type => 'string',
767 description => 'Namespace for the resource',
768 },
769 manifest => {
770 type => 'object',
771 description => 'Resource manifest (apiVersion/kind auto-populated from resource type)',
772 },
773 },
774 required => ['resource', 'manifest'],
775 },
776 2 ▶ code => sub {
Subroutine __ANON__: called CC=11 SCAR=25.4
777 2 my ($tool, $args) = @_;
778  
779 2 my $resource = $args->{resource};
780 2 my $ctx = $self->_scope_context($args, $resource);
781 2 ▶ return $ctx->{error} if $ctx->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $$ctx{"error"} 0 2 2
782 2 my $ns = $ctx->{ns};
783 2 my $manifest = $args->{manifest};
784 2 my $plural = $self->_resource_plural($resource);
785  
786 2 ▶ unless ($self->permissions->can_do('create', $plural, $ctx->{perm_ns})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->permissions->can_do("create", $plural, $$ctx{"perm_ns"})) 1 1 2
787 1 ▶ return "Permission denied: cannot create $resource" . ($ns ? " in namespace $ns" : "");
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 1 0 1
788 }
789  
790 # Auto-populate namespace in metadata
791 1 ▶ if (defined $ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ns) 1 0 1
792 1 ▶ $manifest->{metadata} //= {};
Condition50%1/2
$$manifest{"metadata"} //= {}
l!l
10
MC/DC0%0/1
$$manifest{"metadata"} //= {}
$$manifest{"metadata"}
Truth table50%1/2
A
A= $$manifest{"metadata"}
Aresult
11
00
793 1 ▶ $manifest->{metadata}{namespace} //= $ns;
Condition33%1/3
$$manifest{"metadata"}{"namespace"} //= $ns
l!l&&r!l&&!r
010
MC/DC0%0/2
$$manifest{"metadata"}{"namespace"} //= $ns
$$manifest{"metadata"}{"namespace"} $ns
Truth table33%1/3
A //= B
A= $$manifest{"metadata"}{"namespace"}B= $ns
ABresult
1X1
011
000
794 }
795  
796 1 my $obj = eval { $self->api->new_object($resource, $manifest) };
797 1 ▶ return "Failed to build $resource object: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
798  
799 1 my $created = eval { $self->api->create($obj) };
800 1 ▶ return "Failed to create $resource: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
801  
802 1 ▶ my $created_name = eval { $created->metadata->name } // 'unknown';
Condition50%1/2
eval { do { $created->metadata->name } } // "unknown"
l!l
10
MC/DC0%0/1
eval { do { $created->metadata->name } } // "unknown"
eval { do { $created->metadata->name } }
Truth table50%1/2
A
A= eval { do { $created->metadata->name } }
Aresult
11
00
803 1 ▶ return $self->_to_json({
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 1 0 1
804 status => 'created',
805 kind => $resource,
806 name => $created_name,
807 ($ns ? (namespace => $ns) : ()),
808 });
809 },
810 47 ), 'Create a Kubernetes resource. Available: ', 'create'];
811  
812 # ---- Tool 5: k8s_patch ----
813 47 my $patch_desc = 'Patch (partial update) a Kubernetes resource.';
814 push @desc_map, [$self->tool(
815 name => 'k8s_patch',
816 description => $patch_desc,
817 input_schema => {
818 type => 'object',
819 properties => {
820 resource => {
821 type => 'string',
822 description => 'Resource type (e.g. Deployment, Service)',
823 },
824 name => {
825 type => 'string',
826 description => 'Resource name',
827 },
828 namespace => {
829 type => 'string',
830 description => 'Namespace',
831 },
832 patch => {
833 type => 'object',
834 description => 'Patch body (fields to change)',
835 },
836 patch_type => {
837 type => 'string',
838 description => 'Patch strategy: strategic (default, merge for status), merge, json',
839 enum => ['strategic', 'merge', 'json'],
840 },
841 subresource => {
842 type => 'string',
843 description => 'Write to a subresource instead of the main endpoint. '
844 . 'Use "status" for status fields: the API server strips status '
845 . 'from writes to the main endpoint and still answers 2xx.',
846 enum => ['status'],
847 },
848 },
849 required => ['resource', 'name', 'patch'],
850 },
851 7 ▶ code => sub {
Subroutine __ANON__: called CC=16 SCAR=27.9
852 7 my ($tool, $args) = @_;
853  
854 7 my $resource = $args->{resource};
855 7 my $name = $args->{name};
856 7 my $ctx = $self->_scope_context($args, $resource);
857 7 ▶ return $ctx->{error} if $ctx->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $$ctx{"error"} 0 7 7
858 7 my $ns = $ctx->{ns};
859 7 my $patch = $args->{patch};
860 7 my $subresource = $args->{subresource};
861 7 my $plural = $self->_resource_plural($resource);
862  
863 7 ▶ if (defined $subresource && $subresource ne 'status') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subresource and $subresource ne "status") 1 6 7
Condition100%3/3
defined $subresource and $subresource ne "status"
!ll&&!rl&&r
241
MC/DC100%2/2
defined $subresource and $subresource ne "status"
defined $subresource $subresource ne "status"
Truth table100%3/3
A and B
A= defined $subresourceB= $subresource ne "status"
ABresult
0X0
100
111
864 1 return "Unsupported subresource '$subresource': only 'status' is supported";
865 }
866 6 ▶ my $to_status = defined $subresource && $subresource eq 'status';
Condition66%2/3
defined $subresource && $subresource eq 'status'
!ll&&!rl&&r
204
MC/DC50%1/2
defined $subresource && $subresource eq 'status'
defined $subresource $subresource eq 'status'
Truth table66%2/3
A && B
A= defined $subresourceB= $subresource eq 'status'
ABresult
0X0
100
111
867  
868 # The status subresource is its own RBAC resource: patch on <plural>
869 # does not grant patch on <plural>/status. The plural cache cannot help
870 # here — _discover_resource_plurals skips names containing '/' — so the
871 # subresource is appended to the base plural.
872 6 ▶ my $rbac_resource = $to_status ? "$plural/status" : $plural;
Branch: 1 branch
BranchTrueFalseTotal
$to_status ? : 4 2 6
873  
874 6 ▶ unless ($self->permissions->can_do('patch', $rbac_resource, $ctx->{perm_ns})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->permissions->can_do("patch", $rbac_resource, $$ctx{"perm_ns"})) 2 4 6
875 2 ▶ return "Permission denied: cannot patch "
Branch: 2 branches
BranchTrueFalseTotal
$to_status ? : 1 1 2
$ns ? : 2 0 2
876 . ($to_status ? "$resource status" : $resource)
877 . ($ns ? " in namespace $ns" : "");
878 }
879  
880 # Status writes default to a merge patch: custom resources answer 415 to
881 # a strategic merge patch. An explicit patch_type still wins.
882 my $patch_type = $args->{patch_type}
883 4 ▶ // ($to_status ? 'merge' : 'strategic');
Branch: 1 branch
BranchTrueFalseTotal
$to_status ? : 2 1 3
Condition100%2/2
$$args{"patch_type"} // ($to_status ? "merge" : "strategic")
l!l
13
MC/DC100%1/1
$$args{"patch_type"} // ($to_status ? "merge" : "strategic")
$$args{"patch_type"}
Truth table100%2/2
A
A= $$args{"patch_type"}
Aresult
11
00
884  
885 4 my %api_args = (
886 patch => $patch,
887 type => $patch_type,
888 );
889 4 ▶ $api_args{namespace} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 4 0 4
890  
891 # patch() writes the main endpoint, which silently drops a status stanza
892 # for every resource that has a status subresource.
893 4 ▶ my $method = $to_status ? 'patch_status' : 'patch';
Branch: 1 branch
BranchTrueFalseTotal
$to_status ? : 3 1 4
894 4 my $patched = eval { $self->api->$method($resource, $name, %api_args) };
895 4 ▶ return "Failed to patch $resource/$name: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
896  
897 4 ▶ return $self->_to_json({
Branch: 2 branches
BranchTrueFalseTotal
$to_status ? : 3 1 4
$ns ? : 4 0 4
898 status => 'patched',
899 kind => $resource,
900 name => $name,
901 ($to_status ? (subresource => 'status') : ()),
902 ($ns ? (namespace => $ns) : ()),
903 });
904 },
905 47 ), 'Patch (partial update) a Kubernetes resource. Available: ', 'patch'];
906  
907 # ---- Tool 6: k8s_delete ----
908 47 my $delete_desc = 'Delete a Kubernetes resource.';
909 push @desc_map, [$self->tool(
910 name => 'k8s_delete',
911 description => $delete_desc,
912 input_schema => {
913 type => 'object',
914 properties => {
915 resource => {
916 type => 'string',
917 description => 'Resource type (e.g. Pod, Deployment)',
918 },
919 name => {
920 type => 'string',
921 description => 'Resource name',
922 },
923 namespace => {
924 type => 'string',
925 description => 'Namespace',
926 },
927 },
928 required => ['resource', 'name'],
929 },
930 2 ▶ code => sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
931 2 my ($tool, $args) = @_;
932  
933 2 my $resource = $args->{resource};
934 2 my $name = $args->{name};
935 2 my $ctx = $self->_scope_context($args, $resource);
936 2 ▶ return $ctx->{error} if $ctx->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $$ctx{"error"} 0 2 2
937 2 my $ns = $ctx->{ns};
938 2 my $plural = $self->_resource_plural($resource);
939  
940 2 ▶ unless ($self->permissions->can_do('delete', $plural, $ctx->{perm_ns})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->permissions->can_do("delete", $plural, $$ctx{"perm_ns"})) 1 1 2
941 1 ▶ return "Permission denied: cannot delete $resource" . ($ns ? " in namespace $ns" : "");
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 1 0 1
942 }
943  
944 1 my %api_args = (name => $name);
945 1 ▶ $api_args{namespace} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 1 0 1
946  
947 1 eval { $self->api->delete($resource, %api_args) };
948 1 ▶ return "Failed to delete $resource/$name: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
949  
950 1 ▶ return $self->_to_json({
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 1 0 1
951 status => 'deleted',
952 kind => $resource,
953 name => $name,
954 ($ns ? (namespace => $ns) : ()),
955 });
956 },
957 47 ), 'Delete a Kubernetes resource. Available: ', 'delete'];
958  
959 # ---- Tool 7: k8s_logs ----
960 47 my $logs_desc = 'Get pod logs from Kubernetes pods.';
961 push @desc_map, [$self->tool(
962 name => 'k8s_logs',
963 description => $logs_desc,
964 input_schema => {
965 type => 'object',
966 properties => {
967 name => {
968 type => 'string',
969 description => 'Pod name',
970 },
971 namespace => {
972 type => 'string',
973 description => 'Namespace (auto-detected if only one available)',
974 },
975 container => {
976 type => 'string',
977 description => 'Container name (required for multi-container pods)',
978 },
979 tail_lines => {
980 type => 'integer',
981 description => 'Number of lines from end (default: 100)',
982 },
983 previous => {
984 type => 'boolean',
985 description => 'Get logs from previous container instance',
986 },
987 },
988 required => ['name'],
989 },
990 6 ▶ code => sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
991 6 my ($tool, $args) = @_;
992  
993 6 my $name = $args->{name};
994 6 my $ns = $self->_resolve_namespace($args);
995 6 my $container = $args->{container};
996 6 ▶ my $tail_lines = $args->{tail_lines} // 100;
Condition100%2/2
$$args{"tail_lines"} // 100
l!l
15
MC/DC100%1/1
$$args{"tail_lines"} // 100
$$args{"tail_lines"}
Truth table100%2/2
A
A= $$args{"tail_lines"}
Aresult
11
00
997 6 ▶ my $previous = $args->{previous} // 0;
Condition100%2/2
$$args{"previous"} // 0
l!l
15
MC/DC100%1/1
$$args{"previous"} // 0
$$args{"previous"}
Truth table100%2/2
A
A= $$args{"previous"}
Aresult
11
00
998  
999 6 ▶ unless ($ns) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ns) 1 5 6
1000 1 return "Namespace required for pod logs";
1001 }
1002  
1003 5 ▶ unless ($self->permissions->can_read_logs($ns)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->permissions->can_read_logs($ns)) 1 4 5
1004 1 return "Permission denied: cannot read pod logs in namespace $ns";
1005 }
1006  
1007 # Kubernetes::REST's log() builds /api/v1/namespaces/$ns/pods/$name/log
1008 # and the query parameters itself. It raises API errors (404, 403, ...)
1009 # instead of handing back the response, so the status check lives in the
1010 # eval now.
1011 4 my %log_args = (name => $name, namespace => $ns);
1012 4 ▶ $log_args{tailLines} = $tail_lines if $tail_lines;
Branch: 1 branch
BranchTrueFalseTotal
if $tail_lines 4 0 4
1013 4 ▶ $log_args{container} = $container if $container;
Branch: 1 branch
BranchTrueFalseTotal
if $container 1 3 4
1014 4 ▶ $log_args{previous} = 1 if $previous;
Branch: 1 branch
BranchTrueFalseTotal
if $previous 1 3 4
1015  
1016 4 my $content = eval { $self->api->log('Pod', %log_args) };
1017 4 ▶ return "Failed to get logs for pod/$name: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 3 4
1018  
1019 3 ▶ return ($content // '') || "(no log output)";
Condition75%3/4
($content // '') || '(no log output)'
l!l
21
$content // ""
l!l
30
MC/DC0%0/1
($content // '') || '(no log output)'
$content
Truth table50%1/2
A
A= $content
Aresult
11
00
1020 },
1021 47 ), 'Get pod logs. Available in namespaces: ', '_logs'];
1022  
1023 # ---- Tool 8: k8s_events ----
1024 47 my $events_desc = 'Get Kubernetes events for debugging.';
1025 push @desc_map, [$self->tool(
1026 name => 'k8s_events',
1027 description => $events_desc,
1028 input_schema => {
1029 type => 'object',
1030 properties => {
1031 namespace => {
1032 type => 'string',
1033 description => 'Namespace (auto-detected if only one available)',
1034 },
1035 involved_object => {
1036 type => 'string',
1037 description => 'Filter events by involved object name (e.g. a pod name)',
1038 },
1039 field_selector => {
1040 type => 'string',
1041 description => 'Field selector filter (e.g. reason=BackOff)',
1042 },
1043 },
1044 },
1045 3 ▶ code => sub {
Subroutine __ANON__: called CC=11 SCAR=24.8
1046 3 my ($tool, $args) = @_;
1047  
1048 3 my $ctx = $self->_scope_context($args, 'Event');
1049 3 ▶ return $ctx->{error} if $ctx->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $$ctx{"error"} 0 3 3
1050 3 my $ns = $ctx->{ns};
1051  
1052 3 ▶ unless ($self->permissions->can_do('list', 'events', $ctx->{perm_ns})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->permissions->can_do("list", "events", $$ctx{"perm_ns"})) 1 2 3
1053 1 ▶ return "Permission denied: cannot list events" . ($ns ? " in namespace $ns" : "");
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 1 0 1
1054 }
1055  
1056 2 my %api_args;
1057 2 ▶ $api_args{namespace} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 2 0 2
1058  
1059 2 my @selectors;
1060 2 ▶ if (my $obj = $args->{involved_object}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $obj = $$args{"involved_object"}) 1 1 2
1061 1 push @selectors, "involvedObject.name=$obj";
1062 }
1063 2 ▶ if (my $fs = $args->{field_selector}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fs = $$args{"field_selector"}) 0 2 2
1064 0 push @selectors, $fs;
1065 }
1066 2 ▶ $api_args{fieldSelector} = join(',', @selectors) if @selectors;
Branch: 1 branch
BranchTrueFalseTotal
if @selectors 1 1 2
1067  
1068 2 my $list = eval { $self->api->list('Event', %api_args) };
1069 2 ▶ return "Failed to list events: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
1070  
1071 2 ▶ my $items = $list->items // [];
Condition50%1/2
$list->items // []
l!l
20
MC/DC0%0/1
$list->items // []
$list->items
Truth table50%1/2
A
A= $list->items
Aresult
11
00
1072 2 ▶ return "No events found" unless @$items;
Branch: 1 branch
BranchTrueFalseTotal
unless @$items 0 2 2
1073  
1074 2 my $summaries = $self->_format_list($items);
1075 2 return $self->_to_json({
1076 count => scalar @$items,
1077 items => $summaries,
1078 });
1079 },
1080 47 ), 'Get Kubernetes events for debugging. Available in: ', 'list'];
1081  
1082 # ---- Tool 9: k8s_rollout_restart ----
1083 47 my $restart_desc = 'Trigger rolling restart of a Deployment, StatefulSet, or DaemonSet.';
1084 push @desc_map, [$self->tool(
1085 name => 'k8s_rollout_restart',
1086 description => $restart_desc,
1087 input_schema => {
1088 type => 'object',
1089 properties => {
1090 resource => {
1091 type => 'string',
1092 description => 'Resource type: Deployment, StatefulSet, or DaemonSet',
1093 enum => ['Deployment', 'StatefulSet', 'DaemonSet'],
1094 },
1095 name => {
1096 type => 'string',
1097 description => 'Resource name',
1098 },
1099 namespace => {
1100 type => 'string',
1101 description => 'Namespace',
1102 },
1103 },
1104 required => ['resource', 'name'],
1105 },
1106 2 ▶ code => sub {
Subroutine __ANON__: called CC=7 SCAR=19.8
1107 2 my ($tool, $args) = @_;
1108  
1109 2 my $resource = $args->{resource};
1110 2 my $name = $args->{name};
1111 2 my $ctx = $self->_scope_context($args, $resource);
1112 2 ▶ return $ctx->{error} if $ctx->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $$ctx{"error"} 0 2 2
1113 2 my $ns = $ctx->{ns};
1114 2 my $plural = $self->_resource_plural($resource);
1115  
1116 2 ▶ unless ($self->permissions->can_do('patch', $plural, $ctx->{perm_ns})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->permissions->can_do("patch", $plural, $$ctx{"perm_ns"})) 1 1 2
1117 1 ▶ return "Permission denied: cannot patch $resource" . ($ns ? " in namespace $ns" : "");
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 1 0 1
1118 }
1119  
1120 # Generate ISO 8601 timestamp without POSIX dependency
1121 1 my @t = gmtime;
1122 1 my $timestamp = sprintf('%04d-%02d-%02dT%02d:%02d:%02dZ',
1123 $t[5] + 1900, $t[4] + 1, $t[3], $t[2], $t[1], $t[0]);
1124  
1125 1 my $patch = {
1126 spec => {
1127 template => {
1128 metadata => {
1129 annotations => {
1130 'kubectl.kubernetes.io/restartedAt' => $timestamp,
1131 },
1132 },
1133 },
1134 },
1135 };
1136  
1137 1 my %api_args = (
1138 patch => $patch,
1139 type => 'strategic',
1140 );
1141 1 ▶ $api_args{namespace} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 1 0 1
1142  
1143 1 my $patched = eval { $self->api->patch($resource, $name, %api_args) };
1144 1 ▶ return "Failed to restart $resource/$name: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
1145  
1146 1 ▶ return $self->_to_json({
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 1 0 1
1147 status => 'restarting',
1148 kind => $resource,
1149 name => $name,
1150 restartAt => $timestamp,
1151 ($ns ? (namespace => $ns) : ()),
1152 });
1153 },
1154 47 ), 'Trigger rolling restart of a Deployment, StatefulSet, or DaemonSet. Available: ', 'patch'];
1155  
1156 # ---- Tool 10: k8s_apply ----
1157 47 my $apply_desc = 'Create or update a Kubernetes resource (like kubectl apply).';
1158 push @desc_map, [$self->tool(
1159 name => 'k8s_apply',
1160 description => $apply_desc,
1161 input_schema => {
1162 type => 'object',
1163 properties => {
1164 resource => {
1165 type => 'string',
1166 description => 'Resource type (e.g. Deployment, ConfigMap)',
1167 },
1168 namespace => {
1169 type => 'string',
1170 description => 'Namespace for the resource',
1171 },
1172 manifest => {
1173 type => 'object',
1174 description => 'Resource manifest (must include metadata.name)',
1175 },
1176 },
1177 required => ['resource', 'manifest'],
1178 },
1179 9 ▶ code => sub {
Subroutine __ANON__: called CC=23 SCAR=34.1
1180 9 my ($tool, $args) = @_;
1181  
1182 9 my $resource = $args->{resource};
1183 9 my $ctx = $self->_scope_context($args, $resource);
1184 9 ▶ return $ctx->{error} if $ctx->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $$ctx{"error"} 0 9 9
1185 9 my $ns = $ctx->{ns};
1186 9 my $manifest = $args->{manifest};
1187 9 my $plural = $self->_resource_plural($resource);
1188  
1189 9 my $can_create = $self->permissions->can_do('create', $plural, $ctx->{perm_ns});
1190 9 my $can_patch = $self->permissions->can_do('patch', $plural, $ctx->{perm_ns});
1191  
1192 9 ▶ unless ($can_create || $can_patch) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($can_create or $can_patch) 1 8 9
Condition66%2/3
$can_create or $can_patch
l!l&&r!l&&!r
801
MC/DC50%1/2
$can_create or $can_patch
$can_create $can_patch
Truth table66%2/3
A or B
A= $can_createB= $can_patch
ABresult
1X1
011
000
1193 1 ▶ return "Permission denied: cannot create or patch $resource" . ($ns ? " in namespace $ns" : "");
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 1 0 1
1194 }
1195  
1196 8 my $res_name = eval { $manifest->{metadata}{name} };
1197 8 ▶ return "manifest must include metadata.name" unless defined $res_name && length $res_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res_name and length $res_name 1 7 8
Condition66%2/3
defined $res_name and length $res_name
!ll&&!rl&&r
107
MC/DC50%1/2
defined $res_name and length $res_name
defined $res_name length $res_name
Truth table66%2/3
A and B
A= defined $res_nameB= length $res_name
ABresult
0X0
100
111
1198  
1199 # Auto-populate namespace in metadata
1200 7 ▶ if (defined $ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ns) 7 0 7
1201 7 ▶ $manifest->{metadata} //= {};
Condition50%1/2
$$manifest{"metadata"} //= {}
l!l
70
MC/DC0%0/1
$$manifest{"metadata"} //= {}
$$manifest{"metadata"}
Truth table50%1/2
A
A= $$manifest{"metadata"}
Aresult
11
00
1202 7 ▶ $manifest->{metadata}{namespace} //= $ns;
Condition33%1/3
$$manifest{"metadata"}{"namespace"} //= $ns
l!l&&r!l&&!r
070
MC/DC0%0/2
$$manifest{"metadata"}{"namespace"} //= $ns
$$manifest{"metadata"}{"namespace"} $ns
Truth table33%1/3
A //= B
A= $$manifest{"metadata"}{"namespace"}B= $ns
ABresult
1X1
011
000
1203 }
1204  
1205 # Try create first
1206 7 ▶ if ($can_create) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_create) 7 0 7
1207 7 my $obj = eval { $self->api->new_object($resource, $manifest) };
1208 7 ▶ return "Failed to build $resource object: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
1209  
1210 7 my $created = eval { $self->api->create($obj) };
1211 7 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 1 6 7
1212 1 ▶ my $created_name = eval { $created->metadata->name } // $res_name;
Condition33%1/3
eval { do { $created->metadata->name } } // $res_name
l!l&&r!l&&!r
100
MC/DC0%0/2
eval { do { $created->metadata->name } } // $res_name
eval { do { $created->metadata->name } } $res_name
Truth table33%1/3
A // B
A= eval { do { $created->metadata->name } }B= $res_name
ABresult
1X1
011
000
1213 1 ▶ return $self->_to_json({
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 1 0 1
1214 status => 'created',
1215 kind => $resource,
1216 name => $created_name,
1217 ($ns ? (namespace => $ns) : ()),
1218 });
1219 }
1220  
1221 # If 409 Conflict / AlreadyExists, fall through to patch
1222 6 my $err = $@;
1223 6 ▶ unless ($self->_is_conflict_error($err)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_conflict_error($err)) 2 4 6
1224 2 return "Failed to create $resource: $err";
1225 }
1226 }
1227  
1228 # Fall back to a patch
1229 4 ▶ unless ($can_patch) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($can_patch) 0 4 4
1230 0 ▶ return "Resource already exists and cannot patch $resource" . ($ns ? " in namespace $ns" : "");
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 0 0 0
1231 }
1232  
1233 # Strategic merge is understood by the built-in kinds only
1234 # (IO::K8s::Api::*); a custom resource answers it with 415, so
1235 # everything else gets a JSON merge patch.
1236 4 ▶ my $class = eval { $self->api->expand_class($resource) } // '';
Condition50%1/2
eval { do { $self->api->expand_class($resource) } } // ""
l!l
40
MC/DC0%0/1
eval { do { $self->api->expand_class($resource) } } // ""
eval { do { $self->api->expand_class($resource) } }
Truth table50%1/2
A
A= eval { do { $self->api->expand_class($resource) } }
Aresult
11
00
1237 4 ▶ my %api_args = (
Branch: 1 branch
BranchTrueFalseTotal
$class =~ /^IO::K8s::Api::/ ? : 1 3 4
1238 patch => $manifest,
1239 type => $class =~ /^IO::K8s::Api::/ ? 'strategic' : 'merge',
1240 );
1241 4 ▶ $api_args{namespace} = $ns if defined $ns;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ns 4 0 4
1242  
1243 4 my $patched = eval { $self->api->patch($resource, $res_name, %api_args) };
1244 4 ▶ return "Failed to update $resource/$res_name: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
1245  
1246 4 ▶ return $self->_to_json({
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 4 0 4
1247 status => 'updated',
1248 kind => $resource,
1249 name => $res_name,
1250 ($ns ? (namespace => $ns) : ()),
1251 });
1252 },
1253 47 ), 'Create or update a Kubernetes resource (like kubectl apply). Available: ', 'create'];
1254  
1255 47 $self->_tool_desc_map(\@desc_map);
1256 }
1257  
1258 0 ▶sub run_stdio {
Subroutine run_stdio: not called CC=2 SCAR=17.9
Pod documented
1259 0 my ($self) = @_;
1260  
1261  
1262 0 ▶ $self = $self->new unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
1263 # Not redundant with the _tools override: fails fast before to_stdio.
1264 0 $self->_update_tool_descriptions;
1265 0 $self->to_stdio;
1266 }
1267  
1268 11 1;
1269  
1270 __END__
↑ Summary K8s/Permissions.pm »