lib/Rex/Rancher/Distribution.pm

filter: Statementstmt 98.9% 465 / 470 Branchbran 96.0% 240 / 250 Conditioncond 78.7% 137 / 174 MC/DCmcdc 60.4% 61 / 101 Subroutinesub 100% 79 / 79 Podpod 100% 54 / 54 totaltotal 91.8% 1036 / 1128 CC 203 SCAR 19.7
CC
203
Cov
94%
check_version_skew
19.0
held_version
15.0
established_cluster_cidr
12.0
CRAP 7.1
« lib/Rex/Rancher/Cilium.pm ↑ Summary lib/Rex/Rancher/Server.pm »
1 # ABSTRACT: What RKE2 and K3s differ in, and the host steps they share
2  
3 package Rex::Rancher::Distribution;
4 63 our $VERSION = '0.004';
5 63 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 63 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 63 ▶use Module::Runtime qw( use_module );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 63 ▶use Rex::Commands::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 63 ▶use Rex::Commands::Run ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 63 ▶use Rex::Logger ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 63 ▶use Rex::Rancher::Checksum;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 63 ▶use Rex::Rancher::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 63 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 63 ▶use YAML::PP::Common qw( PRESERVE_ORDER );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 63 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # No `use utf8` here, on purpose: the die messages carry UTF-8 em dashes as
18 # byte strings, exactly as Rex::Rancher::Server always emitted them.
19  
20  
21 has role => (
22 is => 'ro',
23 default => 'server',
24 1596 ▶ isa => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
25 1596 ▶ die "role must be 'server' or 'agent'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and $_[0] eq "server" || $_[0] eq "agent" 1 1595 1596
Condition83%5/6
defined $_[0] and $_[0] eq "server" || $_[0] eq "agent"
!ll&&!rl&&r
011595
$_[0] eq "server" or $_[0] eq "agent"
l!l&&r!l&&!r
13552401
MC/DC66%2/3
defined $_[0] and $_[0] eq "server" || $_[0] eq "agent"
defined $_[0] $_[0] eq "server" $_[0] eq "agent"
Truth table75%3/4
A and B or C
A= defined $_[0]B= $_[0] eq "server"C= $_[0] eq "agent"
ABCresult
0XX0
1000
11X1
1011
26 unless defined $_[0] && ( $_[0] eq 'server' || $_[0] eq 'agent' );
27 },
28 63 );
29  
30  
31 1776 ▶sub is_agent { $_[0]->role eq 'agent' }
Subroutine is_agent: called CC=1 SCAR=0.0
Pod documented
32  
33  
34 1833 ▶sub distribution_classes {
Subroutine distribution_classes: called CC=1 SCAR=0.0
Pod documented
35 return {
36 1833 rke2 => 'Rex::Rancher::Distribution::RKE2',
37 k3s => 'Rex::Rancher::Distribution::K3s',
38 };
39 }
40  
41  
42 # use_module, not a `use` at the top: the classes extend this one, which has
43 # to be complete (its attributes declared) before they load. Which one is
44 # needed is known only from the caller's distribution option, and only a
45 # name from distribution_classes is ever loaded.
46 1495 ▶sub new_for {
Subroutine new_for: called CC=3 SCAR=11.0
Pod documented
47 1495 my ( $class, $name, %args ) = @_;
48 1495 ▶ $name //= $class->default_distribution;
Condition66%2/3
$name //= $class->default_distribution
l!l&&r!l&&!r
1437580
MC/DC0%0/2
$name //= $class->default_distribution
$name $class->default_distribution
Truth table66%2/3
A //= B
A= $nameB= $class->default_distribution
ABresult
1X1
011
000
49 1495 ▶ my $impl = $class->distribution_classes->{$name}
Condition100%2/2
$class->distribution_classes->{$name} // die($class->unknown_distribution($name) . "\n")
l!l
14869
MC/DC100%1/1
$class->distribution_classes->{$name} // die($class->unknown_distribution($name) . "\n")
$class->distribution_classes->{$name}
Truth table100%2/2
A
A= $class->distribution_classes->{$name}
Aresult
11
00
50 // die $class->unknown_distribution($name) . "\n";
51 1486 return use_module($impl)->new(%args);
52 }
53  
54  
55 353 ▶sub default_distribution { 'rke2' }
Subroutine default_distribution: called CC=1 SCAR=0.0
Pod documented
56  
57  
58 19 ▶sub unknown_distribution {
Subroutine unknown_distribution: called CC=2 SCAR=7.0
Pod documented
59 19 my ( $class, $name ) = @_;
60 19 my $default = $class->default_distribution;
61 19 my @names = ( $default, sort grep { $_ ne $default } keys %{ $class->distribution_classes } );
62 19 my $last = pop @names;
63 return "Unknown distribution: $name (expected "
64 19 ▶ . join( ', ', map { "'$_'" } @names ) . ( @names ? ' or ' : '' ) . "'$last')";
Branch: 1 branch
BranchTrueFalseTotal
@names ? : 19 0 19
65 }
66  
67  
68  
69 1489 ▶sub service {
Subroutine service: called CC=2 SCAR=6.9
Pod documented
70 1489 my ( $self ) = @_;
71 1489 ▶ return $self->is_agent ? $self->agent_service : $self->server_service;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_agent ? : 540 949 1489
72 }
73  
74  
75 389 ▶sub config_file { $_[0]->config_dir.'/config.yaml' }
Subroutine config_file: called CC=1 SCAR=0.0
Pod documented
76 82 ▶sub registries_file { $_[0]->config_dir.'/registries.yaml' }
Subroutine registries_file: called CC=1 SCAR=0.0
Pod documented
77  
78  
79 179 ▶sub cni_bin_dir { '/opt/cni/bin' }
Subroutine cni_bin_dir: called CC=1 SCAR=0.0
Pod documented
80 179 ▶sub cni_conf_dir { '/etc/cni/net.d' }
Subroutine cni_conf_dir: called CC=1 SCAR=0.0
Pod documented
81  
82  
83 10 ▶sub restart_services_cmd {
Subroutine restart_services_cmd: called CC=1 SCAR=0.0
Pod documented
84 10 my ( $self ) = @_;
85 10 return 'systemctl restart '.$self->server_service.'.service 2>/dev/null || systemctl restart '
86 .$self->agent_service.' 2>/dev/null';
87 }
88  
89 #
90 # Option checks (pure -- they die before anything touches the host)
91 #
92  
93  
94 9 ▶sub resolve_install_method { shift; Rex::Rancher::Options->resolve_install_method(@_) }
Subroutine resolve_install_method: called CC=1 SCAR=0.0
Pod documented
95 14 ▶sub check_cluster_cidr { shift; Rex::Rancher::Options->check_cluster_cidr(@_) }
Subroutine check_cluster_cidr: called CC=1 SCAR=0.0
Pod documented
96  
97 #
98 # Release artifacts (install_method => 'artifact')
99 #
100  
101  
102 12 ▶sub goarch {
Subroutine goarch: called CC=3 SCAR=11.0
Pod documented
103 12 my ( $self, $uname ) = @_;
104 12 ▶ $uname //= '';
Condition100%2/2
$uname //= ""
l!l
111
MC/DC100%1/1
$uname //= ""
$uname
Truth table100%2/2
A
A= $uname
Aresult
11
00
105 12 $uname =~ s/\s+\z//;
106 12 my %goarch = (
107 x86_64 => 'amd64',
108 amd64 => 'amd64',
109 aarch64 => 'arm64',
110 arm64 => 'arm64',
111 );
112 12 ▶ return $goarch{$uname}
Condition100%2/2
$goarch{$uname} // die("Unsupported node architecture '${uname}' for install_method 'artifact'" . " (amd64 and arm64 only)\n")
l!l
84
MC/DC100%1/1
$goarch{$uname} // die("Unsupported node architecture '${uname}' for install_method 'artifact'" . " (amd64 and arm64 only)\n")
$goarch{$uname}
Truth table100%2/2
A
A= $goarch{$uname}
Aresult
11
00
113 // die "Unsupported node architecture '$uname' for install_method 'artifact'"
114 . " (amd64 and arm64 only)\n";
115 }
116  
117  
118 15 ▶sub artifact_spec {
Subroutine artifact_spec: called CC=3 SCAR=11.0
Pod documented
119 15 my ( $self, $arch, $version ) = @_;
120  
121 15 ▶ die "install_method 'artifact' requires a version\n" unless $version;
Branch: 1 branch
BranchTrueFalseTotal
unless $version 1 14 15
122 14 ▶ die "Invalid version '$version'\n" unless $version =~ /\A[A-Za-z0-9._+-]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ /\A[A-Za-z0-9._+-]+\z/u 1 13 14
123  
124 13 (my $url_version = $version) =~ s/\+/%2B/g;
125 13 my $base = $self->release_url . '/' . $url_version;
126 13 my $dir = $self->artifact_dir;
127 13 my $asset = $self->asset_name($arch);
128 13 my $sums = "sha256sum-$arch.txt";
129  
130 return {
131 13 dir => $dir,
132 script => "$dir/install.sh",
133 script_url => $self->install_url,
134 asset => $asset,
135 asset_url => "$base/$asset",
136 sums => $sums,
137 sums_url => "$base/$sums",
138 };
139 }
140  
141  
142 12 ▶sub expected_sha256 { shift; Rex::Rancher::Checksum->expected_sha256(@_) }
Subroutine expected_sha256: called CC=1 SCAR=0.0
Pod documented
143 6 ▶sub sha256_of { shift; Rex::Rancher::Checksum->sha256_of(@_) }
Subroutine sha256_of: called CC=1 SCAR=0.0
Pod documented
144 10 ▶sub verify_sha256 { shift; Rex::Rancher::Checksum->verify_sha256(@_) }
Subroutine verify_sha256: called CC=1 SCAR=0.0
Pod documented
145  
146  
147 12 ▶sub download_cmd {
Subroutine download_cmd: called CC=2 SCAR=6.9
Pod documented
148 12 my ( $self, $url, $dest, $progress ) = @_;
149 # --progress-bar keeps output flowing during the ~60 MB tarball download,
150 # so a long silent curl does not look like a hung channel.
151 12 ▶ my $flags = $progress ? '-fL --progress-bar' : '-fsSL';
Branch: 1 branch
BranchTrueFalseTotal
$progress ? : 4 8 12
152 12 return "curl $flags -o '$dest' '$url' 2>&1";
153 }
154  
155  
156 5 ▶sub fetch_artifacts {
Subroutine fetch_artifacts: called CC=5 SCAR=16.1
Pod documented
157 5 my ( $self, $version ) = @_;
158 5 my $distribution = $self->name;
159  
160 5 my $arch = $self->goarch(Rex::Commands::Run::run("uname -m", auto_die => 1));
161 4 my $spec = $self->artifact_spec($arch, $version);
162 4 my $dir = $spec->{dir};
163  
164 4 Rex::Logger::info("Downloading $distribution $version artifacts for $arch to $dir");
165  
166 4 Rex::Commands::Run::run("rm -rf '$dir' && mkdir -p '$dir'", auto_die => 1);
167  
168 4 for my $dl (
169 [ $spec->{script_url}, $spec->{script}, 0 ],
170 [ $spec->{sums_url}, "$dir/$spec->{sums}", 0 ],
171 [ $spec->{asset_url}, "$dir/$spec->{asset}", 1 ],
172 ) {
173 12 my ($url, $dest, $progress) = @{$dl};
174 12 my $out = Rex::Commands::Run::run($self->download_cmd($url, $dest, $progress), auto_die => 0);
175 12 ▶ die "Download failed: $url\n"
Branch: 2 branches
BranchTrueFalseTotal
unless $? == 0 1 11 12
$progress ? : 1 0 1
Condition50%1/2
$out // ""
l!l
10
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
176 . ($progress ? "If this 404s, $distribution $version publishes no build for '$arch'.\n" : '')
177 . ($out // '') . "\n"
178 unless $? == 0;
179 }
180  
181 3 my $sums = Rex::Commands::Run::run("cat '$dir/$spec->{sums}'", auto_die => 1);
182 3 my $actual = Rex::Commands::Run::run("sha256sum '$dir/$spec->{asset}'", auto_die => 1);
183 # scalar(): both return empty on no match; in this list they must stay undef.
184 $self->verify_sha256(scalar($self->expected_sha256($sums, $spec->{asset})),
185 3 scalar($self->sha256_of($actual)), $spec->{asset});
186 1 Rex::Logger::info(" $spec->{asset}: sha256 verified");
187  
188 1 return $spec;
189 }
190  
191 #
192 # Server install step: artifact or script, then whatever the distribution
193 # needs to confirm it.
194 #
195  
196  
197 75 ▶sub install_server_package {
Subroutine install_server_package: called CC=4 SCAR=17.1
Pod documented
198 75 my ( $self, $server, $version, $method ) = @_;
199  
200 75 ▶ if (($method // 'script') eq 'artifact') {
Branch: 1 branch
BranchTrueFalseTotal
if (($method // 'script') eq 'artifact') { } 0 75 75
Condition50%1/2
$method // "script"
l!l
750
MC/DC0%0/1
$method // "script"
$method
Truth table50%1/2
A
A= $method
Aresult
11
00
201 0 my $spec = $self->fetch_artifacts($version);
202 0 Rex::Logger::info("Installing " . $self->label . " from verified artifact $spec->{asset}...");
203 # auto_die => 1: with an artifact path RKE2's script takes its tarball
204 # method, which has no GPG key import (the Rocky 10 noise that
205 # Rex::Rancher::Distribution::RKE2 swallows is the RPM method's), so a
206 # non-zero exit here is a real failure.
207 Rex::Commands::Run::run($_, auto_die => 1)
208 0 for $self->artifact_install_cmds($spec, $server, $version);
209 }
210 else {
211 75 Rex::Logger::info("Installing " . $self->label . " via install script...");
212 75 $self->run_server_install_script($server, $version);
213 }
214 }
215  
216 #
217 # Installed version
218 #
219  
220  
221 # "rke2 version v1.30.4+rke2r1 (abc)" / "k3s version v1.30.4+k3s1 (abc)";
222 # the first word is argv[0]'s basename, so through /proc/PID/exe it is
223 # "exe version v1.36.4+rke2r1 (7479a59c)" (k68). Not "go version go1.26.7".
224 625 ▶sub parse_version_output {
Subroutine parse_version_output: called CC=3 SCAR=11.1
Pod documented
225 625 my ( $self, $out ) = @_;
226 625 ▶ return $1 if ($out // '') =~ /^(?!go )\S+ version (\S+)/m;
Branch: 1 branch
BranchTrueFalseTotal
if ($out // "") =~ /^(?!go )\S+ version (\S+)/mu 485 140 625
Condition50%1/2
$out // ""
l!l
6250
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
227 140 return;
228 }
229  
230  
231 252 ▶sub same_version {
Subroutine same_version: called CC=5 SCAR=16.8
Pod documented
232 252 my ( $self, $want, $got ) = @_;
233 252 ▶ return 0 unless defined $want && defined $got;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $want and defined $got 0 252 252
Condition33%1/3
defined $want and defined $got
!ll&&!rl&&r
00252
MC/DC0%0/2
defined $want and defined $got
defined $want defined $got
Truth table33%1/3
A and B
A= defined $wantB= defined $got
ABresult
0X0
100
111
234 252 my ($w, $g) = ($want, $got);
235 252 s/\Av// for $w, $g;
236 252 ▶ return $w eq $g ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$w eq $g ? : 203 49 252
237 }
238  
239  
240 137 ▶sub verify_installed_version {
Subroutine verify_installed_version: called CC=5 SCAR=16.1
Pod documented
241 137 my ( $self, $version ) = @_;
242 137 ▶ return 1 unless $version;
Branch: 1 branch
BranchTrueFalseTotal
unless $version 83 54 137
243  
244 54 my $distribution = $self->name;
245 54 my $binary = $self->binary;
246 54 my $out = Rex::Commands::Run::run("$binary --version 2>&1", auto_die => 0);
247 54 my $got = $self->parse_version_output($out);
248 54 ▶ die "Could not determine installed $distribution version ($binary --version):\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 2 52 54
Condition50%1/2
$out // ""
l!l
20
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
249 . ($out // '') . "\n"
250 unless defined $got;
251 52 ▶ die "Installed $distribution version is $got, expected $version — the "
Branch: 1 branch
BranchTrueFalseTotal
unless $self->same_version($version, $got) 2 50 52
252 . "install or upgrade did not take effect\n"
253 unless $self->same_version($version, $got);
254 50 Rex::Logger::info(" $distribution $got installed");
255 50 return 1;
256 }
257  
258 #
259 # Version skew: what may be installed over a running service, and when it
260 # may be restarted onto it. Kubernetes' version skew policy: a control plane
261 # moves one minor version at a time and never back, servers before agents,
262 # and a kubelet is never newer than the API server.
263 #
264  
265  
266 781 ▶sub parse_release {
Subroutine parse_release: called CC=4 SCAR=13.9
Pod documented
267 781 my ( $self, $version ) = @_;
268 781 ▶ return unless defined $version
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version and $version =~ /\Av?(\d+)\.(\d+)\.(\d+)(?:-[0-9A-Za-z.-]+)?(?:\+(?:rke2r|k3s)(\d+))?\z/u 9 772 781
Condition100%3/3
defined $version and $version =~ /\Av?(\d+)\.(\d+)\.(\d+)(?:-[0-9A-Za-z.-]+)?(?:\+(?:rke2r|k3s)(\d+))?\z/u
!ll&&!rl&&r
18772
MC/DC100%2/2
defined $version and $version =~ /\Av?(\d+)\.(\d+)\.(\d+)(?:-[0-9A-Za-z.-]+)?(?:\+(?:rke2r|k3s)(\d+))?\z/u
defined $version $version =~ /\Av?(\d+)\.(\d+)\.(\d+)(?:-[0-9A-Za-z.-]+)?(?:\+(?:rke2r|k3s)(\d+))?\z/u
Truth table100%3/3
A and B
A= defined $versionB= $version =~ /\Av?(\d+)\.(\d+)\.(\d+)(?:-[0-9A-Za-z.-]+)?(?:\+(?:rke2r|k3s)(\d+))?\z/u
ABresult
0X0
100
111
269 && $version =~ /\Av?(\d+)\.(\d+)\.(\d+)(?:-[0-9A-Za-z.-]+)?(?:\+(?:rke2r|k3s)(\d+))?\z/;
270 772 ▶ return ( $1, $2, $3, $4 // 0 );
Condition100%2/2
$4 // 0
l!l
7711
MC/DC100%1/1
$4 // 0
$4
Truth table100%2/2
A
A= $4
Aresult
11
00
271 }
272  
273  
274 165 ▶sub compare_versions {
Subroutine compare_versions: called CC=5 SCAR=16.1
Pod documented
275 165 my ( $self, $x, $y ) = @_;
276 165 my @a = $self->parse_release($x);
277 165 my @b = $self->parse_release($y);
278 165 ▶ return unless @a && @b;
Branch: 1 branch
BranchTrueFalseTotal
unless @a and @b 2 163 165
Condition100%3/3
@a and @b
!ll&&!rl&&r
11163
MC/DC100%2/2
@a and @b
@a @b
Truth table100%3/3
A and B
A= @aB= @b
ABresult
0X0
100
111
279 163 for my $i (0 .. 3) {
280 552 my $cmp = $a[$i] <=> $b[$i];
281 552 ▶ return $cmp if $cmp;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 58 494 552
282 }
283 105 return 0;
284 }
285  
286  
287 160 ▶sub version_skew {
Subroutine version_skew: called CC=8 SCAR=20.8
Pod documented
288 160 my ( $self, $from, $to ) = @_;
289 160 my $cmp = $self->compare_versions($to, $from);
290 160 ▶ return unless defined $cmp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmp 1 159 160
291 159 ▶ return 'downgrade' if $cmp < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp < 0 12 147 159
292 147 my @f = $self->parse_release($from);
293 147 my @t = $self->parse_release($to);
294 147 ▶ if ( $t[0] == $f[0] && $t[1] == $f[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t[0] == $f[0] and $t[1] == $f[1]) 114 33 147
Condition100%3/3
$t[0] == $f[0] and $t[1] == $f[1]
!ll&&!rl&&r
132114
MC/DC100%2/2
$t[0] == $f[0] and $t[1] == $f[1]
$t[0] == $f[0] $t[1] == $f[1]
Truth table100%3/3
A and B
A= $t[0] == $f[0]B= $t[1] == $f[1]
ABresult
0X0
100
111
295 114 ▶ return $self->same_version($from, $to) ? 'same' : 'patch';
Branch: 1 branch
BranchTrueFalseTotal
$self->same_version($from, $to) ? : 104 10 114
296 }
297 33 ▶ return ( $t[0] == $f[0] && $t[1] == $f[1] + 1 ) ? 'minor' : 'jump';
Branch: 1 branch
BranchTrueFalseTotal
$t[0] == $f[0] && $t[1] == $f[1] + 1 ? : 15 18 33
Condition100%3/3
$t[0] == $f[0] and $t[1] == $f[1] + 1
!ll&&!rl&&r
11715
MC/DC100%2/2
$t[0] == $f[0] and $t[1] == $f[1] + 1
$t[0] == $f[0] $t[1] == $f[1] + 1
Truth table100%3/3
A and B
A= $t[0] == $f[0]B= $t[1] == $f[1] + 1
ABresult
0X0
100
111
298 }
299  
300  
301 13 ▶sub parse_channel_redirect {
Subroutine parse_channel_redirect: called CC=4 SCAR=13.9
Pod documented
302 13 my ( $self, $url ) = @_;
303 13 ▶ return unless ($url // '') =~ m{/releases/tag/([^/\s]+)\s*\z};
Branch: 1 branch
BranchTrueFalseTotal
unless ($url // "") =~ m[/releases/tag/([^/\s]+)\s*\z]u 2 11 13
Condition100%2/2
$url // ""
l!l
121
MC/DC100%1/1
$url // ""
$url
Truth table100%2/2
A
A= $url
Aresult
11
00
304 11 (my $version = $1) =~ s/%2B/+/gi;
305 11 my @parts = $self->parse_release($version);
306 11 ▶ return @parts ? $version : ();
Branch: 1 branch
BranchTrueFalseTotal
@parts ? : 11 0 11
307 }
308  
309  
310 43 ▶sub channel_version {
Subroutine channel_version: called CC=2 SCAR=6.9
Pod documented
311 43 my ( $self ) = @_;
312 43 my $out = Rex::Commands::Run::run("curl -fsSL -o /dev/null -w '%{url_effective}' '"
313 . $self->channel_url . "' 2>/dev/null", auto_die => 0);
314 43 ▶ return unless $? == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $? == 0 34 9 43
315 9 return $self->parse_channel_redirect($out);
316 }
317  
318  
319 455 ▶sub main_pid {
Subroutine main_pid: called CC=1 SCAR=0.0
Pod documented
320 455 my ( $self ) = @_;
321 455 return $self->parse_main_pid(Rex::Commands::Run::run(
322 "systemctl show -p MainPID " . $self->service . " 2>/dev/null", auto_die => 0));
323 }
324  
325  
326 287 ▶sub running_version {
Subroutine running_version: called CC=1 SCAR=0.0
Pod documented
327 287 my ( $self, $pid ) = @_;
328 287 return $self->parse_version_output(
329 Rex::Commands::Run::run("/proc/$pid/exe --version 2>&1", auto_die => 0));
330 }
331  
332 276 ▶sub installed_version {
Subroutine installed_version: called CC=1 SCAR=0.0
Pod documented
333 276 my ( $self ) = @_;
334 276 return $self->parse_version_output(
335 Rex::Commands::Run::run($self->binary . " --version 2>&1", auto_die => 0));
336 }
337  
338  
339 66 ▶sub held_version {
Subroutine held_version: called CC=15 SCAR=27.1
Pod documented
340 66 my ( $self, %args ) = @_;
341 66 my $pinned = $args{version};
342 66 my $service = $self->service;
343 66 my $binary = $self->binary;
344 66 ▶ my $release = sub { defined $_[0] && $self->parse_release($_[0]) ? $_[0] : undef };
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && $self->parse_release($_[0]) ? : 53 22 75
Condition100%3/3
defined $_[0] and $self->parse_release($_[0])
!ll&&!rl&&r
21153
MC/DC100%2/2
defined $_[0] and $self->parse_release($_[0])
defined $_[0] $self->parse_release($_[0])
Truth table100%3/3
A and B
A= defined $_[0]B= $self->parse_release($_[0])
ABresult
0X0
100
111
345  
346 66 my $pid = $self->main_pid;
347 66 my ( $held, $from );
348 66 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 52 14 66
349 52 $held = $release->($self->running_version($pid));
350 52 ▶ $from = "$service runs $held" if defined $held;
Branch: 1 branch
BranchTrueFalseTotal
if defined $held 43 9 52
351 }
352 66 ▶ unless (defined $held) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $held) 23 43 66
353 23 $held = $release->($self->installed_version);
354 23 ▶ $from = ( $pid ? '' : "$service is not running, " ) . "the installed $binary is $held"
Branch: 2 branches
BranchTrueFalseTotal
if defined $held 10 13 23
$pid ? : 5 5 10
355 if defined $held;
356 23 ▶ Rex::Logger::info("Could not ask the running $service (/proc/$pid/exe --version) for "
Branch: 1 branch
BranchTrueFalseTotal
if $pid and defined $held 5 18 23
Condition100%3/3
$pid and defined $held
!ll&&!rl&&r
1445
MC/DC100%2/2
$pid and defined $held
$pid defined $held
Truth table100%3/3
A and B
A= $pidB= defined $held
ABresult
0X0
100
111
357 . "its version: hold_running holds the installed $binary $held instead", 'warn')
358 if $pid && defined $held;
359 }
360  
361 66 ▶ unless (defined $held) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $held) 13 53 66
362 13 ▶ my $applies = defined $pinned && length $pinned
Branch: 1 branch
BranchTrueFalseTotal
defined $pinned && length $pinned ? : 8 5 13
Condition66%2/3
defined $pinned and length $pinned
!ll&&!rl&&r
508
MC/DC50%1/2
defined $pinned and length $pinned
defined $pinned length $pinned
Truth table66%2/3
A and B
A= defined $pinnedB= length $pinned
ABresult
0X0
100
111
363 ? "version $pinned applies"
364 : "version is not pinned, the stable channel's applies";
365 13 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 4 9 13
366 4 Rex::Logger::info("Could not ask the running $service (/proc/$pid/exe --version) for "
367 . "its version, and no installed $binary reports a version: hold_running holds "
368 . "nothing, $applies", 'warn');
369 }
370 else {
371 9 Rex::Logger::info("hold_running: $service is not running and no installed $binary "
372 . "reports a version: $applies");
373 }
374 13 return $pinned;
375 }
376  
377 53 ▶ if (defined $pinned && length $pinned && !$self->same_version($pinned, $held)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pinned and length $pinned and not $self->same_version($pinned, $held)) { } 27 26 53
Condition83%5/6
defined $pinned and length $pinned and not $self->same_version($pinned, $held)
!ll&&!rl&&r
24227
defined $pinned and length $pinned
!ll&&!rl&&r
24029
MC/DC66%2/3
defined $pinned and length $pinned and not $self->same_version($pinned, $held)
defined $pinned length $pinned not $self->same_version($pinned, $held)
Truth table75%3/4
A and B and not(C)
A= defined $pinnedB= length $pinnedC= not $self->same_version($pinned, $held)
ABCresult
0XX0
10X0
1100
1111
378 27 Rex::Logger::info("hold_running: $from; that is the version for this run, not "
379 . "version => '$pinned'", 'warn');
380 }
381 else {
382 26 Rex::Logger::info("hold_running: $from; that is the version for this run");
383 }
384 53 return $held;
385 }
386  
387  
388 210 ▶sub check_version_skew {
Subroutine check_version_skew: called CC=19 SCAR=29.5
Pod documented
389 210 my ( $self, %args ) = @_;
390 210 my $pinned = $args{version};
391 210 my $server = $args{server_version};
392 210 my $service = $self->service;
393  
394 210 my $pid = $self->main_pid;
395 210 ▶ my $running = $pid ? $self->running_version($pid) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$pid ? : 98 112 210
396 210 ▶ Rex::Logger::info("Could not ask the running $service (/proc/$pid/exe --version) "
Branch: 1 branch
BranchTrueFalseTotal
if $pid and not defined $running 9 201 210
Condition100%3/3
$pid and not defined $running
!ll&&!rl&&r
112899
MC/DC100%2/2
$pid and not defined $running
$pid not defined $running
Truth table100%3/3
A and not(B)
A= $pidB= not defined $running
ABresult
0X0
100
111
397 . "for its version: the version skew is checked only after the install, "
398 . "before $service is restarted", 'warn')
399 if $pid && !defined $running;
400 # Not running (stopped, crashed, never started): the binary on disk is what
401 # the node last ran, and start_verb has nothing to hold back once the
402 # installer replaced it. No binary: a fresh install, nothing to check.
403 210 ▶ my $installed = $pid ? undef : $self->installed_version;
Branch: 1 branch
BranchTrueFalseTotal
$pid ? : 98 112 210
404 210 ▶ return unless defined $running || defined $installed || defined $server;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $running or defined $installed or defined $server 91 119 210
Condition100%6/6
defined $running or defined $installed or defined $server
l!l&&r!l&&!r
115491
defined $running or defined $installed
l!l&&r!l&&!r
892695
MC/DC100%3/3
defined $running or defined $installed or defined $server
defined $running defined $installed defined $server
Truth table100%4/4
A or B or C
A= defined $runningB= defined $installedC= defined $server
ABCresult
1XX1
01X1
0011
0000
405  
406 119 my $target = $pinned;
407 119 ▶ unless (defined $target && length $target) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $target and length $target) 41 78 119
Condition66%2/3
defined $target and length $target
!ll&&!rl&&r
41078
MC/DC50%1/2
defined $target and length $target
defined $target length $target
Truth table66%2/3
A and B
A= defined $targetB= length $target
ABresult
0X0
100
111
408 41 $target = $self->channel_version;
409 41 ▶ unless (defined $target) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $target) 33 8 41
410 33 ▶ Rex::Logger::info("Could not resolve the version " . $self->channel_url
Branch: 1 branch
BranchTrueFalseTotal
defined $installed ? : 4 29 33
411 . " would install: "
412 . ( defined $installed
413 ? "the version skew against the installed " . $self->binary . " $installed "
414 . "is not checked, and $service, which is not running, starts on "
415 . "whatever the install brings"
416 : "the version skew is checked only after the install, before $service "
417 . "is (re)started" ), 'warn');
418 33 return;
419 }
420 8 Rex::Logger::info("version not pinned: the stable channel installs $target");
421 }
422  
423 86 ▶ if (defined $running) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $running) { } 60 26 86
elsif (defined $installed) { } 22 4 26
424 60 ▶ my $skew = $self->version_skew($running, $target) // '';
Condition50%1/2
$self->version_skew($running, $target) // ""
l!l
600
MC/DC0%0/1
$self->version_skew($running, $target) // ""
$self->version_skew($running, $target)
Truth table50%1/2
A
A= $self->version_skew($running, $target)
Aresult
11
00
425 60 ▶ die "Refusing to install " . $self->name . " $target"
Branch: 2 branches
BranchTrueFalseTotal
if $skew eq "jump" or $skew eq "downgrade" 14 46 60
$pinned ? : 13 1 14
Condition100%3/3
$skew eq "jump" or $skew eq "downgrade"
l!l&&r!l&&!r
9546
MC/DC100%2/2
$skew eq "jump" or $skew eq "downgrade"
$skew eq "jump" $skew eq "downgrade"
Truth table100%3/3
A or B
A= $skew eq "jump"B= $skew eq "downgrade"
ABresult
1X1
011
000
426 . ( $pinned ? '' : " (the stable channel's version; version is not pinned)" )
427 . ": $service runs $running, and " . $self->_skew_rule($skew, $running)
428 . " Nothing was installed; $service keeps running $running.\n"
429 if $skew eq 'jump' || $skew eq 'downgrade';
430 }
431 elsif (defined $installed) {
432 22 $self->_check_installed_skew($installed, $target, $pinned);
433 }
434  
435 64 ▶ $self->check_agent_version($target, $server) if defined $server;
Branch: 1 branch
BranchTrueFalseTotal
if defined $server 12 52 64
436 56 return $target;
437 }
438  
439 # The service is not running: against the binary it would have started on.
440 22 ▶sub _check_installed_skew {
Subroutine _check_installed_skew: called CC=7 SCAR=19.5
441 22 my ( $self, $installed, $target, $pinned ) = @_;
442 22 my $service = $self->service;
443 22 my $binary = $self->binary;
444 22 ▶ my $skew = $self->version_skew($installed, $target) // '';
Condition50%1/2
$self->version_skew($installed, $target) // ""
l!l
220
MC/DC0%0/1
$self->version_skew($installed, $target) // ""
$self->version_skew($installed, $target)
Truth table50%1/2
A
A= $self->version_skew($installed, $target)
Aresult
11
00
445 22 ▶ die "Refusing to install " . $self->name . " $target"
Branch: 2 branches
BranchTrueFalseTotal
if $skew eq "jump" or $skew eq "downgrade" 8 14 22
$pinned ? : 6 2 8
Condition100%3/3
$skew eq "jump" or $skew eq "downgrade"
l!l&&r!l&&!r
5314
MC/DC100%2/2
$skew eq "jump" or $skew eq "downgrade"
$skew eq "jump" $skew eq "downgrade"
Truth table100%3/3
A or B
A= $skew eq "jump"B= $skew eq "downgrade"
ABresult
1X1
011
000
446 . ( $pinned ? '' : " (the stable channel's version; version is not pinned)" )
447 . ": $service is not running, and the installed $binary is $installed; "
448 . $self->_skew_rule($skew, $installed)
449 . " Nothing was installed; $binary $installed stays in place.\n"
450 if $skew eq 'jump' || $skew eq 'downgrade';
451 14 ▶ Rex::Logger::info("$service is not running, and the installed $binary is "
Branch: 1 branch
BranchTrueFalseTotal
if $skew eq "minor" and not $pinned 2 12 14
Condition100%3/3
$skew eq "minor" and not $pinned
!ll&&!rl&&r
1022
MC/DC100%2/2
$skew eq "minor" and not $pinned
$skew eq "minor" not $pinned
Truth table100%3/3
A and not(B)
A= $skew eq "minor"B= not $pinned
ABresult
0X0
100
111
452 . "$installed: the stable channel's $target, a new minor version, is "
453 . "installed since version is not pinned, and $service starts on it. Pin "
454 . "version => '$installed' to stay on it", 'warn')
455 if $skew eq 'minor' && !$pinned;
456 14 return;
457 }
458  
459 25 ▶sub _skew_rule {
Subroutine _skew_rule: called CC=2 SCAR=6.9
460 25 my ( $self, $skew, $running ) = @_;
461 25 my @r = $self->parse_release($running);
462 25 ▶ return $skew eq 'downgrade'
Branch: 1 branch
BranchTrueFalseTotal
$skew eq 'downgrade' ? : 9 16 25
463 ? "that is a downgrade, which Kubernetes' version skew policy does not "
464 . "allow. Pin version to $running or newer."
465 : "that skips a minor version: Kubernetes' version skew policy moves a "
466 . "node one minor version at a time. Upgrade to a v$r[0]." . ($r[1] + 1)
467 . " release first (pin version).";
468 }
469  
470  
471 20 ▶sub check_agent_version {
Subroutine check_agent_version: called CC=8 SCAR=24.0
Pod documented
472 20 my ( $self, $agent, $server, $installed ) = @_;
473 20 my @a = $self->parse_release($agent);
474 20 my @s = $self->parse_release($server);
475 20 ▶ unless (@a && @s) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@a and @s) 0 20 20
Condition33%1/3
@a and @s
!ll&&!rl&&r
0020
MC/DC0%0/2
@a and @s
@a @s
Truth table33%1/3
A and B
A= @aB= @s
ABresult
0X0
100
111
476 0 ▶ Rex::Logger::info("Could not compare " . $self->name . " $agent with the control "
Condition0%0/2
$server // "unknown"
l!l
00
MC/DC0%0/1
$server // "unknown"
$server
Truth table0%0/2
A
A= $server
Aresult
11
00
477 . "plane's " . ( $server // 'unknown' ) . ": the agent is not checked against it",
478 'warn');
479 0 return;
480 }
481 20 ▶ return 1 if $a[0] < $s[0] || ( $a[0] == $s[0] && $a[1] <= $s[1] );
Branch: 1 branch
BranchTrueFalseTotal
if $a[0] < $s[0] or $a[0] == $s[0] and $a[1] <= $s[1] 9 11 20
Condition66%4/6
$a[0] < $s[0] or $a[0] == $s[0] and $a[1] <= $s[1]
l!l&&r!l&&!r
0911
$a[0] == $s[0] and $a[1] <= $s[1]
!ll&&!rl&&r
0119
MC/DC33%1/3
$a[0] < $s[0] or $a[0] == $s[0] and $a[1] <= $s[1]
$a[0] < $s[0] $a[0] == $s[0] $a[1] <= $s[1]
Truth table50%2/4
A or B and C
A= $a[0] < $s[0]B= $a[0] == $s[0]C= $a[1] <= $s[1]
ABCresult
1XX1
0111
00X0
0100
482 11 ▶ die "Refusing the " . $self->name . " agent $agent: the control plane runs $server, "
Branch: 1 branch
BranchTrueFalseTotal
$installed ? : 2 9 11
483 . "and a kubelet must never be of a newer minor version than the API server "
484 . "(Kubernetes' version skew policy: servers first, then agents). "
485 . ( $installed
486 ? $self->binary . " $agent is installed, but " . $self->service . " was not (re)started."
487 : "Nothing was installed." )
488 . " Upgrade the servers first, or pin version to a v$s[0].$s[1] release.\n";
489 }
490  
491 #
492 # Cluster CIDR: a server that is set up keeps the pod network it was set up
493 # with. Another cluster-cidr in config.yaml restarts it onto that range while
494 # its pods keep their addresses and the node podCIDRs and Cilium's pool stay
495 # on the old one (k67).
496 #
497  
498  
499 208 ▶sub builtin_cluster_cidr { '10.42.0.0/16' }
Subroutine builtin_cluster_cidr: called CC=1 SCAR=0.0
Pod documented
500  
501  
502 119 ▶sub is_established {
Subroutine is_established: called CC=3 SCAR=11.0
Pod documented
503 119 my ( $self ) = @_;
504 119 Rex::Commands::Run::run('systemctl is-active --quiet '.$self->server_service, auto_die => 0);
505 119 ▶ return 1 if $? == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? == 0 90 29 119
506 # Written once the control plane has bootstrapped its datastore.
507 29 Rex::Commands::Run::run('test -e '.$self->_shell_quote($self->server_token), auto_die => 0);
508 29 ▶ return $? == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 4 25 29
509 }
510  
511  
512 118 ▶sub established_cluster_cidr {
Subroutine established_cluster_cidr: called CC=12 SCAR=24.9
Pod documented
513 118 my ( $self ) = @_;
514 118 ▶ return unless $self->is_established;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_established 24 94 118
515 94 my ( @cidr, $from );
516 94 for my $file ($self->_server_config_files) {
517 84 my $config = $self->_read_server_config($file);
518 # In the order of the file, as the distributions read it.
519 78 for my $key (keys %{$config}) {
520 57 ▶ next unless $key =~ /\Acluster-cidr(\+?)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /\Acluster-cidr(\+?)\z/u 21 36 57
521 36 my $append = $1;
522 36 ▶ my @value = ref $config->{$key} eq 'ARRAY' ? @{ $config->{$key} } : ( $config->{$key} );
Branch: 1 branch
BranchTrueFalseTotal
ref $config->{$key} eq 'ARRAY' ? : 3 33 36
523 die $self->_cluster_cidr_unknown(
524 "$file: cluster-cidr is neither a string nor a list of strings")
525 36 ▶ if grep { !defined || ref } @value;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_ or ref $_;} @value 2 34 36
Condition66%2/3
!defined($_) || ref $_
l!l&&r!l&&!r
0235
MC/DC50%1/2
!defined($_) || ref $_
!defined($_) ref $_
Truth table66%2/3
not(A) || B
A= !defined($_)B= ref $_
ABresult
1X1
011
000
526 34 ▶ @cidr = ( ( $append ? @cidr : () ), @value );
Branch: 1 branch
BranchTrueFalseTotal
$append ? : 3 31 34
527 34 $from = $file;
528 }
529 }
530 84 ▶ my $cidr = @cidr ? join(',', @cidr) : $self->builtin_cluster_cidr;
Branch: 1 branch
BranchTrueFalseTotal
@cidr ? : 28 56 84
531 84 ▶ $from = undef unless @cidr;
Branch: 1 branch
BranchTrueFalseTotal
unless @cidr 56 28 84
532 84 ▶ return wantarray ? ( $cidr, $from ) : $cidr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 79 5 84
533 }
534  
535  
536 109 ▶sub check_established_cluster_cidr {
Subroutine check_established_cluster_cidr: called CC=9 SCAR=22.0
Pod documented
537 109 my ( $self, %args ) = @_;
538 109 my ( $running, $from ) = $self->established_cluster_cidr;
539 99 ▶ return unless defined $running;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $running 23 76 99
540  
541 76 my $given = $args{cluster_cidr};
542 76 ▶ my $want = $given // ( $args{cilium} ? $self->cilium_config->{'cluster-cidr'} : undef )
Branch: 1 branch
BranchTrueFalseTotal
$args{'cilium'} ? : 46 5 51
Condition83%5/6
($given // ($args{"cilium"} ? $self->cilium_config->{"cluster-cidr"} : undef)) // $self->builtin_cluster_cidr
l!l&&r!l&&!r
49270
$given // ($args{"cilium"} ? $self->cilium_config->{"cluster-cidr"} : undef)
l!l&&r!l&&!r
252427
MC/DC0%0/3
($given // ($args{"cilium"} ? $self->cilium_config->{"cluster-cidr"} : undef)) // $self->builtin_cluster_cidr
$given ($args{"cilium"} ? $self->cilium_config->{"cluster-cidr"} : undef) $self->builtin_cluster_cidr
Truth table75%3/4
A // B // C
A= $givenB= ($args{"cilium"} ? $self->cilium_config->{"cluster-cidr"} : undef)C= $self->builtin_cluster_cidr
ABCresult
1XX1
01X1
0011
0000
543 // $self->builtin_cluster_cidr;
544 76 s/\A\s+//, s/\s+\z// for $running, $want;
545 76 ▶ return $running if $running eq $want;
Branch: 1 branch
BranchTrueFalseTotal
if $running eq $want 54 22 76
546  
547 22 my $name = $self->name;
548 22 my $config = $self->config_file;
549 22 ▶ die "Refusing to install $name with cluster-cidr $want"
Branch: 2 branches
BranchTrueFalseTotal
defined $given ? : 16 6 22
defined $from ? : 16 6 22
550 . ( defined $given ? ' (cluster_cidr)' : ' (cluster_cidr not given: the default)' )
551 . ": the $name server on this host was set up with $running ("
552 . ( defined $from
553 ? "from $from"
554 : "${name}'s built-in default: no cluster-cidr in $config or $config.d" )
555 . "), and the cluster-cidr of a running cluster cannot be changed. Pass "
556 . "cluster_cidr => '$running'. config.yaml is unchanged and nothing was installed.\n";
557 }
558  
559 # config.yaml if there, then the drop-ins k3s' configfilearg (RKE2 uses it
560 # too) takes: no directories, .yaml/.yml in any case, in os.ReadDir order.
561 94 ▶sub _server_config_files {
Subroutine _server_config_files: called CC=5 SCAR=16.1
562 94 my ( $self ) = @_;
563 94 my $config = $self->config_file;
564 94 my $dropins = $config.'.d';
565 94 my @files;
566 94 Rex::Commands::Run::run('test -e '.$self->_shell_quote($config), auto_die => 0);
567 94 ▶ push @files, $config if $? == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? == 0 70 24 94
568 94 Rex::Commands::Run::run('test -d '.$self->_shell_quote($dropins), auto_die => 0);
569 94 ▶ return @files unless $? == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $? == 0 37 57 94
570 57 my $out = Rex::Commands::Run::run('find '.$self->_shell_quote($dropins)
571 .' -mindepth 1 -maxdepth 1 ! -type d', auto_die => 0);
572 57 ▶ die $self->_cluster_cidr_unknown("Could not list $dropins (find exited ".($? >> 8).")")
Branch: 1 branch
BranchTrueFalseTotal
unless $? == 0 2 55 57
573 unless $? == 0;
574 55 ▶ push @files, sort grep { /\.ya?ml\z/i } split /\n/, $out // '';
Condition50%1/2
$out // ""
l!l
550
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
575 55 return @files;
576 }
577  
578 84 ▶sub _read_server_config {
Subroutine _read_server_config: called CC=8 SCAR=20.9
579 84 my ( $self, $file ) = @_;
580 84 my $content = Rex::Commands::Run::run('cat '.$self->_shell_quote($file), auto_die => 0);
581 84 ▶ die $self->_cluster_cidr_unknown("Could not read $file (cat exited ".($? >> 8).")")
Branch: 1 branch
BranchTrueFalseTotal
unless $? == 0 2 82 84
582 unless $? == 0;
583 # Repeated keys are allowed, the last one wins, as in the distributions.
584 # Never the parser's message: it quotes the line, which may be the token's.
585 82 my @docs;
586 82 ▶ unless (eval {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { @docs = "YAML::PP"->new("duplicate_keys", 1, "preserve", 2)->load_string($content // ""); 1 } }) 2 80 82
587 82 ▶ @docs = YAML::PP->new(duplicate_keys => 1, preserve => PRESERVE_ORDER)
Condition50%1/2
$content // ""
l!l
820
MC/DC0%0/1
$content // ""
$content
Truth table50%1/2
A
A= $content
Aresult
11
00
588 ->load_string($content // '');
589 80 1;
590 }) {
591 2 ▶ my ($line) = ($@ // '') =~ /^Line\s*:\s*(\d+)/m;
Condition50%1/2
$@ // ""
l!l
20
MC/DC0%0/1
$@ // ""
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
592 2 ▶ die $self->_cluster_cidr_unknown("Could not parse $file as YAML"
Branch: 1 branch
BranchTrueFalseTotal
defined $line ? : 2 0 2
593 . ( defined $line ? " (line $line)" : '' ));
594 }
595 80 ▶ my $config = $docs[0] // {};
Condition100%2/2
$docs[0] // {}
l!l
4832
MC/DC100%1/1
$docs[0] // {}
$docs[0]
Truth table100%2/2
A
A= $docs[0]
Aresult
11
00
596 80 ▶ die $self->_cluster_cidr_unknown("$file is not a YAML mapping") unless ref $config eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq "HASH" 2 78 80
597 78 return $config;
598 }
599  
600 10 ▶sub _cluster_cidr_unknown {
Subroutine _cluster_cidr_unknown: called CC=1 SCAR=0.0
601 10 my ( $self, $what ) = @_;
602 10 return "$what, so the cluster-cidr the ".$self->name." server on this host was set up "
603 . "with is unknown. config.yaml is unchanged and nothing was installed.\n";
604 }
605  
606 # One shell word, whatever the name: the drop-in names come from the host.
607 450 ▶sub _shell_quote {
Subroutine _shell_quote: called CC=1 SCAR=0.0
608 450 my ( $self, $word ) = @_;
609 450 $word =~ s/'/'\\''/g;
610 450 return "'$word'";
611 }
612  
613 #
614 # Service start and wait
615 #
616  
617  
618 172 ▶sub start_verb {
Subroutine start_verb: called CC=8 SCAR=20.8
Pod documented
619 172 my ( $self, %args ) = @_;
620 172 my $pid = $self->main_pid;
621 172 ▶ return 'start' if $pid && $self->_hold_new_binary($pid, $args{pinned});
Branch: 1 branch
BranchTrueFalseTotal
if $pid and $self->_hold_new_binary($pid, $args{"pinned"}) 4 165 169
Condition100%3/3
$pid and $self->_hold_new_binary($pid, $args{"pinned"})
!ll&&!rl&&r
97714
MC/DC100%2/2
$pid and $self->_hold_new_binary($pid, $args{"pinned"})
$pid $self->_hold_new_binary($pid, $args{"pinned"})
Truth table100%3/3
A and B
A= $pidB= $self->_hold_new_binary($pid, $args{"pinned"})
ABresult
0X0
100
111
622 165 ▶ return 'restart' if $self->_stale_containerd_restart;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_stale_containerd_restart 15 150 165
623  
624 # k3s restarts anyway, unless held. A `start` of a running service is a
625 # no-op, so it is turned into a restart exactly when the service runs on
626 # something older than what is on disk now.
627 150 ▶ my $default = $args{hold} ? 'start' : $self->default_start_verb;
Branch: 1 branch
BranchTrueFalseTotal
$args{'hold'} ? : 49 101 150
628 150 ▶ return $default unless $default eq 'start' && $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $default eq "start" and $pid 94 56 150
Condition100%3/3
$default eq "start" and $pid
!ll&&!rl&&r
435156
MC/DC100%2/2
$default eq "start" and $pid
$default eq "start" $pid
Truth table100%3/3
A and B
A= $default eq "start"B= $pid
ABresult
0X0
100
111
629 56 my @reasons = ( $self->_restart_reasons_for($pid), $self->_unit_rewritten($args{unit_digest}) );
630 56 ▶ return $default unless @reasons;
Branch: 1 branch
BranchTrueFalseTotal
unless @reasons 41 15 56
631 15 Rex::Logger::info("Restarting " . $self->service . ", which reads these only when "
632 . "it starts: " . join('; ', @reasons));
633 15 return 'restart';
634 }
635  
636  
637 69 ▶sub installer_unit_digest {
Subroutine installer_unit_digest: called CC=3 SCAR=11.0
Pod documented
638 69 my ( $self ) = @_;
639 69 ▶ my @files = $self->installer_unit_files or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@files) = $self->installer_unit_files or return 46 23 69
640 46 ▶ return Rex::Commands::Run::run('sha256sum ' . join(' ', map { $self->_shell_quote($_) } @files)
Condition50%1/2
Rex::Commands::Run::run('sha256sum ' . join(" ", map({$self->_shell_quote($_);} @files)) . ' 2>&1', "auto_die", 0) // ""
l!l
460
MC/DC0%0/1
Rex::Commands::Run::run('sha256sum ' . join(" ", map({$self->_shell_quote($_);} @files)) . ' 2>&1', "auto_die", 0) // ""
Rex::Commands::Run::run('sha256sum ' . join(" ", map({$self->_shell_quote($_);} @files)) . ' 2>&1', "auto_die", 0)
Truth table50%1/2
A
A= Rex::Commands::Run::run('sha256sum ' . join(" ", map({$self->_shell_quote($_);} @files)) . ' 2>&1', "auto_die", 0)
Aresult
11
00
641 . ' 2>&1', auto_die => 0) // '';
642 }
643  
644 # The installer rewrote the unit or its env file with other content than
645 # before it ran: the service's arguments or environment (K3S_URL, proxies)
646 # changed, which it takes only when it starts.
647 56 ▶sub _unit_rewritten {
Subroutine _unit_rewritten: called CC=4 SCAR=13.9
648 56 my ( $self, $before ) = @_;
649 56 ▶ return unless defined $before;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $before 36 20 56
650 20 ▶ my $after = $self->installer_unit_digest // '';
Condition50%1/2
$self->installer_unit_digest // ""
l!l
200
MC/DC0%0/1
$self->installer_unit_digest // ""
$self->installer_unit_digest
Truth table50%1/2
A
A= $self->installer_unit_digest
Aresult
11
00
651 20 ▶ return if $after eq $before;
Branch: 1 branch
BranchTrueFalseTotal
if $after eq $before 18 2 20
652 2 return "rewritten by the installer with other content: "
653 . join(', ', $self->installer_unit_files);
654 }
655  
656 # A restart onto the installed binary, checked against the running one.
657 # Rejected upgrades die here too: check_version_skew could not see them
658 # before the install when the channel did not resolve or moved in between.
659 # An unpinned new minor stays installed but not running (maintainer
660 # decision, k56): an unpinned re-run must not silently upgrade a running
661 # control plane. KillMode=process keeps pods up across a restart either way.
662 75 ▶sub _hold_new_binary {
Subroutine _hold_new_binary: called CC=8 SCAR=20.8
663 75 my ( $self, $pid, $pinned ) = @_;
664 75 my $running = $self->running_version($pid);
665 75 my $installed = $self->installed_version;
666 75 ▶ return 0 unless defined $running && defined $installed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $running and defined $installed 9 66 75
Condition66%2/3
defined $running and defined $installed
!ll&&!rl&&r
9066
MC/DC50%1/2
defined $running and defined $installed
defined $running defined $installed
Truth table66%2/3
A and B
A= defined $runningB= defined $installed
ABresult
0X0
100
111
667 66 ▶ my $skew = $self->version_skew($running, $installed) // return 0;
Condition50%1/2
$self->version_skew($running, $installed) // (return 0)
l!l
660
MC/DC0%0/1
$self->version_skew($running, $installed) // (return 0)
$self->version_skew($running, $installed)
Truth table50%1/2
A
A= $self->version_skew($running, $installed)
Aresult
11
00
668 66 my $service = $self->service;
669 66 my $binary = $self->binary;
670  
671 66 ▶ die "$binary $installed is installed, but $service runs $running, and "
Branch: 1 branch
BranchTrueFalseTotal
if $skew eq "jump" or $skew eq "downgrade" 3 63 66
Condition100%3/3
$skew eq "jump" or $skew eq "downgrade"
l!l&&r!l&&!r
2163
MC/DC100%2/2
$skew eq "jump" or $skew eq "downgrade"
$skew eq "jump" $skew eq "downgrade"
Truth table100%3/3
A or B
A= $skew eq "jump"B= $skew eq "downgrade"
ABresult
1X1
011
000
672 . $self->_skew_rule($skew, $running) . " $service was not restarted and "
673 . "keeps running $running; its next start (a reboot) runs $installed. "
674 . "Install $running again (pin version) or a release the policy allows.\n"
675 if $skew eq 'jump' || $skew eq 'downgrade';
676 63 ▶ return 0 unless $skew eq 'minor' && !$pinned;
Branch: 1 branch
BranchTrueFalseTotal
unless $skew eq "minor" and not $pinned 59 4 63
Condition100%3/3
$skew eq "minor" and not $pinned
!ll&&!rl&&r
5634
MC/DC100%2/2
$skew eq "minor" and not $pinned
$skew eq "minor" not $pinned
Truth table100%3/3
A and not(B)
A= $skew eq "minor"B= not $pinned
ABresult
0X0
100
111
677  
678 4 Rex::Logger::info("$service was NOT restarted: it runs $running, and $installed "
679 . "is now installed, a new minor version from the stable channel since "
680 . "version is not pinned. It keeps running $running, and anything else "
681 . "changed for it waits too, until its next start (a reboot starts "
682 . "$installed). Pin version => '$installed' to have it restarted, or run: "
683 . "systemctl restart $service", 'warn');
684 4 return 1;
685 }
686  
687 # Rex::GPU 0.001 wrote agent/etc/containerd/config.toml.tmpl as a bare
688 # `imports = [...]` + `version = 2`. rke2 renders a template instead of its
689 # own config, so config.toml became exactly that: no SystemdCgroup, sandbox
690 # image or registry mirrors. remove_bare_containerd_template (which
691 # install_server and install_agent run before this) and Rex::GPU 0.002's
692 # gpu_setup remove the template, but restart nothing, and config.toml is
693 # rewritten only when the service starts. So: config.toml still in that
694 # shape and the template gone, or replaced by another one since -> restart a
695 # running service once; the rendered config no longer matches, so the next
696 # re-run starts (a no-op) again. The bare template still there (start_verb
697 # asked without removing it first) -> a restart would render the same file:
698 # warn with what to do.
699 173 ▶sub _stale_containerd_restart {
Subroutine _stale_containerd_restart: called CC=9 SCAR=22.0
700 173 my ( $self ) = @_;
701 173 my $distribution = $self->name;
702 173 my $service = $self->service;
703  
704 173 my $dir = $self->containerd_dir;
705 173 my $config = Rex::Commands::Run::run("cat $dir/config.toml 2>/dev/null", auto_die => 0);
706 173 ▶ return 0 unless $self->is_bare_template_output($config);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_bare_template_output($config) 144 29 173
707  
708 29 my $tmpl = Rex::Commands::Run::run("cat $dir/config.toml.tmpl 2>/dev/null", auto_die => 0);
709 29 my $has_tmpl = $? == 0;
710 29 ▶ if ($has_tmpl && $self->is_bare_template_output($tmpl)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_tmpl and $self->is_bare_template_output($tmpl)) 2 27 29
Condition100%3/3
$has_tmpl and $self->is_bare_template_output($tmpl)
!ll&&!rl&&r
2252
MC/DC100%2/2
$has_tmpl and $self->is_bare_template_output($tmpl)
$has_tmpl $self->is_bare_template_output($tmpl)
Truth table100%3/3
A and B
A= $has_tmplB= $self->is_bare_template_output($tmpl)
ABresult
0X0
100
111
711 2 Rex::Logger::info("$dir/config.toml.tmpl holds only imports and version = 2 (as "
712 . "Rex::GPU 0.001 wrote it) and replaces ${distribution}'s own containerd config: "
713 . "no SystemdCgroup, sandbox image or registry mirrors. Remove it "
714 . "(remove_bare_containerd_template, as install_server and install_agent do) "
715 . "and run: systemctl restart $service", 'warn');
716 2 return 0;
717 }
718  
719 27 Rex::Commands::Run::run("systemctl is-active --quiet $service", auto_die => 0);
720 27 my $prefix = '';
721 27 ▶ unless ($? == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($? == 0) 13 14 27
722 # containerd 2.x exits on that bare config.toml, so the service never gets
723 # to active: it hangs in activating (its Restart= loop) or ends failed
724 # (k91). Either one wants to run and is restarted once. Stopped on purpose
725 # (inactive): its next start renders a fresh config.toml anyway.
726 13 ▶ my $state = Rex::Commands::Run::run("systemctl is-active $service 2>/dev/null", auto_die => 0) // '';
Condition50%1/2
Rex::Commands::Run::run("systemctl is-active $service 2>/dev/null", "auto_die", 0) // ""
l!l
130
MC/DC0%0/1
Rex::Commands::Run::run("systemctl is-active $service 2>/dev/null", "auto_die", 0) // ""
Rex::Commands::Run::run("systemctl is-active $service 2>/dev/null", "auto_die", 0)
Truth table50%1/2
A
A= Rex::Commands::Run::run("systemctl is-active $service 2>/dev/null", "auto_die", 0)
Aresult
11
00
727 13 $state =~ s/\s+//g;
728 13 ▶ return 0 unless $state eq 'activating' || $state eq 'failed';
Branch: 1 branch
BranchTrueFalseTotal
unless $state eq "activating" or $state eq "failed" 7 6 13
Condition100%3/3
$state eq "activating" or $state eq "failed"
l!l&&r!l&&!r
337
MC/DC100%2/2
$state eq "activating" or $state eq "failed"
$state eq "activating" $state eq "failed"
Truth table100%3/3
A or B
A= $state eq "activating"B= $state eq "failed"
ABresult
1X1
011
000
729 6 $prefix = "$service is $state (containerd exits on its bare config); ";
730 }
731 20 ▶ Rex::Logger::info( $prefix . ( $has_tmpl
Branch: 1 branch
BranchTrueFalseTotal
$has_tmpl ? : 3 17 20
732 ? "$dir/config.toml was rendered from Rex::GPU 0.001's bare config.toml.tmpl, "
733 . "which another template has replaced since; restarting $service so it "
734 . "renders that one"
735 : "$dir/config.toml was rendered from a config.toml.tmpl that is gone "
736 . "(Rex::GPU 0.001's); restarting $service so it regenerates its "
737 . "containerd config" ), 'warn');
738 20 return 1;
739 }
740  
741  
742 # kubernetes-ocp before its k23 wrote the same two lines (its
743 # cleanup_legacy_containerd_template). Same narrow match as Rex::GPU 0.002's
744 # heal on gpu_setup; this covers a node re-run without Rex::GPU, or set up by
745 # kubernetes-ocp. `{{ template "base" . }}` or any further line keeps it.
746 151 ▶sub remove_bare_containerd_template {
Subroutine remove_bare_containerd_template: called CC=5 SCAR=16.1
Pod documented
747 151 my ( $self ) = @_;
748 151 my $tmpl = $self->containerd_dir.'/config.toml.tmpl';
749 151 my $content = Rex::Commands::Run::run("cat $tmpl 2>/dev/null", auto_die => 0);
750 # No template: nothing to do.
751 151 ▶ return 0 unless $? == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $? == 0 44 107 151
752  
753 107 ▶ unless ($self->is_bare_template_output($content)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_bare_template_output($content)) 82 25 107
754 82 Rex::Logger::info("Keeping $tmpl: it is not Rex::GPU 0.001's bare template "
755 . "(only imports and version = 2) but somebody's containerd configuration");
756 82 return 0;
757 }
758  
759 25 my $label = $self->label;
760 25 my $service = $self->service;
761 25 Rex::Logger::info("Removing $tmpl: it holds only imports and version = 2 (Rex::GPU "
762 . "0.001's bare template), which $label renders instead of its own containerd "
763 . "config: no SystemdCgroup, sandbox image or registry mirrors", 'warn');
764 # auto_die => 0 only to die naming the file: a template left in place would
765 # be rendered again by the start that follows.
766 25 my $out = Rex::Commands::Run::run("rm -f $tmpl 2>&1", auto_die => 0);
767 25 ▶ die "Could not remove $tmpl, the bare containerd template that replaces "
Branch: 1 branch
BranchTrueFalseTotal
unless $? == 0 6 19 25
Condition50%1/2
$out // ""
l!l
60
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
768 . "${label}'s own containerd config: " . ( ( $out // '' ) =~ s/\s+\z//r ) . "\n"
769 unless $? == 0;
770 19 return 1;
771 }
772  
773 # remove_bare_containerd_template right before a (re)start of the service
774 # (install_server, install_agent, update_registries): a failure there also
775 # means that start did not happen, which the error says. Outside a start
776 # (heal_bare_containerd_template) the sentence would mislead (k83).
777 140 ▶sub _remove_bare_template_before_start {
Subroutine _remove_bare_template_before_start: called CC=2 SCAR=6.9
778 140 my ( $self ) = @_;
779 140 my $removed = eval { $self->remove_bare_containerd_template };
780 140 ▶ die $@ . $self->service . " was not (re)started.\n" unless defined $removed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $removed 6 134 140
781 134 return $removed;
782 }
783  
784  
785 8 ▶sub heal_bare_containerd_template {
Subroutine heal_bare_containerd_template: called CC=4 SCAR=13.9
Pod documented
786 8 my ( $self ) = @_;
787 8 my $removed = $self->remove_bare_containerd_template;
788 8 ▶ return { removed => $removed, restarted => 0 } unless $self->_stale_containerd_restart;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_stale_containerd_restart 3 5 8
789  
790 5 my $service = $self->service;
791 5 my $out = Rex::Commands::Run::run("systemctl restart $service 2>&1", auto_die => 0);
792 5 ▶ die "Could not restart $service after removing the bare containerd template: "
Branch: 1 branch
BranchTrueFalseTotal
unless $? == 0 1 4 5
Condition50%1/2
$out // ""
l!l
10
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
793 . ( ( $out // '' ) =~ s/\s+\z//r ) . "\n"
794 unless $? == 0;
795 4 return { removed => $removed, restarted => 1 };
796 }
797  
798  
799 66 ▶sub restart_watch {
Subroutine restart_watch: called CC=2 SCAR=6.9
Pod documented
800 66 my ( $self ) = @_;
801 66 my $containerd = $self->containerd_dir;
802 return (
803 66 ▶ $self->config_file,
Branch: 1 branch
BranchTrueFalseTotal
defined $self->env_file ? : 45 21 66
804 $self->config_dir.'/config.yaml.d',
805 $self->registries_file,
806 ( defined $self->env_file ? ( $self->env_file ) : () ),
807 "$containerd/config.toml.tmpl",
808 "$containerd/config-v3.toml.tmpl",
809 "$containerd/config-v3.toml.d",
810 );
811 }
812  
813  
814 7 ▶sub restart_reasons {
Subroutine restart_reasons: called CC=2 SCAR=6.9
Pod documented
815 7 my ( $self ) = @_;
816 7 my $pid = $self->main_pid;
817 7 ▶ return unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 1 6 7
818 6 return $self->_restart_reasons_for($pid);
819 }
820  
821 62 ▶sub _restart_reasons_for {
Subroutine _restart_reasons_for: called CC=8 SCAR=20.8
822 62 my ( $self, $pid ) = @_;
823 62 my $service = $self->service;
824  
825 62 my @reasons;
826  
827 # Epoch seconds, both on the host's clock. Only files written after that
828 # second count, so one written in the second the process started is missed;
829 # everything here is written before the start, minutes earlier.
830 62 my $since = Rex::Commands::Run::run(
831 "echo \$(( \$(date +%s) - \$(ps -o etimes= -p $pid) ))", auto_die => 0);
832 62 ▶ if (($since // '') =~ /\A\s*(\d+)\s*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (($since // '') =~ /\A\s*(\d+)\s*\z/u) { } 56 6 62
Condition50%1/2
$since // ""
l!l
620
MC/DC0%0/1
$since // ""
$since
Truth table50%1/2
A
A= $since
Aresult
11
00
833 56 $since = $1;
834 56 my $paths = join(' ', map { "'$_'" } $self->restart_watch);
835 56 my $changed = Rex::Commands::Run::run("find $paths -newermt \@$since 2>/dev/null", auto_die => 0);
836 56 ▶ my @changed = grep { length } split /\n/, $changed // '';
Condition50%1/2
$changed // ""
l!l
560
MC/DC0%0/1
$changed // ""
$changed
Truth table50%1/2
A
A= $changed
Aresult
11
00
837 56 ▶ push @reasons, "changed since it started: " . join(', ', @changed) if @changed;
Branch: 1 branch
BranchTrueFalseTotal
if @changed 11 45 56
838 }
839 else {
840 6 Rex::Logger::info("Could not tell when $service started (ps -o etimes= -p $pid): "
841 . "changes to " . join(', ', $self->restart_watch) . " are not detected; "
842 . "restart $service yourself if one of them changed", 'warn');
843 }
844  
845 # /proc/PID/exe still runs a binary the installer replaced (unlinked).
846 62 my $running = $self->running_version($pid);
847 62 my $installed = $self->installed_version;
848 62 ▶ if (defined $running && defined $installed) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $running and defined $installed) { } 53 9 62
Condition66%2/3
defined $running and defined $installed
!ll&&!rl&&r
9053
MC/DC50%1/2
defined $running and defined $installed
defined $running defined $installed
Truth table66%2/3
A and B
A= defined $runningB= defined $installed
ABresult
0X0
100
111
849 53 ▶ push @reasons, "it runs $running, $installed is installed"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->same_version($running, $installed) 8 45 53
850 unless $self->same_version($running, $installed);
851 }
852 else {
853 9 Rex::Logger::info("Could not compare the running $service (/proc/$pid/exe "
854 . "--version) with the installed " . $self->binary . " --version: a new binary "
855 . "is not detected; restart $service yourself after an upgrade", 'warn');
856 }
857  
858 62 return @reasons;
859 }
860  
861  
862 460 ▶sub parse_main_pid {
Subroutine parse_main_pid: called CC=3 SCAR=11.0
Pod documented
863 460 my ( $self, $out ) = @_;
864 460 ▶ return $1 if ($out // '') =~ /^MainPID=([1-9]\d*)\s*$/m;
Branch: 1 branch
BranchTrueFalseTotal
if ($out // "") =~ /^MainPID=([1-9]\d*)\s*$/mu 233 227 460
Condition100%2/2
$out // ""
l!l
4591
MC/DC100%1/1
$out // ""
$out
Truth table100%2/2
A
A= $out
Aresult
11
00
865 227 return;
866 }
867  
868  
869 # Same narrow match as Rex::GPU 0.002's _is_rke2_clobber_tmpl.
870 296 ▶sub is_bare_template_output {
Subroutine is_bare_template_output: called CC=10 SCAR=23.0
Pod documented
871 296 my ( $self, $content ) = @_;
872 296 ▶ return 0 unless defined $content && length $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and length $content 196 100 296
Condition100%3/3
defined $content and length $content
!ll&&!rl&&r
1195100
MC/DC100%2/2
defined $content and length $content
defined $content length $content
Truth table100%3/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
873 100 ▶ my @lines = grep { /\S/ && !/^\s*#/ } split /\n/, $content;
Condition100%3/3
/\S/u && !/^\s*#/u
!ll&&!rl&&r
3523244
MC/DC100%2/2
/\S/u && !/^\s*#/u
/\S/u !/^\s*#/u
Truth table100%3/3
A && not(B)
A= /\S/uB= !/^\s*#/u
ABresult
0X0
100
111
874 100 ▶ return 0 unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 100 100
875 100 my ($imports, $version2) = (0, 0);
876 100 for my $l (@lines) {
877 168 ▶ if ($l =~ /^\s*imports\s*=/) { $imports = 1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($l =~ /^\s*imports\s*=/u) { } 64 104 168
elsif ($l =~ /^\s*version\s*=\s*2\s*$/u) { } 64 40 104
878 64 elsif ($l =~ /^\s*version\s*=\s*2\s*$/) { $version2 = 1 }
879 40 else { return 0 }
880 }
881 60 ▶ return ($imports && $version2) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$imports && $version2 ? : 58 2 60
Condition100%3/3
$imports and $version2
!ll&&!rl&&r
1158
MC/DC100%2/2
$imports and $version2
$imports $version2
Truth table100%3/3
A and B
A= $importsB= $version2
ABresult
0X0
100
111
882 }
883  
884  
885 133 ▶sub wait_for_service {
Subroutine wait_for_service: called CC=10 SCAR=23.1
Pod documented
886 133 my ( $self, %args ) = @_;
887 133 my $service = $self->service;
888 133 ▶ my $attempts = $args{attempts} // 60;
Condition100%2/2
$args{"attempts"} // 60
l!l
6127
MC/DC100%1/1
$args{"attempts"} // 60
$args{"attempts"}
Truth table100%2/2
A
A= $args{"attempts"}
Aresult
11
00
889 133 ▶ my $interval = $args{interval} // 10;
Condition100%2/2
$args{"interval"} // 10
l!l
6127
MC/DC100%1/1
$args{"interval"} // 10
$args{"interval"}
Truth table100%2/2
A
A= $args{"interval"}
Aresult
11
00
890 133 my $hint = $args{hint};
891  
892 133 Rex::Logger::info("Waiting for $service to become active...");
893  
894 133 my $state = '';
895 133 for my $i (1 .. $attempts) {
896 138 $state = Rex::Commands::Run::run("systemctl is-active $service", auto_die => 0);
897 138 ▶ $state = '' unless defined $state;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state 0 138 138
898 138 $state =~ s/\s+\z//;
899 138 ▶ if ($state eq 'active') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "active") 126 12 138
900 126 Rex::Logger::info(" $service is active");
901 126 return 1;
902 }
903 12 ▶ die $self->_service_failure("is failed", $hint) if $state eq 'failed';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "failed" 5 7 12
904 7 ▶ Rex::Logger::info(" $service is " . ($state || 'unknown') . " ($i/$attempts)");
Condition50%1/2
$state || 'unknown'
l!l
70
MC/DC0%0/1
$state || 'unknown'
$state
Truth table50%1/2
A
A= $state
Aresult
11
00
905 7 ▶ sleep $interval if $i < $attempts;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $attempts 5 2 7
906 }
907  
908 2 ▶ die $self->_service_failure(
Condition50%1/2
$state || 'unknown'
l!l
20
MC/DC0%0/1
$state || 'unknown'
$state
Truth table50%1/2
A
A= $state
Aresult
11
00
909 "did not become active within " . ($attempts * $interval) . "s (last state: "
910 . ($state || 'unknown') . ")", $hint);
911 }
912  
913 7 ▶sub _service_failure {
Subroutine _service_failure: called CC=4 SCAR=13.9
914 7 my ( $self, $reason, $hint ) = @_;
915 7 my $service = $self->service;
916 7 my $journal = Rex::Commands::Run::run("journalctl -u $service -n 50 --no-pager 2>&1", auto_die => 0);
917 7 ▶ $journal = '' unless defined $journal;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $journal 0 7 7
918 7 $journal =~ s/\s+\z//;
919 7 ▶ return "$service $reason\n"
Branch: 2 branches
BranchTrueFalseTotal
defined $hint ? : 4 3 7
$journal eq '' ? : 3 4 7
920 . ( defined $hint ? "$hint\n" : '' )
921 . "--- journalctl -u $service -n 50 ---\n"
922 . ($journal eq '' ? '(no journal output)' : $journal) . "\n";
923 }
924  
925 #
926 # NVIDIA runtime lookup: a PATH for the rke2 units.
927 #
928  
929  
930 44 ▶sub runtime_path_line { 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' }
Subroutine runtime_path_line: called CC=1 SCAR=0.0
Pod documented
931  
932  
933 17 ▶sub env_with_runtime_path {
Subroutine env_with_runtime_path: called CC=3 SCAR=11.0
Pod documented
934 17 my ( $self, $current ) = @_;
935 17 ▶ $current //= '';
Condition100%2/2
$current //= ""
l!l
161
MC/DC100%1/1
$current //= ""
$current
Truth table100%2/2
A
A= $current
Aresult
11
00
936 17 my @keep = grep { !/^\s*PATH=/ } split /\n/, $current;
937 17 my $content = join('', map { $_."\n" } @keep, $self->runtime_path_line);
938 17 ▶ return $content eq $current ? undef : $content;
Branch: 1 branch
BranchTrueFalseTotal
$content eq $current ? : 3 14 17
939 }
940  
941  
942 # rke2-server/-agent.service carry no Environment= and read
943 # EnvironmentFile=-/etc/default/%N; rke2 scans PATH for nvidia-container-runtime
944 # at service start only, and the RKE2 GPU docs say to set PATH there. The rke2
945 # install script does not touch /etc/default, so this survives the install.
946 # k3s has no env_file: its agent code does the same scan and wired a host
947 # toolkit plus the nvidia RuntimeClass on a DGX without help (kubernetes-ocp,
948 # _configure_nvidia_runtime_path).
949 8 ▶sub ensure_nvidia_runtime_path {
Subroutine ensure_nvidia_runtime_path: called CC=6 SCAR=17.9
Pod documented
950 8 my ( $self ) = @_;
951 8 ▶ my $env_file = $self->env_file or return;
Branch: 1 branch
BranchTrueFalseTotal
my $env_file = $self->env_file or return 6 2 8
952  
953 6 ▶ unless (Rex::Commands::Run::can_run('nvidia-container-runtime')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Rex::Commands::Run::can_run("nvidia-container-runtime")) 1 5 6
954 1 Rex::Logger::info("nvidia-container-runtime not on PATH, $env_file left alone "
955 . "(the GPU Operator's toolkit is found without it)");
956 1 return;
957 }
958  
959 5 my $current = Rex::Commands::Run::run("cat $env_file 2>/dev/null", auto_die => 0);
960 5 ▶ my $content = $self->env_with_runtime_path($? == 0 ? $current : '');
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 2 3 5
961 5 ▶ unless (defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 1 4 5
962 1 Rex::Logger::info("$env_file already carries the PATH for the NVIDIA runtime");
963 1 return;
964 }
965  
966 4 Rex::Logger::info("Writing PATH to $env_file for the NVIDIA runtime lookup");
967 4 Rex::Commands::Run::run("mkdir -p /etc/default", auto_die => 1);
968 # No secret in here: the file keeps its mode, a new one gets root's umask.
969 4 Rex::Commands::File::file($env_file, content => $content);
970  
971 # Only on a re-run: the service reads the file when it starts.
972 4 my $service = $self->service;
973 4 Rex::Commands::Run::run("systemctl is-active --quiet $service", auto_die => 0);
974 4 ▶ Rex::Logger::info("$service is running: it takes the new PATH at its next start "
Branch: 1 branch
BranchTrueFalseTotal
if $? == 0 1 3 4
975 . "(install_server and install_agent restart it for that)")
976 if $? == 0;
977 }
978  
979  
980 55 ▶sub env_files {
Subroutine env_files: called CC=2 SCAR=7.0
Pod documented
981 55 my ( $self ) = @_;
982 55 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
5410
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
983 55 return grep { defined } map { $class->new( role => $_ )->env_file } qw( server agent );
984 }
985  
986 #
987 # Secret files: config.yaml, registries.yaml
988 #
989  
990  
991 # config.yaml carries the join token and registries.yaml may carry registry
992 # passwords, so both end up 0600 root:root. Rex's `file` writes content to a
993 # ".rex.tmp.<name>" sibling (LibSSH: `cat >` over an exec channel, no SFTP),
994 # renames it over the target and only then chmods -- the tmp file would be
995 # umask-mode (0644) in between. Pre-creating that tmp name at 0600 closes the
996 # window: `cat >` and SFTP open-truncate both keep an existing inode's mode,
997 # and the rename carries it to the target. The explicit chmod afterwards is
998 # what fixes an existing 0644 file whose content is unchanged (Rex then drops
999 # the tmp file and never touches the target), and fails loudly.
1000 137 ▶sub write_secret_file {
Subroutine write_secret_file: called CC=1 SCAR=0.0
Pod documented
1001 137 my ( $self, $path, $content ) = @_;
1002  
1003 137 my $tmp = Rex::Commands::File::get_tmp_file_name($path);
1004 137 Rex::Commands::Run::run("install -m 600 -o root -g root /dev/null $tmp", auto_die => 1);
1005  
1006 137 Rex::Commands::File::file($path, content => $content);
1007  
1008 137 Rex::Commands::Run::run("chown root:root $path && chmod 600 $path", auto_die => 1);
1009 }
1010  
1011  
1012 12 ▶sub write_registries {
Subroutine write_registries: called CC=1 SCAR=0.0
Pod documented
1013 12 my ( $self, $registries ) = @_;
1014  
1015 12 my $registries_file = $self->registries_file;
1016 12 Rex::Logger::info("Writing registries config to $registries_file");
1017  
1018 12 $self->write_secret_file($registries_file,
1019 YAML::PP->new(boolean => 'JSON::PP')->dump_string($registries));
1020 }
1021  
1022 63 1;
1023  
1024 __END__
« lib/Rex/Rancher/Cilium.pm ↑ Summary lib/Rex/Rancher/Server.pm »