lib/Rex/Rancher/Distribution/K3s.pm

filter: Statementstmt 100% 67 / 67 Branchbran 100% 12 / 12 Conditioncond 66.6% 4 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 35 / 35 Podpod 100% 30 / 30 totaltotal 96.1% 148 / 154 CC 9 SCAR 4.5
CC
9
Cov
98%
cilium_helm_defaults
4.1
script_install_cmd
3.0
installer_unit_files
2.0
CRAP 1.6
« lib/Rex/Rancher/Uninstall.pm ↑ Summary lib/Rex/Rancher/Distribution/RKE2.pm »
1 # ABSTRACT: K3s: paths, services and installer
2  
3 package Rex::Rancher::Distribution::K3s;
4 30 our $VERSION = '0.004';
5 30 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use Rex::Commands::Run ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 30 extends 'Rex::Rancher::Distribution';
12  
13 218 ▶sub name { 'k3s' }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
14 105 ▶sub label { 'K3s' }
Subroutine label: called CC=1 SCAR=0.0
Pod documented
15 291 ▶sub config_dir { '/etc/rancher/k3s' }
Subroutine config_dir: called CC=1 SCAR=0.0
Pod documented
16 82 ▶sub install_url { 'https://get.k3s.io' }
Subroutine install_url: called CC=1 SCAR=0.0
Pod documented
17 34 ▶sub channel_url { 'https://update.k3s.io/v1-release/channels/stable' }
Subroutine channel_url: called CC=1 SCAR=0.0
Pod documented
18 39 ▶sub kubeconfig { '/etc/rancher/k3s/k3s.yaml' }
Subroutine kubeconfig: called CC=1 SCAR=0.0
Pod documented
19 1 ▶sub token_file { '/var/lib/rancher/k3s/server/node-token' }
Subroutine token_file: called CC=1 SCAR=0.0
Pod documented
20 34 ▶sub server_token { '/var/lib/rancher/k3s/server/token' }
Subroutine server_token: called CC=1 SCAR=0.0
Pod documented
21 445 ▶sub binary { 'k3s' }
Subroutine binary: called CC=1 SCAR=0.0
Pod documented
22 7 ▶sub release_url { 'https://github.com/k3s-io/k3s/releases/download' }
Subroutine release_url: called CC=1 SCAR=0.0
Pod documented
23 7 ▶sub artifact_dir { '/tmp/k3s-artifacts' }
Subroutine artifact_dir: called CC=1 SCAR=0.0
Pod documented
24 167 ▶sub containerd_dir { '/var/lib/rancher/k3s/agent/etc/containerd' }
Subroutine containerd_dir: called CC=1 SCAR=0.0
Pod documented
25 668 ▶sub server_service { 'k3s' }
Subroutine server_service: called CC=1 SCAR=0.0
Pod documented
26 453 ▶sub agent_service { 'k3s-agent.service' }
Subroutine agent_service: called CC=1 SCAR=0.0
Pod documented
27 # A re-run picks up a new binary and config.yaml, as the install script's own
28 # restart did before INSTALL_K3S_SKIP_START. Not narrowed to restart_reasons
29 # like rke2: the script rewrites k3s.service and k3s.service.env on every run
30 # (so their mtime alone would say "changed" each time), and the unit's
31 # arguments come from this run's installer line.
32 # hold_running narrows it (start_verb): restart_reasons plus the content of
33 # those two files before and after the installer, which is what the install
34 # script itself compares (with the binary's) before its own restart.
35 50 ▶sub default_start_verb { 'restart' }
Subroutine default_start_verb: called CC=1 SCAR=0.0
Pod documented
36 50 ▶sub live_verified { 1 }
Subroutine live_verified: called CC=1 SCAR=0.0
Pod documented
37  
38 # Where get.k3s.io puts them with systemd (SYSTEMD_DIR default, SYSTEM_NAME
39 # k3s / k3s-agent).
40 50 ▶sub installer_unit_files {
Subroutine installer_unit_files: called CC=2 SCAR=6.9
Pod documented
41 50 my ( $self ) = @_;
42 50 ▶ my $unit = '/etc/systemd/system/' . ( $self->is_agent ? 'k3s-agent' : 'k3s' ) . '.service';
Branch: 1 branch
BranchTrueFalseTotal
$self->is_agent ? : 25 25 50
43 50 return ( $unit, "$unit.env" );
44 }
45  
46 # Named after the service the install script set up: k3s-uninstall.sh on a
47 # server, k3s-agent-uninstall.sh on an agent (a worker has only the latter).
48 27 ▶sub uninstall_scripts { qw( k3s-uninstall.sh k3s-agent-uninstall.sh ) }
Subroutine uninstall_scripts: called CC=1 SCAR=0.0
Pod documented
49  
50 # k3s agents serve the API on 127.0.0.1:6444, not 6443: Cilium's kube-proxy
51 # replacement needs the control plane's address.
52 126 ▶sub needs_k8s_service_host { 1 }
Subroutine needs_k8s_service_host: called CC=1 SCAR=0.0
Pod documented
53  
54 # No packaged Gateway API CRDs to keep out of the way.
55 3 ▶sub gateway_api_crd_chart { undef }
Subroutine gateway_api_crd_chart: called CC=1 SCAR=0.0
Pod documented
56  
57 # Formerly --disable flags on the installer line; config.yaml carries the same
58 # and keeps caller-supplied names out of the shell.
59 54 ▶sub default_disable { [ 'traefik', 'servicelb' ] }
Subroutine default_disable: called CC=1 SCAR=0.0
Pod documented
60  
61 # k3s' built-in default, written out with cilium because Cilium's
62 # cluster-pool IPAM has to hand out the same range (Rex::Rancher::Cilium).
63 123 ▶sub default_cluster_cidr { $_[0]->builtin_cluster_cidr }
Subroutine default_cluster_cidr: called CC=1 SCAR=0.0
Pod documented
64  
65 # None: k3s' agent code finds a host NVIDIA toolkit and wires it plus the
66 # nvidia RuntimeClass without help (kubernetes-ocp).
67 79 ▶sub env_file { undef }
Subroutine env_file: called CC=1 SCAR=0.0
Pod documented
68  
69 8 ▶sub asset_name {
Subroutine asset_name: called CC=2 SCAR=6.9
Pod documented
70 8 my ( $self, $arch ) = @_;
71 8 ▶ return $arch eq 'amd64' ? 'k3s' : "k3s-$arch";
Branch: 1 branch
BranchTrueFalseTotal
$arch eq 'amd64' ? : 4 4 8
72 }
73  
74 # Flannel, the embedded network policy controller and kube-proxy go, Cilium
75 # takes over all three; cluster-cidr is stated so Cilium's cluster-pool gets
76 # the same range (as kubernetes-ocp k178, verified live there). All
77 # server-side; k3s agents take them from the server, and every server of a
78 # cluster must carry the same.
79 76 ▶sub cilium_config {
Subroutine cilium_config: called CC=1 SCAR=0.0
Pod documented
80 76 my ( $self ) = @_;
81 return {
82 76 'flannel-backend' => 'none',
83 'disable-network-policy' => JSON()->true,
84 'disable-kube-proxy' => JSON()->true,
85 'cluster-cidr' => $self->default_cluster_cidr,
86 };
87 }
88  
89 # Cilium cuts the node ranges from its own pool, as kubernetes-ocp k178.
90 50 ▶sub default_ipam_mode { 'cluster-pool' }
Subroutine default_ipam_mode: called CC=1 SCAR=0.0
Pod documented
91  
92 # The control plane address (validated in Rex::Rancher::Cilium), and
93 # Cilium's own pool on k3s' cluster-cidr, as kubernetes-ocp k178; the mode
94 # is ipam_mode's where one is given.
95 53 ▶sub cilium_helm_defaults {
Subroutine cilium_helm_defaults: called CC=4 SCAR=14.2
Pod documented
96 53 my ( $self, %args ) = @_;
97 return {
98 cni => { exclusive => JSON()->true },
99 ( defined $args{k8s_service_host} ? ( k8sServiceHost => $args{k8s_service_host} ) : () ),
100 ipam => {
101 mode => $args{ipam_mode} // $self->default_ipam_mode,
102 53 ▶ operator => { clusterPoolIPv4PodCIDRList => [ $args{cluster_cidr} // $self->default_cluster_cidr ] },
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'k8s_service_host'} ? : 41 12 53
Condition66%4/6
$args{"ipam_mode"} // $self->default_ipam_mode
l!l&&r!l&&!r
4490
$args{"cluster_cidr"} // $self->default_cluster_cidr
l!l&&r!l&&!r
8450
MC/DC0%0/2
$args{"ipam_mode"} // $self->default_ipam_mode
$args{"ipam_mode"} $self->default_ipam_mode
MC/DC0%0/2
$args{"cluster_cidr"} // $self->default_cluster_cidr
$args{"cluster_cidr"} $self->default_cluster_cidr
Truth table66%2/3
A // B
A= $args{"ipam_mode"}B= $self->default_ipam_mode
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $args{"cluster_cidr"}B= $self->default_cluster_cidr
ABresult
1X1
011
000
103 },
104 };
105 }
106  
107 # INSTALL_K3S_SKIP_START, server and agent: the script's own `systemctl
108 # restart` of the Type=notify unit blocks until k3s is up, forever for a join
109 # that cannot reach its server; the caller starts it --no-block and waits
110 # bounded. No K3S_TOKEN: the token is in config.yaml, and anything on this
111 # line shows up in ps. traefik/servicelb are disabled in config.yaml too.
112 # No --write-kubeconfig-mode either: k3s.yaml holds the cluster admin's key
113 # and stays at k3s' own 0600, as rke2.yaml does; another mode is the server's
114 # kubeconfig_mode, written to config.yaml on both distributions.
115 75 ▶sub script_install_cmd {
Subroutine script_install_cmd: called CC=3 SCAR=11.0
Pod documented
116 75 my ( $self, $server, $version ) = @_;
117 75 my @env;
118 75 ▶ push @env, "K3S_URL=$server" if $server;
Branch: 1 branch
BranchTrueFalseTotal
if $server 41 34 75
119 75 ▶ push @env, "INSTALL_K3S_VERSION=$version" if $version;
Branch: 1 branch
BranchTrueFalseTotal
if $version 31 44 75
120 75 push @env, 'INSTALL_K3S_SKIP_START=true';
121 75 my $env_str = join('', map { "$_ " } @env);
122 75 return "curl -sfL " . $self->install_url . " | ${env_str}sh -s - " . $self->role;
123 }
124  
125 # Put the verified binary where the K3s install script looks for it: next to
126 # it, then rename, so a running k3s ("text file busy") is replaced atomically.
127 # SKIP_DOWNLOAD=binary skips only the binary; the SELinux RPM on RHEL-likes is
128 # still fetched, as with curl | sh. BIN_DIR pinned to where we put the binary.
129 6 ▶sub artifact_install_cmds {
Subroutine artifact_install_cmds: called CC=2 SCAR=6.9
Pod documented
130 6 my ( $self, $spec, $server, $version ) = @_;
131 6 my $role = $self->role;
132 6 my $place = "install -m 0755 -o root -g root '$spec->{dir}/$spec->{asset}' /usr/local/bin/.k3s.rex-new"
133 . " && mv -f /usr/local/bin/.k3s.rex-new /usr/local/bin/k3s";
134 6 my @env;
135 6 ▶ push @env, "K3S_URL=$server" if $server;
Branch: 1 branch
BranchTrueFalseTotal
if $server 3 3 6
136 6 push @env, 'INSTALL_K3S_SKIP_DOWNLOAD=binary', 'INSTALL_K3S_BIN_DIR=/usr/local/bin',
137 "INSTALL_K3S_VERSION=$version";
138 6 push @env, 'INSTALL_K3S_SKIP_START=true';
139 6 return ( $place, join(' ', @env) . " sh $spec->{script} $role" );
140 }
141  
142 36 ▶sub run_server_install_script {
Subroutine run_server_install_script: called CC=1 SCAR=0.0
Pod documented
143 36 my ( $self, $server, $version ) = @_;
144 36 Rex::Commands::Run::run($self->script_install_cmd($server, $version), auto_die => 1);
145 }
146  
147 30 1;
148  
149 __END__
« lib/Rex/Rancher/Uninstall.pm ↑ Summary lib/Rex/Rancher/Distribution/RKE2.pm »