lib/Rex/Rancher/Server.pm

filter: Statementstmt 92.6% 214 / 231 Branchbran 87.5% 91 / 104 Conditioncond 78.4% 40 / 51 MC/DCmcdc 66.6% 20 / 30 Subroutinesub 93.9% 31 / 33 Podpod 87.5% 7 / 8 totaltotal 88.1% 403 / 457 CC 72 SCAR 18.8
CC
72
Cov
89%
_build_server_config
13.0
fetch_kubeconfig
12.0
split_server_address
9.0
CRAP 6.6
« lib/Rex/Rancher/Distribution.pm ↑ Summary blib/lib/Rex/Rancher/Node.pm »
1 # ABSTRACT: Rancher Kubernetes server (control plane) installation
2  
3 package Rex::Rancher::Server;
4 56 our $VERSION = '0.004';
5 56 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 56 ▶use Fcntl qw( O_CREAT O_TRUNC O_WRONLY );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 56 ▶use Rex::Commands::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 56 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 56 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 56 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 56 ▶use Rex::Rancher::Distribution;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 56 ▶use Rex::Rancher::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 56 ▶use Rex::Rancher::Uninstall ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 56 ▶use Socket qw( AF_INET6 inet_pton );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 56 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 56 require Rex::Exporter;
20 56 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 56 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 56 @EXPORT = qw(
25 install_server
26 update_registries
27 get_kubeconfig
28 fetch_kubeconfig
29 patch_kubeconfig_server
30 get_token
31 );
32  
33  
34  
35 101 ▶sub install_server {
Subroutine install_server: called CC=6 SCAR=18.0
Pod documented
36 101 my (%opts) = @_;
37  
38 101 ▶ my $distribution = $opts{distribution} // 'rke2';
Condition100%2/2
$opts{"distribution"} // "rke2"
l!l
983
MC/DC100%1/1
$opts{"distribution"} // "rke2"
$opts{"distribution"}
Truth table100%2/2
A
A= $opts{"distribution"}
Aresult
11
00
39 101 my $dist = Rex::Rancher::Distribution->new_for($distribution);
40 100 ▶ Rex::Logger::info(
Branch: 1 branch
BranchTrueFalseTotal
unless $dist->live_verified 0 100 100
41 "$distribution has not been run live through Rex::Rancher; rke2 is the "
42 . "verified distribution.", "warn")
43 unless $dist->live_verified;
44 # Everything that may refuse, before anything is written (the token lookup
45 # below is the first read after it); rancher_deploy_server asked the same
46 # before it prepared the node (k78).
47 100 my $checked = preflight_server(%opts);
48 66 my $method = $checked->{install_method};
49 66 my $cluster_cidr = $checked->{cluster_cidr};
50 66 my $version = $checked->{version};
51 66 my $kubeconfig_mode = $checked->{kubeconfig_mode};
52 66 my $hold = $opts{hold_running};
53 66 ▶ my $cilium = exists $opts{cilium} ? $opts{cilium} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'cilium'} ? : 0 66 66
54 66 my $token = _resolve_token($dist, $opts{token});
55 66 my $server = $opts{server};
56 66 my $tls_san = $opts{tls_san};
57 66 my $node_labels = $opts{node_labels};
58 66 my $registries = $opts{registries};
59 66 my $node_name = $opts{node_name};
60 66 my $disable = $opts{disable};
61  
62 66 Rex::Logger::info("Installing $distribution server (control plane)...");
63  
64 # Ensure config directory exists
65 66 file $dist->config_dir . '/', ensure => 'directory';
66  
67 # Write config.yaml
68 66 _write_config($dist, $token, $server, $tls_san, $node_labels, $cilium,
69 $node_name, $disable, $cluster_cidr, $kubeconfig_mode);
70  
71 # Write registries.yaml if configured
72 66 ▶ if ($registries) {
Branch: 1 branch
BranchTrueFalseTotal
if ($registries) 0 66 66
73 0 $dist->write_registries($registries);
74 }
75  
76 # Before the installer: rke2 looks for the NVIDIA runtime only when its
77 # service starts.
78 66 ▶ $dist->ensure_nvidia_runtime_path if $opts{nvidia_runtime_path};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"nvidia_runtime_path"} 1 65 66
79  
80 # Install and start
81 66 _install($dist, $server, $version, $method, $hold);
82  
83 64 Rex::Logger::info("$distribution server installation complete");
84  
85 64 return 1;
86 }
87  
88  
89 151 ▶sub preflight_server {
Subroutine preflight_server: called CC=3 SCAR=11.0
Pod documented
90 151 my (%opts) = @_;
91 151 my $dist = Rex::Rancher::Distribution->new_for($opts{distribution});
92  
93 # Pure, before anything touches the host. scalar(): no cluster_cidr
94 # returns an empty list, which would shift the pairs here.
95 my %checked = (
96 install_method => scalar Rex::Rancher::Options->resolve_install_method($opts{install_method}, $opts{version}),
97 cluster_cidr => scalar Rex::Rancher::Options->check_cluster_cidr($opts{cluster_cidr}),
98 151 kubeconfig_mode => scalar Rex::Rancher::Options->check_kubeconfig_mode($opts{kubeconfig_mode}),
99 );
100 # The first read of the host: Cilium state an earlier cluster left on a host
101 # without RKE2/K3s would hang every image pull of this install (k71).
102 143 Rex::Rancher::Uninstall->check_cilium_residue;
103 # hold_running: the version on the host is this run's version, for the
104 # skew check and everything after it (k77).
105 $checked{version} = $opts{hold_running}
106 139 ▶ ? $dist->held_version(version => $opts{version}) : $opts{version};
Branch: 1 branch
BranchTrueFalseTotal
$opts{'hold_running'} ? : 29 110 139
107 # A rejected upgrade leaves the host as it was.
108 139 $dist->check_version_skew(version => $checked{version});
109 # Nor another pod network for a server already set up here: config.yaml
110 # would carry it and the service be restarted onto it (k67).
111 $dist->check_established_cluster_cidr(cluster_cidr => $checked{cluster_cidr},
112 132 ▶ cilium => ( exists $opts{cilium} ? $opts{cilium} : 1 ));
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'cilium'} ? : 14 118 132
113 108 return \%checked;
114 }
115  
116  
117 8 ▶sub update_registries {
Subroutine update_registries: called CC=3 SCAR=11.1
Pod documented
118 8 my (%opts) = @_;
119  
120 8 ▶ my $distribution = $opts{distribution} // 'rke2';
Condition50%1/2
$opts{"distribution"} // "rke2"
l!l
80
MC/DC0%0/1
$opts{"distribution"} // "rke2"
$opts{"distribution"}
Truth table50%1/2
A
A= $opts{"distribution"}
Aresult
11
00
121 8 ▶ my $registries = $opts{registries} or die "update_registries requires 'registries' option\n";
Branch: 1 branch
BranchTrueFalseTotal
my $registries = $opts{"registries"} or die "update_registries requires 'registries' option\n" 8 0 8
122 8 my $dist = Rex::Rancher::Distribution->new_for($distribution);
123  
124 8 Rex::Logger::info("Updating registries.yaml for $distribution");
125  
126 8 $dist->write_registries($registries);
127  
128 # Before the restart: it would render Rex::GPU 0.001's bare containerd
129 # template again, which carries no registry mirrors. Dies if it cannot.
130 8 $dist->_remove_bare_template_before_start;
131  
132 # Restart containerd to pick up new config: whichever unit this node runs.
133 6 run $dist->restart_services_cmd, auto_die => 0;
134  
135 6 Rex::Logger::info("Registries updated, containerd restarted");
136 }
137  
138  
139 0 ▶sub get_kubeconfig {
Subroutine get_kubeconfig: not called CC=1 SCAR=6.9
Pod documented
140 0 my ($distribution) = @_;
141 0 my $dist = Rex::Rancher::Distribution->new_for($distribution);
142  
143 0 Rex::Logger::info("Retrieving kubeconfig from " . $dist->kubeconfig);
144  
145 0 my $content = run "cat " . $dist->kubeconfig, auto_die => 1;
146 0 return $content;
147 }
148  
149  
150 38 ▶sub fetch_kubeconfig {
Subroutine fetch_kubeconfig: called CC=12 SCAR=24.9
Pod documented
151 38 my (%opts) = @_;
152 38 my $server = $opts{server};
153 38 my $filter = $opts{filter};
154 38 my $file = $opts{file};
155  
156 38 ▶ die "fetch_kubeconfig: filter must be a code reference\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $filter and ref $filter ne "CODE" 1 37 38
Condition100%3/3
defined $filter and ref $filter ne "CODE"
!ll&&!rl&&r
14231
MC/DC100%2/2
defined $filter and ref $filter ne "CODE"
defined $filter ref $filter ne "CODE"
Truth table100%3/3
A and B
A= defined $filterB= ref $filter ne "CODE"
ABresult
0X0
100
111
157 if defined $filter && ref $filter ne 'CODE';
158 37 my $dist = Rex::Rancher::Distribution->new_for($opts{distribution});
159  
160 36 my $content = eval { get_kubeconfig($dist->name) };
161 36 ▶ unless ($content) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content) 9 27 36
162 9 ▶ my $err = $@ ? $@ =~ s/\s+\z//r : 'empty file';
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 6 3 9
163 9 die "Could not fetch the kubeconfig from the " . $dist->name
164 . " server ($err)\n";
165 }
166  
167 27 ▶ $content = patch_kubeconfig_server($content, $server)
Branch: 1 branch
BranchTrueFalseTotal
if defined $server and length $server 23 4 27
Condition100%3/3
defined $server and length $server
!ll&&!rl&&r
3123
MC/DC100%2/2
defined $server and length $server
defined $server length $server
Truth table100%3/3
A and B
A= defined $serverB= length $server
ABresult
0X0
100
111
168 if defined $server && length $server;
169  
170 27 ▶ if ($filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) 16 11 27
171 16 $content = $filter->($content);
172 16 ▶ die "fetch_kubeconfig: filter returned no kubeconfig, nothing was written\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content and length $content 2 14 16
Condition100%3/3
defined $content and length $content
!ll&&!rl&&r
1114
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
173 unless defined $content && length $content;
174 }
175  
176 25 ▶ if (defined $file && length $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and length $file) 19 6 25
Condition66%2/3
defined $file and length $file
!ll&&!rl&&r
6019
MC/DC50%1/2
defined $file and length $file
defined $file length $file
Truth table66%2/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
177 19 _write_kubeconfig($file, $content);
178 14 Rex::Logger::info("Kubeconfig saved to $file");
179 }
180  
181 20 return $content;
182 }
183  
184  
185 54 ▶sub patch_kubeconfig_server {
Subroutine patch_kubeconfig_server: called CC=5 SCAR=16.1
Pod documented
186 54 my ( $content, $server ) = @_;
187 54 ▶ die "patch_kubeconfig_server: no kubeconfig given\n" unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 1 53 54
188 53 ▶ die "patch_kubeconfig_server: no server address given\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $server and length $server 2 51 53
Condition100%3/3
defined $server and length $server
!ll&&!rl&&r
1151
MC/DC100%2/2
defined $server and length $server
defined $server length $server
Truth table100%3/3
A and B
A= defined $serverB= length $server
ABresult
0X0
100
111
189 unless defined $server && length $server;
190  
191 51 $server = '' . $server; # a Rex server object names its host when stringified
192 51 my ( $host, $port ) = split_server_address($server);
193 42 ▶ if ( defined $port ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $port) { } 5 37 42
194 5 $content =~ s{https://(?:127\.0\.0\.1|\[::1\]):\d+}{https://$host:$port}g;
195 }
196 else {
197 37 $content =~ s{https://(?:127\.0\.0\.1|\[::1\]):(\d+)}{https://$host:$1}g;
198 }
199 42 return $content;
200 }
201  
202 # "host", "host:port", "[v6]", "[v6]:port" or a bare v6 address into the host
203 # as it goes into a URL (a v6 address bracketed) and the port, undef when
204 # none was given. A malformed port or address dies.
205 51 ▶sub split_server_address {
Subroutine split_server_address: called CC=9 SCAR=22.0
Pod undocumented
206 51 my ($server) = @_;
207 51 my ( $host, $port );
208 51 ▶ if ( $server =~ /^\[([^\]]*)\](?::(.*))?$/s ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($server =~ /^\[([^\]]*)\](?::(.*))?$/su) { } 8 43 51
elsif (defined inet_pton(10, $server)) { } 5 38 43
elsif ($server =~ /^([^:\[\]]+)(?::(.*))?$/su) { } 38 0 38
209 8 ( $host, $port ) = ( $1, $2 );
210 8 ▶ die "patch_kubeconfig_server: '$server' is not a valid server address\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined inet_pton(10, $host) 2 6 8
211 unless defined inet_pton( AF_INET6, $host );
212 6 $host = '[' . $host . ']';
213 }
214 elsif ( defined inet_pton( AF_INET6, $server ) ) {
215 5 $host = '[' . $server . ']';
216 }
217 elsif ( $server =~ /^([^:\[\]]+)(?::(.*))?$/s ) {
218 38 ( $host, $port ) = ( $1, $2 );
219 }
220 else {
221 0 die "patch_kubeconfig_server: '$server' is not a valid server address\n";
222 }
223 49 ▶ if ( defined $port ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $port) 12 37 49
224 12 ▶ die "patch_kubeconfig_server: invalid port '$port' in server address '$server'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $port =~ /^[0-9]{1,5}$/u and $port >= 1 and $port <= 65535 7 5 12
Condition100%6/6
$port =~ /^[0-9]{1,5}$/u and $port >= 1 and $port <= 65535
!ll&&!rl&&r
615
$port =~ /^[0-9]{1,5}$/u and $port >= 1
!ll&&!rl&&r
516
MC/DC100%3/3
$port =~ /^[0-9]{1,5}$/u and $port >= 1 and $port <= 65535
$port =~ /^[0-9]{1,5}$/u $port >= 1 $port <= 65535
Truth table100%4/4
A and B and C
A= $port =~ /^[0-9]{1,5}$/uB= $port >= 1C= $port <= 65535
ABCresult
0XX0
10X0
1100
1111
225 unless $port =~ /^[0-9]{1,5}$/ && $port >= 1 && $port <= 65535;
226 5 $port += 0;
227 }
228 42 return ( $host, $port );
229 }
230  
231 # The admin client certificate and key are in there: created 0600, and an
232 # existing file (older versions wrote it umask-mode) narrowed before the
233 # content goes in. In place, not rename, so a symlinked path keeps its link.
234 # A local file: CORE::chmod, because the chmod Rex::Commands::Fs exports into
235 # this package runs on the remote host.
236 19 ▶sub _write_kubeconfig {
Subroutine _write_kubeconfig: called CC=5 SCAR=16.4
237 19 my ( $file, $content ) = @_;
238 19 ▶ my $fail = sub { die "Could not write the kubeconfig to $file: $!\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
239  
240 19 ▶ sysopen(my $fh, $file, O_WRONLY | O_CREAT | O_TRUNC, 0600) or $fail->();
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $file, 577, 384 or &$fail() 14 5 19
241 14 ▶ CORE::chmod(0600, $file) or $fail->();
Branch: 1 branch
BranchTrueFalseTotal
CORE::chmod 0600, $file or &$fail() 14 0 14
242 14 ▶ print {$fh} $content or $fail->();
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $content or &$fail() 14 0 14
243 14 ▶ close $fh or $fail->();
Branch: 1 branch
BranchTrueFalseTotal
close $fh or &$fail() 14 0 14
244 14 return;
245 }
246  
247  
248 0 ▶sub get_token {
Subroutine get_token: not called CC=1 SCAR=6.9
Pod documented
249 0 my ($distribution) = @_;
250 0 my $dist = Rex::Rancher::Distribution->new_for($distribution);
251  
252 0 Rex::Logger::info("Retrieving node token from " . $dist->token_file);
253  
254 0 my $content = run "cat " . $dist->token_file, auto_die => 1;
255 0 chomp $content;
256 0 return $content;
257 }
258  
259 # Never rotate the token a control plane is already sealed with: the datastore
260 # encryption key derives from it at bootstrap and is only re-checked at the
261 # NEXT start, so a fresh token in config.yaml arms a fatal "bootstrap data
262 # already found and encrypted with different token" on the next restart.
263 90 ▶sub _resolve_token {
Subroutine _resolve_token: called CC=5 SCAR=16.1
264 90 my ($dist, $given) = @_;
265 90 ▶ return $given if defined $given;
Branch: 1 branch
BranchTrueFalseTotal
if defined $given 66 24 90
266 24 my $existing = _existing_server_token($dist);
267 24 ▶ if (defined $existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $existing) 18 6 24
268 # The server keeps the token as K10<ca-hash>::server:<secret>, the first
269 # deploy wrote the bare secret into config.yaml. Same secret: keep what
270 # config.yaml has, or the re-run changes the file and restarts the server.
271 18 my $configured = _configured_token($dist);
272 18 ▶ if (defined $configured && _token_secret($configured) eq _token_secret($existing)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $configured and _token_secret($configured) eq _token_secret($existing)) 4 14 18
Condition100%3/3
defined $configured and _token_secret($configured) eq _token_secret($existing)
!ll&&!rl&&r
1224
MC/DC100%2/2
defined $configured and _token_secret($configured) eq _token_secret($existing)
defined $configured _token_secret($configured) eq _token_secret($existing)
Truth table100%3/3
A and B
A= defined $configuredB= _token_secret($configured) eq _token_secret($existing)
ABresult
0X0
100
111
273 4 Rex::Logger::info("Reusing the cluster token in " . $dist->config_file
274 . " (same secret as " . $dist->server_token . ")");
275 4 return $configured;
276 }
277 14 Rex::Logger::info("Reusing existing cluster token from " . $dist->server_token);
278 14 return $existing;
279 }
280 6 return _generate_token();
281 }
282  
283 # The secret part of a token: what follows "::<user>:" in the K10 form, the
284 # token itself otherwise.
285 12 ▶sub _token_secret {
Subroutine _token_secret: called CC=2 SCAR=6.9
286 12 my ($token) = @_;
287 12 ▶ return $token =~ /\AK10[0-9a-f]+::[^:]+:(.+)\z/s ? $1 : $token;
Branch: 1 branch
BranchTrueFalseTotal
$token =~ /\AK10[0-9a-f]+::[^:]+:(.+)\z/su ? : 8 4 12
288 }
289  
290 # The token in config.yaml, if it holds one as a plain string. Anything else
291 # -- no file, unreadable, not YAML, no or no string token -- is undef: the
292 # caller then takes server/token, as before. Never the parser's message, it
293 # may quote the token.
294 18 ▶sub _configured_token {
Subroutine _configured_token: called CC=8 SCAR=20.9
295 18 my ($dist) = @_;
296 18 my $out = run "cat " . $dist->config_file . " 2>/dev/null", auto_die => 0;
297 18 ▶ return unless $? == 0 && defined $out && length $out;
Branch: 1 branch
BranchTrueFalseTotal
unless $? == 0 and defined $out and length $out 4 14 18
Condition66%4/6
$? == 0 and defined $out and length $out
!ll&&!rl&&r
4014
$? == 0 and defined $out
!ll&&!rl&&r
4014
MC/DC33%1/3
$? == 0 and defined $out and length $out
$? == 0 defined $out length $out
Truth table50%2/4
A and B and C
A= $? == 0B= defined $outC= length $out
ABCresult
0XX0
10X0
1100
1111
298 14 my $config = eval { YAML::PP->new->load_string($out) };
299 14 ▶ return unless ref $config eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq "HASH" 4 10 14
300 10 my $token = $config->{token};
301 10 ▶ return unless defined $token && !ref $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and not ref $token 4 6 10
Condition100%3/3
defined $token and not ref $token
!ll&&!rl&&r
226
MC/DC100%2/2
defined $token and not ref $token
defined $token not ref $token
Truth table100%3/3
A and not(B)
A= defined $tokenB= not ref $token
ABresult
0X0
100
111
302 6 $token =~ s/\s+\z//;
303 6 ▶ return length $token ? $token : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $token ? : 6 0 6
304 }
305  
306 # Read over the exec channel (no SFTP). A missing or unreadable file means
307 # "fresh server" and degrades to undef; it must never abort the install.
308 24 ▶sub _existing_server_token {
Subroutine _existing_server_token: called CC=4 SCAR=13.9
309 24 my ($dist) = @_;
310 24 my $out = run "cat " . $dist->server_token . " 2>/dev/null", auto_die => 0;
311 24 ▶ return unless $? == 0 && defined $out;
Branch: 1 branch
BranchTrueFalseTotal
unless $? == 0 and defined $out 4 20 24
Condition66%2/3
$? == 0 and defined $out
!ll&&!rl&&r
4020
MC/DC50%1/2
$? == 0 and defined $out
$? == 0 defined $out
Truth table66%2/3
A and B
A= $? == 0B= defined $out
ABresult
0X0
100
111
312 20 $out =~ s/\s+\z//;
313 20 ▶ return length $out ? $out : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $out ? : 18 2 20
314 }
315  
316 6 ▶sub _generate_token {
Subroutine _generate_token: called CC=3 SCAR=11.8
317 6 my $token = run "head -c 36 /dev/urandom | base64 | tr -d '\\n/+=' | head -c 48",
318 auto_die => 0;
319 6 chomp $token;
320 6 ▶ die "Failed to generate random token\n" unless $token && length($token) >= 32;
Branch: 1 branch
BranchTrueFalseTotal
unless $token and length $token >= 32 0 6 6
Condition33%1/3
$token and length $token >= 32
!ll&&!rl&&r
006
MC/DC0%0/2
$token and length $token >= 32
$token length $token >= 32
Truth table33%1/3
A and B
A= $tokenB= length $token >= 32
ABresult
0X0
100
111
321 6 Rex::Logger::info("Generated cluster token (auto)");
322 6 return $token;
323 }
324  
325 #
326 # Config file generation
327 #
328  
329 111 ▶sub _build_server_config {
Subroutine _build_server_config: called CC=13 SCAR=25.7
330 111 my ($dist, $token, $server, $tls_san, $node_labels, $cilium,
331 $node_name, $disable, $cluster_cidr, $kubeconfig_mode) = @_;
332  
333 111 my %config = (
334 'token' => $token,
335 );
336  
337 # With cilium, Cilium is the only CNI and replaces kube-proxy on both
338 # distributions (Rex::Rancher::Cilium wires kubeProxyReplacement); which
339 # keys that takes is the distribution's cilium_config.
340 111 ▶ %config = ( %config, %{ $dist->cilium_config } ) if $cilium;
Branch: 1 branch
BranchTrueFalseTotal
if $cilium 103 8 111
341  
342 # A given cluster_cidr is written on both distributions, with or without
343 # cilium; every server of a cluster must carry the same one (RKE2 refuses
344 # a join that differs). Without it rke2 keeps its own default, unwritten.
345 111 ▶ $config{'cluster-cidr'} = $cluster_cidr if defined $cluster_cidr;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cluster_cidr 21 90 111
346  
347 # Packaged components to switch off. Undef means the distribution's
348 # default_disable (rke2: ingress-nginx + traefik charts, unknown chart
349 # names are ignored by RKE2; k3s: traefik + servicelb). An explicit empty
350 # list disables nothing. Independent of cilium.
351 103 ▶ my @disable = !defined $disable ? @{ $dist->default_disable }
Branch: 1 branch
BranchTrueFalseTotal
ref $disable eq 'ARRAY' ? : 6 2 8
352 111 ▶ : ref $disable eq 'ARRAY' ? @{$disable}
Branch: 1 branch
BranchTrueFalseTotal
!defined($disable) ? : 103 8 111
353 : split(/,/, $disable);
354 111 ▶ $config{'disable'} = \@disable if @disable;
Branch: 1 branch
BranchTrueFalseTotal
if @disable 109 2 111
355  
356 111 ▶ $config{server} = $server if $server;
Branch: 1 branch
BranchTrueFalseTotal
if $server 10 101 111
357 111 ▶ $config{'node-name'} = $node_name if $node_name;
Branch: 1 branch
BranchTrueFalseTotal
if $node_name 3 108 111
358  
359 # Only when asked for: unwritten, rke2.yaml / k3s.yaml get the
360 # distribution's own 0600, which the cluster admin's key in them wants. A
361 # string, so that YAML::PP quotes it: an unquoted 0644 is the number 420 to
362 # the distributions' YAML reader.
363 111 ▶ $config{'write-kubeconfig-mode'} = "$kubeconfig_mode" if defined $kubeconfig_mode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $kubeconfig_mode 4 107 111
364  
365 111 ▶ if ($tls_san) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tls_san) 1 110 111
366 1 ▶ my @sans = ref $tls_san eq 'ARRAY' ? @{$tls_san} : split(/,/, $tls_san);
Branch: 1 branch
BranchTrueFalseTotal
ref $tls_san eq 'ARRAY' ? : 1 0 1
367 1 $config{'tls-san'} = \@sans;
368 }
369  
370 111 ▶ if ($node_labels) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node_labels) 2 109 111
371 2 ▶ my @labels = ref $node_labels eq 'ARRAY' ? @{$node_labels} : ($node_labels);
Branch: 1 branch
BranchTrueFalseTotal
ref $node_labels eq 'ARRAY' ? : 2 0 2
372 2 $config{'node-label'} = \@labels;
373 }
374  
375 111 return \%config;
376 }
377  
378 68 ▶sub _write_config {
Subroutine _write_config: called CC=1 SCAR=0.0
379 68 my ($dist, $token, $server, $tls_san, $node_labels, $cilium,
380 $node_name, $disable, $cluster_cidr, $kubeconfig_mode) = @_;
381  
382 68 my $config =
383 _build_server_config($dist, $token, $server, $tls_san, $node_labels, $cilium,
384 $node_name, $disable, $cluster_cidr, $kubeconfig_mode);
385  
386 68 my $config_file = $dist->config_file;
387 68 Rex::Logger::info("Writing config to $config_file");
388  
389 68 $dist->write_secret_file($config_file,
390 YAML::PP->new(boolean => 'JSON::PP')->dump_string($config));
391 }
392  
393 #
394 # Install and start. Server and agent share the steps in
395 # Rex::Rancher::Distribution; what differs between rke2 and k3s is there too.
396 #
397  
398 75 ▶sub _install {
Subroutine _install: called CC=3 SCAR=11.0
399 75 my ($dist, $server, $version, $method, $hold) = @_;
400  
401 # Held, k3s is restarted only for a change: its installer rewrites the
402 # unit and env file on every run, so their content is compared (start_verb).
403 75 ▶ my $units = $hold ? $dist->installer_unit_digest : undef;
Branch: 1 branch
BranchTrueFalseTotal
$hold ? : 25 50 75
404 # No token on any installer line: it is already in config.yaml (written
405 # before the installer runs), and anything on these lines shows up in ps.
406 75 $dist->install_server_package($server, $version, $method);
407 # The binary being there is not enough when a version is pinned: a failed
408 # pinned upgrade leaves the old one in place.
409 75 $dist->verify_installed_version($version);
410 # Before the start decision: this start would render Rex::GPU 0.001's bare
411 # containerd template again (start_verb restarts a running service whose
412 # config.toml is still its output).
413 75 $dist->_remove_bare_template_before_start;
414  
415 # Enable and start the service. --no-block: return immediately; RKE2 first
416 # start pulls many images and exceeds systemctl's default 90s activation
417 # timeout, and k3s' Type=notify unit blocks until k3s is up, forever for
418 # an HA join that cannot reach its first server. Start or restart as the
419 # distribution wants it (start_verb), then the bounded wait.
420 73 my $service = $dist->service;
421 73 run "systemctl enable " . $service, auto_die => 1;
422 73 ▶ run "systemctl " . $dist->start_verb(pinned => ( defined $version && length $version ),
Condition66%2/3
defined $version && length $version
!ll&&!rl&&r
48025
MC/DC50%1/2
defined $version && length $version
defined $version length $version
Truth table66%2/3
A && B
A= defined $versionB= length $version
ABresult
0X0
100
111
423 hold => $hold, unit_digest => $units)
424 . " --no-block " . $service, auto_die => 1;
425  
426 72 $dist->wait_for_service;
427  
428 # Then wait until kubeconfig is written — API readiness is checked locally
429 # by the caller via Rex::Rancher::K8s::wait_for_api after saving the file.
430 72 _wait_for_kubeconfig($dist);
431 }
432  
433 #
434 # Wait until the kubeconfig file appears on the remote host.
435 # API readiness is checked locally by the caller via Rex::Rancher::K8s::wait_for_api.
436 #
437  
438 72 ▶sub _wait_for_kubeconfig {
Subroutine _wait_for_kubeconfig: called CC=5 SCAR=19.3
439 72 my ($dist) = @_;
440 72 my $kubeconfig = $dist->kubeconfig;
441  
442 72 Rex::Logger::info("Waiting for " . $dist->service . " to write kubeconfig...");
443  
444 72 for my $i (1..60) {
445 72 my $out = run "test -f $kubeconfig && echo yes", auto_die => 0;
446 72 ▶ if ($? == 0 && ($out // '') =~ /yes/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0 and ($out // "") =~ /yes/u) 72 0 72
Condition40%2/5
$? == 0 and ($out // "") =~ /yes/u
!ll&&!rl&&r
0072
$out // ""
l!l
720
MC/DC0%0/2
$? == 0 and ($out // "") =~ /yes/u
$? == 0 $out
Truth table33%1/3
A and B
A= $? == 0B= $out
ABresult
0X0
100
111
447 72 Rex::Logger::info(" Kubeconfig ready at $kubeconfig");
448 72 return 1;
449 }
450 0 Rex::Logger::info(" Not ready yet ($i/60), waiting...");
451 0 sleep 5;
452 }
453  
454 0 Rex::Logger::info($dist->service . " kubeconfig did not appear — check manually", "warn");
455 0 return 0;
456 }
457  
458 56 1;
459  
460 __END__
« lib/Rex/Rancher/Distribution.pm ↑ Summary blib/lib/Rex/Rancher/Node.pm »