lib/Kubernetes/REST/Kubeconfig.pm

filter: Statementstmt 100% 211 / 211 Branchbran 93.4% 99 / 106 Conditioncond 76.6% 59 / 77 MC/DCmcdc 53.1% 25 / 47 Subroutinesub 100% 35 / 35 Podpod 100% 6 / 6 totaltotal 90.2% 435 / 482 CC 96 SCAR 20.9
CC
96
Cov
94%
api
20.0
_merge_configs
13.2
_resolve_file_references
11.2
CRAP 8.0
« lib/Kubernetes/REST/CLI/Minikube.pm ↑ Summary blib/lib/Kubernetes/REST/CLI/Cmd/Get.pm »
1 package Kubernetes::REST::Kubeconfig;
2 # ABSTRACT: Parse kubeconfig files and create Kubernetes::REST instances
3 40 our $VERSION = '1.113';
4 40 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use YAML::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 40 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Kubernetes::REST;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use Kubernetes::REST::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use Kubernetes::REST::AuthToken;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use Kubernetes::REST::AuthTokenFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use Kubernetes::REST::AuthExec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 has kubeconfig_path => (
19 is => 'ro',
20 28 ▶ default => sub { $_[0]->_default_kubeconfig_path },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 40 );
22  
23  
24 28 ▶sub _default_kubeconfig_path {
Subroutine _default_kubeconfig_path: called CC=5 SCAR=16.1
25 28 my $self = shift;
26 28 ▶ if (defined $ENV{KUBECONFIG} and length $ENV{KUBECONFIG}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"KUBECONFIG"} and length $ENV{"KUBECONFIG"}) 15 13 28
Condition100%3/3
defined $ENV{"KUBECONFIG"} and length $ENV{"KUBECONFIG"}
!ll&&!rl&&r
12115
MC/DC100%2/2
defined $ENV{"KUBECONFIG"} and length $ENV{"KUBECONFIG"}
defined $ENV{"KUBECONFIG"} length $ENV{"KUBECONFIG"}
Truth table100%3/3
A and B
A= defined $ENV{"KUBECONFIG"}B= length $ENV{"KUBECONFIG"}
ABresult
0X0
100
111
27 15 $self->_set_kubeconfig_path_from_env(1);
28 15 return $ENV{KUBECONFIG};
29 }
30 13 ▶ return undef unless defined $ENV{HOME} and length $ENV{HOME};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{"HOME"} and length $ENV{"HOME"} 3 10 13
Condition66%2/3
defined $ENV{"HOME"} and length $ENV{"HOME"}
!ll&&!rl&&r
3010
MC/DC50%1/2
defined $ENV{"HOME"} and length $ENV{"HOME"}
defined $ENV{"HOME"} length $ENV{"HOME"}
Truth table66%2/3
A and B
A= defined $ENV{"HOME"}B= length $ENV{"HOME"}
ABresult
0X0
100
111
31 10 return "$ENV{HOME}/.kube/config";
32 }
33  
34 # Whether kubeconfig_path is the KUBECONFIG environment variable, for the one
35 # message that has to say where a value came from. Only the default knows: once
36 # constructed, an explicit kubeconfig_path => $ENV{KUBECONFIG} looks the same.
37 40 has _kubeconfig_path_from_env => (
38 is => 'ro',
39 init_arg => undef,
40 writer => '_set_kubeconfig_path_from_env',
41 );
42  
43 40 ▶my $PATH_SEP = $Config::Config{path_sep} || ':';
Condition50%1/2
$Config::Config{'path_sep'} || ':'
l!l
400
MC/DC0%0/1
$Config::Config{'path_sep'} || ':'
$Config::Config{'path_sep'}
Truth table50%1/2
A
A= $Config::Config{'path_sep'}
Aresult
11
00
44  
45 # The kubeconfig content where its file name belongs - kubeconfig_path => $yaml,
46 # KUBECONFIG="$(cat config)". No file name a caller means holds a line break or
47 # a NUL, and none opens like a JSON or YAML document. Stat-ing such a value
48 # warns, and naming it in "Kubeconfig not found" put every token and key of the
49 # kubeconfig into the logs.
50 973 ▶sub _is_document {
Subroutine _is_document: called CC=2 SCAR=6.9
51 973 my ($self, $value) = @_;
52 973 ▶ return $value =~ /[\n\r\0]/ || $value =~ /\A\s*(?:[{\[]|---)/;
Condition100%3/3
$value =~ /[\n\r\0]/ || $value =~ /\A\s*(?:[{\[]|---)/
l!l&&r!l&&!r
275147551
MC/DC100%2/2
$value =~ /[\n\r\0]/ || $value =~ /\A\s*(?:[{\[]|---)/
$value =~ /[\n\r\0]/ $value =~ /\A\s*(?:[{\[]|---)/
Truth table100%3/3
A || B
A= $value =~ /[\n\r\0]/B= $value =~ /\A\s*(?:[{\[]|---)/
ABresult
1X1
011
000
53 }
54  
55 has kubeconfig_paths => (
56 is => 'lazy',
57 init_arg => undef,
58 125 ▶ builder => sub {
Subroutine _build_kubeconfig_paths: called CC=7 SCAR=20.5
59 125 my $self = shift;
60 125 my $path = $self->kubeconfig_path;
61 125 ▶ return [] unless defined $path;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $path 3 122 125
unless defined $path 0 0 0
62 122 ▶ my @entries = ref $path eq 'ARRAY' ? @$path : split /\Q$PATH_SEP\E/, $path, -1;
Branch: 1 branch
BranchTrueFalseTotal
ref $path eq 'ARRAY' ? : 8 114 122
63 # Per entry, so a document appended to a list ("path:{...}") is caught
64 # too. The split leaves line breaks and NULs where they were, so for
65 # those this is the same as looking at the whole value.
66 122 ▶ if (grep { defined and $self->_is_document($_) } @entries) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {defined $_ and $self->_is_document($_);} @entries) 41 81 122
Condition50%3/6
defined $_ && $self->_is_document($_)
!ll&&!rl&&r
0458422
defined $_ && $self->_is_document($_)
!ll&&!rl&&r
0930
MC/DC50%1/2
defined $_ && $self->_is_document($_)
defined $_ $self->_is_document($_)
MC/DC0%0/2
defined $_ && $self->_is_document($_)
defined $_ $self->_is_document($_)
Truth table66%2/3
A && B
A= defined $_B= $self->_is_document($_)
ABresult
0X0
100
111
Truth table33%1/3
A && B
A= defined $_B= $self->_is_document($_)
ABresult
0X0
100
111
67 # Reached through lazy builders (api -> _config -> here): trusting
68 # Moo's generated accessors for this croak names the caller's line,
69 # not one in this file. Names the option or the variable, never the
70 # value: the value is the secret.
71 41 local our @CARP_NOT = ('Method::Generate::Accessor::_Generated');
72 41 ▶ croak 'kubeconfig expects a path, got a document in '
Branch: 1 branch
BranchTrueFalseTotal
$self->_kubeconfig_path_from_env ? : 12 29 41
73 . ($self->_kubeconfig_path_from_env
74 ? 'the environment variable KUBECONFIG' : 'kubeconfig_path')
75 . ' - pass the name of a kubeconfig file, not its content';
76 }
77 81 ▶ return [ grep { defined and length } @entries ];
Condition50%3/6
defined $_ && length $_
!ll&&!rl&&r
03101
defined $_ && length $_
!ll&&!rl&&r
0093
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
78 },
79 40 );
80  
81  
82 40 has context_name => (
83 is => 'ro',
84 predicate => 1,
85 );
86  
87  
88 has refresh_token_files => (
89 is => 'ro',
90 228 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
91 40 );
92  
93  
94 has _config => (
95 is => 'lazy',
96 73 ▶ builder => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
Subroutine _build__config: called CC=5 SCAR=16.1
97 73 my $self = shift;
98 73 my @configs;
99 73 for my $path (@{ $self->kubeconfig_paths }) {
100 # A file the list names but that is not there is not an error, the
101 # same way it is not one for kubectl.
102 87 ▶ next unless -f $path;
Branch: 2 branches
BranchTrueFalseTotal
unless -f $path 6 81 87
unless -f $path 5 192 197
103 81 my $config = YAML::XS::LoadFile($path);
104 81 ▶ next unless ref $config eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $config eq "HASH" 0 81 81
unless ref $config eq "HASH" 1 191 192
105 # While the file is being read is the only place where its own
106 # directory is still known: the merge below keeps whole entries but
107 # not where they came from, and with several files merged that
108 # directory differs per entry.
109 81 push @configs, $self->_resolve_file_references($config, $path);
110 }
111 73 ▶ return undef unless @configs;
Branch: 2 branches
BranchTrueFalseTotal
unless @configs 4 69 73
unless @configs 3 190 193
112 69 return $self->_merge_configs(@configs);
113 },
114 40 );
115  
116 # The file references a kubeconfig entry can hold, per section: the key of the
117 # entry body, and the fields in it that name a file. The *-data twins hold
118 # base64 content rather than a path and are none of this code's business, and
119 # neither is an exec plugin's command, which is looked up in PATH.
120 40 my %FILE_REFERENCES = (
121 clusters => { body => 'cluster', fields => ['certificate-authority'] },
122 users => { body => 'user', fields => [qw(client-certificate client-key tokenFile)] },
123 );
124  
125 181 ▶sub _resolve_file_references {
Subroutine _resolve_file_references: called CC=11 SCAR=24.2
126 181 my ($self, $config, $file) = @_;
127  
128 # Absolute, so the result survives a later chdir - the file itself may well
129 # have been named relatively.
130 181 my $dir = path($file)->absolute->parent;
131  
132 181 for my $section (sort keys %FILE_REFERENCES) {
133 362 my $spec = $FILE_REFERENCES{$section};
134 362 ▶ for my $entry (@{ $config->{$section} // [] }) {
Condition100%2/2
$config->{$section} // []
l!l
34616
MC/DC100%1/1
$config->{$section} // []
$config->{$section}
Truth table100%2/2
A
A= $config->{$section}
Aresult
11
00
135 923 ▶ next unless ref $entry eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 0 923 923
136 923 my $body = $entry->{ $spec->{body} };
137 923 ▶ next unless ref $body eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 0 923 923
138 923 for my $field (@{ $spec->{fields} }) {
139 2223 my $value = $body->{$field};
140 2223 ▶ next unless defined $value and !ref $value and length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and length $value 1771 452 2223
Condition66%4/6
defined $value and not ref $value and length $value
!ll&&!rl&&r
17710452
defined $value and not ref $value
!ll&&!rl&&r
17710452
MC/DC33%1/3
defined $value and not ref $value and length $value
defined $value not ref $value length $value
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= length $value
ABCresult
0XX0
10X0
1100
1111
141 452 ▶ next if path($value)->is_absolute;
Branch: 1 branch
BranchTrueFalseTotal
if path($value)->is_absolute 92 360 452
142 360 $body->{$field} = $dir->child($value)->stringify;
143 }
144 }
145 }
146  
147 181 return $config;
148 }
149  
150 # The sections keyed by name, which are unioned; everything else is a plain
151 # top-level key taken from the first file that has it.
152 40 my @NAMED_SECTIONS = qw(clusters contexts users);
153 40 my %IS_NAMED_SECTION = map { $_ => 1 } @NAMED_SECTIONS;
154  
155 167 ▶sub _merge_configs {
Subroutine _merge_configs: called CC=13 SCAR=25.8
156 167 my ($self, @configs) = @_;
157  
158 167 my (%merged, %seen);
159 167 for my $config (@configs) {
160 181 for my $section (@NAMED_SECTIONS) {
161 543 ▶ for my $entry (@{ $config->{$section} // [] }) {
Condition100%2/2
$config->{$section} // []
l!l
52023
MC/DC100%1/1
$config->{$section} // []
$config->{$section}
Truth table100%2/2
A
A= $config->{$section}
Aresult
11
00
162 1547 ▶ next unless ref $entry eq 'HASH' and defined $entry->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" and defined $entry->{"name"} 0 1547 1547
Condition33%1/3
ref $entry eq "HASH" and defined $entry->{"name"}
!ll&&!rl&&r
001547
MC/DC0%0/2
ref $entry eq "HASH" and defined $entry->{"name"}
ref $entry eq "HASH" defined $entry->{"name"}
Truth table33%1/3
A and B
A= ref $entry eq "HASH"B= defined $entry->{"name"}
ABresult
0X0
100
111
163 1547 ▶ next if $seen{$section}{ $entry->{name} }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$section}{$entry->{"name"}}++ 31 1516 1547
164 1516 push @{ $merged{$section} }, $entry;
165 }
166 }
167 181 for my $key (grep { !$IS_NAMED_SECTION{$_} } keys %$config) {
168 546 ▶ next if exists $merged{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $merged{$key} 40 506 546
169 506 my $value = $config->{$key};
170 506 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 506 506
171 506 ▶ next if !ref $value and !length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value or length $value 12 494 506
Condition100%3/3
ref $value or length $value
l!l&&r!l&&!r
748712
MC/DC100%2/2
ref $value or length $value
ref $value length $value
Truth table100%3/3
A or B
A= ref $valueB= length $value
ABresult
1X1
011
000
172 494 $merged{$key} = $value;
173 }
174 }
175  
176 167 return \%merged;
177 }
178  
179 23 ▶sub _kubeconfig_paths_string {
Subroutine _kubeconfig_paths_string: called CC=2 SCAR=6.9
180 23 my $self = shift;
181 23 my $paths = $self->kubeconfig_paths;
182 23 ▶ return join($PATH_SEP, @$paths) if @$paths;
Branch: 1 branch
BranchTrueFalseTotal
if @$paths 17 6 23
183 6 return 'neither KUBECONFIG nor HOME is set';
184 }
185  
186 10 ▶sub _not_found_error {
Subroutine _not_found_error: called CC=1 SCAR=0.0
187 10 my $self = shift;
188 10 return 'Kubeconfig not found: ' . $self->_kubeconfig_paths_string;
189 }
190  
191 # The two ways a lookup fails before a single name is compared: the kubeconfig
192 # defines no such section at all, or there is no name to look for. Both used to
193 # reach _find_by_name, which compared against undef and croaked "not found: "
194 # with nothing behind the colon - an unusable kubeconfig, described in a way
195 # nobody can act on.
196 418 ▶sub _assert_lookup {
Subroutine _assert_lookup: called CC=5 SCAR=16.2
197 418 my ($self, $config, $section, $name, $missing_name_error) = @_;
198 418 my $entries = $config->{$section};
199 418 ▶ croak "kubeconfig defines no $section: " . $self->_kubeconfig_paths_string
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entries eq "ARRAY" and @$entries 7 411 418
Condition66%2/3
ref $entries eq "ARRAY" and @$entries
!ll&&!rl&&r
70411
MC/DC50%1/2
ref $entries eq "ARRAY" and @$entries
ref $entries eq "ARRAY" @$entries
Truth table66%2/3
A and B
A= ref $entries eq "ARRAY"B= @$entries
ABresult
0X0
100
111
200 unless ref $entries eq 'ARRAY' and @$entries;
201 411 ▶ croak "$missing_name_error: " . $self->_kubeconfig_paths_string
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 6 405 411
Condition66%2/3
defined $name and length $name
!ll&&!rl&&r
60405
MC/DC50%1/2
defined $name and length $name
defined $name length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
202 unless defined $name and length $name;
203 405 return;
204 }
205  
206 111 ▶sub current_context_name {
Subroutine current_context_name: called CC=3 SCAR=11.0
Pod documented
207 111 my $self = shift;
208  
209  
210 111 ▶ return $self->context_name if $self->has_context_name;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_context_name 5 106 111
211 106 ▶ my $config = $self->_config
Branch: 1 branch
BranchTrueFalseTotal
my $config = $self->_config or croak($self->_not_found_error) 100 1 101
212 or croak $self->_not_found_error;
213 100 return $config->{'current-context'};
214 }
215  
216 23 ▶sub contexts {
Subroutine contexts: called CC=3 SCAR=11.0
Pod documented
217 23 my $self = shift;
218  
219  
220 23 ▶ my $config = $self->_config
Branch: 1 branch
BranchTrueFalseTotal
my $config = $self->_config or croak($self->_not_found_error) 8 5 13
221 or croak $self->_not_found_error;
222 8 ▶ return [ map { $_->{name} } @{$config->{contexts} // []} ];
Condition100%2/2
$config->{"contexts"} // []
l!l
71
MC/DC100%1/1
$config->{"contexts"} // []
$config->{"contexts"}
Truth table100%2/2
A
A= $config->{"contexts"}
Aresult
11
00
223 }
224  
225 405 ▶sub _find_by_name {
Subroutine _find_by_name: called CC=4 SCAR=13.9
226 405 my ($self, $list, $name) = @_;
227 405 ▶ for my $item (@{$list // []}) {
Condition50%1/2
$list // []
l!l
4050
MC/DC0%0/1
$list // []
$list
Truth table50%1/2
A
A= $list
Aresult
11
00
228 730 ▶ return $item if $item->{name} eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"name"} eq $name 399 331 730
229 }
230 6 return undef;
231 }
232  
233 137 ▶sub context {
Subroutine context: called CC=4 SCAR=13.9
Pod documented
234 137 my ($self, $name) = @_;
235  
236  
237 137 ▶ my $config = $self->_config
Branch: 1 branch
BranchTrueFalseTotal
my $config = $self->_config or croak($self->_not_found_error) 136 1 137
238 or croak $self->_not_found_error;
239 136 ▶ $name //= $self->current_context_name;
Condition100%3/3
$name //= $self->current_context_name
l!l&&r!l&&!r
12817
MC/DC100%2/2
$name //= $self->current_context_name
$name $self->current_context_name
Truth table100%3/3
A //= B
A= $nameB= $self->current_context_name
ABresult
1X1
011
000
240 136 $self->_assert_lookup($config, 'contexts', $name,
241 'kubeconfig has no current-context set');
242 128 ▶ my $ctx = $self->_find_by_name($config->{contexts}, $name)
Branch: 1 branch
BranchTrueFalseTotal
my $ctx = $self->_find_by_name($config->{"contexts"}, $name) or croak("Context not found: $name") 126 2 128
243 or croak "Context not found: $name";
244 126 return $ctx->{context};
245 }
246  
247 146 ▶sub cluster {
Subroutine cluster: called CC=3 SCAR=11.0
Pod documented
248 146 my ($self, $name) = @_;
249  
250  
251 146 ▶ my $config = $self->_config
Branch: 1 branch
BranchTrueFalseTotal
my $config = $self->_config or croak($self->_not_found_error) 145 1 146
252 or croak $self->_not_found_error;
253 145 $self->_assert_lookup($config, 'clusters', $name, 'no cluster name given');
254 142 ▶ my $cluster = $self->_find_by_name($config->{clusters}, $name)
Branch: 1 branch
BranchTrueFalseTotal
my $cluster = $self->_find_by_name($config->{"clusters"}, $name) or croak("Cluster not found: $name") 140 2 142
255 or croak "Cluster not found: $name";
256 140 return $cluster->{cluster};
257 }
258  
259 138 ▶sub user {
Subroutine user: called CC=3 SCAR=11.0
Pod documented
260 138 my ($self, $name) = @_;
261  
262  
263 138 ▶ my $config = $self->_config
Branch: 1 branch
BranchTrueFalseTotal
my $config = $self->_config or croak($self->_not_found_error) 137 1 138
264 or croak $self->_not_found_error;
265 137 $self->_assert_lookup($config, 'users', $name, 'no user name given');
266 135 ▶ my $user = $self->_find_by_name($config->{users}, $name)
Branch: 1 branch
BranchTrueFalseTotal
my $user = $self->_find_by_name($config->{"users"}, $name) or croak("User not found: $name") 133 2 135
267 or croak "User not found: $name";
268 133 return $user->{user};
269 }
270  
271 363 ▶sub _resolve_cert {
Subroutine _resolve_cert: called CC=3 SCAR=11.0
272 363 my ($self, $hash, $key) = @_;
273  
274 363 my $data_key = "${key}-data";
275 363 ▶ if (my $data = $hash->{$data_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $hash->{$data_key}) 7 356 363
276 7 return (pem => decode_base64($data));
277 }
278  
279 356 ▶ if (my $file = $hash->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $hash->{$key}) 20 336 356
280 20 return (file => $file);
281 }
282  
283 336 return ();
284 }
285  
286 144 ▶sub api {
Subroutine api: called CC=20 SCAR=30.0
Pod documented
287 144 my ($self, $context_name, @options) = @_;
288  
289  
290 # Checked before the kubeconfig is read: a mistake in the call, not in the
291 # kubeconfig. The message names the key, never the value.
292 144 ▶ croak 'api() takes a context name and then key/value options,'
Branch: 1 branch
BranchTrueFalseTotal
if @options % 2 1 143 144
293 . ' got an odd number of option elements'
294 if @options % 2;
295 143 my %options = @options;
296 143 for my $key (qw(server credentials)) {
297 croak "api() builds $key from the kubeconfig, $key is not an option"
298 285 ▶ if exists $options{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{$key} 2 283 285
299 }
300  
301 # If no kubeconfig, try in-cluster
302 141 ▶ unless ($self->_config) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_config) 3 124 127
303 3 ▶ return $self->_in_cluster_api(%options)
Condition100%2/2
$self->_in_cluster_api(%options) // croak($self->_not_found_error . ' and not running in-cluster')
l!l
21
MC/DC0%0/1
$self->_in_cluster_api(%options) // croak($self->_not_found_error . ' and not running in-cluster')
$self->_in_cluster_api(%options)
Truth table50%1/2
A
A= $self->_in_cluster_api(%options)
Aresult
11
00
304 // croak $self->_not_found_error . " and not running in-cluster";
305 }
306  
307 124 ▶ $context_name //= $self->current_context_name;
Condition100%3/3
$context_name //= $self->current_context_name
l!l&&r!l&&!r
41812
MC/DC100%2/2
$context_name //= $self->current_context_name
$context_name $self->current_context_name
Truth table100%3/3
A //= B
A= $context_nameB= $self->current_context_name
ABresult
1X1
011
000
308 124 my $ctx = $self->context($context_name);
309 122 my $cluster = $self->cluster($ctx->{cluster});
310 121 my $user = $self->user($ctx->{user});
311  
312 # Build server config
313 my %server = (
314 endpoint => $cluster->{server},
315 121 );
316  
317 121 ▶ if (my %ca = $self->_resolve_cert($cluster, 'certificate-authority')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%ca) = $self->_resolve_cert($cluster, "certificate-authority")) 11 110 121
318 11 ▶ $server{ $ca{pem} ? 'ssl_ca_pem' : 'ssl_ca_file' } = $ca{pem} // $ca{file};
Branch: 1 branch
BranchTrueFalseTotal
$ca{'pem'} ? : 5 6 11
Condition66%2/3
$ca{"pem"} // $ca{"file"}
l!l&&r!l&&!r
560
MC/DC0%0/2
$ca{"pem"} // $ca{"file"}
$ca{"pem"} $ca{"file"}
Truth table66%2/3
A // B
A= $ca{"pem"}B= $ca{"file"}
ABresult
1X1
011
000
319 }
320  
321 121 ▶ if ($cluster->{'insecure-skip-tls-verify'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cluster->{'insecure-skip-tls-verify'}) { } 104 17 121
322 104 $server{ssl_verify_server} = 0;
323 } else {
324 17 $server{ssl_verify_server} = 1;
325 }
326  
327 121 ▶ if (my %cert = $self->_resolve_cert($user, 'client-certificate')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%cert) = $self->_resolve_cert($user, "client-certificate")) 8 113 121
328 8 ▶ $server{ $cert{pem} ? 'ssl_cert_pem' : 'ssl_cert_file' } = $cert{pem} // $cert{file};
Branch: 1 branch
BranchTrueFalseTotal
$cert{'pem'} ? : 1 7 8
Condition66%2/3
$cert{"pem"} // $cert{"file"}
l!l&&r!l&&!r
170
MC/DC0%0/2
$cert{"pem"} // $cert{"file"}
$cert{"pem"} $cert{"file"}
Truth table66%2/3
A // B
A= $cert{"pem"}B= $cert{"file"}
ABresult
1X1
011
000
329 }
330  
331 121 ▶ if (my %key = $self->_resolve_cert($user, 'client-key')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%key) = $self->_resolve_cert($user, "client-key")) 8 113 121
332 8 ▶ $server{ $key{pem} ? 'ssl_key_pem' : 'ssl_key_file' } = $key{pem} // $key{file};
Branch: 1 branch
BranchTrueFalseTotal
$key{'pem'} ? : 1 7 8
Condition66%2/3
$key{"pem"} // $key{"file"}
l!l&&r!l&&!r
170
MC/DC0%0/2
$key{"pem"} // $key{"file"}
$key{"pem"} $key{"file"}
Truth table66%2/3
A // B
A= $key{"pem"}B= $key{"file"}
ABresult
1X1
011
000
333 }
334  
335 # Build credentials
336 121 my $credentials;
337 121 ▶ if (my $token = $user->{token}) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $token = $user->{'token'}) { } 31 90 121
elsif (my $token_file = $user->{'tokenFile'}) { } 29 61 90
elsif (my $exec = $user->{'exec'}) { } 51 10 61
338 31 $credentials = Kubernetes::REST::AuthToken->new(token => $token);
339 } elsif (my $token_file = $user->{tokenFile}) {
340 29 $credentials = $self->_token_file_credential(
341 $token_file, "tokenFile of user '$ctx->{user}'"
342 );
343 } elsif (my $exec = $user->{exec}) {
344 51 $credentials = $self->_exec_credential($exec);
345 } else {
346 # No token auth, might be using client certs only
347 10 $credentials = Kubernetes::REST::AuthToken->new(token => '');
348 }
349  
350 96 return Kubernetes::REST->new(
351 %options,
352 server => Kubernetes::REST::Server->new(%server),
353 credentials => $credentials,
354 );
355 }
356  
357 # Credentials for a file holding a bearer token: a user's tokenFile, or the
358 # service account token mounted into a pod. Both are written by something else
359 # - the kubelet, a login helper - and both are rotated under a running process,
360 # so both get credentials that notice.
361 38 ▶sub _token_file_credential {
Subroutine _token_file_credential: called CC=2 SCAR=6.9
362 38 my ($self, $file, $what) = @_;
363  
364 38 ▶ return Kubernetes::REST::AuthTokenFile->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->refresh_token_files ? : 36 2 38
365 file => $file,
366 description => $what,
367 refresh => $self->refresh_token_files ? 1 : 0,
368 );
369 }
370  
371 51 ▶sub _exec_credential {
Subroutine _exec_credential: called CC=3 SCAR=11.0
372 51 my ($self, $exec) = @_;
373  
374 return Kubernetes::REST::AuthExec->new(
375 command => $exec->{command},
376 args => $exec->{args} // [],
377 51 ▶ env => $exec->{env} // [],
Condition100%4/4
$exec->{"args"} // []
l!l
483
$exec->{"env"} // []
l!l
645
MC/DC100%1/1
$exec->{"args"} // []
$exec->{"args"}
MC/DC100%1/1
$exec->{"env"} // []
$exec->{"env"}
Truth table100%2/2
A
A= $exec->{"args"}
Aresult
11
00
Truth table100%2/2
A
A= $exec->{"env"}
Aresult
11
00
378 );
379 }
380  
381 40 our $SA_TOKEN = '/var/run/secrets/kubernetes.io/serviceaccount/token';
382 40 our $SA_CA = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt';
383  
384 9 ▶sub _in_cluster_api {
Subroutine _in_cluster_api: called CC=6 SCAR=17.9
385 9 my ($self, %options) = @_;
386 9 ▶ return undef unless -f $SA_TOKEN;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $Kubernetes::REST::Kubeconfig::SA_TOKEN 2 7 9
387  
388 7 ▶ my $host = $ENV{KUBERNETES_SERVICE_HOST} // 'kubernetes.default.svc';
Condition100%2/2
$ENV{"KUBERNETES_SERVICE_HOST"} // "kubernetes.default.svc"
l!l
61
MC/DC100%1/1
$ENV{"KUBERNETES_SERVICE_HOST"} // "kubernetes.default.svc"
$ENV{"KUBERNETES_SERVICE_HOST"}
Truth table100%2/2
A
A= $ENV{"KUBERNETES_SERVICE_HOST"}
Aresult
11
00
389 7 ▶ my $port = $ENV{KUBERNETES_SERVICE_PORT} // '443';
Condition100%2/2
$ENV{"KUBERNETES_SERVICE_PORT"} // 443
l!l
43
MC/DC100%1/1
$ENV{"KUBERNETES_SERVICE_PORT"} // 443
$ENV{"KUBERNETES_SERVICE_PORT"}
Truth table100%2/2
A
A= $ENV{"KUBERNETES_SERVICE_PORT"}
Aresult
11
00
390 7 ▶ $host = "[$host]" if $host =~ /:/ && $host !~ /^\[/;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /:/ and not $host =~ /^\[/ 1 6 7
Condition100%3/3
$host =~ /:/ and not $host =~ /^\[/
!ll&&!rl&&r
511
MC/DC100%2/2
$host =~ /:/ and not $host =~ /^\[/
$host =~ /:/ not $host =~ /^\[/
Truth table100%3/3
A and not(B)
A= $host =~ /:/B= not $host =~ /^\[/
ABresult
0X0
100
111
391  
392 7 return Kubernetes::REST->new(
393 %options,
394 server => Kubernetes::REST::Server->new(
395 endpoint => "https://$host:$port",
396 ssl_ca_file => $SA_CA,
397 ssl_verify_server => 1,
398 ),
399 credentials => $self->_token_file_credential(
400 $SA_TOKEN, 'service account token'
401 ),
402 );
403 }
404  
405 40 1;
406  
407 __END__
« lib/Kubernetes/REST/CLI/Minikube.pm ↑ Summary blib/lib/Kubernetes/REST/CLI/Cmd/Get.pm »