lib/Rex/Rancher/Agent.pm

filter: Statementstmt 97.1% 103 / 106 Branchbran 92.8% 26 / 28 Conditioncond 70.0% 7 / 10 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 20 / 20 Podpod 100% 2 / 2 totaltotal 93.6% 161 / 172 CC 20 SCAR 12.6
CC
20
Cov
94%
install_agent
7.0
_run_installer
5.5
_control_plane_version
5.0
CRAP 3.5
« lib/Rex/Rancher/Options.pm ↑ Summary lib/Rex/Rancher/Checksum.pm »
1 # ABSTRACT: Rancher Kubernetes agent (worker node) installation
2  
3 package Rex::Rancher::Agent;
4 49 our $VERSION = '0.004';
5 49 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 49 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 49 ▶use Rex::Commands::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 49 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 49 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 49 ▶use Rex::Rancher::Distribution;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 49 ▶use Rex::Rancher::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 49 ▶use Rex::Rancher::K8s ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 49 ▶use Rex::Rancher::Uninstall ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 49 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 49 require Rex::Exporter;
18 49 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 49 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 49 @EXPORT = qw(
23 install_agent
24 );
25  
26  
27 69 ▶sub install_agent {
Subroutine install_agent: called CC=7 SCAR=19.5
Pod documented
28 69 my (%opts) = @_;
29  
30 69 ▶ my $distribution = $opts{distribution} // 'rke2';
Condition100%2/2
$opts{"distribution"} // "rke2"
l!l
5910
MC/DC100%1/1
$opts{"distribution"} // "rke2"
$opts{"distribution"}
Truth table100%2/2
A
A= $opts{"distribution"}
Aresult
11
00
31 69 ▶ my $server = $opts{server} or die "server is required for install_agent\n";
Branch: 1 branch
BranchTrueFalseTotal
my $server = $opts{"server"} or die "server is required for install_agent\n" 68 1 69
32 68 ▶ my $token = $opts{token} or die "token is required for install_agent\n";
Branch: 1 branch
BranchTrueFalseTotal
my $token = $opts{"token"} or die "token is required for install_agent\n" 67 1 68
33 67 my $node_name = $opts{node_name};
34  
35 # Everything that may refuse, before anything is written or installed;
36 # rancher_deploy_agent asked the same before it prepared the node (k78).
37 67 my $checked = preflight_agent(%opts);
38 55 my $method = $checked->{install_method};
39 55 my $version = $checked->{version};
40 55 my $server_version = $checked->{server_version};
41 55 my $hold = $opts{hold_running};
42  
43 55 my $dist = Rex::Rancher::Distribution->new_for($distribution, role => 'agent');
44  
45 55 Rex::Logger::info("Installing $distribution agent to join $server");
46  
47 55 _write_config($dist, %opts);
48 55 _write_registries($dist, %opts);
49 # Before the installer and the first start, as on the server.
50 55 ▶ $dist->ensure_nvidia_runtime_path if $opts{nvidia_runtime_path};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"nvidia_runtime_path"} 2 53 55
51 # Held, k3s is restarted only for a change, its unit's content included.
52 55 ▶ my $units = $hold ? $dist->installer_unit_digest : undef;
Branch: 1 branch
BranchTrueFalseTotal
$hold ? : 24 31 55
53 55 _run_installer($dist, $version, $server, $method);
54 55 $dist->verify_installed_version($version);
55 # Again with what was installed: the check above had to go without it
56 # when the channel did not resolve.
57 55 ▶ $dist->check_agent_version($dist->installed_version, $server_version, 1)
Branch: 1 branch
BranchTrueFalseTotal
if defined $server_version 4 51 55
58 if defined $server_version;
59 54 _enable_service($dist, $server, $version, $hold, $units);
60  
61 50 Rex::Logger::info("$distribution agent installed and running");
62 }
63  
64  
65 90 ▶sub preflight_agent {
Subroutine preflight_agent: called CC=2 SCAR=6.9
Pod documented
66 90 my (%opts) = @_;
67 my %checked = (
68 90 install_method => Rex::Rancher::Options->resolve_install_method($opts{install_method}, $opts{version}),
69 );
70 87 my $dist = Rex::Rancher::Distribution->new_for($opts{distribution}, role => 'agent');
71  
72 # An agent never goes to a newer minor than the control plane, nor skips
73 # or goes back a minor itself.
74 86 $checked{server_version} = _control_plane_version($opts{kubeconfig});
75 # Nor onto Cilium state an earlier cluster left on a host without RKE2/K3s,
76 # which would hang every image pull of this agent (k71). The first read of
77 # the host; the control plane above is asked through the API.
78 85 Rex::Rancher::Uninstall->check_cilium_residue;
79 # hold_running: the agent's own version on the host is this run's version,
80 # still checked against the control plane (k77).
81 $checked{version} = $opts{hold_running}
82 81 ▶ ? $dist->held_version(version => $opts{version}) : $opts{version};
Branch: 1 branch
BranchTrueFalseTotal
$opts{'hold_running'} ? : 32 49 81
83 $dist->check_version_skew(version => $checked{version},
84 81 server_version => $checked{server_version});
85 68 return \%checked;
86 }
87  
88 # The control plane's version through the given kubeconfig, or nothing
89 # without one. Asked for with a kubeconfig, so it has to answer: an API
90 # error or no version dies before the host is touched.
91 86 ▶sub _control_plane_version {
Subroutine _control_plane_version: called CC=5 SCAR=16.2
92 86 my ($kubeconfig) = @_;
93 86 ▶ return unless defined $kubeconfig && length $kubeconfig;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kubeconfig and length $kubeconfig 72 14 86
Condition66%2/3
defined $kubeconfig and length $kubeconfig
!ll&&!rl&&r
72014
MC/DC50%1/2
defined $kubeconfig and length $kubeconfig
defined $kubeconfig length $kubeconfig
Truth table66%2/3
A and B
A= defined $kubeconfigB= length $kubeconfig
ABresult
0X0
100
111
94 14 my $version = eval { Rex::Rancher::K8s::control_plane_version(kubeconfig => $kubeconfig) };
95 14 ▶ die "Could not read the control plane's version through $kubeconfig ("
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 13 14
96 . ( $@ =~ s/\s+\z//r ) . "); nothing was installed\n" if $@;
97 13 ▶ die "The API behind $kubeconfig reports no control plane version; nothing "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 0 13 13
98 . "was installed. Omit kubeconfig to join without the version check\n"
99 unless defined $version;
100 13 Rex::Logger::info("Control plane runs $version");
101 13 return $version;
102 }
103  
104 # Same keys on rke2 and k3s agents; node-label as in the server's
105 # config.yaml (Rex::Rancher::Server).
106 63 ▶sub _build_agent_config {
Subroutine _build_agent_config: called CC=4 SCAR=13.9
107 63 my (%opts) = @_;
108  
109 my %config = (
110 server => $opts{server},
111 token => $opts{token},
112 63 );
113 63 ▶ $config{'node-name'} = $opts{node_name} if $opts{node_name};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"node_name"} 2 61 63
114  
115 63 ▶ if (my $node_labels = $opts{node_labels}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node_labels = $opts{"node_labels"}) 4 59 63
116 4 ▶ my @labels = ref $node_labels eq 'ARRAY' ? @{$node_labels} : ($node_labels);
Branch: 1 branch
BranchTrueFalseTotal
ref $node_labels eq 'ARRAY' ? : 3 1 4
117 4 $config{'node-label'} = \@labels;
118 }
119  
120 63 return \%config;
121 }
122  
123 57 ▶sub _write_config {
Subroutine _write_config: called CC=1 SCAR=0.0
124 57 my ($dist, %opts) = @_;
125  
126 57 Rex::Logger::info("Writing " . $dist->name . " agent config");
127  
128 57 run "mkdir -p " . $dist->config_dir, auto_die => 1;
129  
130 57 $dist->write_secret_file($dist->config_file,
131 YAML::PP->new->dump_string(_build_agent_config(%opts)));
132 }
133  
134 57 ▶sub _write_registries {
Subroutine _write_registries: called CC=2 SCAR=6.9
135 57 my ($dist, %opts) = @_;
136  
137 57 ▶ return unless $opts{registries};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"registries"} 55 2 57
138  
139 2 $dist->write_registries($opts{registries});
140 }
141  
142 # The token is NOT on any installer line, for either distribution:
143 # _write_config has already put it into config.yaml, and anything on these
144 # lines shows up in ps. k3s' script does not start the agent
145 # (INSTALL_K3S_SKIP_START); _enable_service starts it bounded.
146 55 ▶sub _run_installer {
Subroutine _run_installer: called CC=4 SCAR=17.1
147 55 my ($dist, $version, $server, $method) = @_;
148  
149 55 Rex::Logger::info("Running " . $dist->name . " agent installer");
150  
151 55 ▶ if (($method // 'script') eq 'artifact') {
Branch: 1 branch
BranchTrueFalseTotal
if (($method // "script") eq "artifact") 0 55 55
Condition50%1/2
$method // "script"
l!l
550
MC/DC0%0/1
$method // "script"
$method
Truth table50%1/2
A
A= $method
Aresult
11
00
152 0 my $spec = $dist->fetch_artifacts($version);
153 0 run $_, auto_die => 1 for $dist->artifact_install_cmds($spec, $server, $version);
154 0 return;
155 }
156  
157 55 run $dist->script_install_cmd($server, $version), auto_die => 1;
158 }
159  
160 57 ▶sub _enable_service {
Subroutine _enable_service: called CC=2 SCAR=6.9
161 57 my ($dist, $server, $version, $hold, $units) = @_;
162  
163 57 my $service = $dist->service;
164 # Before the start decision, as on the server: this start would render
165 # Rex::GPU 0.001's bare containerd template again.
166 57 $dist->_remove_bare_template_before_start;
167 # k3s: restart, as the install script did before INSTALL_K3S_SKIP_START,
168 # so a re-run still picks up a new binary and config.yaml. rke2's
169 # installer never started the agent: start, or restart for a stale
170 # containerd config or a change since it started (see
171 # Rex::Rancher::Distribution's start_verb). Held (hold_running), k3s is
172 # started like rke2, and restarted also for a rewritten unit.
173 55 ▶ my $verb = $dist->start_verb(pinned => ( defined $version && length $version ),
Condition66%2/3
defined $version && length $version
!ll&&!rl&&r
32023
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
174 hold => $hold, unit_digest => $units);
175 55 Rex::Logger::info("Enabling and starting $service");
176 55 run "systemctl enable $service", auto_die => 1;
177 # --no-block, same as the server: a start that fails or outlasts systemd's
178 # activation timeout ends in wait_for_service, which reports the journal,
179 # instead of a bare systemctl error (or, for an agent that cannot reach
180 # its server, a Type=notify start that never returns).
181 55 run "systemctl $verb --no-block $service", auto_die => 1;
182 # An unreachable server address (a wrong or private IP) is the usual
183 # reason an agent never gets active: name it next to the journal.
184 55 $dist->wait_for_service(
185 hint => "It joins the cluster via $server -- check that this node can reach that address");
186 }
187  
188 49 1;
189  
190 __END__
« lib/Rex/Rancher/Options.pm ↑ Summary lib/Rex/Rancher/Checksum.pm »