blib/lib/Rex/Rancher.pm

filter: Statementstmt 74.4% 160 / 215 Branchbran 66.6% 80 / 120 Conditioncond 47.3% 35 / 74 MC/DCmcdc 40.0% 18 / 45 Subroutinesub 83.3% 25 / 30 Podpod 100% 3 / 3 totaltotal 65.9% 321 / 487 CC 91 SCAR 44.0
CC
91
Cov
67%
rancher_scan_known_hosts
420.0
_check_connection
56.0
_have_local_command
30.0
CRAP 81.3
↑ Summary eg/lan.Rexfile »
1 # ABSTRACT: Rancher Kubernetes (RKE2/K3s) deployment automation for Rex
2  
3 package Rex::Rancher;
4  
5 39 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 39 our $VERSION = '0.004';
9  
10 39 ▶use Rex::Rancher::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 39 ▶use Rex::Rancher::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 39 ▶use Rex::Rancher::Agent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 39 ▶use Rex::Rancher::Cilium;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 39 ▶use Rex::Rancher::K8s;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 39 ▶use Rex::Rancher::Distribution;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 39 ▶use Rex::Rancher::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 39 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 39 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 39 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 39 require Rex::Exporter;
23 39 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 39 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 39 @EXPORT = qw(
28 rancher_deploy_server
29 rancher_deploy_agent
30 rancher_scan_known_hosts
31 wait_for_api
32 untaint_node
33 deploy_nvidia_device_plugin
34 );
35  
36  
37 0 ▶sub _check_connection {
Subroutine _check_connection: not called CC=7 SCAR=40.3
38 0 ▶ my $conn = Rex::get_current_connection() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $conn = Rex::get_current_connection() or return 0 0 0
39 0 ▶ return if Rex::is_local();
Branch: 1 branch
BranchTrueFalseTotal
if Rex::is_local() 0 0 0
40  
41 0 ▶ my $type = eval { $conn->{conn}->get_connection_type() } // '';
Condition0%0/2
eval { do { $conn->{"conn"}->get_connection_type } } // ""
l!l
00
MC/DC0%0/1
eval { do { $conn->{"conn"}->get_connection_type } } // ""
eval { do { $conn->{"conn"}->get_connection_type } }
Truth table0%0/2
A
A= eval { do { $conn->{"conn"}->get_connection_type } }
Aresult
11
00
42 0 ▶ return if $type eq 'LibSSH';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "LibSSH" 0 0 0
43  
44 0 my $sftp = eval { Rex::get_sftp() };
45 0 ▶ return if $sftp && eval { $sftp->stat('/'); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if $sftp and eval { do { $sftp->stat("/"); 1 } } 0 0 0
Condition0%0/3
$sftp and eval { do { $sftp->stat("/"); 1 } }
!ll&&!rl&&r
000
MC/DC0%0/2
$sftp and eval { do { $sftp->stat("/"); 1 } }
$sftp eval { do { $sftp->stat("/"); 1 } }
Truth table0%0/3
A and B
A= $sftpB= eval { do { $sftp->stat("/"); 1 } }
ABresult
0X0
100
111
46  
47 0 die "This host has no SFTP subsystem and you are not using the LibSSH "
48 . "connection backend.\n"
49 . "Add 'set connection => \"LibSSH\"' to your Rexfile and install "
50 . "Rex::LibSSH to deploy to SFTP-less hosts.\n";
51 }
52  
53 # Before prepare_node and gpu_setup: an unknown distribution would otherwise
54 # pass node prep and Rex::GPU's driver install (containerd_config =>
55 # $distribution) and only die in install_server/install_agent.
56 126 ▶sub _check_distribution {
Subroutine _check_distribution: called CC=2 SCAR=6.9
57 126 my ( $distribution ) = @_;
58 126 ▶ return if exists Rex::Rancher::Distribution->distribution_classes->{$distribution};
Branch: 1 branch
BranchTrueFalseTotal
if exists "Rex::Rancher::Distribution"->distribution_classes->{$distribution} 118 8 126
59 8 die Rex::Rancher::Distribution->unknown_distribution($distribution)
60 . "; nothing was done on the host\n";
61 }
62  
63 79 ▶sub rancher_deploy_server {
Subroutine rancher_deploy_server: called CC=22 SCAR=31.0
Pod documented
64 79 my (%opts) = @_;
65 79 ▶ my $distribution = $opts{distribution} // 'rke2';
Condition100%2/2
$opts{"distribution"} // "rke2"
l!l
5425
MC/DC100%1/1
$opts{"distribution"} // "rke2"
$opts{"distribution"}
Truth table100%2/2
A
A= $opts{"distribution"}
Aresult
11
00
66 79 my $kubeconfig_file = $opts{kubeconfig_file};
67 79 _check_distribution($distribution);
68 75 _check_gpu_module(%opts);
69  
70 my %cilium_opts = (
71 distribution => $distribution,
72 201 ▶ ( map { exists $opts{"cilium_$_"} ? ( $_ => $opts{"cilium_$_"} ) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{"cilium_$_"} ? : 4 197 201
73 qw( version cli_version helm_values ) ),
74 67 ▶ ( map { exists $opts{$_} ? ( $_ => $opts{$_} ) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{$_} ? : 23 379 402
75 qw( gateway_api gateway_api_version gateway_api_channel k8s_service_host
76 cluster_cidr ipam_mode ) ),
77 );
78 # k3s: Cilium reaches the API at the control plane's address, which is the
79 # first tls_san, the name the certificate is made for. No fallback to the
80 # kubeconfig_server (this machine's view) or localhost (agents have no
81 # 6443 there); install_cilium dies without one.
82 67 ▶ if (Rex::Rancher::Distribution->new_for($distribution)->needs_k8s_service_host
Branch: 1 branch
BranchTrueFalseTotal
if ("Rex::Rancher::Distribution"->new_for($distribution)->needs_k8s_service_host and not exists $cilium_opts{"k8s_service_host"}) 23 44 67
Condition100%3/3
"Rex::Rancher::Distribution"->new_for($distribution)->needs_k8s_service_host and not exists $cilium_opts{"k8s_service_host"}
!ll&&!rl&&r
42223
MC/DC100%2/2
"Rex::Rancher::Distribution"->new_for($distribution)->needs_k8s_service_host and not exists $cilium_opts{"k8s_service_host"}
"Rex::Rancher::Distribution"->new_for($distribution)->needs_k8s_service_host not exists $cilium_opts{"k8s_service_host"}
Truth table100%3/3
A and not(B)
A= "Rex::Rancher::Distribution"->new_for($distribution)->needs_k8s_service_hostB= not exists $cilium_opts{"k8s_service_host"}
ABresult
0X0
100
111
83 && !exists $cilium_opts{k8s_service_host}) {
84 23 my $first_san = _kubeconfig_server_addr(tls_san => $opts{tls_san});
85 23 ▶ $cilium_opts{k8s_service_host} = $first_san if defined $first_san && length $first_san;
Branch: 1 branch
BranchTrueFalseTotal
if defined $first_san and length $first_san 19 4 23
Condition66%2/3
defined $first_san and length $first_san
!ll&&!rl&&r
4019
MC/DC50%1/2
defined $first_san and length $first_san
defined $first_san length $first_san
Truth table66%2/3
A and B
A= defined $first_sanB= length $first_san
ABresult
0X0
100
111
86 }
87 67 ▶ my $cilium = exists $opts{cilium} ? $opts{cilium} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'cilium'} ? : 28 39 67
88  
89 # Refuse bad or contradictory Cilium options before the node is touched,
90 # not at step 7. cluster_cidr is install_server's too, cilium or not.
91 67 Rex::Rancher::Options->check_cluster_cidr($opts{cluster_cidr});
92 65 ▶ if ($cilium) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cilium) { } 40 25 65
93 # install_cilium could read k8sServiceHost from a running Cilium, but a
94 # first deploy has none: the address must come from the options here.
95 40 Rex::Rancher::Cilium::validate_cilium_opts(%cilium_opts, kubeconfig => $kubeconfig_file);
96 }
97 else {
98 my @set = (
99 ( $opts{gateway_api} ? 'gateway_api' : () ),
100 25 ▶ ( grep { defined $opts{$_} }
Branch: 1 branch
BranchTrueFalseTotal
$opts{'gateway_api'} ? : 1 24 25
101 qw( cilium_version cilium_cli_version cilium_helm_values k8s_service_host
102 ipam_mode ) ),
103 );
104 25 ▶ die "cilium => 0 keeps the distribution's built-in CNI, but @set "
Branch: 1 branch
BranchTrueFalseTotal
if @set 6 19 25
105 . "configure Cilium: drop them or leave cilium on\n" if @set;
106 }
107  
108 55 _check_connection();
109 # Everything install_server would refuse (Cilium residue, version skew with
110 # hold_running, cluster-cidr), before the node is prepared: a refused host
111 # is left as it was (k78). install_server asks again on the host as node
112 # preparation and GPU setup (a reboot) left it; both only read.
113 55 my @install = ( _install_opts(%opts), _gateway_api_disable(%opts) );
114 55 Rex::Rancher::Server::preflight_server(@install);
115 47 prepare_node(%opts);
116  
117 47 _gpu_setup_if_requested($distribution, %opts);
118  
119 47 install_server(@install);
120  
121 # Fetch and save kubeconfig locally, then wait for the API from this machine.
122 # install_server only waits for the kubeconfig file to appear on the remote;
123 # actual API readiness is confirmed here via Rex::Rancher::K8s::wait_for_api.
124 47 my $local_kc = _save_kubeconfig_locally($distribution, $kubeconfig_file, %opts);
125 42 ▶ my $api_up = $local_kc && wait_for_api(kubeconfig => $local_kc);
Condition100%3/3
$local_kc && wait_for_api('kubeconfig', $local_kc)
!ll&&!rl&&r
8232
MC/DC100%2/2
$local_kc && wait_for_api('kubeconfig', $local_kc)
$local_kc wait_for_api('kubeconfig', $local_kc)
Truth table100%3/3
A && B
A= $local_kcB= wait_for_api('kubeconfig', $local_kc)
ABresult
0X0
100
111
126  
127 # A saved kubeconfig whose API never answered: everything after needs that
128 # API (Helm release state, gateway_api, device plugin), so stop here rather
129 # than fall back to the remote-only Cilium path and fail later on a
130 # misleading error. The server itself is installed; a re-run picks up.
131 42 ▶ if ($local_kc && !$api_up) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local_kc and not $api_up) 2 40 42
Condition100%3/3
$local_kc and not $api_up
!ll&&!rl&&r
8322
MC/DC100%2/2
$local_kc and not $api_up
$local_kc not $api_up
Truth table100%3/3
A and not(B)
A= $local_kcB= not $api_up
ABresult
0X0
100
111
132 2 ▶ my $addr = _kubeconfig_server_addr(%opts) // '127.0.0.1';
Condition50%1/2
_kubeconfig_server_addr(%opts) // "127.0.0.1"
l!l
20
MC/DC0%0/1
_kubeconfig_server_addr(%opts) // "127.0.0.1"
_kubeconfig_server_addr(%opts)
Truth table50%1/2
A
A= _kubeconfig_server_addr(%opts)
Aresult
11
00
133 2 ▶ my $reach = $addr =~ /^\[.*\]:\d+$/ || $addr =~ /^[^:]+:\d+$/ ? $addr : "$addr:6443";
Branch: 1 branch
BranchTrueFalseTotal
$addr =~ /^\[.*\]:\d+$/u || $addr =~ /^[^:]+:\d+$/u ? : 0 2 2
Condition33%1/3
$addr =~ /^\[.*\]:\d+$/u or $addr =~ /^[^:]+:\d+$/u
l!l&&r!l&&!r
002
MC/DC0%0/2
$addr =~ /^\[.*\]:\d+$/u or $addr =~ /^[^:]+:\d+$/u
$addr =~ /^\[.*\]:\d+$/u $addr =~ /^[^:]+:\d+$/u
Truth table33%1/3
A or B
A= $addr =~ /^\[.*\]:\d+$/uB= $addr =~ /^[^:]+:\d+$/u
ABresult
1X1
011
000
134 2 die "Kubernetes API at $addr did not answer through $local_kc within "
135 . "wait_for_api's timeout; $distribution server is installed, Cilium "
136 . "and later steps did not run. Check that this machine reaches "
137 . "$reach (firewall, tls_san, kubeconfig_server), or omit "
138 . "kubeconfig_file to install Cilium through the remote host only\n";
139 }
140  
141 # Without a saved kubeconfig install_cilium keeps its remote-only path.
142 # cilium => 0: install_server kept Canal/Flannel, nothing to install here.
143 40 ▶ install_cilium(%cilium_opts, $api_up ? ( kubeconfig => $local_kc ) : ()) if $cilium;
Branch: 2 branches
BranchTrueFalseTotal
if $cilium 29 11 40
$api_up ? : 28 1 29
144  
145 40 my %gpu_steps = _gpu_steps(%opts);
146 40 ▶ if ($gpu_steps{device_plugin} && $local_kc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gpu_steps{"device_plugin"} and $local_kc) 2 38 40
Condition100%3/3
$gpu_steps{"device_plugin"} and $local_kc
!ll&&!rl&&r
3622
MC/DC100%2/2
$gpu_steps{"device_plugin"} and $local_kc
$gpu_steps{"device_plugin"} $local_kc
Truth table100%3/3
A and B
A= $gpu_steps{"device_plugin"}B= $local_kc
ABresult
0X0
100
111
147 2 deploy_nvidia_device_plugin(kubeconfig => $local_kc);
148 }
149  
150 40 Rex::Logger::info("$distribution server deployment complete");
151 }
152  
153  
154 47 ▶sub rancher_deploy_agent {
Subroutine rancher_deploy_agent: called CC=5 SCAR=16.1
Pod documented
155 47 my (%opts) = @_;
156 47 ▶ my $distribution = $opts{distribution} // 'rke2';
Condition100%2/2
$opts{"distribution"} // "rke2"
l!l
389
MC/DC100%1/1
$opts{"distribution"} // "rke2"
$opts{"distribution"}
Truth table100%2/2
A
A= $opts{"distribution"}
Aresult
11
00
157 47 _check_distribution($distribution);
158 43 _check_gpu_module(%opts);
159  
160 # install_agent needs both; refuse before prepare_node and gpu_setup
161 # (driver install, possibly a reboot) have touched the host.
162 35 ▶ die "server is required for rancher_deploy_agent\n" unless $opts{server};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"server"} 1 34 35
163 34 ▶ die "token is required for rancher_deploy_agent\n" unless $opts{token};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"token"} 1 33 34
164  
165 33 _check_connection();
166 # The server's saved kubeconfig, if given, lets the agent be checked
167 # against the control plane's version. Everything install_agent would
168 # refuse, before the node is prepared, as on the server (k78).
169 my @install = ( _install_opts(%opts),
170 32 ▶ ( defined $opts{kubeconfig_file} ? ( kubeconfig => $opts{kubeconfig_file} ) : () ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'kubeconfig_file'} ? : 5 27 32
171 32 Rex::Rancher::Agent::preflight_agent(@install);
172 22 prepare_node(%opts);
173  
174 22 _gpu_setup_if_requested($distribution, %opts);
175  
176 22 install_agent(@install);
177  
178 22 Rex::Logger::info("$distribution agent deployment complete");
179 }
180  
181  
182 0 ▶sub rancher_scan_known_hosts {
Subroutine rancher_scan_known_hosts: not called CC=20 SCAR=60.4
Pod documented
183 0 my ($host, %opts) = @_;
184  
185 0 ▶ $host = "$host" if ref $host; # Rex server objects stringify to the host
Branch: 1 branch
BranchTrueFalseTotal
if ref $host 0 0 0
186 0 ▶ return unless defined $host && length $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and length $host 0 0 0
Condition0%0/3
defined $host and length $host
!ll&&!rl&&r
000
MC/DC0%0/2
defined $host and length $host
defined $host length $host
Truth table0%0/3
A and B
A= defined $hostB= length $host
ABresult
0X0
100
111
187 0 ▶ return if $host eq 'localhost' || $host eq '127.0.0.1' || $host eq '::1';
Branch: 1 branch
BranchTrueFalseTotal
if $host eq "localhost" or $host eq "127.0.0.1" or $host eq "::1" 0 0 0
Condition0%0/6
$host eq "localhost" or $host eq "127.0.0.1" or $host eq "::1"
l!l&&r!l&&!r
000
$host eq "localhost" or $host eq "127.0.0.1"
l!l&&r!l&&!r
000
MC/DC0%0/3
$host eq "localhost" or $host eq "127.0.0.1" or $host eq "::1"
$host eq "localhost" $host eq "127.0.0.1" $host eq "::1"
Truth table0%0/4
A or B or C
A= $host eq "localhost"B= $host eq "127.0.0.1"C= $host eq "::1"
ABCresult
1XX1
01X1
0011
0000
188  
189 0 ▶ unless (_have_local_command('ssh-keyscan')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_have_local_command("ssh-keyscan")) 0 0 0
190 0 Rex::Logger::info(
191 "ssh-keyscan not found locally — cannot pre-seed the host key for "
192 . "$host; a verified connect will fail if the key is unknown", "warn");
193 0 return;
194 }
195  
196 0 my $known_hosts = $opts{known_hosts};
197 0 ▶ unless (defined $known_hosts && length $known_hosts) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $known_hosts and length $known_hosts) 0 0 0
Condition0%0/3
defined $known_hosts and length $known_hosts
!ll&&!rl&&r
000
MC/DC0%0/2
defined $known_hosts and length $known_hosts
defined $known_hosts length $known_hosts
Truth table0%0/3
A and B
A= defined $known_hostsB= length $known_hosts
ABresult
0X0
100
111
198 0 ▶ unless ($ENV{HOME}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"HOME"}) 0 0 0
199 0 Rex::Logger::info(
200 "Cannot scan host key for $host: \$HOME is unset and no known_hosts "
201 . "path was given", "warn");
202 0 return;
203 }
204 0 $known_hosts = "$ENV{HOME}/.ssh/known_hosts";
205 }
206  
207 # Idempotent: if the key is already trusted, leave known_hosts untouched —
208 # never disturb a key the operator has already pinned.
209 0 ▶ return if -f $known_hosts && _known_host_present($host, $known_hosts);
Branch: 1 branch
BranchTrueFalseTotal
if -f $known_hosts and _known_host_present($host, $known_hosts) 0 0 0
Condition0%0/3
-f $known_hosts and _known_host_present($host, $known_hosts)
!ll&&!rl&&r
000
MC/DC0%0/2
-f $known_hosts and _known_host_present($host, $known_hosts)
-f $known_hosts _known_host_present($host, $known_hosts)
Truth table0%0/3
A and B
A= -f $known_hostsB= _known_host_present($host, $known_hosts)
ABresult
0X0
100
111
210  
211 0 ▶ my $keys = _run_local_capture('ssh-keyscan', $host) // '';
Condition0%0/2
_run_local_capture("ssh-keyscan", $host) // ""
l!l
00
MC/DC0%0/1
_run_local_capture("ssh-keyscan", $host) // ""
_run_local_capture("ssh-keyscan", $host)
Truth table0%0/2
A
A= _run_local_capture("ssh-keyscan", $host)
Aresult
11
00
212 0 ▶ my @key_lines = grep { /\S/ && !/^\s*#/ } split /\n/, $keys;
Condition0%0/3
/\S/u && !/^\s*#/u
!ll&&!rl&&r
000
MC/DC0%0/2
/\S/u && !/^\s*#/u
/\S/u !/^\s*#/u
Truth table0%0/3
A && not(B)
A= /\S/uB= !/^\s*#/u
ABresult
0X0
100
111
213 0 ▶ unless (@key_lines) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@key_lines) 0 0 0
214 0 Rex::Logger::info(
215 "ssh-keyscan returned no host key for $host (host unreachable?) — "
216 . "known_hosts left unchanged", "warn");
217 0 return;
218 }
219  
220 0 my $dir = dirname($known_hosts);
221 0 ▶ make_path($dir, { mode => 0700 }) if length $dir && !-d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if length $dir and not -d $dir 0 0 0
Condition0%0/3
length $dir and not -d $dir
!ll&&!rl&&r
000
MC/DC0%0/2
length $dir and not -d $dir
length $dir not -d $dir
Truth table0%0/3
A and not(B)
A= length $dirB= not -d $dir
ABresult
0X0
100
111
222  
223 open(my $fh, '>>', $known_hosts)
224 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $known_hosts or do { Rex::Logger::info("Cannot append to ${known_hosts}: $! \342\200\224 host key for $host not saved", "warn"); return } 0 0 0
225 0 Rex::Logger::info(
226 "Cannot append to $known_hosts: $! — host key for $host not saved",
227 "warn");
228 0 return;
229 };
230 0 print $fh "$_\n" for @key_lines;
231 0 close $fh;
232  
233 0 Rex::Logger::info("Scanned host key for $host into $known_hosts");
234 0 return 1;
235 }
236  
237 0 ▶sub _known_host_present {
Subroutine _known_host_present: not called CC=3 SCAR=24.8
238 0 my ($host, $known_hosts) = @_;
239 0 ▶ return 0 unless _have_local_command('ssh-keygen');
Branch: 1 branch
BranchTrueFalseTotal
unless _have_local_command("ssh-keygen") 0 0 0
240 # ssh-keygen -F exits 0 when a matching entry exists (handles hashed hosts),
241 # 1 otherwise. Its stdout is captured so the found line is not echoed.
242 0 _run_local_capture('ssh-keygen', '-F', $host, '-f', $known_hosts);
243 0 ▶ return ($? >> 8) == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? >> 8 == 0 ? : 0 0 0
244 }
245  
246 0 ▶sub _have_local_command {
Subroutine _have_local_command: not called CC=5 SCAR=34.0
247 0 my ($name) = @_;
248 0 ▶ for my $dir (split /:/, ($ENV{PATH} // '')) {
Condition0%0/2
$ENV{"PATH"} // ""
l!l
00
MC/DC0%0/1
$ENV{"PATH"} // ""
$ENV{"PATH"}
Truth table0%0/2
A
A= $ENV{"PATH"}
Aresult
11
00
249 0 ▶ return 1 if length $dir && -x "$dir/$name";
Branch: 1 branch
BranchTrueFalseTotal
if length $dir and -x "$dir/$name" 0 0 0
Condition0%0/3
length $dir and -x "$dir/$name"
!ll&&!rl&&r
000
MC/DC0%0/2
length $dir and -x "$dir/$name"
length $dir -x "$dir/$name"
Truth table0%0/3
A and B
A= length $dirB= -x "$dir/$name"
ABresult
0X0
100
111
250 }
251 0 return 0;
252 }
253  
254 0 ▶sub _run_local_capture {
Subroutine _run_local_capture: not called CC=3 SCAR=24.8
255 0 my (@cmd) = @_;
256 0 ▶ my $pid = open(my $out, '-|', @cmd) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pid = open(my $out, "-|", @cmd) or return 0 0 0
257 0 local $/;
258 0 my $content = <$out>;
259 0 close $out; # sets $? to the child exit status
260 0 ▶ return defined $content ? $content : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 0 0 0
261 }
262  
263 # Which GPU steps run, from the options alone. Without gpu nothing does; with
264 # gpu each step runs unless its own switch turns it off. runtime_path: the
265 # rke2 unit PATH for a toolkit the host brought, needed only without gpu_setup
266 # (Rex::GPU's containerd drop-in names the runtime by absolute path).
267 296 ▶sub _gpu_steps {
Subroutine _gpu_steps: called CC=7 SCAR=19.5
268 296 my (%opts) = @_;
269 296 ▶ return ( setup => 0, device_plugin => 0, runtime_path => 0 ) unless $opts{gpu};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"gpu"} 183 113 296
270 113 ▶ my $setup = ( $opts{gpu_setup} // 1 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'gpu_setup'} // 1 ? : 66 47 113
Condition100%2/2
$opts{"gpu_setup"} // 1
l!l
4766
MC/DC100%1/1
$opts{"gpu_setup"} // 1
$opts{"gpu_setup"}
Truth table100%2/2
A
A= $opts{"gpu_setup"}
Aresult
11
00
271 return (
272 setup => $setup,
273 113 ▶ device_plugin => ( $opts{gpu_device_plugin} // 1 ) ? 1 : 0,
Branch: 2 branches
BranchTrueFalseTotal
$opts{'gpu_device_plugin'} // 1 ? : 61 52 113
$setup ? : 66 47 113
Condition100%2/2
$opts{"gpu_device_plugin"} // 1
l!l
5261
MC/DC100%1/1
$opts{"gpu_device_plugin"} // 1
$opts{"gpu_device_plugin"}
Truth table100%2/2
A
A= $opts{"gpu_device_plugin"}
Aresult
11
00
274 runtime_path => $setup ? 0 : 1,
275 );
276 }
277  
278 # The options install_server/install_agent get: the caller's, plus
279 # nvidia_runtime_path from the GPU switches unless the caller set it.
280 92 ▶sub _install_opts {
Subroutine _install_opts: called CC=2 SCAR=7.0
281 92 my (%opts) = @_;
282 92 my %steps = _gpu_steps(%opts);
283 92 ▶ return ( %opts, nvidia_runtime_path => $opts{nvidia_runtime_path} // $steps{runtime_path} );
Condition66%2/3
$opts{"nvidia_runtime_path"} // $steps{"runtime_path"}
l!l&&r!l&&!r
2900
MC/DC0%0/2
$opts{"nvidia_runtime_path"} // $steps{"runtime_path"}
$opts{"nvidia_runtime_path"} $steps{"runtime_path"}
Truth table66%2/3
A // B
A= $opts{"nvidia_runtime_path"}B= $steps{"runtime_path"}
ABresult
1X1
011
000
284 }
285  
286 # gateway_api on a distribution with its own Gateway API CRD chart (rke2
287 # v1.37+; validated earlier): the chart must stay off, or it overwrites what
288 # install_cilium applies. The default disable list gains it; a caller's own
289 # list is theirs to keep.
290 64 ▶sub _gateway_api_disable {
Subroutine _gateway_api_disable: called CC=6 SCAR=17.9
291 64 my (%opts) = @_;
292 64 ▶ return unless $opts{gateway_api};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"gateway_api"} 54 10 64
293  
294 10 my $dist = Rex::Rancher::Distribution->new_for($opts{distribution});
295 10 ▶ my $chart = $dist->gateway_api_crd_chart // return;
Condition100%2/2
$dist->gateway_api_crd_chart // (return)
l!l
82
MC/DC100%1/1
$dist->gateway_api_crd_chart // (return)
$dist->gateway_api_crd_chart
Truth table100%2/2
A
A= $dist->gateway_api_crd_chart
Aresult
11
00
296 8 my $disable = $opts{disable};
297 8 ▶ return ( disable => [ @{ $dist->default_disable }, $chart ] )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $disable 4 4 8
298 unless defined $disable;
299  
300 4 ▶ my @given = ref $disable eq 'ARRAY' ? @$disable : split(/,/, $disable);
Branch: 1 branch
BranchTrueFalseTotal
ref $disable eq 'ARRAY' ? : 3 1 4
301 Rex::Logger::info("gateway_api with a disable list lacking $chart: on RKE2 "
302 . "v1.37+ that chart overwrites the Gateway API CRDs Cilium relies on", 'warn')
303 4 ▶ unless grep { $_ eq $chart } @given;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $chart;} @given 2 2 4
304 4 return;
305 }
306  
307 # Before the host is touched: gpu_setup needs Rex::GPU 0.002 or later. 0.001
308 # writes Rex::GPU's bare containerd config.toml.tmpl again on every run, which
309 # rke2/k3s render instead of their own config (install_* removes it before
310 # the start; the NVIDIA runtime then comes only from rke2's own PATH scan).
311 # Rex::GPU is an optional peer distribution: loaded here, at runtime, only
312 # when gpu_setup is going to run.
313 108 ▶sub _check_gpu_module {
Subroutine _check_gpu_module: called CC=6 SCAR=17.9
314 108 my (%opts) = @_;
315 108 my %steps = _gpu_steps(%opts);
316 108 ▶ return unless $steps{setup};
Branch: 1 branch
BranchTrueFalseTotal
unless $steps{"setup"} 81 27 108
317  
318 27 my $min = '0.002';
319 27 my $hint = "Install Rex-GPU $min or later, or pass gpu_setup => 0 if the GPU "
320 . "Operator or the host provides the driver; nothing was done on the host\n";
321 27 ▶ unless (eval { require Rex::GPU; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Rex::GPU; 1 } }) 8 19 27
322 8 my $err = $@;
323 8 ▶ die "gpu => 1 requested but Rex::GPU is not installed. $hint"
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ m[^Can't locate Rex/GPU\.pm in \@INC]u 4 4 8
324 if $err =~ m{^Can't locate Rex/GPU\.pm in \@INC};
325 4 die "gpu => 1 requested but Rex::GPU could not be loaded: "
326 . ( $err =~ s/\s+\z//r ) . "\n$hint";
327 }
328 19 ▶ return if eval { Rex::GPU->VERSION($min); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { "Rex::GPU"->VERSION($min); 1 } } 11 8 19
329 8 my $have = Rex::GPU->VERSION;
330 8 ▶ die "gpu => 1 requested but Rex::GPU "
Branch: 1 branch
BranchTrueFalseTotal
defined $have ? : 4 4 8
331 . ( defined $have ? $have : 'without a version' ) . " is installed, and "
332 . "gpu_setup needs $min or later: older versions write a bare containerd "
333 . "config.toml.tmpl on every run. $hint";
334 }
335  
336 50 ▶sub _gpu_setup_if_requested {
Subroutine _gpu_setup_if_requested: called CC=6 SCAR=17.9
337 50 my ($distribution, %opts) = @_;
338  
339 50 my %steps = _gpu_steps(%opts);
340 50 ▶ if ($opts{gpu} && !$steps{setup}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"gpu"} and not $steps{"setup"}) 13 37 50
Condition100%3/3
$opts{"gpu"} and not $steps{"setup"}
!ll&&!rl&&r
261113
MC/DC100%2/2
$opts{"gpu"} and not $steps{"setup"}
$opts{"gpu"} not $steps{"setup"}
Truth table100%3/3
A and not(B)
A= $opts{"gpu"}B= not $steps{"setup"}
ABresult
0X0
100
111
341 13 Rex::Logger::info("gpu_setup => 0: Rex::GPU not used, driver, toolkit and containerd config are left to the host or the GPU Operator");
342 13 ▶ Rex::Logger::info("reboot is ignored with gpu_setup => 0", "warn") if $opts{reboot};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"reboot"} 1 12 13
343 }
344 50 ▶ return unless $steps{setup};
Branch: 1 branch
BranchTrueFalseTotal
unless $steps{"setup"} 39 11 50
345  
346 # Loaded and version-checked by _check_gpu_module, which rancher_deploy_*
347 # run under the same _gpu_steps condition before the host is touched.
348 11 Rex::GPU->import();
349  
350 Rex::GPU::gpu_setup(
351 containerd_config => $distribution,
352 11 ▶ reboot => ($opts{reboot} // 0),
Condition100%2/2
$opts{"reboot"} // 0
l!l
110
MC/DC100%1/1
$opts{"reboot"} // 0
$opts{"reboot"}
Truth table100%2/2
A
A= $opts{"reboot"}
Aresult
11
00
353 );
354 }
355  
356 28 ▶sub _save_kubeconfig_locally {
Subroutine _save_kubeconfig_locally: called CC=3 SCAR=11.0
357 28 my ($distribution, $output_file, %opts) = @_;
358  
359 28 ▶ return unless $output_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $output_file 8 20 28
360  
361 # kubeconfig_file was asked for: without it wait_for_api, the Helm-aware
362 # Cilium path, gateway_api and the device plugin cannot run, so a failed
363 # fetch or write stops the deploy instead of degrading to the remote-only
364 # path. The server is installed by now; a re-run reuses its token.
365 20 my $stopped = "; $distribution server is installed, Cilium and later steps "
366 . "did not run. Fix the cause and re-run, or omit kubeconfig_file to "
367 . "install Cilium through the remote host only\n";
368  
369 # RKE2/K3s writes 127.0.0.1 in the kubeconfig; fetch_kubeconfig patches it
370 # to the real address, or, without one, leaves it as it is.
371 20 my $server_addr = _kubeconfig_server_addr(%opts);
372 20 my %loopback = map { $_ => 1 } qw(127.0.0.1 localhost ::1);
373  
374 # The address warnings run as the filter: after the fetch, before the
375 # write, where they always stood. A failed fetch says nothing about them.
376 13 ▶ my $warn_address = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
377 13 ▶ if (!defined $server_addr || !length $server_addr) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $server_addr && length $server_addr) { } 1 12 13
elsif ($loopback{lc(($server_addr =~ s/^\[([^\]]*)\](?::\d+)?$/$1/ru) =~ s/^([^:]+):\d+$/$1/ru)}) { } 4 8 12
Condition66%2/3
defined $server_addr && length $server_addr
!ll&&!rl&&r
1012
MC/DC50%1/2
defined $server_addr && length $server_addr
defined $server_addr length $server_addr
Truth table66%2/3
A && B
A= defined $server_addrB= length $server_addr
ABresult
0X0
100
111
378 # No tls_san / kubeconfig_server given: nothing to patch to, so the saved
379 # file keeps pointing at 127.0.0.1 and cannot reach the cluster remotely.
380 # Still save it (a local-only operator may want it) but make it loud.
381 1 Rex::Logger::info(
382 "Kubeconfig saved to $output_file but no server address could be "
383 . "derived — it still points at https://127.0.0.1 and will not reach "
384 . "the cluster from this machine; pass tls_san or kubeconfig_server",
385 "warn");
386 }
387 elsif ($loopback{ lc( $server_addr =~ s/^\[([^\]]*)\](?::\d+)?$/$1/r
388 =~ s/^([^:]+):\d+$/$1/r ) }) {
389 # A loopback address patches 127.0.0.1 to itself (or another loopback):
390 # still unreachable from the operator's machine.
391 4 Rex::Logger::info(
392 "Kubeconfig server address '$server_addr' is a loopback address — the "
393 . "kubeconfig saved to $output_file will still point at the loopback "
394 . "interface and will not reach the cluster from this machine; pass "
395 . "a routable tls_san or kubeconfig_server", "warn");
396 }
397 13 return $_[0];
398 20 };
399  
400 20 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { fetch_kubeconfig("distribution", $distribution, "server", $server_addr, "filter", $warn_address, "file", $output_file); 1 } } or die $@ =~ s/\s+\z//ru . $stopped 9 11 20
401 20 fetch_kubeconfig(
402 distribution => $distribution,
403 server => $server_addr,
404 filter => $warn_address,
405 file => $output_file,
406 );
407 9 1;
408 } or die $@ =~ s/\s+\z//r . $stopped;
409  
410 9 return $output_file;
411 }
412  
413 50 ▶sub _kubeconfig_server_addr {
Subroutine _kubeconfig_server_addr: called CC=5 SCAR=16.2
414 50 my (%opts) = @_;
415 50 ▶ return $opts{kubeconfig_server} if $opts{kubeconfig_server};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"kubeconfig_server"} 6 44 50
416 44 my $tls_san = $opts{tls_san};
417 44 ▶ return unless $tls_san;
Branch: 1 branch
BranchTrueFalseTotal
unless $tls_san 10 34 44
418 34 ▶ my @sans = ref $tls_san eq 'ARRAY' ? @{$tls_san} : split(/,/, $tls_san);
Branch: 1 branch
BranchTrueFalseTotal
ref $tls_san eq 'ARRAY' ? : 6 28 34
419 34 ▶ return $sans[0] if @sans;
Branch: 1 branch
BranchTrueFalseTotal
if @sans 34 0 34
420 0 return;
421 }
422  
423 39 1;
424  
425 __END__
↑ Summary eg/lan.Rexfile »