lib/Rex/Rancher/Cilium.pm

filter: Statementstmt 96.8% 523 / 540 Branchbran 88.7% 284 / 320 Conditioncond 74.0% 225 / 304 MC/DCmcdc 53.7% 86 / 160 Subroutinesub 97.4% 77 / 79 Podpod 100% 4 / 4 totaltotal 85.2% 1199 / 1407 CC 293 SCAR 26.8
CC
293
Cov
89%
_readiness
48.5
_adopt_running
34.6
_resolve_opts
24.0
CRAP 14.6
« lib/Rex/Rancher/K8s.pm ↑ Summary lib/Rex/Rancher/Distribution.pm »
1 # ABSTRACT: Cilium CNI installation for Rancher Kubernetes distributions
2  
3 package Rex::Rancher::Cilium;
4 42 our $VERSION = '0.004';
5 42 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 42 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 42 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 42 ▶use IO::Uncompress::Gunzip qw( gunzip $GunzipError );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 42 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 42 ▶use Kubernetes::REST::Kubeconfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 42 ▶use MIME::Base64 qw( decode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 42 ▶use POSIX qw( strftime );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 42 ▶use Rex::Commands::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 42 ▶use Rex::Commands::Gather;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 42 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 42 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 42 ▶use Rex::Rancher::Checksum;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 42 ▶use Rex::Rancher::Distribution;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 42 ▶use Rex::Rancher::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 42 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 42 require Rex::Exporter;
24 42 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 42 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 42 @EXPORT = qw(
29 install_cilium
30 upgrade_cilium
31 ensure_gateway_api_crds
32 );
33  
34 # The pair kubernetes-ocp runs live (OCP::Versions). Cilium 1.20 is e2e
35 # tested on Kubernetes 1.33-1.36, the CLI supports Cilium 1.16 and newer.
36 42 ▶use constant CILIUM_VERSION => '1.20.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 42 ▶use constant CILIUM_CLI_VERSION => 'v0.19.7';
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # Cilium 1.20 requires TLSRoute and BackendTLSPolicy at v1, which the
40 # Gateway API bundles carry from this version.
41 42 ▶use constant GATEWAY_API_MIN_FOR_1_20 => 'v1.6.1';
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # The cilium CLI installs a Helm release of this name into this namespace;
44 # Helm keeps one Secret per revision there (labels owner=helm,name=cilium).
45 42 ▶use constant RELEASE_NAME => 'cilium';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 42 ▶use constant RELEASE_NAMESPACE => 'kube-system';
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 # Present in both Gateway API channels; its annotations name the bundle
49 # version and channel that were last applied.
50 42 ▶use constant GATEWAY_API_PROBE_CRD => 'gateways.gateway.networking.k8s.io';
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # RKE2 v1.37+ ships the Gateway API CRDs as its own Helm chart; the release
53 # lives in RELEASE_NAMESPACE like Cilium's.
54 42 ▶use constant RKE2_GATEWAY_API_RELEASE => 'rke2-gateway-api-crd';
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 # Cilium's running configuration: the agent reads it from this ConfigMap
57 # (the chart renders ipam.mode as "ipam" and the pool list, space-separated,
58 # as "cluster-pool-ipv4-cidr"), whatever the Helm release values say.
59 42 ▶use constant CILIUM_CONFIGMAP => 'cilium-config';
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 # Default for wait: how long install_cilium/upgrade_cilium wait for the
62 # cilium DaemonSet and the cilium-operator to be ready, and the poll step.
63 42 ▶use constant WAIT_DURATION => 600;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 42 ▶use constant WAIT_INTERVAL => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 # Addresses that name the node itself. k3s agents serve the API on
67 # 127.0.0.1:6444, not 6443, so on k3s k8sServiceHost must not be one of these.
68 42 my %LOOPBACK = map { $_ => 1 } qw( 127.0.0.1 localhost ::1 );
69  
70  
71  
72 44 ▶sub install_cilium {
Subroutine install_cilium: called CC=14 SCAR=26.5
Pod documented
73 44 my (%opts) = @_;
74 44 my $o = _resolve_opts(%opts);
75  
76 40 Rex::Logger::info("Installing Cilium $o->{version} on " . $o->{dist}->name . " cluster");
77  
78 40 ▶ my $api = $o->{kubeconfig} ? _api($o->{kubeconfig}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$o->{'kubeconfig'} ? : 38 2 40
79 40 ▶ my $release = $api ? _read_release($api) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$api ? : 38 2 40
80 40 _refuse_stuck_release($release);
81  
82 # A Cilium on the cluster keeps its IPAM mode, pool, k8sServiceHost and
83 # operator replicas unless the caller asked for them -- whatever the
84 # release state: a failed or pending install can have left cilium-config
85 # and pods behind, and the ConfigMap is what those pods run with. Read and
86 # settled before the host is touched, so a refusal leaves it as it was.
87 33 ▶ if ($api) {
Branch: 1 branch
BranchTrueFalseTotal
if ($api) 31 2 33
88 31 my $running = _read_running($api, $release);
89 30 _adopt_running($o, $running);
90 28 _settle_version($o, $running->{version});
91 }
92 29 _require_k8s_service_host($o);
93 29 _check_gateway_api_version($o);
94  
95 28 _install_cilium_cli($o->{cli_version});
96  
97 my $crds_applied = $o->{gateway_api}
98 ? _ensure_gateway_api_crds($api, $o->{gateway_api_version}, $o->{gateway_api_channel})
99 28 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
$o->{'gateway_api'} ? : 2 26 28
100  
101 28 my $values_file = _write_helm_values($o);
102  
103 28 ▶ unless ($api) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($api) 2 26 28
104 2 _install_unchecked($o, $values_file);
105 1 return;
106 }
107  
108 26 my $action = _release_action($release, $o->{version}, $o->{values});
109  
110 25 ▶ if ($action eq 'noop') {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'noop') { } 2 23 25
elsif ($action eq 'upgrade') { } 7 16 23
111 2 Rex::Logger::info(" Cilium $o->{version} already deployed with the requested values, nothing to do");
112 }
113 elsif ($action eq 'upgrade') {
114 Rex::Logger::info(" Helm release $release->{status} at "
115 7 ▶ . ($release->{chart_version} // 'unknown version') . ", upgrading");
Condition50%1/2
$release->{"chart_version"} // "unknown version"
l!l
70
MC/DC0%0/1
$release->{"chart_version"} // "unknown version"
$release->{"chart_version"}
Truth table50%1/2
A
A= $release->{"chart_version"}
Aresult
11
00
116 7 _run_cilium(_cilium_command('upgrade', $o, $values_file), 'upgrade');
117 }
118 else {
119 16 ▶ _purge_release($api, $o, $release) if $action eq 'reinstall';
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "reinstall" 8 8 16
120 15 _run_cilium(_cilium_command('install', $o, $values_file), 'install');
121 14 _verify_daemonset($api);
122 }
123  
124 22 ▶ _restart_operator($api)
Branch: 1 branch
BranchTrueFalseTotal
if $crds_applied and $action eq "noop" || $action eq "upgrade" 0 22 22
Condition50%3/6
$crds_applied and $action eq "noop" || $action eq "upgrade"
!ll&&!rl&&r
2020
$action eq "noop" or $action eq "upgrade"
l!l&&r!l&&!r
002
MC/DC0%0/3
$crds_applied and $action eq "noop" || $action eq "upgrade"
$crds_applied $action eq "noop" $action eq "upgrade"
Truth table50%2/4
A and B or C
A= $crds_appliedB= $action eq "noop"C= $action eq "upgrade"
ABCresult
0XX0
1000
11X1
1011
125 if $crds_applied && ($action eq 'noop' || $action eq 'upgrade');
126  
127 22 ▶ _wait_ready($api, $o->{wait_duration}) if $o->{wait};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"wait"} 2 20 22
128  
129 21 Rex::Logger::info("Cilium $o->{version} ready on " . $o->{dist}->name . " cluster");
130 }
131  
132  
133 35 ▶sub upgrade_cilium {
Subroutine upgrade_cilium: called CC=5 SCAR=16.1
Pod documented
134 35 my (%opts) = @_;
135  
136 # First, so no other option error hides it and nothing reaches the host:
137 # an upgrade applies our values over a running Cilium, and only the API
138 # tells which IPAM mode and pool it runs.
139 die "upgrade_cilium needs kubeconfig (a local kubeconfig the API answers "
140 . "through): without it the running IPAM mode and pool cannot be checked, "
141 . "and an upgrade that switches a cluster-pool Cilium to kubernetes IPAM "
142 . "(the rke2 default) costs every pod its address. Pass the kubeconfig "
143 35 ▶ . "rancher_deploy_server saved (kubeconfig_file)\n" unless $opts{kubeconfig};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"kubeconfig"} 4 31 35
144  
145 31 my $o = _resolve_opts(%opts);
146  
147 31 Rex::Logger::info("Upgrading Cilium to $o->{version} on " . $o->{dist}->name . " cluster");
148  
149 31 my $api = _api($o->{kubeconfig});
150 31 my $release = _read_release($api);
151 31 _require_deployed_release($release);
152 10 my $running = _read_running($api, $release);
153 9 _adopt_running($o, $running);
154 9 _settle_version($o, $running->{version});
155 8 _require_k8s_service_host($o);
156 7 _check_gateway_api_version($o);
157  
158 7 _install_cilium_cli($o->{cli_version});
159  
160 my $crds_applied = $o->{gateway_api}
161 ? _ensure_gateway_api_crds($api, $o->{gateway_api_version}, $o->{gateway_api_channel})
162 7 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
$o->{'gateway_api'} ? : 3 4 7
163  
164 7 my $values_file = _write_helm_values($o);
165 7 _run_cilium(_cilium_command('upgrade', $o, $values_file), 'upgrade');
166  
167 7 ▶ _restart_operator($api) if $crds_applied;
Branch: 1 branch
BranchTrueFalseTotal
if $crds_applied 3 4 7
168  
169 7 ▶ _wait_ready($api, $o->{wait_duration}) if $o->{wait};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"wait"} 1 6 7
170  
171 6 Rex::Logger::info("Cilium upgraded to $o->{version} on " . $o->{dist}->name . " cluster");
172 }
173  
174  
175 5 ▶sub ensure_gateway_api_crds {
Subroutine ensure_gateway_api_crds: called CC=4 SCAR=13.9
Pod documented
176 5 my (%opts) = @_;
177  
178 die "ensure_gateway_api_crds needs kubeconfig (a local kubeconfig the API "
179 5 ▶ . "answers through)\n" unless $opts{kubeconfig};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"kubeconfig"} 1 4 5
180 die "ensure_gateway_api_crds needs version (e.g. v1.2.0), matching what "
181 4 ▶ . "Cilium supports\n" unless $opts{version};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"version"} 1 3 4
182 3 my $channel = _gateway_api_channel($opts{channel});
183  
184 2 my $api = _api($opts{kubeconfig});
185 2 my $applied = _ensure_gateway_api_crds($api, $opts{version}, $channel);
186 2 ▶ _restart_operator($api) if $applied;
Branch: 1 branch
BranchTrueFalseTotal
if $applied 1 1 2
187 2 return $applied;
188 }
189  
190  
191 40 ▶sub validate_cilium_opts {
Subroutine validate_cilium_opts: called CC=1 SCAR=0.0
Pod documented
192 40 my (%opts) = @_;
193 40 _require_k8s_service_host(_resolve_opts(%opts));
194 36 return 1;
195 }
196  
197 #
198 # Option resolution (pure — dies before anything touches the host)
199 #
200  
201 193 ▶sub _resolve_opts {
Subroutine _resolve_opts: called CC=24 SCAR=31.8
202 193 my (%opts) = @_;
203  
204 193 my $dist = Rex::Rancher::Distribution->new_for($opts{distribution});
205 192 ▶ my $helm_values = $opts{helm_values} // {};
Condition100%2/2
$opts{"helm_values"} // {}
l!l
25167
MC/DC100%1/1
$opts{"helm_values"} // {}
$opts{"helm_values"}
Truth table100%2/2
A
A= $opts{"helm_values"}
Aresult
11
00
206 192 ▶ my $gateway_api = $opts{gateway_api} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'gateway_api'} ? : 47 145 192
207 192 ▶ my $wait = $opts{wait} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'wait'} ? : 7 185 192
208 192 ▶ my $duration = $opts{wait_duration} // WAIT_DURATION;
Condition100%2/2
$opts{"wait_duration"} // 600
l!l
4188
MC/DC100%1/1
$opts{"wait_duration"} // 600
$opts{"wait_duration"}
Truth table100%2/2
A
A= $opts{"wait_duration"}
Aresult
11
00
209 192 my $cluster_cidr = Rex::Rancher::Options->check_cluster_cidr($opts{cluster_cidr});
210 191 my $ipam_mode = Rex::Rancher::Options->check_ipam_mode($opts{ipam_mode});
211  
212 186 ▶ die "helm_values must be a hashref\n" unless ref $helm_values eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $helm_values eq "HASH" 1 185 186
213  
214 # Two answers to one question: helm_values would win the merge and make
215 # ipam_mode a silent no-op.
216 185 ▶ if (defined $ipam_mode && ref $helm_values->{ipam} eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ipam_mode and ref $helm_values->{"ipam"} eq "HASH" and defined $helm_values->{"ipam"}{"mode"} and $helm_values->{"ipam"}{"mode"} ne $ipam_mode) 1 184 185
Condition88%8/9
defined $ipam_mode and ref $helm_values->{"ipam"} eq "HASH" and defined $helm_values->{"ipam"}{"mode"} and $helm_values->{"ipam"}{"mode"} ne $ipam_mode
!ll&&!rl&&r
18311
defined $ipam_mode and ref $helm_values->{"ipam"} eq "HASH" and defined $helm_values->{"ipam"}{"mode"}
!ll&&!rl&&r
18302
defined $ipam_mode and ref $helm_values->{"ipam"} eq "HASH"
!ll&&!rl&&r
169142
MC/DC75%3/4
defined $ipam_mode and ref $helm_values->{"ipam"} eq "HASH" and defined $helm_values->{"ipam"}{"mode"} and $helm_values->{"ipam"}{"mode"} ne $ipam_mode
defined $ipam_mode ref $helm_values->{"ipam"} eq "HASH" defined $helm_values->{"ipam"}{"mode"} $helm_values->{"ipam"}{"mode"} ne $ipam_mode
Truth table80%4/5
A and B and C and D
A= defined $ipam_modeB= ref $helm_values->{"ipam"} eq "HASH"C= defined $helm_values->{"ipam"}{"mode"}D= $helm_values->{"ipam"}{"mode"} ne $ipam_mode
ABCDresult
0XXX0
10XX0
110X0
11100
11111
217 && defined $helm_values->{ipam}{mode} && $helm_values->{ipam}{mode} ne $ipam_mode) {
218 1 die "ipam_mode $ipam_mode and helm_values ipam.mode $helm_values->{ipam}{mode} "
219 . "contradict each other: give one of them\n";
220 }
221  
222 die "k8s_service_host is k3s-only: rke2 serves the API on 127.0.0.1:6443 "
223 184 ▶ . "on every node\n" if !$dist->needs_k8s_service_host && defined $opts{k8s_service_host};
Branch: 1 branch
BranchTrueFalseTotal
if not $dist->needs_k8s_service_host and defined $opts{"k8s_service_host"} 1 183 184
Condition100%3/3
not $dist->needs_k8s_service_host and defined $opts{"k8s_service_host"}
!ll&&!rl&&r
501331
MC/DC100%2/2
not $dist->needs_k8s_service_host and defined $opts{"k8s_service_host"}
not $dist->needs_k8s_service_host defined $opts{"k8s_service_host"}
Truth table100%3/3
not(A) and B
A= not $dist->needs_k8s_service_hostB= defined $opts{"k8s_service_host"}
ABresult
0X0
100
111
224  
225 183 ▶ my $channel = $opts{gateway_api_channel} // 'experimental';
Condition100%2/2
$opts{"gateway_api_channel"} // "experimental"
l!l
3180
MC/DC100%1/1
$opts{"gateway_api_channel"} // "experimental"
$opts{"gateway_api_channel"}
Truth table100%2/2
A
A= $opts{"gateway_api_channel"}
Aresult
11
00
226 183 ▶ if ($gateway_api) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gateway_api) 47 136 183
227 die "gateway_api needs kubeconfig (a local kubeconfig the API answers "
228 47 ▶ . "through): the CRDs are applied via Kubernetes::REST\n" unless $opts{kubeconfig};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"kubeconfig"} 1 46 47
229 die "gateway_api needs gateway_api_version (e.g. v1.2.0), matching what "
230 46 ▶ . "Cilium supports\n" unless $opts{gateway_api_version};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"gateway_api_version"} 2 44 46
231 44 $channel = _gateway_api_channel($channel);
232 }
233  
234 179 ▶ if ($wait) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wait) 7 172 179
235 die "wait needs kubeconfig (a local kubeconfig the API answers through): "
236 7 ▶ . "readiness is read via Kubernetes::REST\n" unless $opts{kubeconfig};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"kubeconfig"} 1 6 7
237 6 ▶ die "wait_duration must be a whole number of seconds above 0\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $duration =~ /\A[1-9][0-9]*\z/u 1 5 6
238 unless $duration =~ /\A[1-9][0-9]*\z/;
239 }
240  
241 my $values = _helm_values($dist, $gateway_api, $helm_values,
242 177 $opts{k8s_service_host}, $cluster_cidr, $ipam_mode);
243  
244 my $o = {
245 dist => $dist,
246 version => $opts{version} // CILIUM_VERSION,
247 version_pinned => defined $opts{version} ? 1 : 0,
248 cli_version => $opts{cli_version} // CILIUM_CLI_VERSION,
249 api_server => $opts{api_server},
250 kubeconfig => $opts{kubeconfig},
251 gateway_api => $gateway_api,
252 gateway_api_version => $opts{gateway_api_version},
253 gateway_api_channel => $channel,
254 wait => $wait,
255 wait_duration => $duration,
256 values => $values,
257 cluster_cidr => $cluster_cidr,
258 ipam_mode => $ipam_mode,
259 177 ▶ explicit => _explicit_values($helm_values, $opts{k8s_service_host}),
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'version'} ? : 8 169 177
Condition75%3/4
$opts{"version"} // "1.20.0"
l!l
8169
$opts{"cli_version"} // "v0.19.7"
l!l
0177
MC/DC100%1/1
$opts{"version"} // "1.20.0"
$opts{"version"}
MC/DC0%0/1
$opts{"cli_version"} // "v0.19.7"
$opts{"cli_version"}
Truth table100%2/2
A
A= $opts{"version"}
Aresult
11
00
Truth table50%1/2
A
A= $opts{"cli_version"}
Aresult
11
00
260 };
261  
262 # With a kubeconfig a running Cilium's k8sServiceHost is read later, so
263 # the check waits for that; without one it can only come from the caller.
264 177 ▶ _require_k8s_service_host($o) unless $opts{kubeconfig};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"kubeconfig"} 33 144 177
265  
266 # A pinned version is the one installed; the default may still give way
267 # to a running Cilium (_settle_version), so it is checked after that.
268 171 ▶ _check_gateway_api_version($o) if $o->{version_pinned};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"version_pinned"} 8 163 171
269  
270 170 return $o;
271 }
272  
273 47 ▶sub _gateway_api_channel {
Subroutine _gateway_api_channel: called CC=4 SCAR=13.9
274 47 my ($channel) = @_;
275 47 ▶ $channel //= 'experimental';
Condition100%2/2
$channel //= "experimental"
l!l
461
MC/DC100%1/1
$channel //= "experimental"
$channel
Truth table100%2/2
A
A= $channel
Aresult
11
00
276 47 ▶ die "gateway_api_channel must be 'standard' or 'experimental'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $channel eq "standard" or $channel eq "experimental" 2 45 47
Condition100%3/3
$channel eq "standard" or $channel eq "experimental"
l!l&&r!l&&!r
3422
MC/DC100%2/2
$channel eq "standard" or $channel eq "experimental"
$channel eq "standard" $channel eq "experimental"
Truth table100%3/3
A or B
A= $channel eq "standard"B= $channel eq "experimental"
ABresult
1X1
011
000
277 unless $channel eq 'standard' || $channel eq 'experimental';
278 45 return $channel;
279 }
280  
281 # kube-proxy replacement on k3s: Cilium must reach the API server before
282 # any Service works, on agents too, where 127.0.0.1:6443 does not exist.
283 108 ▶sub _require_k8s_service_host {
Subroutine _require_k8s_service_host: called CC=6 SCAR=17.9
284 108 my ($o) = @_;
285 108 ▶ return unless $o->{dist}->needs_k8s_service_host;
Branch: 1 branch
BranchTrueFalseTotal
unless $o->{"dist"}->needs_k8s_service_host 73 35 108
286  
287 35 ▶ my $host = $o->{values}{k8sServiceHost} // '';
Condition100%2/2
$o->{"values"}{"k8sServiceHost"} // ""
l!l
323
MC/DC100%1/1
$o->{"values"}{"k8sServiceHost"} // ""
$o->{"values"}{"k8sServiceHost"}
Truth table100%2/2
A
A= $o->{"values"}{"k8sServiceHost"}
Aresult
11
00
288 die "install_cilium on k3s needs k8s_service_host, the control plane "
289 . "address every node reaches the API at on port 6443 (k3s agents serve "
290 . "it on 127.0.0.1:6444, so localhost does not work)"
291 . ( length $host ? ", got '$host'" : '' ) . "\n"
292 35 ▶ if !length $host || $LOOPBACK{lc $host};
Branch: 2 branches
BranchTrueFalseTotal
if not length $host or $LOOPBACK{lc $host} 9 26 35
length $host ? : 6 3 9
Condition100%3/3
not length $host or $LOOPBACK{lc $host}
l!l&&r!l&&!r
3626
MC/DC100%2/2
not length $host or $LOOPBACK{lc $host}
not length $host $LOOPBACK{lc $host}
Truth table100%3/3
not(A) or B
A= not length $hostB= $LOOPBACK{lc $host}
ABresult
1X1
011
000
293 }
294  
295 # Which values the caller set, as opposed to our defaults: only defaults
296 # give way to what a running Cilium already uses. A non-hash where a hash
297 # belongs counts as setting everything below it. cluster_cidr is not a
298 # requested pool, nor ipam_mode a requested mode: they are what a fresh
299 # install gets, and a running Cilium's pool and mode win over them (with a
300 # warning, in _adopt_running).
301 185 ▶sub _explicit_values {
Subroutine _explicit_values: called CC=16 SCAR=27.8
302 185 my ($hv, $k8s_service_host) = @_;
303  
304 185 my $ipam = $hv->{ipam};
305 185 ▶ my $ipam_all = exists $hv->{ipam} && ref $ipam ne 'HASH';
Condition100%3/3
exists $hv->{'ipam'} && ref $ipam ne 'HASH'
!ll&&!rl&&r
164192
MC/DC100%2/2
exists $hv->{'ipam'} && ref $ipam ne 'HASH'
exists $hv->{'ipam'} ref $ipam ne 'HASH'
Truth table100%3/3
A && B
A= exists $hv->{'ipam'}B= ref $ipam ne 'HASH'
ABresult
0X0
100
111
306 185 ▶ my $op = ref $ipam eq 'HASH' ? $ipam->{operator} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $ipam eq 'HASH' ? : 19 166 185
307 185 my $operator = $hv->{operator};
308  
309 return {
310 ipam_mode => ( $ipam_all || ( ref $ipam eq 'HASH' && exists $ipam->{mode} ) ) ? 1 : 0,
311 pool => ( $ipam_all || ( ref $ipam eq 'HASH' && exists $ipam->{operator}
312 && ( ref $op ne 'HASH' || exists $op->{clusterPoolIPv4PodCIDRList} ) ) ) ? 1 : 0,
313 k8s_service_host => ( defined $k8s_service_host || exists $hv->{k8sServiceHost} ) ? 1 : 0,
314 operator_replicas => ( exists $hv->{operator}
315 185 ▶ && ( ref $operator ne 'HASH' || exists $operator->{replicas} ) ) ? 1 : 0,
Branch: 4 branches
BranchTrueFalseTotal
$ipam_all || ref $ipam eq 'HASH' && exists $ipam->{'mode'} ? : 13 172 185
$ipam_all || ref $ipam eq 'HASH' && exists $ipam->{'operator'} && (ref $op ne 'HASH' || exists $op->{'clusterPoolIPv4PodCIDRList'}) ? : 8 177 185
defined $k8s_service_host || exists $hv->{'k8sServiceHost'} ? : 44 141 185
exists $hv->{'operator'} && (ref $operator ne 'HASH' || exists $operator->{'replicas'}) ? : 3 182 185
Condition85%23/27
$ipam_all or ref $ipam eq "HASH" and exists $ipam->{"mode"}
l!l&&r!l&&!r
211172
ref $ipam eq "HASH" and exists $ipam->{"mode"}
!ll&&!rl&&r
164811
$ipam_all or ref $ipam eq "HASH" and exists $ipam->{"operator"} and ref $op ne "HASH" || exists $op->{"clusterPoolIPv4PodCIDRList"}
l!l&&r!l&&!r
26177
ref $ipam eq "HASH" and exists $ipam->{"operator"} and ref $op ne "HASH" || exists $op->{"clusterPoolIPv4PodCIDRList"}
!ll&&!rl&&r
17526
ref $ipam eq "HASH" and exists $ipam->{"operator"}
!ll&&!rl&&r
164118
ref $op ne "HASH" or exists $op->{"clusterPoolIPv4PodCIDRList"}
l!l&&r!l&&!r
062
defined $k8s_service_host or exists $hv->{"k8sServiceHost"}
l!l&&r!l&&!r
413141
exists $hv->{"operator"} and ref $operator ne "HASH" || exists $operator->{"replicas"}
!ll&&!rl&&r
18203
ref $operator ne "HASH" or exists $operator->{"replicas"}
l!l&&r!l&&!r
030
MC/DC100%3/3
$ipam_all or ref $ipam eq "HASH" and exists $ipam->{"mode"}
$ipam_all ref $ipam eq "HASH" exists $ipam->{"mode"}
MC/DC80%4/5
$ipam_all or ref $ipam eq "HASH" and exists $ipam->{"operator"} and ref $op ne "HASH" || exists $op->{"clusterPoolIPv4PodCIDRList"}
$ipam_all ref $ipam eq "HASH" exists $ipam->{"operator"} ref $op ne "HASH" exists $op->{"clusterPoolIPv4PodCIDRList"}
MC/DC100%2/2
defined $k8s_service_host or exists $hv->{"k8sServiceHost"}
defined $k8s_service_host exists $hv->{"k8sServiceHost"}
MC/DC33%1/3
exists $hv->{"operator"} and ref $operator ne "HASH" || exists $operator->{"replicas"}
exists $hv->{"operator"} ref $operator ne "HASH" exists $operator->{"replicas"}
Truth table100%4/4
A or B and C
A= $ipam_allB= ref $ipam eq "HASH"C= exists $ipam->{"mode"}
ABCresult
1XX1
0111
00X0
0100
Truth table83%5/6
A or B and C and D or E
A= $ipam_allB= ref $ipam eq "HASH"C= exists $ipam->{"operator"}D= ref $op ne "HASH"E= exists $op->{"clusterPoolIPv4PodCIDRList"}
ABCDEresult
1XXXX1
0111X1
011011
00XXX0
010XX0
011000
Truth table100%3/3
A or B
A= defined $k8s_service_hostB= exists $hv->{"k8sServiceHost"}
ABresult
1X1
011
000
Truth table50%2/4
A and B or C
A= exists $hv->{"operator"}B= ref $operator ne "HASH"C= exists $operator->{"replicas"}
ABCresult
0XX0
1000
11X1
1011
316 };
317 }
318  
319 #
320 # Cilium CLI installation
321 #
322  
323 38 ▶sub _install_cilium_cli {
Subroutine _install_cilium_cli: called CC=7 SCAR=19.5
324 38 my ($cli_version) = @_;
325  
326 # Check if already installed at the right version
327 38 my $current = run "cilium version --client 2>/dev/null | head -1", auto_die => 0;
328 38 ▶ if ($current && $current =~ /\Q$cli_version\E/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current and $current =~ /\Q$cli_version\E/u) 35 3 38
Condition66%2/3
$current and $current =~ /\Q$cli_version\E/u
!ll&&!rl&&r
3035
MC/DC50%1/2
$current and $current =~ /\Q$cli_version\E/u
$current $current =~ /\Q$cli_version\E/u
Truth table66%2/3
A and B
A= $currentB= $current =~ /\Q$cli_version\E/u
ABresult
0X0
100
111
329 35 Rex::Logger::info("Cilium CLI $cli_version already installed");
330 35 return;
331 }
332  
333 3 Rex::Logger::info("Installing Cilium CLI $cli_version");
334  
335 3 my $arch = run "uname -m", auto_die => 1;
336 3 chomp $arch;
337 3 ▶ $arch = 'amd64' if $arch eq 'x86_64';
Branch: 1 branch
BranchTrueFalseTotal
if $arch eq "x86_64" 3 0 3
338 3 ▶ $arch = 'arm64' if $arch eq 'aarch64';
Branch: 1 branch
BranchTrueFalseTotal
if $arch eq "aarch64" 0 3 3
339  
340 3 my $url = "https://github.com/cilium/cilium-cli/releases/download/$cli_version/cilium-linux-$arch.tar.gz";
341  
342 3 my $asset = "cilium-linux-$arch.tar.gz";
343  
344 # Own directory: a predictable /tmp name is a symlink target for any
345 # local user, and the download runs as root.
346 3 my $dir = run "mktemp -d", auto_die => 1;
347 3 chomp $dir;
348 3 ▶ die "mktemp -d returned no usable directory: '$dir'\n" unless $dir =~ m{\A/\S+\z};
Branch: 1 branch
BranchTrueFalseTotal
unless $dir =~ m[\A/\S+\z]u 0 3 3
349  
350 3 my $ok = eval {
351 3 run "curl -fsSL '$url' -o '$dir/$asset'", auto_die => 1;
352 3 run "curl -fsSL '$url.sha256sum' -o '$dir/$asset.sha256sum'", auto_die => 1;
353  
354 3 my $sums = run "cat '$dir/$asset.sha256sum'", auto_die => 1;
355 3 my $actual = run "sha256sum '$dir/$asset'", auto_die => 1;
356 # scalar(): both return empty on no match; in this list they must stay undef.
357 3 Rex::Rancher::Checksum->verify_sha256(
358 scalar(Rex::Rancher::Checksum->expected_sha256($sums, $asset)),
359 scalar(Rex::Rancher::Checksum->sha256_of($actual)),
360 $asset,
361 );
362 1 Rex::Logger::info(" $asset: sha256 verified");
363  
364 1 run "tar xzf '$dir/$asset' -C '$dir' cilium", auto_die => 1;
365 1 run "mv '$dir/cilium' /usr/local/bin/cilium", auto_die => 1;
366 1 run "chmod 755 /usr/local/bin/cilium", auto_die => 1;
367 1 1;
368 };
369 3 my $err = $@;
370 3 run "rm -rf '$dir'", auto_die => 0;
371 3 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 2 1 3
372  
373 1 Rex::Logger::info("Cilium CLI $cli_version installed to /usr/local/bin/cilium");
374 }
375  
376 #
377 # Running the CLI on the remote host
378 #
379  
380 33 ▶sub _cilium_command {
Subroutine _cilium_command: called CC=2 SCAR=7.0
381 33 my ($verb, $o, $values_file) = @_;
382  
383 33 my @cmd = (
384 "cilium $verb",
385 "--version $o->{version}",
386 "--helm-values $values_file",
387 );
388 33 push @cmd, "--set kubeProxyReplacement=true";
389 33 ▶ push @cmd, "--api-server $o->{api_server}" if $o->{api_server};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"api_server"} 0 33 33
390  
391 33 return "KUBECONFIG=" . $o->{dist}->kubeconfig . " " . join(" ", @cmd);
392 }
393  
394 29 ▶sub _run_cilium {
Subroutine _run_cilium: called CC=3 SCAR=11.0
395 29 my ($cmd, $what) = @_;
396  
397 29 Rex::Logger::info("Running: $cmd");
398 # auto_die => 0 only to put the CLI's output into the error message.
399 29 my $out = run "$cmd 2>&1", auto_die => 0;
400 29 ▶ die "cilium $what failed: " . ($out // '') . "\n" if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 1 28 29
Condition50%1/2
$out // ""
l!l
10
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
401 28 return $out;
402 }
403  
404 # No local kubeconfig: the release state cannot be read, so keep the old
405 # contract. "cannot re-use a name that is still in use" is Helm refusing a
406 # second install of an existing release — on a re-run against a cluster
407 # that already has Cilium that is the expected outcome, and failing on it
408 # made every re-deploy die. Version and values are not reconciled here.
409 2 ▶sub _install_unchecked {
Subroutine _install_unchecked: called CC=5 SCAR=16.6
410 2 my ($o, $values_file) = @_;
411  
412 2 my $cmd = _cilium_command('install', $o, $values_file);
413 2 Rex::Logger::info("Running: $cmd");
414 2 my $out = run "$cmd 2>&1", auto_die => 0;
415  
416 2 ▶ if ($? != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 2 0 2
417 2 ▶ if (($out // '') =~ /cannot re-use a name/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (($out // "") =~ /cannot re-use a name/iu) 1 1 2
Condition50%1/2
$out // ""
l!l
20
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
418 1 Rex::Logger::info(" Cilium already installed (Helm release exists); without "
419 . "kubeconfig its version and values are not checked -- pass kubeconfig "
420 . "to reconcile them (upgrade_cilium needs it too)", 'warn');
421 1 return;
422 }
423 1 ▶ die "cilium install failed: " . ($out // '') . "\n";
Condition50%1/2
$out // ""
l!l
10
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
424 }
425  
426 0 Rex::Logger::info("Cilium $o->{version} installed on " . $o->{dist}->name . " cluster");
427 }
428  
429 #
430 # Helm release state (read locally via Kubernetes::REST)
431 #
432  
433 0 ▶sub _api {
Subroutine _api: not called CC=1 SCAR=6.9
434 0 my ($kubeconfig) = @_;
435 0 return Kubernetes::REST::Kubeconfig->new(
436 kubeconfig_path => $kubeconfig,
437 )->api;
438 }
439  
440 # The release named $name (default: Cilium's), or undef when there is none.
441 73 ▶sub _read_release {
Subroutine _read_release: called CC=5 SCAR=16.7
442 73 my ($api, $name) = @_;
443  
444 73 ▶ my $list = $api->list('Secret',
Condition100%2/2
$name // "cilium"
l!l
469
MC/DC100%1/1
$name // "cilium"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
445 namespace => RELEASE_NAMESPACE,
446 labelSelector => 'owner=helm,name=' . ($name // RELEASE_NAME),
447 );
448  
449 return _release_from_secrets([ map {
450 +{
451 name => $_->metadata->name,
452 labels => $_->metadata->labels // {},
453 release => ($_->data // {})->{release},
454 }
455 73 ▶ } @{ $list->items // [] } ]);
Condition50%3/6
$_->metadata->labels // {}
l!l
770
$_->data // {}
l!l
770
$list->items // []
l!l
730
MC/DC0%0/1
$_->metadata->labels // {}
$_->metadata->labels
MC/DC0%0/1
$_->data // {}
$_->data
MC/DC0%0/1
$list->items // []
$list->items
Truth table50%1/2
A
A= $_->metadata->labels
Aresult
11
00
Truth table50%1/2
A
A= $_->data
Aresult
11
00
Truth table50%1/2
A
A= $list->items
Aresult
11
00
456 }
457  
458 # Collapse Helm's per-revision Secrets into the state of the release: the
459 # newest revision's status, chart version and user-supplied values, and
460 # whether any revision is still 'deployed'. undef when there is no release.
461 78 ▶sub _release_from_secrets {
Subroutine _release_from_secrets: called CC=9 SCAR=22.3
462 78 my ($secrets) = @_;
463 78 ▶ return unless @$secrets;
Branch: 1 branch
BranchTrueFalseTotal
unless @$secrets 19 59 78
464  
465 59 ▶ my @sorted = sort { ($b->{labels}{version} // 0) <=> ($a->{labels}{version} // 0) } @$secrets;
Condition50%2/4
$Rex::Rancher::Cilium::b->{"labels"}{"version"} // 0
l!l
230
$Rex::Rancher::Cilium::a->{"labels"}{"version"} // 0
l!l
230
MC/DC0%0/1
$Rex::Rancher::Cilium::b->{"labels"}{"version"} // 0
$Rex::Rancher::Cilium::b->{"labels"}{"version"}
MC/DC0%0/1
$Rex::Rancher::Cilium::a->{"labels"}{"version"} // 0
$Rex::Rancher::Cilium::a->{"labels"}{"version"}
Truth table50%1/2
A
A= $Rex::Rancher::Cilium::b->{"labels"}{"version"}
Aresult
11
00
Truth table50%1/2
A
A= $Rex::Rancher::Cilium::a->{"labels"}{"version"}
Aresult
11
00
466 59 my $latest = $sorted[0];
467 59 ▶ my $payload = _decode_release($latest->{release}) // {};
Condition100%2/2
_decode_release($latest->{"release"}) // {}
l!l
581
MC/DC100%1/1
_decode_release($latest->{"release"}) // {}
_decode_release($latest->{"release"})
Truth table100%2/2
A
A= _decode_release($latest->{"release"})
Aresult
11
00
468  
469 return {
470 revision => $latest->{labels}{version},
471 status => $latest->{labels}{status} // 'unknown',
472 59 ▶ chart_version => eval { $payload->{chart}{metadata}{version} },
Branch: 1 branch
BranchTrueFalseTotal
grep({($_->{'labels'}{'status'} // '') eq 'deployed';} @sorted) ? : 43 16 59
Condition100%2/2
$payload->{"config"} // {}
l!l
581
MC/DC100%1/1
$payload->{"config"} // {}
$payload->{"config"}
Truth table100%2/2
A
A= $payload->{"config"}
Aresult
11
00
473 config => $payload->{config} // {},
474 82 ▶ has_deployed => (grep { ($_->{labels}{status} // '') eq 'deployed' } @sorted) ? 1 : 0,
Condition50%1/2
$_->{"labels"}{"status"} // ""
l!l
820
MC/DC0%0/1
$_->{"labels"}{"status"} // ""
$_->{"labels"}{"status"}
Truth table50%1/2
A
A= $_->{"labels"}{"status"}
Aresult
11
00
475 59 ▶ secrets => [ map { $_->{name} } @sorted ],
Condition50%1/2
$latest->{"labels"}{"status"} // "unknown"
l!l
590
MC/DC0%0/1
$latest->{"labels"}{"status"} // "unknown"
$latest->{"labels"}{"status"}
Truth table50%1/2
A
A= $latest->{"labels"}{"status"}
Aresult
11
00
476 };
477 }
478  
479 # Secret data is base64 (Kubernetes) of base64 (Helm) of gzipped JSON.
480 # Returns undef when the payload cannot be decoded; the caller then treats
481 # the chart version as unknown, which leads to an upgrade, never a no-op.
482 60 ▶sub _decode_release {
Subroutine _decode_release: called CC=5 SCAR=16.1
483 60 my ($data) = @_;
484 60 ▶ return unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 1 59 60
485  
486 59 my $json = eval {
487 59 my $raw = decode_base64(decode_base64($data));
488 59 ▶ if (substr($raw, 0, 2) eq "\x1f\x8b") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($raw, 0, 2) eq "\37\213") 58 1 59
489 58 my $plain;
490 58 ▶ gunzip(\$raw => \$plain) or die "gunzip: $GunzipError\n";
Branch: 1 branch
BranchTrueFalseTotal
gunzip(\$raw, \$plain) or die "gunzip: $Rex::Rancher::Cilium::GunzipError\n" 58 0 58
491 58 $raw = $plain;
492 }
493 59 JSON::MaybeXS->new->decode($raw);
494 };
495 59 ▶ return ref $json eq 'HASH' ? $json : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $json eq 'HASH' ? : 58 1 59
496 }
497  
498 # The decision the whole release handling hangs on. Pure: release state in,
499 # one of install / noop / upgrade / reinstall out, or a die for the states
500 # where acting automatically could take down a working pod network.
501 47 ▶sub _release_action {
Subroutine _release_action: called CC=11 SCAR=24.0
502 47 my ($release, $version, $values) = @_;
503  
504 47 ▶ return 'install' unless $release;
Branch: 1 branch
BranchTrueFalseTotal
unless $release 9 38 47
505  
506 38 _refuse_stuck_release($release);
507  
508 35 my $status = $release->{status};
509  
510 35 ▶ if ($status eq 'deployed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "deployed") 20 15 35
511 my $same_version = defined $release->{chart_version}
512 20 ▶ && _norm_version($release->{chart_version}) eq _norm_version($version);
Condition100%3/3
defined $release->{'chart_version'} && _norm_version($release->{'chart_version'}) eq _norm_version($version)
!ll&&!rl&&r
1415
MC/DC100%2/2
defined $release->{'chart_version'} && _norm_version($release->{'chart_version'}) eq _norm_version($version)
defined $release->{'chart_version'} _norm_version($release->{'chart_version'}) eq _norm_version($version)
Truth table100%3/3
A && B
A= defined $release->{'chart_version'}B= _norm_version($release->{'chart_version'}) eq _norm_version($version)
ABresult
0X0
100
111
513 20 ▶ return 'noop' if $same_version && _values_subset($values, $release->{config});
Branch: 1 branch
BranchTrueFalseTotal
if $same_version and _values_subset($values, $release->{"config"}) 5 15 20
Condition100%3/3
$same_version and _values_subset($values, $release->{"config"})
!ll&&!rl&&r
5105
MC/DC100%2/2
$same_version and _values_subset($values, $release->{"config"})
$same_version _values_subset($values, $release->{"config"})
Truth table100%3/3
A and B
A= $same_versionB= _values_subset($values, $release->{"config"})
ABresult
0X0
100
111
514 15 _refuse_ipam_change($release, $values);
515 13 return 'upgrade';
516 }
517  
518 15 ▶ if ($status eq 'failed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "failed") 8 7 15
519 # A failed upgrade leaves the previous revision deployed: upgrade again.
520 # A failed first install has nothing deployed: Helm refuses to upgrade it
521 # ("has no deployed releases") and to install over it.
522 8 ▶ return 'reinstall' unless $release->{has_deployed};
Branch: 1 branch
BranchTrueFalseTotal
unless $release->{"has_deployed"} 6 2 8
523 2 _refuse_ipam_change($release, $values);
524 1 return 'upgrade';
525 }
526  
527 # A pending install gets here only without a deployed revision (an
528 # interrupted first install): _refuse_stuck_release died on the others.
529 7 ▶ return 'reinstall'
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "pending-install" or $status eq "uninstalling" or $status eq "uninstalled" 6 1 7
Condition100%6/6
$status eq "pending-install" or $status eq "uninstalling" or $status eq "uninstalled"
l!l&&r!l&&!r
511
$status eq "pending-install" or $status eq "uninstalling"
l!l&&r!l&&!r
412
MC/DC100%3/3
$status eq "pending-install" or $status eq "uninstalling" or $status eq "uninstalled"
$status eq "pending-install" $status eq "uninstalling" $status eq "uninstalled"
Truth table100%4/4
A or B or C
A= $status eq "pending-install"B= $status eq "uninstalling"C= $status eq "uninstalled"
ABCresult
1XX1
01X1
0011
0000
530 if $status eq 'pending-install'
531 || $status eq 'uninstalling'
532 || $status eq 'uninstalled';
533  
534 1 die "Helm release " . RELEASE_NAME . " is in unexpected state '$status'\n";
535 }
536  
537 # A pending upgrade or rollback, or a pending install over a deployed
538 # revision (Helm itself does not produce one): a Helm operation hangs over
539 # the revision that carries the pod network, and a reinstall would purge
540 # that revision with the rest. So this dies instead, in install_cilium and
541 # upgrade_cilium right after the release is read, before the host is
542 # touched. A pending install without a deployed revision is an interrupted
543 # first install and passes.
544 109 ▶sub _refuse_stuck_release {
Subroutine _refuse_stuck_release: called CC=6 SCAR=17.9
545 109 my ($release) = @_;
546 109 ▶ return unless $release;
Branch: 1 branch
BranchTrueFalseTotal
unless $release 17 92 109
547 92 my $status = $release->{status};
548 _die_release_stuck($release)
549 if $status eq 'pending-upgrade' || $status eq 'pending-rollback'
550 92 ▶ || ( $status eq 'pending-install' && $release->{has_deployed} );
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "pending-upgrade" or $status eq "pending-rollback" or $status eq "pending-install" and $release->{"has_deployed"} 21 71 92
Condition100%9/9
$status eq "pending-upgrade" or $status eq "pending-rollback" or $status eq "pending-install" and $release->{"has_deployed"}
l!l&&r!l&&!r
13871
$status eq "pending-upgrade" or $status eq "pending-rollback"
l!l&&r!l&&!r
7679
$status eq "pending-install" and $release->{"has_deployed"}
!ll&&!rl&&r
6298
MC/DC100%4/4
$status eq "pending-upgrade" or $status eq "pending-rollback" or $status eq "pending-install" and $release->{"has_deployed"}
$status eq "pending-upgrade" $status eq "pending-rollback" $status eq "pending-install" $release->{"has_deployed"}
Truth table100%5/5
A or B or C and D
A= $status eq "pending-upgrade"B= $status eq "pending-rollback"C= $status eq "pending-install"D= $release->{"has_deployed"}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
551 }
552  
553 # Helm refuses any operation on a release while another one is pending on
554 # it: an earlier run was interrupted, or another deploy still runs. The
555 # revision below it still carries the pod network, so nothing is done
556 # automatically. install_cilium and upgrade_cilium both die here.
557 21 ▶sub _die_release_stuck {
Subroutine _die_release_stuck: called CC=2 SCAR=7.0
558 21 my ($release) = @_;
559 21 ▶ my $revision = $release->{revision} // '?';
Condition50%1/2
$release->{"revision"} // "?"
l!l
210
MC/DC0%0/1
$release->{"revision"} // "?"
$release->{"revision"}
Truth table50%1/2
A
A= $release->{"revision"}
Aresult
11
00
560 21 my $secret = 'sh.helm.release.v1.' . RELEASE_NAME . '.v' . $revision;
561 21 die "Helm release " . RELEASE_NAME . " is stuck in $release->{status} (revision "
562 . "$revision): an earlier run was interrupted or "
563 . "another deploy is still running. Once none is, delete Secret $secret "
564 . "in " . RELEASE_NAMESPACE . " and re-run.\n";
565 }
566  
567 # Cilium cannot switch IPAM mode under running pods: an upgrade that changes
568 # ipam.mode leaves them without addresses. Only an ipam.mode the release set
569 # explicitly is compared; without one the deployed mode is not known here.
570 17 ▶sub _refuse_ipam_change {
Subroutine _refuse_ipam_change: called CC=4 SCAR=13.9
571 17 my ($release, $values) = @_;
572  
573 17 my $have = eval { $release->{config}{ipam}{mode} };
574 17 my $want = eval { $values->{ipam}{mode} };
575 17 ▶ return unless defined $have && defined $want && $have ne $want;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $have and defined $want and $have ne $want 14 3 17
Condition83%5/6
defined $have and defined $want and $have ne $want
!ll&&!rl&&r
593
defined $have and defined $want
!ll&&!rl&&r
5012
MC/DC66%2/3
defined $have and defined $want and $have ne $want
defined $have defined $want $have ne $want
Truth table75%3/4
A and B and C
A= defined $haveB= defined $wantC= $have ne $want
ABCresult
0XX0
10X0
1100
1111
576  
577 3 die "Helm release " . RELEASE_NAME . " runs ipam.mode $have, the requested "
578 . "values ipam.mode $want: Cilium cannot change the IPAM mode of a running "
579 . "cluster (pods lose their addresses). Redeploy the cluster, or pass "
580 . "helm_values => { ipam => { mode => '$have' } } to keep it.\n";
581 }
582  
583 #
584 # The running Cilium (read locally via Kubernetes::REST)
585 #
586  
587 # True for the error Kubernetes::REST raises on a 404 response, and only
588 # for that: "not found" in some other error's body (a webhook, a proxy, a
589 # name lookup) is not an object that is missing.
590 135 ▶sub _is_not_found {
Subroutine _is_not_found: called CC=3 SCAR=11.1
591 135 my ($err) = @_;
592 135 ▶ return ( $err // '' ) =~ /\bKubernetes API error \([^)]*\): 404\b/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($err // '') =~ /\bKubernetes API error \([^)]*\): 404\b/u ? : 124 11 135
Condition50%1/2
$err // ""
l!l
1350
MC/DC0%0/1
$err // ""
$err
Truth table50%1/2
A
A= $err
Aresult
11
00
593 }
594  
595 # undef when the object does not exist; any other API error dies, because
596 # guessing here is how a cluster-pool cluster gets switched to another mode.
597 # Namespaced objects live in RELEASE_NAMESPACE; a CustomResourceDefinition
598 # is cluster-scoped.
599 196 ▶sub _get_optional {
Subroutine _get_optional: called CC=6 SCAR=17.9
600 196 my ($api, $kind, $name) = @_;
601 196 ▶ my @ns = $kind eq 'CustomResourceDefinition' ? () : ( namespace => RELEASE_NAMESPACE );
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'CustomResourceDefinition' ? : 6 190 196
602 196 my $obj = eval { $api->get($kind, $name, @ns) };
603 196 ▶ return $obj if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 63 133 196
604 133 ▶ return if !$@ || _is_not_found($@);
Branch: 1 branch
BranchTrueFalseTotal
if not $@ or _is_not_found($@) 123 10 133
Condition66%2/3
not $@ or _is_not_found($@)
l!l&&r!l&&!r
012310
MC/DC50%1/2
not $@ or _is_not_found($@)
not $@ _is_not_found($@)
Truth table66%2/3
not(A) or B
A= not $@B= _is_not_found($@)
ABresult
1X1
011
000
605 10 ▶ die "Cannot read $kind " . ( @ns ? RELEASE_NAMESPACE . "/" : '' ) . "$name: $@";
Branch: 1 branch
BranchTrueFalseTotal
@ns ? : 8 2 10
606 }
607  
608 # What a running Cilium uses, each undef when unknown: the IPAM mode and
609 # pool from its ConfigMap (not the release values -- a `cilium install`
610 # without ipam.mode runs the chart default cluster-pool and records no mode),
611 # k8sServiceHost from the agent's KUBERNETES_SERVICE_HOST, operator.replicas
612 # from the cilium-operator Deployment (a release that never set it runs the
613 # chart's default, which the values do not record), else the release values.
614 55 ▶sub _read_running {
Subroutine _read_running: called CC=14 SCAR=26.4
615 55 my ($api, $release) = @_;
616  
617 # Guarded: without a release the deref would autovivify one, and its
618 # missing status would warn below.
619 my %running = (
620 55 ▶ operator_replicas => $release ? eval { $release->{config}{operator}{replicas} } : undef,
Branch: 1 branch
BranchTrueFalseTotal
$release ? : 38 17 55
621 );
622  
623 55 ▶ if (my $op = _get_optional($api, 'Deployment', 'cilium-operator')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $op = _get_optional($api, "Deployment", "cilium-operator")) 6 48 54
624 6 my $replicas = eval { $op->spec->replicas };
625 6 ▶ $running{operator_replicas} = $replicas if defined $replicas;
Branch: 1 branch
BranchTrueFalseTotal
if defined $replicas 6 0 6
626 }
627  
628 54 ▶ if (my $cm = _get_optional($api, 'ConfigMap', CILIUM_CONFIGMAP)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cm = _get_optional($api, "ConfigMap", "cilium-config")) 16 34 50
629 16 ▶ my $data = $cm->data // {};
Condition50%1/2
$cm->data // {}
l!l
160
MC/DC0%0/1
$cm->data // {}
$cm->data
Truth table50%1/2
A
A= $cm->data
Aresult
11
00
630 # No ipam key: the agent's own default, cluster-pool.
631 16 ▶ $running{ipam_mode} = $data->{ipam} // 'cluster-pool';
Condition100%2/2
$data->{"ipam"} // "cluster-pool"
l!l
151
MC/DC100%1/1
$data->{"ipam"} // "cluster-pool"
$data->{"ipam"}
Truth table100%2/2
A
A= $data->{"ipam"}
Aresult
11
00
632 16 ▶ my @pool = grep { length } split /[\s,]+/, $data->{'cluster-pool-ipv4-cidr'} // '';
Condition100%2/2
$data->{"cluster-pool-ipv4-cidr"} // ""
l!l
115
MC/DC100%1/1
$data->{"cluster-pool-ipv4-cidr"} // ""
$data->{"cluster-pool-ipv4-cidr"}
Truth table100%2/2
A
A= $data->{"cluster-pool-ipv4-cidr"}
Aresult
11
00
633 16 ▶ $running{pool} = \@pool if @pool;
Branch: 1 branch
BranchTrueFalseTotal
if @pool 11 5 16
634 }
635  
636 # The version the agents run: their image tag, else the chart of a
637 # deployed release (a failed upgrade's newest revision names the chart
638 # that did not make it).
639 $running{version} = $release->{chart_version}
640 50 ▶ if $release && $release->{status} eq 'deployed' && defined $release->{chart_version};
Branch: 1 branch
BranchTrueFalseTotal
if $release and $release->{"status"} eq "deployed" and defined $release->{"chart_version"} 23 27 50
Condition100%6/6
$release and $release->{"status"} eq "deployed" and defined $release->{"chart_version"}
!ll&&!rl&&r
23423
$release and $release->{"status"} eq "deployed"
!ll&&!rl&&r
121127
MC/DC100%3/3
$release and $release->{"status"} eq "deployed" and defined $release->{"chart_version"}
$release $release->{"status"} eq "deployed" defined $release->{"chart_version"}
Truth table100%4/4
A and B and C
A= $releaseB= $release->{"status"} eq "deployed"C= defined $release->{"chart_version"}
ABCresult
0XX0
10X0
1100
1111
641  
642 50 ▶ if (my $ds = _get_optional($api, 'DaemonSet', RELEASE_NAME)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ds = _get_optional($api, "DaemonSet", "cilium")) 18 31 49
643 18 $running{k8s_service_host} = _daemonset_env($ds, 'KUBERNETES_SERVICE_HOST');
644 18 my $image = _daemonset_version($ds);
645 18 ▶ $running{version} = $image if defined $image;
Branch: 1 branch
BranchTrueFalseTotal
if defined $image 1 17 18
646 }
647  
648 49 return \%running;
649 }
650  
651 # upgrade_cilium upgrades the deployed revision of Cilium's Helm release:
652 # `cilium upgrade` is a Helm upgrade without --install. Helm refuses it while
653 # another operation is pending on the release, and without a deployed
654 # revision ("has no deployed releases") -- a cilium-config or cilium
655 # DaemonSet does not change that. Either way it would fail only after the
656 # CLI, the Gateway API CRDs and the values file are in place, so this dies
657 # before the host is touched, before the running Cilium is read, and before
658 # a k3s k8sServiceHost check that would only report a consequence. A pending
659 # upgrade or rollback, or a pending install over a deployed revision, dies
660 # as in install_cilium (_refuse_stuck_release). A failed upgrade over a
661 # deployed revision upgrades again.
662 31 ▶sub _require_deployed_release {
Subroutine _require_deployed_release: called CC=7 SCAR=19.5
663 31 my ($release) = @_;
664 31 ▶ my $status = $release ? $release->{status} : '';
Branch: 1 branch
BranchTrueFalseTotal
$release ? : 26 5 31
665  
666 31 _refuse_stuck_release($release);
667  
668 20 ▶ return if $release && $release->{has_deployed};
Branch: 1 branch
BranchTrueFalseTotal
if $release and $release->{"has_deployed"} 10 10 20
Condition100%3/3
$release and $release->{"has_deployed"}
!ll&&!rl&&r
5510
MC/DC100%2/2
$release and $release->{"has_deployed"}
$release $release->{"has_deployed"}
Truth table100%3/3
A and B
A= $releaseB= $release->{"has_deployed"}
ABresult
0X0
100
111
669  
670 die "upgrade_cilium found no deployed revision of Helm release " . RELEASE_NAME
671 . " in " . RELEASE_NAMESPACE . " ("
672 10 ▶ . ( $release ? "latest revision " . ( $release->{revision} // '?' ) . " is $status" : 'no release' )
Branch: 2 branches
BranchTrueFalseTotal
$release ? : 5 5 10
$release ? : 5 5 10
Condition50%1/2
$release->{"revision"} // "?"
l!l
50
MC/DC0%0/1
$release->{"revision"} // "?"
$release->{"revision"}
Truth table50%1/2
A
A= $release->{"revision"}
Aresult
11
00
673 . "): cilium upgrade upgrades a deployed Helm release and cannot install "
674 . "one, whether or not cilium-config or the cilium DaemonSet exist. "
675 . ( $release
676 ? "Use install_cilium, which removes the release left behind and installs Cilium again\n"
677 : "Install Cilium with install_cilium\n" );
678 }
679  
680 # The cilium-agent image tag as a version (quay.io/cilium/cilium:v1.20.0@sha256:...
681 # gives 1.20.0), or undef when the tag is no version (a digest only, latest).
682 22 ▶sub _daemonset_version {
Subroutine _daemonset_version: called CC=5 SCAR=16.1
683 22 my ($ds) = @_;
684 22 ▶ my $containers = eval { $ds->spec->template->spec->containers } // [];
Condition100%2/2
eval { do { $ds->spec->template->spec->containers } } // []
l!l
1210
MC/DC100%1/1
eval { do { $ds->spec->template->spec->containers } } // []
eval { do { $ds->spec->template->spec->containers } }
Truth table100%2/2
A
A= eval { do { $ds->spec->template->spec->containers } }
Aresult
11
00
685 22 for my $c (grep { $_->name eq 'cilium-agent' } @$containers) {
686 12 ▶ my ($tag) = ( $c->image // '' ) =~ m{:v?(\d+\.\d+\.\d+[^\@/:]*)(?:\@|\z)};
Condition50%1/2
$c->image // ""
l!l
120
MC/DC0%0/1
$c->image // ""
$c->image
Truth table50%1/2
A
A= $c->image
Aresult
11
00
687 12 ▶ return $tag if defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag 3 9 12
688 }
689 19 return;
690 }
691  
692 18 ▶sub _daemonset_env {
Subroutine _daemonset_env: called CC=8 SCAR=22.5
693 18 my ($ds, $name) = @_;
694 18 ▶ my $containers = eval { $ds->spec->template->spec->containers } // [];
Condition100%2/2
eval { do { $ds->spec->template->spec->containers } } // []
l!l
810
MC/DC100%1/1
eval { do { $ds->spec->template->spec->containers } } // []
eval { do { $ds->spec->template->spec->containers } }
Truth table100%2/2
A
A= eval { do { $ds->spec->template->spec->containers } }
Aresult
11
00
695 0 for my $c (sort { ($b->name eq 'cilium-agent') <=> ($a->name eq 'cilium-agent') } @$containers) {
696 8 ▶ for my $env (@{ $c->env // [] }) {
Condition100%2/2
$c->env // []
l!l
71
MC/DC100%1/1
$c->env // []
$c->env
Truth table100%2/2
A
A= $c->env
Aresult
11
00
697 2 ▶ return $env->value if $env->name eq $name && defined $env->value && length $env->value;
Branch: 1 branch
BranchTrueFalseTotal
if $env->name eq $name and defined $env->value and length $env->value 2 0 2
Condition33%2/6
$env->name eq $name and defined $env->value and length $env->value
!ll&&!rl&&r
002
$env->name eq $name and defined $env->value
!ll&&!rl&&r
002
MC/DC0%0/3
$env->name eq $name and defined $env->value and length $env->value
$env->name eq $name defined $env->value length $env->value
Truth table25%1/4
A and B and C
A= $env->name eq $nameB= defined $env->valueC= length $env->value
ABCresult
0XX0
10X0
1100
1111
698 }
699 }
700 16 return;
701 }
702  
703 # Fold the running configuration into $o->{values} wherever the caller left
704 # a default, and die where the caller asked for something a running Cilium
705 # cannot switch to. Nothing running reads as an empty $running, which
706 # changes nothing. Pure but for the cluster_cidr and ipam_mode warnings.
707 68 ▶sub _adopt_running {
Subroutine _adopt_running: called CC=32 SCAR=35.4
708 68 my ($o, $running) = @_;
709 68 my %values = %{ $o->{values} };
710 68 my $explicit = $o->{explicit};
711  
712 68 ▶ if (defined( my $mode = $running->{ipam_mode} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $mode = $running->{"ipam_mode"})) 36 32 68
713 36 ▶ my %ipam = ref $values{ipam} eq 'HASH' ? %{ $values{ipam} } : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $values{'ipam'} eq 'HASH' ? : 36 0 36
714 36 my $want = $ipam{mode};
715  
716 36 ▶ if ($explicit->{ipam_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($explicit->{'ipam_mode'}) { } 7 29 36
717 die "Cilium runs ipam.mode $mode (ConfigMap " . RELEASE_NAMESPACE . "/"
718 . CILIUM_CONFIGMAP . "), the requested values ipam.mode "
719 . ( $want // 'unset' ) . ": Cilium cannot change the IPAM mode of a "
720 . "running cluster (pods lose their addresses). Redeploy the cluster, "
721 . "or leave ipam.mode out of helm_values to keep it.\n"
722 7 ▶ if ref $values{ipam} eq 'HASH' && ( $want // '' ) ne $mode;
Branch: 1 branch
BranchTrueFalseTotal
if ref $values{"ipam"} eq "HASH" and ($want // "") ne $mode 4 3 7
Condition57%4/7
ref $values{"ipam"} eq "HASH" and ($want // "") ne $mode
!ll&&!rl&&r
034
$want // ""
l!l
70
$want // "unset"
l!l
40
MC/DC0%0/2
ref $values{"ipam"} eq "HASH" and ($want // "") ne $mode
ref $values{"ipam"} eq "HASH" $want
MC/DC0%0/1
$want // "unset"
$want
Truth table33%1/3
A and B
A= ref $values{"ipam"} eq "HASH"B= $want
ABresult
0X0
100
111
Truth table50%1/2
A
A= $want
Aresult
11
00
723 }
724 else {
725 # ipam_mode, like the distribution's default, is the mode of a fresh
726 # install; only the caller's ipam_mode is worth a warning.
727 _warn_ipam_mode_kept($o->{ipam_mode}, $mode)
728 29 ▶ if defined $o->{ipam_mode} && $o->{ipam_mode} ne $mode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $o->{"ipam_mode"} and $o->{"ipam_mode"} ne $mode 4 25 29
Condition100%3/3
defined $o->{"ipam_mode"} and $o->{"ipam_mode"} ne $mode
!ll&&!rl&&r
2414
MC/DC100%2/2
defined $o->{"ipam_mode"} and $o->{"ipam_mode"} ne $mode
defined $o->{"ipam_mode"} $o->{"ipam_mode"} ne $mode
Truth table100%3/3
A and B
A= defined $o->{"ipam_mode"}B= $o->{"ipam_mode"} ne $mode
ABresult
0X0
100
111
729 29 ▶ $ipam{mode} = $mode if ref $values{ipam} eq 'HASH' || !exists $values{ipam};
Branch: 1 branch
BranchTrueFalseTotal
if ref $values{"ipam"} eq "HASH" or not exists $values{"ipam"} 29 0 29
Condition33%1/3
ref $values{"ipam"} eq "HASH" or not exists $values{"ipam"}
l!l&&r!l&&!r
2900
MC/DC0%0/2
ref $values{"ipam"} eq "HASH" or not exists $values{"ipam"}
ref $values{"ipam"} eq "HASH" not exists $values{"ipam"}
Truth table33%1/3
A or not(B)
A= ref $values{"ipam"} eq "HASH"B= not exists $values{"ipam"}
ABresult
1X1
011
000
730 }
731  
732 32 ▶ if (ref $values{ipam} eq 'HASH' || !exists $values{ipam}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $values{"ipam"} eq "HASH" or not exists $values{"ipam"}) 32 0 32
Condition33%1/3
ref $values{"ipam"} eq "HASH" or not exists $values{"ipam"}
l!l&&r!l&&!r
3200
MC/DC0%0/2
ref $values{"ipam"} eq "HASH" or not exists $values{"ipam"}
ref $values{"ipam"} eq "HASH" not exists $values{"ipam"}
Truth table33%1/3
A or not(B)
A= ref $values{"ipam"} eq "HASH"B= not exists $values{"ipam"}
ABresult
1X1
011
000
733 32 ▶ my %op = ref $ipam{operator} eq 'HASH' ? %{ $ipam{operator} } : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $ipam{'operator'} eq 'HASH' ? : 20 12 32
734 32 ▶ if ($explicit->{pool}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($explicit->{'pool'}) { } 4 28 32
735 4 my $pool = $op{clusterPoolIPv4PodCIDRList};
736 4 ▶ my @want = ref $pool eq 'ARRAY' ? @$pool : defined $pool ? ($pool) : ();
Branch: 2 branches
BranchTrueFalseTotal
ref $pool eq 'ARRAY' ? : 4 0 4
defined $pool ? : 0 0 0
737 2 die "Cilium's cluster-pool is @{ $running->{pool} } (ConfigMap "
738 . RELEASE_NAMESPACE . "/" . CILIUM_CONFIGMAP . "), the requested "
739 . "clusterPoolIPv4PodCIDRList @want: Cilium cannot move the pool of a "
740 . "running cluster. Redeploy the cluster, or leave the pool out of "
741 . "helm_values to keep it.\n"
742 if $mode eq 'cluster-pool' && $running->{pool}
743 4 ▶ && join(' ', sort @want) ne join(' ', sort @{ $running->{pool} });
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "cluster-pool" and $running->{"pool"} and join(" ", sort(@want)) ne join(" ", sort(@{$running->{"pool"};})) 2 2 4
Condition83%5/6
$mode eq "cluster-pool" and $running->{"pool"} and join(" ", sort(@want)) ne join(" ", sort(@{$running->{"pool"};}))
!ll&&!rl&&r
112
$mode eq "cluster-pool" and $running->{"pool"}
!ll&&!rl&&r
103
MC/DC66%2/3
$mode eq "cluster-pool" and $running->{"pool"} and join(" ", sort(@want)) ne join(" ", sort(@{$running->{"pool"};}))
$mode eq "cluster-pool" $running->{"pool"} join(" ", sort(@want)) ne join(" ", sort(@{$running->{"pool"};}))
Truth table75%3/4
A and B and C
A= $mode eq "cluster-pool"B= $running->{"pool"}C= join(" ", sort(@want)) ne join(" ", sort(@{$running->{"pool"};}))
ABCresult
0XX0
10X0
1100
1111
744 }
745 else {
746 # Our default pool gives way: the running one in cluster-pool mode,
747 # none otherwise (without a readable one the chart's default runs,
748 # and stays). cluster_cidr is such a default: it only says what a
749 # fresh install gets.
750 _warn_cluster_cidr_kept($o->{cluster_cidr}, $running->{pool})
751 28 ▶ if $mode eq 'cluster-pool' && defined $o->{cluster_cidr};
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "cluster-pool" and defined $o->{"cluster_cidr"} 5 23 28
Condition100%3/3
$mode eq "cluster-pool" and defined $o->{"cluster_cidr"}
!ll&&!rl&&r
8155
MC/DC100%2/2
$mode eq "cluster-pool" and defined $o->{"cluster_cidr"}
$mode eq "cluster-pool" defined $o->{"cluster_cidr"}
Truth table100%3/3
A and B
A= $mode eq "cluster-pool"B= defined $o->{"cluster_cidr"}
ABresult
0X0
100
111
752 28 delete $op{clusterPoolIPv4PodCIDRList};
753 18 $op{clusterPoolIPv4PodCIDRList} = [ @{ $running->{pool} } ]
754 28 ▶ if $mode eq 'cluster-pool' && $running->{pool};
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "cluster-pool" and $running->{"pool"} 18 10 28
Condition100%3/3
$mode eq "cluster-pool" and $running->{"pool"}
!ll&&!rl&&r
8218
MC/DC100%2/2
$mode eq "cluster-pool" and $running->{"pool"}
$mode eq "cluster-pool" $running->{"pool"}
Truth table100%3/3
A and B
A= $mode eq "cluster-pool"B= $running->{"pool"}
ABresult
0X0
100
111
755 }
756 30 ▶ if (%op) { $ipam{operator} = \%op } else { delete $ipam{operator} }
Branch: 1 branch
BranchTrueFalseTotal
if (%op) { } 20 10 30
757 30 $values{ipam} = \%ipam;
758 }
759 }
760  
761 $values{k8sServiceHost} = $running->{k8s_service_host}
762 if $o->{dist}->needs_k8s_service_host && !$explicit->{k8s_service_host}
763 62 ▶ && defined $running->{k8s_service_host};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"dist"}->needs_k8s_service_host and not $explicit->{"k8s_service_host"} and defined $running->{"k8s_service_host"} 2 60 62
Condition100%6/6
$o->{"dist"}->needs_k8s_service_host and not $explicit->{"k8s_service_host"} and defined $running->{"k8s_service_host"}
!ll&&!rl&&r
5912
$o->{"dist"}->needs_k8s_service_host and not $explicit->{"k8s_service_host"}
!ll&&!rl&&r
47123
MC/DC100%3/3
$o->{"dist"}->needs_k8s_service_host and not $explicit->{"k8s_service_host"} and defined $running->{"k8s_service_host"}
$o->{"dist"}->needs_k8s_service_host not $explicit->{"k8s_service_host"} defined $running->{"k8s_service_host"}
Truth table100%4/4
A and not(B) and C
A= $o->{"dist"}->needs_k8s_service_hostB= not $explicit->{"k8s_service_host"}C= defined $running->{"k8s_service_host"}
ABCresult
0XX0
10X0
1100
1111
764  
765 62 ▶ if (!$explicit->{operator_replicas} && defined $running->{operator_replicas}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $explicit->{"operator_replicas"} and defined $running->{"operator_replicas"}) 14 48 62
Condition100%3/3
not $explicit->{"operator_replicas"} and defined $running->{"operator_replicas"}
!ll&&!rl&&r
14714
MC/DC100%2/2
not $explicit->{"operator_replicas"} and defined $running->{"operator_replicas"}
not $explicit->{"operator_replicas"} defined $running->{"operator_replicas"}
Truth table100%3/3
not(A) and B
A= not $explicit->{"operator_replicas"}B= defined $running->{"operator_replicas"}
ABresult
0X0
100
111
766 14 ▶ my %operator = ref $values{operator} eq 'HASH' ? %{ $values{operator} } : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $values{'operator'} eq 'HASH' ? : 14 0 14
767 14 $operator{replicas} = $running->{operator_replicas};
768 14 $values{operator} = \%operator;
769 }
770  
771 62 $o->{values} = \%values;
772 62 return $o;
773 }
774  
775 # cluster_cidr on a running cluster-pool Cilium with another (or an
776 # unreadable) pool: the running pool stays, loudly. Not fatal -- the caller
777 # (kubernetes-ocp passes its pod_cidr on every run) cannot tell a fresh
778 # cluster from an old one, and dying would make an old cluster undeployable.
779 5 ▶sub _warn_cluster_cidr_kept {
Subroutine _warn_cluster_cidr_kept: called CC=5 SCAR=16.1
780 5 my ($cidr, $pool) = @_;
781 5 ▶ return if $pool && @$pool == 1 && $pool->[0] eq $cidr;
Branch: 1 branch
BranchTrueFalseTotal
if $pool and @$pool == 1 and $pool->[0] eq $cidr 1 4 5
Condition83%5/6
$pool and @$pool == 1 and $pool->[0] eq $cidr
!ll&&!rl&&r
131
$pool and @$pool == 1
!ll&&!rl&&r
104
MC/DC66%2/3
$pool and @$pool == 1 and $pool->[0] eq $cidr
$pool @$pool == 1 $pool->[0] eq $cidr
Truth table75%3/4
A and B and C
A= $poolB= @$pool == 1C= $pool->[0] eq $cidr
ABCresult
0XX0
10X0
1100
1111
782  
783 4 ▶ Rex::Logger::info("cluster_cidr $cidr is not applied: Cilium already runs "
Branch: 1 branch
BranchTrueFalseTotal
$pool ? : 3 1 4
784 . "cluster-pool " . ( $pool ? "@$pool" : "with the chart's default pool" )
785 . " (ConfigMap " . RELEASE_NAMESPACE . "/" . CILIUM_CONFIGMAP . "), and the "
786 . "pool of a running cluster cannot move. Keeping the running pool; "
787 . "redeploy the cluster to use $cidr", 'warn');
788 }
789  
790 # ipam_mode on a running Cilium in another mode: the running mode stays,
791 # loudly, for the same reason as the pool above -- the caller may not know
792 # whether the cluster is fresh.
793 4 ▶sub _warn_ipam_mode_kept {
Subroutine _warn_ipam_mode_kept: called CC=1 SCAR=0.0
794 4 my ($want, $mode) = @_;
795 4 Rex::Logger::info("ipam_mode $want is not applied: Cilium already runs "
796 . "ipam.mode $mode (ConfigMap " . RELEASE_NAMESPACE . "/" . CILIUM_CONFIGMAP
797 . "), and the IPAM mode of a running cluster cannot change. Keeping $mode; "
798 . "redeploy the cluster to use $want", 'warn');
799 }
800  
801 # Cilium upgrades and rolls back one minor version at a time (its upgrade
802 # guide: the only tested path is between consecutive minors). Against the
803 # version a Cilium on the cluster runs:
804 # - pinned: at most one minor away, either way; more dies before the host
805 # is touched, naming the step to take;
806 # - default: a patch release of the running minor is taken; a newer minor
807 # or an older version is not -- the running version is kept, a newer
808 # minor with a warning. The default is what a fresh install gets, as
809 # for rke2/k3s's own version: a re-run after a library upgrade must not
810 # jump a cluster several minors or pull it back.
811 # Nothing running, or a version that does not parse: nothing to compare.
812 53 ▶sub _settle_version {
Subroutine _settle_version: called CC=13 SCAR=25.7
813 53 my ($o, $running) = @_;
814 53 ▶ return unless defined $running;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $running 19 34 53
815 34 ▶ my @have = _minor_version($running) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@have) = _minor_version($running) or return 33 1 34
816 33 ▶ my @want = _minor_version($o->{version}) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@want) = _minor_version($o->{"version"}) or return 33 0 33
817  
818 33 ▶ my $apart = $want[0] != $have[0] ? 99 : abs($want[1] - $have[1]);
Branch: 1 branch
BranchTrueFalseTotal
$want[0] != $have[0] ? : 1 32 33
819  
820 33 ▶ if ($o->{version_pinned}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"version_pinned"}) 10 23 33
821 10 ▶ return if $apart <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $apart <= 1 5 5 10
822 5 ▶ my $step = $want[0] == $have[0]
Branch: 2 branches
BranchTrueFalseTotal
$want[0] == $have[0] ? : 4 1 5
$want[1] > $have[1] ? : 3 1 4
823 ? "$have[0]." . ( $want[1] > $have[1] ? $have[1] + 1 : $have[1] - 1 )
824 : 'the next minor';
825 die "Cilium runs " . _norm_version($running) . ", version "
826 5 . _norm_version($o->{version}) . " is more than one minor version away: "
827 . "Cilium upgrades and rolls back one minor at a time. Go to the latest "
828 . "$step.x first, then on\n";
829 }
830  
831 23 my $cmp = _cmp_version($o->{version}, $running);
832 23 ▶ return if $cmp > 0 && $apart == 0; # a newer patch of the running minor
Branch: 1 branch
BranchTrueFalseTotal
if $cmp > 0 and $apart == 0 1 22 23
Condition100%3/3
$cmp > 0 and $apart == 0
!ll&&!rl&&r
7151
MC/DC100%2/2
$cmp > 0 and $apart == 0
$cmp > 0 $apart == 0
Truth table100%3/3
A and B
A= $cmp > 0B= $apart == 0
ABresult
0X0
100
111
833 22 ▶ return if $cmp == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp == 0 5 17 22
834  
835 17 ▶ if ($cmp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp > 0) { } 15 2 17
836 Rex::Logger::info("Cilium runs " . _norm_version($running) . "; the default "
837 15 . _norm_version($o->{version}) . " is a newer minor version and is not "
838 . "applied to a running Cilium. Keeping " . _norm_version($running)
839 . "; pass version (cilium_version) to upgrade, one minor at a time", 'warn');
840 }
841 else {
842 Rex::Logger::info("Cilium runs " . _norm_version($running) . ", newer than "
843 2 . "the default " . _norm_version($o->{version}) . "; keeping it");
844 }
845 17 $o->{version} = _norm_version($running);
846 17 return $o;
847 }
848  
849 # (major, minor) of 1.20.0 / v1.20.0-rc.1, or () when it is no version.
850 78 ▶sub _minor_version {
Subroutine _minor_version: called CC=2 SCAR=7.2
851 78 my ($v) = @_;
852 78 ▶ return ( ( $v // '' ) =~ /\Av?(\d+)\.(\d+)(?:\.\d+)?/ );
Condition50%1/2
$v // ""
l!l
780
MC/DC0%0/1
$v // ""
$v
Truth table50%1/2
A
A= $v
Aresult
11
00
853 }
854  
855 # <=> on the numeric major.minor.patch; anything after it is ignored.
856 32 ▶sub _cmp_version {
Subroutine _cmp_version: called CC=7 SCAR=20.3
857 32 my ($x, $y) = @_;
858 32 ▶ my @x = ( ( $x // '' ) =~ /\Av?(\d+)\.(\d+)(?:\.(\d+))?/ );
Condition50%1/2
$x // ""
l!l
320
MC/DC0%0/1
$x // ""
$x
Truth table50%1/2
A
A= $x
Aresult
11
00
859 32 ▶ my @y = ( ( $y // '' ) =~ /\Av?(\d+)\.(\d+)(?:\.(\d+))?/ );
Condition50%1/2
$y // ""
l!l
320
MC/DC0%0/1
$y // ""
$y
Truth table50%1/2
A
A= $y
Aresult
11
00
860 32 for my $i (0 .. 2) {
861 77 ▶ my $c = ( $x[$i] // 0 ) <=> ( $y[$i] // 0 );
Condition50%2/4
$x[$i] // 0
l!l
770
$y[$i] // 0
l!l
770
MC/DC0%0/1
$x[$i] // 0
$x[$i]
MC/DC0%0/1
$y[$i] // 0
$y[$i]
Truth table50%1/2
A
A= $x[$i]
Aresult
11
00
Truth table50%1/2
A
A= $y[$i]
Aresult
11
00
862 77 ▶ return $c if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 21 56 77
863 }
864 11 return 0;
865 }
866  
867 # Cilium 1.20 refuses to start its Gateway controller on a bundle older than
868 # v1.6.1 (TLSRoute and BackendTLSPolicy at v1). Checked on the version that
869 # will run, before the host is touched.
870 47 ▶sub _check_gateway_api_version {
Subroutine _check_gateway_api_version: called CC=7 SCAR=19.7
871 47 my ($o) = @_;
872 47 ▶ return unless $o->{gateway_api};
Branch: 1 branch
BranchTrueFalseTotal
unless $o->{"gateway_api"} 36 11 47
873 11 ▶ my @cilium = _minor_version($o->{version}) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@cilium) = _minor_version($o->{"version"}) or return 11 0 11
874 11 ▶ return unless $cilium[0] > 1 || ( $cilium[0] == 1 && $cilium[1] >= 20 );
Branch: 1 branch
BranchTrueFalseTotal
unless $cilium[0] > 1 or $cilium[0] == 1 and $cilium[1] >= 20 2 9 11
Condition66%4/6
$cilium[0] > 1 or $cilium[0] == 1 and $cilium[1] >= 20
l!l&&r!l&&!r
092
$cilium[0] == 1 and $cilium[1] >= 20
!ll&&!rl&&r
029
MC/DC33%1/3
$cilium[0] > 1 or $cilium[0] == 1 and $cilium[1] >= 20
$cilium[0] > 1 $cilium[0] == 1 $cilium[1] >= 20
Truth table50%2/4
A or B and C
A= $cilium[0] > 1B= $cilium[0] == 1C= $cilium[1] >= 20
ABCresult
1XX1
0111
00X0
0100
875 9 ▶ return if _cmp_version($o->{gateway_api_version}, GATEWAY_API_MIN_FOR_1_20) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if _cmp_version($o->{"gateway_api_version"}, "v1.6.1") >= 0 7 2 9
876 2 die "Cilium " . _norm_version($o->{version}) . " needs Gateway API "
877 . GATEWAY_API_MIN_FOR_1_20 . " or newer (TLSRoute and BackendTLSPolicy at "
878 . "v1), gateway_api_version is $o->{gateway_api_version}: raise it, or pin "
879 . "version (cilium_version) to a Cilium that supports it\n";
880 }
881  
882 #
883 # Readiness: the cilium DaemonSet and the cilium-operator Deployment
884 #
885  
886 0 ▶sub _sleep { sleep $_[0] }
Subroutine _sleep: not called CC=1 SCAR=6.9
887  
888 3 ▶sub _wait_ready {
Subroutine _wait_ready: called CC=5 SCAR=16.1
889 3 my ($api, $duration) = @_;
890  
891 3 ▶ my $attempts = int(($duration + WAIT_INTERVAL - 1) / WAIT_INTERVAL) || 1;
Condition50%1/2
int(($duration + 5 - 1) / 5) || 1
l!l
30
MC/DC0%0/1
int(($duration + 5 - 1) / 5) || 1
int(($duration + 5 - 1) / 5)
Truth table50%1/2
A
A= int(($duration + 5 - 1) / 5)
Aresult
11
00
892 3 Rex::Logger::info("Waiting up to ${duration}s for Cilium to be ready");
893  
894 3 my $state;
895 3 for my $i (1 .. $attempts) {
896 # Missing is a state to wait out; any other API error (401, 403, no
897 # connection) dies now instead of reading as "not found" for 600s.
898 6 $state = _readiness(
899 scalar _get_optional($api, 'DaemonSet', RELEASE_NAME),
900 scalar _get_optional($api, 'Deployment', 'cilium-operator'),
901 );
902 6 ▶ if ($state->{ready}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"ready"}) 1 5 6
903 1 Rex::Logger::info(" Cilium ready: $state->{detail}");
904 1 return 1;
905 }
906 5 Rex::Logger::info(" $state->{detail} ($i/$attempts)");
907 5 ▶ _sleep(WAIT_INTERVAL) if $i < $attempts;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $attempts 3 2 5
908 }
909  
910 2 die "Cilium was not ready within ${duration}s: $state->{detail}. Check the "
911 . "cilium and cilium-operator pods in " . RELEASE_NAMESPACE . " (events, "
912 . "logs), or `cilium status` on the server\n";
913 }
914  
915 # Pure: ready when the DaemonSet has rolled out its current generation to
916 # every node it wants and all of those pods are ready, and the operator has
917 # all its replicas updated and ready.
918 12 ▶sub _readiness {
Subroutine _readiness: called CC=30 SCAR=38.8
919 12 my ($ds, $op) = @_;
920 12 my ($ds_ok, $ds_txt) = (0, "DaemonSet " . RELEASE_NAMESPACE . "/cilium not found");
921 12 my ($op_ok, $op_txt) = (0, "Deployment " . RELEASE_NAMESPACE . "/cilium-operator not found");
922  
923 12 ▶ if ($ds) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ds) 9 3 12
924 9 my $st = $ds->status;
925 9 ▶ my $desired = $st ? $st->desiredNumberScheduled // 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$st ? : 9 0 9
Condition50%1/2
$st->desiredNumberScheduled // 0
l!l
90
MC/DC0%0/1
$st->desiredNumberScheduled // 0
$st->desiredNumberScheduled
Truth table50%1/2
A
A= $st->desiredNumberScheduled
Aresult
11
00
926 9 ▶ my $ready = $st ? $st->numberReady // 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$st ? : 9 0 9
Condition50%1/2
$st->numberReady // 0
l!l
90
MC/DC0%0/1
$st->numberReady // 0
$st->numberReady
Truth table50%1/2
A
A= $st->numberReady
Aresult
11
00
927 9 ▶ my $updated = $st ? $st->updatedNumberScheduled // 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$st ? : 9 0 9
Condition50%1/2
$st->updatedNumberScheduled // 0
l!l
90
MC/DC0%0/1
$st->updatedNumberScheduled // 0
$st->updatedNumberScheduled
Truth table50%1/2
A
A= $st->updatedNumberScheduled
Aresult
11
00
928 9 ▶ my $current = ( $st ? $st->observedGeneration // 0 : 0 ) >= ( $ds->metadata->generation // 0 );
Branch: 1 branch
BranchTrueFalseTotal
$st ? : 9 0 9
Condition50%2/4
$st->observedGeneration // 0
l!l
90
$ds->metadata->generation // 0
l!l
90
MC/DC0%0/1
$st->observedGeneration // 0
$st->observedGeneration
MC/DC0%0/1
$ds->metadata->generation // 0
$ds->metadata->generation
Truth table50%1/2
A
A= $st->observedGeneration
Aresult
11
00
Truth table50%1/2
A
A= $ds->metadata->generation
Aresult
11
00
929 9 ▶ $ds_ok = $current && $desired > 0 && $ready == $desired && $updated == $desired;
Condition77%7/9
$current && $desired > 0 && $ready == $desired && $updated == $desired
!ll&&!rl&&r
603
$current and $desired > 0 and $ready == $desired
!ll&&!rl&&r
153
$current and $desired > 0
!ll&&!rl&&r
108
MC/DC50%2/4
$current && $desired > 0 && $ready == $desired && $updated == $desired
$current $desired > 0 $ready == $desired $updated == $desired
Truth table60%3/5
A and B and C && D
A= $currentB= $desired > 0C= $ready == $desiredD= $updated == $desired
ABCDresult
0XXX0
10XX0
110X0
11100
11111
930 9 ▶ $ds_txt = "cilium $ready/$desired ready, $updated/$desired updated"
Branch: 1 branch
BranchTrueFalseTotal
$current ? : 8 1 9
931 . ( $current ? '' : ', rollout not observed yet' );
932 }
933  
934 12 ▶ if ($op) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op) 11 1 12
935 11 my $st = $op->status;
936 11 ▶ my $want = ( $op->spec ? $op->spec->replicas : undef ) // 1;
Branch: 1 branch
BranchTrueFalseTotal
$op->spec ? : 11 0 11
Condition50%1/2
($op->spec ? $op->spec->replicas : undef) // 1
l!l
110
MC/DC0%0/1
($op->spec ? $op->spec->replicas : undef) // 1
($op->spec ? $op->spec->replicas : undef)
Truth table50%1/2
A
A= ($op->spec ? $op->spec->replicas : undef)
Aresult
11
00
937 11 ▶ my $ready = $st ? $st->readyReplicas // 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$st ? : 11 0 11
Condition50%1/2
$st->readyReplicas // 0
l!l
110
MC/DC0%0/1
$st->readyReplicas // 0
$st->readyReplicas
Truth table50%1/2
A
A= $st->readyReplicas
Aresult
11
00
938 11 ▶ my $updated = $st ? $st->updatedReplicas // 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$st ? : 11 0 11
Condition50%1/2
$st->updatedReplicas // 0
l!l
110
MC/DC0%0/1
$st->updatedReplicas // 0
$st->updatedReplicas
Truth table50%1/2
A
A= $st->updatedReplicas
Aresult
11
00
939 11 ▶ my $current = ( $st ? $st->observedGeneration // 0 : 0 ) >= ( $op->metadata->generation // 0 );
Branch: 1 branch
BranchTrueFalseTotal
$st ? : 11 0 11
Condition50%2/4
$st->observedGeneration // 0
l!l
110
$op->metadata->generation // 0
l!l
110
MC/DC0%0/1
$st->observedGeneration // 0
$st->observedGeneration
MC/DC0%0/1
$op->metadata->generation // 0
$op->metadata->generation
Truth table50%1/2
A
A= $st->observedGeneration
Aresult
11
00
Truth table50%1/2
A
A= $op->metadata->generation
Aresult
11
00
940 11 ▶ $op_ok = $current && $ready >= $want && $updated >= $want;
Condition66%4/6
$current && $ready >= $want && $updated >= $want
!ll&&!rl&&r
1010
$current and $ready >= $want
!ll&&!rl&&r
0110
MC/DC33%1/3
$current && $ready >= $want && $updated >= $want
$current $ready >= $want $updated >= $want
Truth table50%2/4
A and B && C
A= $currentB= $ready >= $wantC= $updated >= $want
ABCresult
0XX0
10X0
1100
1111
941 11 ▶ $op_txt = "cilium-operator $ready/$want ready, $updated/$want updated"
Branch: 1 branch
BranchTrueFalseTotal
$current ? : 11 0 11
942 . ( $current ? '' : ', rollout not observed yet' );
943 }
944  
945 12 ▶ return { ready => ( $ds_ok && $op_ok ) ? 1 : 0, detail => "$ds_txt; $op_txt" };
Branch: 1 branch
BranchTrueFalseTotal
$ds_ok && $op_ok ? : 2 10 12
Condition100%3/3
$ds_ok and $op_ok
!ll&&!rl&&r
912
MC/DC100%2/2
$ds_ok and $op_ok
$ds_ok $op_ok
Truth table100%3/3
A and B
A= $ds_okB= $op_ok
ABresult
0X0
100
111
946 }
947  
948 116 ▶sub _norm_version {
Subroutine _norm_version: called CC=1 SCAR=0.0
949 116 my ($v) = @_;
950 116 $v =~ s/^v//;
951 116 return $v;
952 }
953  
954 # True when every value in $want is present with the same value in $have.
955 # $have is the release's full user config, which carries more than we set
956 # (the CLI adds its own detected values), so equality would never hold.
957 118 ▶sub _values_subset {
Subroutine _values_subset: called CC=13 SCAR=26.0
958 118 my ($want, $have) = @_;
959  
960 118 ▶ if (ref $want eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $want eq "HASH") 50 68 118
961 50 ▶ return 0 unless ref $have eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $have eq "HASH" 1 49 50
962 49 for my $k (keys %$want) {
963 101 ▶ return 0 unless exists $have->{$k} && _values_subset($want->{$k}, $have->{$k});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $have->{$k} and _values_subset($want->{$k}, $have->{$k}) 20 81 101
Condition100%3/3
exists $have->{$k} and _values_subset($want->{$k}, $have->{$k})
!ll&&!rl&&r
61481
MC/DC100%2/2
exists $have->{$k} and _values_subset($want->{$k}, $have->{$k})
exists $have->{$k} _values_subset($want->{$k}, $have->{$k})
Truth table100%3/3
A and B
A= exists $have->{$k}B= _values_subset($want->{$k}, $have->{$k})
ABresult
0X0
100
111
964 }
965 29 return 1;
966 }
967 68 ▶ if (ref $want eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $want eq "ARRAY") 2 66 68
968 2 ▶ return 0 unless ref $have eq 'ARRAY' && @$want == @$have;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $have eq "ARRAY" and @$want == @$have 1 1 2
Condition66%2/3
ref $have eq "ARRAY" and @$want == @$have
!ll&&!rl&&r
011
MC/DC50%1/2
ref $have eq "ARRAY" and @$want == @$have
ref $have eq "ARRAY" @$want == @$have
Truth table66%2/3
A and B
A= ref $have eq "ARRAY"B= @$want == @$have
ABresult
0X0
100
111
969 1 for my $i (0 .. $#$want) {
970 2 ▶ return 0 unless _values_subset($want->[$i], $have->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless _values_subset($want->[$i], $have->[$i]) 0 2 2
971 }
972 1 return 1;
973 }
974 66 ▶ return 0 if ref $have eq 'HASH' || ref $have eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $have eq "HASH" or ref $have eq "ARRAY" 0 66 66
Condition33%1/3
ref $have eq "HASH" or ref $have eq "ARRAY"
l!l&&r!l&&!r
0066
MC/DC0%0/2
ref $have eq "HASH" or ref $have eq "ARRAY"
ref $have eq "HASH" ref $have eq "ARRAY"
Truth table33%1/3
A or B
A= ref $have eq "HASH"B= ref $have eq "ARRAY"
ABresult
1X1
011
000
975 66 return _scalar_str($want) eq _scalar_str($have);
976 }
977  
978 132 ▶sub _scalar_str {
Subroutine _scalar_str: called CC=4 SCAR=13.9
979 132 my ($v) = @_;
980 132 ▶ return '~' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 132 132
981 132 ▶ return $v ? 'true' : 'false' if JSON::MaybeXS::is_bool($v);
Branch: 2 branches
BranchTrueFalseTotal
if JSON::MaybeXS::is_bool($v) 38 94 132
$v ? : 20 18 38
982 94 return "$v";
983 }
984  
985 # Remove a release that holds no working Cilium so a fresh install can run:
986 # cilium uninstall clears whatever the half-install created, then the Helm
987 # release Secrets go, since `cilium install` refuses (or silently no-ops)
988 # while any of them is left.
989 8 ▶sub _purge_release {
Subroutine _purge_release: called CC=4 SCAR=13.9
990 8 my ($api, $o, $release) = @_;
991  
992 8 Rex::Logger::info(" Helm release is $release->{status} with no working Cilium, "
993 . "removing it before a fresh install", 'warn');
994  
995 # auto_die => 0: on a half-installed release some of what uninstall
996 # removes was never created, and it reports that as an error.
997 8 run "KUBECONFIG=" . $o->{dist}->kubeconfig . " cilium uninstall --wait=false 2>&1", auto_die => 0;
998  
999 # Already gone is what we want; any other API error dies before the
1000 # install, which Helm would refuse over a Secret left behind anyway.
1001 8 for my $name (@{ $release->{secrets} }) {
1002 9 ▶ eval { $api->delete('Secret', $name, namespace => RELEASE_NAMESPACE); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $api->delete("Secret", $name, "namespace", "kube-system"); 1 } } or _is_not_found($@) or die 'Cannot delete Secret kube-system' . "/${name}: $@" 8 1 9
Condition100%3/3
eval { do { $api->delete("Secret", $name, "namespace", "kube-system"); 1 } } or _is_not_found($@)
l!l&&r!l&&!r
711
MC/DC100%2/2
eval { do { $api->delete("Secret", $name, "namespace", "kube-system"); 1 } } or _is_not_found($@)
eval { do { $api->delete("Secret", $name, "namespace", "kube-system"); 1 } } _is_not_found($@)
Truth table100%3/3
A or B
A= eval { do { $api->delete("Secret", $name, "namespace", "kube-system"); 1 } }B= _is_not_found($@)
ABresult
1X1
011
000
1003 or _is_not_found($@)
1004 or die "Cannot delete Secret " . RELEASE_NAMESPACE . "/$name: $@";
1005 }
1006 }
1007  
1008 13 ▶sub _verify_daemonset {
Subroutine _verify_daemonset: called CC=2 SCAR=6.9
1009 13 my ($api) = @_;
1010  
1011 13 ▶ die "cilium install reported success but DaemonSet "
Branch: 1 branch
BranchTrueFalseTotal
unless _get_optional($api, "DaemonSet", "cilium") 1 11 12
1012 . RELEASE_NAMESPACE . "/cilium does not exist\n"
1013 unless _get_optional($api, 'DaemonSet', RELEASE_NAME);
1014 }
1015  
1016 6 ▶sub _restart_operator {
Subroutine _restart_operator: called CC=3 SCAR=11.0
1017 6 my ($api) = @_;
1018 6 ▶ return unless $api;
Branch: 1 branch
BranchTrueFalseTotal
unless $api 0 6 6
1019  
1020 # No operator (yet): nothing to restart. Any other API error dies -- a
1021 # 403 here used to skip the restart silently, leaving the operator on the
1022 # old CRDs.
1023 6 ▶ _get_optional($api, 'Deployment', 'cilium-operator') or return;
Branch: 1 branch
BranchTrueFalseTotal
_get_optional($api, "Deployment", "cilium-operator") or return 1 4 5
1024  
1025 1 Rex::Logger::info(" Restarting cilium-operator so it picks up the Gateway API CRDs");
1026 1 $api->patch('Deployment', 'cilium-operator',
1027 namespace => RELEASE_NAMESPACE,
1028 patch => { spec => { template => { metadata => { annotations => {
1029 'kubectl.kubernetes.io/restartedAt' => strftime('%Y-%m-%dT%H:%M:%SZ', gmtime),
1030 } } } } },
1031 );
1032 }
1033  
1034 #
1035 # Gateway API CRDs
1036 #
1037  
1038 3 ▶sub _gateway_api_url {
Subroutine _gateway_api_url: called CC=1 SCAR=0.0
1039 3 my ($version, $channel) = @_;
1040 3 return "https://github.com/kubernetes-sigs/gateway-api/releases/download/"
1041 . "$version/$channel-install.yaml";
1042 }
1043  
1044 # Apply unless the cluster already carries this bundle version and channel.
1045 6 ▶sub _gateway_api_needs_apply {
Subroutine _gateway_api_needs_apply: called CC=6 SCAR=18.1
1046 6 my ($annotations, $version, $channel) = @_;
1047 6 ▶ return 1 unless $annotations;
Branch: 1 branch
BranchTrueFalseTotal
unless $annotations 3 3 6
1048 3 ▶ return 1 unless ($annotations->{'gateway.networking.k8s.io/bundle-version'} // '') eq $version;
Branch: 1 branch
BranchTrueFalseTotal
unless ($annotations->{"gateway.networking.k8s.io/bundle-version"} // "") eq $version 1 2 3
Condition50%1/2
$annotations->{"gateway.networking.k8s.io/bundle-version"} // ""
l!l
30
MC/DC0%0/1
$annotations->{"gateway.networking.k8s.io/bundle-version"} // ""
$annotations->{"gateway.networking.k8s.io/bundle-version"}
Truth table50%1/2
A
A= $annotations->{"gateway.networking.k8s.io/bundle-version"}
Aresult
11
00
1049 2 ▶ return 1 unless ($annotations->{'gateway.networking.k8s.io/channel'} // '') eq $channel;
Branch: 1 branch
BranchTrueFalseTotal
unless ($annotations->{"gateway.networking.k8s.io/channel"} // "") eq $channel 1 1 2
Condition50%1/2
$annotations->{"gateway.networking.k8s.io/channel"} // ""
l!l
20
MC/DC0%0/1
$annotations->{"gateway.networking.k8s.io/channel"} // ""
$annotations->{"gateway.networking.k8s.io/channel"}
Truth table50%1/2
A
A= $annotations->{"gateway.networking.k8s.io/channel"}
Aresult
11
00
1050 1 return 0;
1051 }
1052  
1053 # Returns 1 when CRDs were applied, 0 when they were already current.
1054 4 ▶sub _ensure_gateway_api_crds {
Subroutine _ensure_gateway_api_crds: called CC=8 SCAR=31.2
1055 4 my ($api, $version, $channel) = @_;
1056  
1057 4 _refuse_rke2_gateway_api_chart($api);
1058  
1059 # Missing means apply; an unreadable probe dies instead of reading as
1060 # missing.
1061 3 my $probe = _get_optional($api, 'CustomResourceDefinition', GATEWAY_API_PROBE_CRD);
1062 2 ▶ my $annotations = $probe ? $probe->metadata->annotations : undef;
Branch: 1 branch
BranchTrueFalseTotal
$probe ? : 0 2 2
1063  
1064 2 ▶ unless (_gateway_api_needs_apply($annotations, $version, $channel)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_gateway_api_needs_apply($annotations, $version, $channel)) 0 2 2
1065 0 Rex::Logger::info("Gateway API CRDs $version ($channel) already applied");
1066 0 return 0;
1067 }
1068  
1069 2 my $url = _gateway_api_url($version, $channel);
1070 2 Rex::Logger::info("Applying Gateway API CRDs $version ($channel) from $url");
1071  
1072 2 my $res = HTTP::Tiny->new(verify_SSL => 1)->get($url);
1073 die "Cannot fetch Gateway API bundle $url: $res->{status} $res->{reason}\n"
1074 1 ▶ unless $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"success"} 1 0 1
1075  
1076 0 ▶ my @docs = grep { ref $_ eq 'HASH' && $_->{kind} }
Condition0%0/3
ref $_ eq 'HASH' && $_->{'kind'}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ eq 'HASH' && $_->{'kind'}
ref $_ eq 'HASH' $_->{'kind'}
Truth table0%0/3
A && B
A= ref $_ eq 'HASH'B= $_->{'kind'}
ABresult
0X0
100
111
1077 0 YAML::PP->new(boolean => 'JSON::PP')->load_string($res->{content});
1078 0 ▶ die "Gateway API bundle $url holds no objects\n" unless @docs;
Branch: 1 branch
BranchTrueFalseTotal
unless @docs 0 0 0
1079  
1080 # File order: CRDs first, the safe-upgrades admission policy after them.
1081 0 $api->ensure($_) for @docs;
1082  
1083 0 for my $crd (grep { $_->{kind} eq 'CustomResourceDefinition' } @docs) {
1084 0 _wait_crd_established($api, $crd->{metadata}{name});
1085 }
1086  
1087 0 Rex::Logger::info("Gateway API CRDs $version ($channel) applied");
1088 0 return 1;
1089 }
1090  
1091 # Two owners would take the CRDs from each other: RKE2's chart applies with
1092 # take-ownership and force-conflicts once pods can run (after Cilium), and
1093 # its safe-upgrades policy then refuses our experimental bundle. Only a live
1094 # release counts: disabling the chart uninstalls it but keeps the CRDs
1095 # (helm.sh/resource-policy: keep), Helm annotations and all.
1096 4 ▶sub _refuse_rke2_gateway_api_chart {
Subroutine _refuse_rke2_gateway_api_chart: called CC=3 SCAR=11.1
1097 4 my ($api) = @_;
1098 4 ▶ my $release = _read_release($api, RKE2_GATEWAY_API_RELEASE) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $release = _read_release($api, "rke2-gateway-api-crd") or return 1 3 4
1099  
1100 die "The Gateway API CRDs belong to RKE2's Helm release "
1101 . RKE2_GATEWAY_API_RELEASE . " ($release->{status}, chart "
1102 1 ▶ . ($release->{chart_version} // 'unknown') . "), which would overwrite "
Condition50%1/2
$release->{"chart_version"} // "unknown"
l!l
01
MC/DC0%0/1
$release->{"chart_version"} // "unknown"
$release->{"chart_version"}
Truth table50%1/2
A
A= $release->{"chart_version"}
Aresult
11
00
1103 . "what gateway_api applies. Add " . RKE2_GATEWAY_API_RELEASE
1104 . " to disable and restart rke2-server: the chart is uninstalled, its "
1105 . "gateway.networking.k8s.io CRDs (and the Gateways and routes) are "
1106 . "kept. Or drop gateway_api "
1107 . "and set gatewayAPI.enabled in helm_values to use RKE2's CRDs.\n";
1108 }
1109  
1110 2 ▶sub _wait_crd_established {
Subroutine _wait_crd_established: called CC=9 SCAR=24.0
1111 2 my ($api, $name) = @_;
1112  
1113 # Not visible yet is waited out; any other API error dies at once
1114 # instead of reading as "not Established" for 30s.
1115 2 for (1 .. 30) {
1116 3 my $crd = _get_optional($api, 'CustomResourceDefinition', $name);
1117 2 ▶ my $conditions = $crd && $crd->status ? $crd->status->conditions // [] : [];
Branch: 1 branch
BranchTrueFalseTotal
$crd && $crd->status ? : 1 1 2
Condition60%3/5
$crd and $crd->status
!ll&&!rl&&r
101
$crd->status->conditions // []
l!l
10
MC/DC50%1/2
$crd and $crd->status
$crd $crd->status
MC/DC0%0/1
$crd->status->conditions // []
$crd->status->conditions
Truth table66%2/3
A and B
A= $crdB= $crd->status
ABresult
0X0
100
111
Truth table50%1/2
A
A= $crd->status->conditions
Aresult
11
00
1118 return 1 if grep {
1119 2 ▶ ($_->type // '') eq 'Established' && ($_->status // '') eq 'True'
Branch: 1 branch
BranchTrueFalseTotal
if grep {($_->type // "") eq "Established" and ($_->status // "") eq "True";} @$conditions 1 1 2
Condition42%3/7
($_->type // '') eq 'Established' && ($_->status // '') eq 'True'
!ll&&!rl&&r
001
$_->type // ""
l!l
10
$_->status // ""
l!l
10
MC/DC0%0/2
($_->type // '') eq 'Established' && ($_->status // '') eq 'True'
$_->type $_->status
Truth table33%1/3
A && B
A= $_->typeB= $_->status
ABresult
0X0
100
111
1120 } @$conditions;
1121 1 _sleep(1);
1122 }
1123 0 die "CustomResourceDefinition $name was not Established within 30s\n";
1124 }
1125  
1126 #
1127 # Helm values generation
1128 #
1129  
1130 # The defaults both distributions share, the distribution's own
1131 # (cilium_helm_defaults: cni.exclusive, k8sServiceHost, the IPAM mode and
1132 # pool) over them, then gatewayAPI, then the caller's values on top.
1133 177 ▶sub _helm_values {
Subroutine _helm_values: called CC=3 SCAR=11.0
1134 177 my ($dist, $gateway_api, $extra, $k8s_service_host, $cluster_cidr, $ipam_mode) = @_;
1135  
1136 177 my $values = _merge_values({
1137 cni => {
1138 binPath => $dist->cni_bin_dir,
1139 confPath => $dist->cni_conf_dir,
1140 },
1141 operator => { replicas => 1 },
1142 kubeProxyReplacement => JSON()->true,
1143 k8sServicePort => '6443',
1144 }, $dist->cilium_helm_defaults(
1145 cluster_cidr => $cluster_cidr,
1146 k8s_service_host => $k8s_service_host,
1147 ipam_mode => $ipam_mode,
1148 ));
1149  
1150 177 ▶ $values->{gatewayAPI} = { enabled => JSON()->true } if $gateway_api;
Branch: 1 branch
BranchTrueFalseTotal
if $gateway_api 43 134 177
1151  
1152 177 ▶ return _merge_values($values, $extra // {});
Condition50%1/2
$extra // {}
l!l
1770
MC/DC0%0/1
$extra // {}
$extra
Truth table50%1/2
A
A= $extra
Aresult
11
00
1153 }
1154  
1155 # Deep merge, $over wins; hashes merge key by key, anything else replaces.
1156 # Returns a new structure — neither input is modified.
1157 552 ▶sub _merge_values {
Subroutine _merge_values: called CC=4 SCAR=13.9
1158 552 my ($base, $over) = @_;
1159  
1160 552 my %out = %$base;
1161 552 for my $k (keys %$over) {
1162 $out{$k} = ref $out{$k} eq 'HASH' && ref $over->{$k} eq 'HASH'
1163 ? _merge_values($out{$k}, $over->{$k})
1164 746 ▶ : $over->{$k};
Branch: 1 branch
BranchTrueFalseTotal
ref $out{$k} eq 'HASH' && ref $over->{$k} eq 'HASH' ? : 198 548 746
Condition100%3/3
ref $out{$k} eq "HASH" and ref $over->{$k} eq "HASH"
!ll&&!rl&&r
5471198
MC/DC100%2/2
ref $out{$k} eq "HASH" and ref $over->{$k} eq "HASH"
ref $out{$k} eq "HASH" ref $over->{$k} eq "HASH"
Truth table100%3/3
A and B
A= ref $out{$k} eq "HASH"B= ref $over->{$k} eq "HASH"
ABresult
0X0
100
111
1165 }
1166 552 return \%out;
1167 }
1168  
1169 37 ▶sub _helm_values_yaml {
Subroutine _helm_values_yaml: called CC=1 SCAR=0.0
1170 37 my ($values) = @_;
1171 37 return YAML::PP->new(boolean => 'JSON::PP')->dump_string($values);
1172 }
1173  
1174 35 ▶sub _write_helm_values {
Subroutine _write_helm_values: called CC=1 SCAR=0.0
1175 35 my ($o) = @_;
1176  
1177 35 my $values_file = "/tmp/cilium-values-" . $o->{dist}->name . ".yaml";
1178 35 file $values_file, content => _helm_values_yaml($o->{values});
1179  
1180 35 Rex::Logger::info("Wrote Helm values to $values_file");
1181 35 return $values_file;
1182 }
1183  
1184 42 1;
1185  
1186 __END__
« lib/Rex/Rancher/K8s.pm ↑ Summary lib/Rex/Rancher/Distribution.pm »