lib/Rex/Rancher/K8s.pm

filter: Statementstmt 55.4% 66 / 119 Branchbran 50.0% 16 / 32 Conditioncond 43.4% 10 / 23 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 73.3% 11 / 15 Podpod 100% 4 / 4 totaltotal 53.4% 110 / 206 CC 31 SCAR 27.5
CC
31
Cov
53%
untaint_node
42.0
deploy_nvidia_device_plugin
30.0
wait_for_api
20.0
CRAP 15.6
« eg/lan.Rexfile ↑ Summary lib/Rex/Rancher/Cilium.pm »
1 # ABSTRACT: Kubernetes API operations for Rex::Rancher (device plugin, readiness)
2  
3 package Rex::Rancher::K8s;
4 51 our $VERSION = '0.004';
5 51 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 51 ▶use Kubernetes::REST::Kubeconfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 51 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 51 ▶use Rex::Rancher::Distribution;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 51 require Rex::Exporter;
13 51 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 51 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 51 @EXPORT = qw(
18 control_plane_version
19 deploy_nvidia_device_plugin
20 wait_for_api
21 untaint_node
22 );
23  
24 51 ▶use constant DEVICE_PLUGIN_VERSION => 'v0.17.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26  
27 0 ▶sub wait_for_api {
Subroutine wait_for_api: not called CC=4 SCAR=30.0
Pod documented
28 0 my (%opts) = @_;
29 0 ▶ my $kubeconfig = $opts{kubeconfig} or die "kubeconfig required\n";
Branch: 1 branch
BranchTrueFalseTotal
my $kubeconfig = $opts{"kubeconfig"} or die "kubeconfig required\n" 0 0 0
30  
31 0 Rex::Logger::info("Waiting for Kubernetes API server...");
32  
33 0 for my $i (1..60) {
34 0 my $up = eval {
35 0 my $api = _api($kubeconfig);
36 0 $api->list('Node');
37 0 1;
38 };
39 0 ▶ if ($up) {
Branch: 1 branch
BranchTrueFalseTotal
if ($up) 0 0 0
40 0 Rex::Logger::info(" API server is up");
41 0 return 1;
42 }
43 0 Rex::Logger::info(" Not responding yet, waiting... ($i/60)");
44 0 sleep 5;
45 }
46  
47 0 Rex::Logger::info("Kubernetes API server did not respond in time", "warn");
48 0 return 0;
49 }
50  
51  
52 0 ▶sub deploy_nvidia_device_plugin {
Subroutine deploy_nvidia_device_plugin: not called CC=5 SCAR=34.0
Pod documented
53 0 my (%opts) = @_;
54 0 ▶ my $kubeconfig = $opts{kubeconfig} or die "kubeconfig required\n";
Branch: 1 branch
BranchTrueFalseTotal
my $kubeconfig = $opts{"kubeconfig"} or die "kubeconfig required\n" 0 0 0
55 0 ▶ my $version = $opts{version} // DEVICE_PLUGIN_VERSION;
Condition0%0/2
$opts{"version"} // "v0.17.0"
l!l
00
MC/DC0%0/1
$opts{"version"} // "v0.17.0"
$opts{"version"}
Truth table0%0/2
A
A= $opts{"version"}
Aresult
11
00
56  
57 0 Rex::Logger::info("Deploying NVIDIA device plugin $version...");
58  
59 0 my $api = _api($kubeconfig);
60  
61 my $ds = $api->new_object(DaemonSet =>
62 0 %{ _nvidia_device_plugin_daemonset_spec($version) });
63  
64 0 eval { $api->create($ds) };
65 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
66 0 ▶ if ($@ =~ /already exist/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /already exist/iu) { } 0 0 0
67 0 Rex::Logger::info(" DaemonSet already exists, updating...");
68 0 my $existing = $api->get('DaemonSet', 'nvidia-device-plugin-daemonset',
69 namespace => 'kube-system');
70 0 $ds->metadata->resourceVersion($existing->metadata->resourceVersion);
71 0 $api->update($ds);
72 }
73 else {
74 0 die $@;
75 }
76 }
77  
78 0 Rex::Logger::info(" DaemonSet applied, waiting for GPU resources...");
79 0 _wait_for_gpu_resource($api);
80  
81 0 Rex::Logger::info("NVIDIA device plugin ready");
82 }
83  
84  
85 0 ▶sub untaint_node {
Subroutine untaint_node: not called CC=6 SCAR=37.4
Pod documented
86 0 my (%opts) = @_;
87 0 ▶ my $kubeconfig = $opts{kubeconfig} or die "kubeconfig required\n";
Branch: 1 branch
BranchTrueFalseTotal
my $kubeconfig = $opts{"kubeconfig"} or die "kubeconfig required\n" 0 0 0
88  
89 0 Rex::Logger::info("Removing control-plane taints (single-node)...");
90  
91 0 my $api = _api($kubeconfig);
92 0 my $nodes = $api->list('Node');
93  
94 0 for my $node (@{ $nodes->items }) {
95 0 my $name = $node->metadata->name;
96 0 ▶ my @taints = @{ $node->spec->taints // [] };
Condition0%0/2
$node->spec->taints // []
l!l
00
MC/DC0%0/1
$node->spec->taints // []
$node->spec->taints
Truth table0%0/2
A
A= $node->spec->taints
Aresult
11
00
97 my @keep = grep {
98 0 ▶ ($_->{key} // '') !~ /^node-role\.kubernetes\.io\/(control-plane|master)$/
Condition0%0/2
$_->{"key"} // ""
l!l
00
MC/DC0%0/1
$_->{"key"} // ""
$_->{"key"}
Truth table0%0/2
A
A= $_->{"key"}
Aresult
11
00
99 } @taints;
100  
101 0 ▶ next if @keep == @taints; # nothing to remove
Branch: 1 branch
BranchTrueFalseTotal
if @keep == @taints 0 0 0
102  
103 0 my $fresh = $api->get('Node', $name);
104 0 $fresh->spec->taints(\@keep);
105 0 $api->update($fresh);
106 0 Rex::Logger::info(" Untainted: $name");
107 }
108 }
109  
110  
111 19 ▶sub control_plane_version {
Subroutine control_plane_version: called CC=11 SCAR=24.6
Pod documented
112 19 my (%opts) = @_;
113 19 ▶ my $kubeconfig = $opts{kubeconfig} or die "kubeconfig required\n";
Branch: 1 branch
BranchTrueFalseTotal
my $kubeconfig = $opts{"kubeconfig"} or die "kubeconfig required\n" 19 0 19
114 19 my $D = 'Rex::Rancher::Distribution';
115  
116 19 my $api = _api($kubeconfig);
117 19 my @versions;
118 19 for my $node (@{ $api->list('Node')->items }) {
119 19 ▶ my $labels = $node->metadata->labels // {};
Condition50%1/2
$node->metadata->labels // {}
l!l
190
MC/DC0%0/1
$node->metadata->labels // {}
$node->metadata->labels
Truth table50%1/2
A
A= $node->metadata->labels
Aresult
11
00
120 next unless exists $labels->{'node-role.kubernetes.io/control-plane'}
121 19 ▶ || exists $labels->{'node-role.kubernetes.io/master'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $labels->{"node-role.kubernetes.io/control-plane"} or exists $labels->{"node-role.kubernetes.io/master"} 3 16 19
Condition100%3/3
exists $labels->{"node-role.kubernetes.io/control-plane"} or exists $labels->{"node-role.kubernetes.io/master"}
l!l&&r!l&&!r
1513
MC/DC100%2/2
exists $labels->{"node-role.kubernetes.io/control-plane"} or exists $labels->{"node-role.kubernetes.io/master"}
exists $labels->{"node-role.kubernetes.io/control-plane"} exists $labels->{"node-role.kubernetes.io/master"}
Truth table100%3/3
A or B
A= exists $labels->{"node-role.kubernetes.io/control-plane"}B= exists $labels->{"node-role.kubernetes.io/master"}
ABresult
1X1
011
000
122 16 ▶ my $info = $node->status && $node->status->nodeInfo;
Condition33%1/3
$node->status && $node->status->nodeInfo
!ll&&!rl&&r
0016
MC/DC0%0/2
$node->status && $node->status->nodeInfo
$node->status $node->status->nodeInfo
Truth table33%1/3
A && B
A= $node->statusB= $node->status->nodeInfo
ABresult
0X0
100
111
123 16 ▶ my $version = $info && $info->kubeletVersion;
Condition33%1/3
$info && $info->kubeletVersion
!ll&&!rl&&r
0016
MC/DC0%0/2
$info && $info->kubeletVersion
$info $info->kubeletVersion
Truth table33%1/3
A && B
A= $infoB= $info->kubeletVersion
ABresult
0X0
100
111
124 16 my @parts = $D->parse_release($version);
125 16 ▶ push @versions, $version if @parts;
Branch: 1 branch
BranchTrueFalseTotal
if @parts 16 0 16
126 }
127 17 my ($lowest) = sort { $D->compare_versions($a, $b) } @versions;
128 17 ▶ return $lowest if defined $lowest;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lowest 15 2 17
129  
130 2 my $version = $api->cluster_version;
131 2 my @parts = $D->parse_release($version);
132 2 ▶ return @parts ? $version : ();
Branch: 1 branch
BranchTrueFalseTotal
@parts ? : 1 1 2
133 }
134  
135 # ============================================================
136 # Internal helpers
137 # ============================================================
138  
139 0 ▶sub _api {
Subroutine _api: not called CC=1 SCAR=6.9
140 0 my ($kubeconfig) = @_;
141 0 return Kubernetes::REST::Kubeconfig->new(
142 kubeconfig_path => $kubeconfig,
143 )->api;
144 }
145  
146 # The nvidia-device-plugin DaemonSet as a plain hashref, split out verbatim
147 # from deploy_nvidia_device_plugin so its shape can be unit-tested offline —
148 # no cluster, no kubeconfig, no Kubernetes::REST client. $version is the only
149 # value that varies; the caller resolves the DEVICE_PLUGIN_VERSION default
150 # before calling. new_object(DaemonSet => %{ ... }) receives exactly the same
151 # named args as the previous inline literal.
152 2 ▶sub _nvidia_device_plugin_daemonset_spec {
Subroutine _nvidia_device_plugin_daemonset_spec: called CC=1 SCAR=0.0
153 2 my ($version) = @_;
154 return {
155 2 metadata => {
156 name => 'nvidia-device-plugin-daemonset',
157 namespace => 'kube-system',
158 },
159 spec => {
160 selector => {
161 matchLabels => { name => 'nvidia-device-plugin-ds' },
162 },
163 updateStrategy => { type => 'RollingUpdate' },
164 template => {
165 metadata => {
166 labels => { name => 'nvidia-device-plugin-ds' },
167 },
168 spec => {
169 runtimeClassName => 'nvidia',
170 priorityClassName => 'system-node-critical',
171 tolerations => [{
172 key => 'nvidia.com/gpu',
173 operator => 'Exists',
174 effect => 'NoSchedule',
175 }],
176 containers => [{
177 name => 'nvidia-device-plugin-ctr',
178 image => "nvcr.io/nvidia/k8s-device-plugin:$version",
179 env => [{
180 name => 'FAIL_ON_INIT_ERROR',
181 value => 'false',
182 }],
183 securityContext => {
184 allowPrivilegeEscalation => \0,
185 capabilities => { drop => ['ALL'] },
186 },
187 volumeMounts => [{
188 name => 'device-plugin',
189 mountPath => '/var/lib/kubelet/device-plugins',
190 }],
191 }],
192 volumes => [{
193 name => 'device-plugin',
194 hostPath => { path => '/var/lib/kubelet/device-plugins' },
195 }],
196 },
197 },
198 },
199 };
200 }
201  
202 4 ▶sub _wait_for_gpu_resource {
Subroutine _wait_for_gpu_resource: called CC=9 SCAR=22.0
203 4 my ($api) = @_;
204  
205 # The error of the last attempt, if it failed: a 403 or a dead API reads
206 # the same as a plugin that has not reported yet until the warning names it.
207 4 my $last_error;
208 4 for my $i (1..24) {
209 74 my $found = eval {
210 74 my $nodes = $api->list('Node');
211 46 for my $node (@{ $nodes->items }) {
212 46 my $cap = $node->status->capacity;
213 46 ▶ if ($cap && $cap->{'nvidia.com/gpu'} && $cap->{'nvidia.com/gpu'} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cap and $cap->{"nvidia.com/gpu"} and $cap->{"nvidia.com/gpu"} > 0) 1 45 46
Condition66%4/6
$cap and $cap->{"nvidia.com/gpu"} and $cap->{"nvidia.com/gpu"} > 0
!ll&&!rl&&r
4501
$cap and $cap->{"nvidia.com/gpu"}
!ll&&!rl&&r
0451
MC/DC33%1/3
$cap and $cap->{"nvidia.com/gpu"} and $cap->{"nvidia.com/gpu"} > 0
$cap $cap->{"nvidia.com/gpu"} $cap->{"nvidia.com/gpu"} > 0
Truth table50%2/4
A and B and C
A= $capB= $cap->{"nvidia.com/gpu"}C= $cap->{"nvidia.com/gpu"} > 0
ABCresult
0XX0
10X0
1100
1111
214 Rex::Logger::info(" [ok] nvidia.com/gpu: "
215 1 . $cap->{'nvidia.com/gpu'} . " on "
216 . $node->metadata->name);
217 1 return 1; # returns 1 as value of the eval block
218 }
219 }
220 45 0;
221 };
222 74 ▶ $last_error = $@ ? ( $@ =~ s/\s+\z//r ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 28 46 74
223 74 ▶ return 1 if $found; # exit the sub once GPU capacity is confirmed
Branch: 1 branch
BranchTrueFalseTotal
if $found 1 73 74
224 73 Rex::Logger::info(" No GPU capacity yet ($i/24), waiting...");
225 73 sleep 5;
226 }
227  
228 3 ▶ Rex::Logger::info(" nvidia.com/gpu resource did not appear — check device plugin"
Branch: 1 branch
BranchTrueFalseTotal
defined $last_error ? : 1 2 3
229 . ( defined $last_error ? " (last API error: $last_error)" : '' ), "warn");
230 3 return 0;
231 }
232  
233 51 1;
234  
235 __END__
« eg/lan.Rexfile ↑ Summary lib/Rex/Rancher/Cilium.pm »