| 1 | package Kubernetes::REST::CLI::Minikube; | ||
| 2 | our $VERSION = '1.109'; | ||
| 3 | # ABSTRACT: Bring up a minikube test cluster and run a command against it | ||
| 4 | use Moo; | ||
| 5 | use MooX::Options; | ||
| 6 | use Config; | ||
| 7 | use Digest::SHA; | ||
| 8 | use JSON::MaybeXS; | ||
| 9 | use LWP::UserAgent; | ||
| 10 | use POSIX (); | ||
| 11 | use Path::Tiny qw( path ); | ||
| 12 | |||
| 13 | |||
| 14 | option profile => ( | ||
| 15 | is => 'ro', | ||
| 16 | format => 's', | ||
| 17 | short => 'p', | ||
| 18 | default => sub { 'kube-rest-test' }, | ||
| 19 | doc => 'minikube profile name', | ||
| 20 | ); | ||
| 21 | |||
| 22 | |||
| 23 | option driver => ( | ||
| 24 | is => 'ro', | ||
| 25 | format => 's', | ||
| 26 | short => 'd', | ||
| 27 | default => sub { 'docker' }, | ||
| 28 | doc => 'minikube driver (passed as --driver)', | ||
| 29 | ); | ||
| 30 | |||
| 31 | |||
| 32 | option kubernetes_version => ( | ||
| 33 | is => 'ro', | ||
| 34 | format => 's', | ||
| 35 | short => 'k', | ||
| 36 | doc => 'Kubernetes version to run (passed as --kubernetes-version)', | ||
| 37 | ); | ||
| 38 | |||
| 39 | |||
| 40 | option minikube_version => ( | ||
| 41 | is => 'ro', | ||
| 42 | format => 's', | ||
| 43 | doc => 'minikube release to use, e.g. v1.38.1 (default: latest)', | ||
| 44 | ); | ||
| 45 | |||
| 46 | |||
| 47 | option cpus => ( | ||
| 48 | is => 'ro', | ||
| 49 | format => 's', | ||
| 50 | short => 'c', | ||
| 51 | doc => 'CPUs for the cluster (passed as --cpus)', | ||
| 52 | ); | ||
| 53 | |||
| 54 | |||
| 55 | option memory => ( | ||
| 56 | is => 'ro', | ||
| 57 | format => 's', | ||
| 58 | short => 'm', | ||
| 59 | doc => 'Memory for the cluster, e.g. 4g (passed as --memory)', | ||
| 60 | ); | ||
| 61 | |||
| 62 | |||
| 63 | option container_runtime => ( | ||
| 64 | is => 'ro', | ||
| 65 | format => 's', | ||
| 66 | doc => 'Container runtime for the cluster (passed as --container-runtime)', | ||
| 67 | ); | ||
| 68 | |||
| 69 | |||
| 70 | option rootless => ( | ||
| 71 | is => 'ro', | ||
| 72 | doc => 'Run the cluster rootless (passed as --rootless)', | ||
| 73 | ); | ||
| 74 | |||
| 75 | |||
| 76 | option install_dir => ( | ||
| 77 | is => 'lazy', | ||
| 78 | format => 's', | ||
| 79 | doc => 'Where a downloaded minikube binary and the kubeconfig go', | ||
| 80 | ); | ||
| 81 | |||
| 82 | sub _build_install_dir { | ||
| 83 | my ($self) = @_; | ||
| 84 | my $cache = $ENV{XDG_CACHE_HOME}; | ||
| 85 | unless (defined $cache && length $cache) { | ||
| 86 | my $home = $ENV{HOME}; | ||
| 87 | die "Cannot determine a cache directory: neither XDG_CACHE_HOME nor HOME is set (use --install-dir)\n" | ||
| 88 | unless defined $home && length $home; | ||
| 89 | $cache = path($home, '.cache')->stringify; | ||
| 90 | } | ||
| 91 | return path($cache, 'kube_test_minikube')->stringify; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | option kubeconfig => ( | ||
| 96 | is => 'lazy', | ||
| 97 | format => 's', | ||
| 98 | doc => 'Isolated kubeconfig file (default: <install_dir>/<profile>.kubeconfig)', | ||
| 99 | ); | ||
| 100 | |||
| 101 | sub _build_kubeconfig { | ||
| 102 | my ($self) = @_; | ||
| 103 | return $self->_install_dir_path->child($self->profile . '.kubeconfig')->stringify; | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | option env => ( | ||
| 108 | is => 'ro', | ||
| 109 | format => 's@', | ||
| 110 | short => 'e', | ||
| 111 | default => sub { [] }, | ||
| 112 | doc => 'Extra environment variable that also receives the kubeconfig path (repeatable)', | ||
| 113 | ); | ||
| 114 | |||
| 115 | |||
| 116 | option context => ( | ||
| 117 | is => 'ro', | ||
| 118 | format => 's', | ||
| 119 | doc => 'Also export TEST_KUBERNETES_REST_CONTEXT with this value', | ||
| 120 | ); | ||
| 121 | |||
| 122 | |||
| 123 | option stop => ( | ||
| 124 | is => 'ro', | ||
| 125 | short => 's', | ||
| 126 | doc => 'Stop the profile afterwards (or only stop it, without a command)', | ||
| 127 | ); | ||
| 128 | |||
| 129 | |||
| 130 | option delete => ( | ||
| 131 | is => 'ro', | ||
| 132 | short => 'D', | ||
| 133 | doc => 'Delete the profile and its kubeconfig afterwards (or only delete, without a command)', | ||
| 134 | ); | ||
| 135 | |||
| 136 | |||
| 137 | option restart => ( | ||
| 138 | is => 'ro', | ||
| 139 | short => 'r', | ||
| 140 | doc => 'Run minikube start even when the profile is already running', | ||
| 141 | ); | ||
| 142 | |||
| 143 | |||
| 144 | option verbose => ( | ||
| 145 | is => 'ro', | ||
| 146 | short => 'v', | ||
| 147 | doc => 'Print every minikube command before running it', | ||
| 148 | ); | ||
| 149 | |||
| 150 | |||
| 151 | has minikube_bin => ( | ||
| 152 | is => 'lazy', | ||
| 153 | ); | ||
| 154 | |||
| 155 | sub _build_minikube_bin { | ||
| 156 | my ($self) = @_; | ||
| 157 | my $installed = $self->_install_dir_path->child('minikube'); | ||
| 158 | if (my $pinned = $self->minikube_version) { | ||
| 159 | if (-x $installed && $self->_minikube_version_of($installed->stringify) eq $pinned) { | ||
| 160 | return $installed->stringify; | ||
| 161 | } | ||
| 162 | return $self->_install_minikube; | ||
| 163 | } | ||
| 164 | my $on_path = $self->_find_in_path('minikube'); | ||
| 165 | return $on_path if defined $on_path; | ||
| 166 | return $installed->stringify if -x $installed; | ||
| 167 | return $self->_install_minikube; | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | has _install_dir_path => ( | ||
| 172 | is => 'lazy', | ||
| 173 | ); | ||
| 174 | |||
| 175 | sub _build__install_dir_path { path($_[0]->install_dir)->absolute } | ||
| 176 | |||
| 177 | has _kubeconfig_file => ( | ||
| 178 | is => 'lazy', | ||
| 179 | ); | ||
| 180 | |||
| 181 | sub _build__kubeconfig_file { path($_[0]->kubeconfig)->absolute } | ||
| 182 | |||
| 183 | has _ua => ( | ||
| 184 | is => 'lazy', | ||
| 185 | ); | ||
| 186 | |||
| 187 | sub _build__ua { | ||
| 188 | return LWP::UserAgent->new(agent => 'kube_test_minikube/' . $VERSION); | ||
| 189 | } | ||
| 190 | |||
| 191 | my $LOG_PREFIX = '[kube_test_minikube] '; | ||
| 192 | my $RELEASES = 'https://storage.googleapis.com/minikube/releases/'; | ||
| 193 | |||
| 194 | sub split_argv { | ||
| 195 | my ($class, @argv) = @_; | ||
| 196 | my %data = $class->_options_data; | ||
| 197 | my %by_short = map { $data{$_}{short} => $_ } grep { defined $data{$_}{short} } keys %data; | ||
| 198 | my @options; | ||
| 199 | while (@argv) { | ||
| 200 | my $arg = shift @argv; | ||
| 201 | last if $arg eq '--'; | ||
| 202 | unless ($arg =~ /^-./) { | ||
| 203 | unshift @argv, $arg; | ||
| 204 | last; | ||
| 205 | } | ||
| 206 | push @options, $arg; | ||
| 207 | next if $arg =~ /=/; | ||
| 208 | my $name; | ||
| 209 | if ($arg =~ /^--(?:no-)?(.+)$/) { | ||
| 210 | (my $long = $1) =~ s/-/_/g; | ||
| 211 | $name = $class->_expand_option_name($long, \%data); | ||
| 212 | } elsif ($arg =~ /^-(.)(.*)$/) { | ||
| 213 | next if length $2; | ||
| 214 | $name = $by_short{$1}; | ||
| 215 | } | ||
| 216 | push @options, shift @argv | ||
| 217 | if defined $name && $data{$name}{format} && @argv; | ||
| 218 | } | ||
| 219 | return (\@options, \@argv); | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | sub _expand_option_name { | ||
| 224 | my ($class, $name, $data) = @_; | ||
| 225 | return $name if exists $data->{$name}; | ||
| 226 | my @matches = grep { index($_, $name) == 0 } keys %$data; | ||
| 227 | return @matches == 1 ? $matches[0] : undef; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub run { | ||
| 231 | my ($self, @command) = @_; | ||
| 232 | |||
| 233 | if (!@command && ($self->stop || $self->delete)) { | ||
| 234 | return $self->_exit_code($self->_teardown); | ||
| 235 | } | ||
| 236 | |||
| 237 | $self->_ensure_running; | ||
| 238 | my $env = $self->_env_for_child; | ||
| 239 | |||
| 240 | unless (@command) { | ||
| 241 | print $self->_export_lines($env); | ||
| 242 | return 0; | ||
| 243 | } | ||
| 244 | |||
| 245 | my ($status, $error); | ||
| 246 | { | ||
| 247 | local @ENV{keys %$env} = values %$env; | ||
| 248 | $status = $self->_run(@command); | ||
| 249 | $error = $!; | ||
| 250 | } | ||
| 251 | my $code = $self->_exit_code($status); | ||
| 252 | $self->_log('cannot run ' . $command[0] . ': ' . $error) if $status == -1; | ||
| 253 | |||
| 254 | $self->_teardown; | ||
| 255 | return $code; | ||
| 256 | } | ||
| 257 | |||
| 258 | |||
| 259 | sub _ensure_running { | ||
| 260 | my ($self) = @_; | ||
| 261 | my $profile = $self->profile; | ||
| 262 | |||
| 263 | if (!$self->restart && $self->_running && -e $self->_kubeconfig_file) { | ||
| 264 | $self->_log('profile ' . $profile . ' is already running'); | ||
| 265 | return; | ||
| 266 | } | ||
| 267 | |||
| 268 | $self->_log('starting profile ' . $profile . ' (driver ' . $self->driver . ') ...'); | ||
| 269 | my $status = $self->_minikube_run($self->_start_args); | ||
| 270 | return if $status == 0; | ||
| 271 | |||
| 272 | my $code = $self->_exit_code($status); | ||
| 273 | # die exits with $! when it is non-zero: pass minikube's exit code on. | ||
| 274 | $! = $code; | ||
| 275 | die 'minikube start failed for profile ' . $profile | ||
| 276 | . ' (driver ' . $self->driver . ') with exit code ' . $code . "\n"; | ||
| 277 | } | ||
| 278 | |||
| 279 | |||
| 280 | sub _running { | ||
| 281 | my ($self) = @_; | ||
| 282 | my $json = eval { $self->_minikube_capture($self->_status_args) }; | ||
| 283 | return 0 unless defined $json; | ||
| 284 | my $status = eval { decode_json($json) }; | ||
| 285 | if (ref $status eq 'ARRAY') { | ||
| 286 | my ($control_plane) = grep { ref $_ eq 'HASH' && !$_->{Worker} } @$status; | ||
| 287 | $status = $control_plane // $status->[0]; | ||
| 288 | } | ||
| 289 | return 0 unless ref $status eq 'HASH'; | ||
| 290 | return ($status->{Host} // '') eq 'Running' | ||
| 291 | && ($status->{APIServer} // '') eq 'Running' ? 1 : 0; | ||
| 292 | } | ||
| 293 | |||
| 294 | |||
| 295 | sub _teardown { | ||
| 296 | my ($self) = @_; | ||
| 297 | my $profile = $self->profile; | ||
| 298 | my $status = 0; | ||
| 299 | if ($self->delete) { | ||
| 300 | $self->_log('deleting profile ' . $profile . ' ...'); | ||
| 301 | $status = $self->_minikube_run($self->_delete_args); | ||
| 302 | $self->_log('minikube delete exited with ' . $self->_exit_code($status)) if $status; | ||
| 303 | my $kubeconfig = $self->_kubeconfig_file; | ||
| 304 | if (-e $kubeconfig) { | ||
| 305 | $kubeconfig->remove; | ||
| 306 | $self->_log('removed ' . $kubeconfig); | ||
| 307 | } | ||
| 308 | } elsif ($self->stop) { | ||
| 309 | $self->_log('stopping profile ' . $profile . ' ...'); | ||
| 310 | $status = $self->_minikube_run($self->_stop_args); | ||
| 311 | $self->_log('minikube stop exited with ' . $self->_exit_code($status)) if $status; | ||
| 312 | } | ||
| 313 | return $status; | ||
| 314 | } | ||
| 315 | |||
| 316 | |||
| 317 | sub _env_for_child { | ||
| 318 | my ($self) = @_; | ||
| 319 | my $kubeconfig = $self->_kubeconfig_file->stringify; | ||
| 320 | my %env = map { $_ => $kubeconfig } | ||
| 321 | qw( KUBECONFIG TEST_KUBERNETES_REST_KUBECONFIG TEST_IO_K8S_KUBECONFIG ), | ||
| 322 | @{ $self->env }; | ||
| 323 | $env{TEST_KUBERNETES_REST_CONTEXT} = $self->context | ||
| 324 | if defined $self->context && length $self->context; | ||
| 325 | my $bin_dir = path($self->minikube_bin)->absolute->parent; | ||
| 326 | $env{PATH} = join $Config{path_sep}, $bin_dir->stringify, ($ENV{PATH} // '') | ||
| 327 | if $bin_dir eq $self->_install_dir_path; | ||
| 328 | return \%env; | ||
| 329 | } | ||
| 330 | |||
| 331 | |||
| 332 | sub _export_lines { | ||
| 333 | my ($self, $env) = @_; | ||
| 334 | return join '', map { | ||
| 335 | (my $value = $env->{$_}) =~ s/'/'\\''/g; | ||
| 336 | 'export ' . $_ . "='" . $value . "'\n"; | ||
| 337 | } sort keys %$env; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub _start_args { | ||
| 341 | my ($self) = @_; | ||
| 342 | return ( | ||
| 343 | 'start', '-p', $self->profile, | ||
| 344 | '--driver=' . $self->driver, | ||
| 345 | '--interactive=false', | ||
| 346 | (defined $self->kubernetes_version ? ('--kubernetes-version=' . $self->kubernetes_version) : ()), | ||
| 347 | (defined $self->cpus ? ('--cpus=' . $self->cpus) : ()), | ||
| 348 | (defined $self->memory ? ('--memory=' . $self->memory) : ()), | ||
| 349 | (defined $self->container_runtime ? ('--container-runtime=' . $self->container_runtime) : ()), | ||
| 350 | ($self->rootless ? ('--rootless') : ()), | ||
| 351 | ); | ||
| 352 | } | ||
| 353 | |||
| 354 | sub _status_args { ('status', '-p', $_[0]->profile, '-o', 'json') } | ||
| 355 | sub _stop_args { ('stop', '-p', $_[0]->profile) } | ||
| 356 | sub _delete_args { ('delete', '-p', $_[0]->profile) } | ||
| 357 | sub _version_args { ('version', '--output=json') } | ||
| 358 | |||
| 359 | sub _minikube_run { | ||
| 360 | my ($self, @args) = @_; | ||
| 361 | local $ENV{KUBECONFIG} = $self->_kubeconfig_file->stringify; | ||
| 362 | $self->_log('running: ' . join(' ', $self->minikube_bin, @args)) if $self->verbose; | ||
| 363 | return $self->_run($self->minikube_bin, @args); | ||
| 364 | } | ||
| 365 | |||
| 366 | sub _minikube_capture { | ||
| 367 | my ($self, @args) = @_; | ||
| 368 | local $ENV{KUBECONFIG} = $self->_kubeconfig_file->stringify; | ||
| 369 | $self->_log('running: ' . join(' ', $self->minikube_bin, @args)) if $self->verbose; | ||
| 370 | return $self->_capture($self->minikube_bin, @args); | ||
| 371 | } | ||
| 372 | |||
| 373 | sub _run { | ||
| 374 | my ($self, @cmd) = @_; | ||
| 375 | no warnings qw( exec ); | ||
| 376 | system { $cmd[0] } @cmd; | ||
| 377 | return $?; | ||
| 378 | } | ||
| 379 | |||
| 380 | |||
| 381 | sub _capture { | ||
| 382 | my ($self, @cmd) = @_; | ||
| 383 | no warnings qw( exec ); | ||
| 384 | open my $fh, '-|', @cmd | ||
| 385 | or die 'Cannot run ' . $cmd[0] . ': ' . $! . "\n"; | ||
| 386 | my $output = do { local $/; <$fh> }; | ||
| 387 | close $fh; | ||
| 388 | die 'Command "' . join(' ', @cmd) . '" failed with exit code ' . $self->_exit_code($?) . "\n" | ||
| 389 | if $?; | ||
| 390 | return $output // ''; | ||
| 391 | } | ||
| 392 | |||
| 393 | |||
| 394 | sub _download { | ||
| 395 | my ($self, $url, $file) = @_; | ||
| 396 | my $res = $self->_ua->get($url, ':content_file' => $file); | ||
| 397 | die 'Download of ' . $url . ' failed: ' . $res->status_line . "\n" | ||
| 398 | unless $res->is_success; | ||
| 399 | return $file; | ||
| 400 | } | ||
| 401 | |||
| 402 | |||
| 403 | sub _install_minikube { | ||
| 404 | my ($self) = @_; | ||
| 405 | my $dir = $self->_install_dir_path; | ||
| 406 | $dir->mkpath; | ||
| 407 | my $target = $dir->child('minikube'); | ||
| 408 | my $tmp = $dir->child('minikube.tmp'); | ||
| 409 | my $url = $self->_download_url; | ||
| 410 | |||
| 411 | $self->_log('downloading ' . $url . ' to ' . $tmp . ' ...'); | ||
| 412 | $self->_download($url, $tmp->stringify); | ||
| 413 | |||
| 414 | $self->_log('verifying SHA-256 checksum ...'); | ||
| 415 | my $expected = $self->_expected_sha256($url, $dir); | ||
| 416 | my $actual = Digest::SHA->new(256)->addfile($tmp->stringify, 'b')->hexdigest; | ||
| 417 | unless ($actual eq $expected) { | ||
| 418 | $tmp->remove; | ||
| 419 | die 'SHA-256 mismatch for ' . $url . ': expected ' . $expected . ', got ' . $actual . "\n"; | ||
| 420 | } | ||
| 421 | |||
| 422 | chmod 0755, $tmp->stringify; | ||
| 423 | $tmp->move($target->stringify); | ||
| 424 | $self->_log('installed ' . $target); | ||
| 425 | return $target->stringify; | ||
| 426 | } | ||
| 427 | |||
| 428 | sub _expected_sha256 { | ||
| 429 | my ($self, $url, $dir) = @_; | ||
| 430 | my $file = $dir->child('minikube.tmp.sha256'); | ||
| 431 | $self->_download($url . '.sha256', $file->stringify); | ||
| 432 | my ($digest) = $file->slurp =~ /\b([0-9a-fA-F]{64})\b/ | ||
| 433 | or die 'No SHA-256 digest found in ' . $url . ".sha256\n"; | ||
| 434 | $file->remove; | ||
| 435 | return lc $digest; | ||
| 436 | } | ||
| 437 | |||
| 438 | sub _download_url { | ||
| 439 | my ($self) = @_; | ||
| 440 | my ($os, $arch) = $self->_platform; | ||
| 441 | return $RELEASES . ($self->minikube_version // 'latest') . '/minikube-' . $os . '-' . $arch; | ||
| 442 | } | ||
| 443 | |||
| 444 | my %OS = (linux => 'linux', darwin => 'darwin'); | ||
| 445 | my %ARCH = (x86_64 => 'amd64', amd64 => 'amd64', aarch64 => 'arm64', arm64 => 'arm64'); | ||
| 446 | |||
| 447 | sub _platform { | ||
| 448 | my ($self) = @_; | ||
| 449 | my $os = $OS{$^O} | ||
| 450 | or die 'Unsupported operating system "' . $^O . '": minikube releases exist for linux and darwin' . "\n"; | ||
| 451 | my $machine = (POSIX::uname())[4]; | ||
| 452 | my $arch = $ARCH{$machine} | ||
| 453 | or die 'Unsupported architecture "' . $machine . '": minikube releases exist for x86_64 and arm64' . "\n"; | ||
| 454 | return ($os, $arch); | ||
| 455 | } | ||
| 456 | |||
| 457 | sub _minikube_version_of { | ||
| 458 | my ($self, $bin) = @_; | ||
| 459 | my $output = eval { $self->_capture($bin, $self->_version_args) }; | ||
| 460 | return '' unless defined $output; | ||
| 461 | my $data = eval { decode_json($output) }; | ||
| 462 | return ref $data eq 'HASH' ? ($data->{minikubeVersion} // '') : ''; | ||
| 463 | } | ||
| 464 | |||
| 465 | sub _find_in_path { | ||
| 466 | my ($self, $name) = @_; | ||
| 467 | for my $dir (split /\Q$Config{path_sep}\E/, $ENV{PATH} // '') { | ||
| 468 | next unless length $dir; | ||
| 469 | my $candidate = path($dir, $name); | ||
| 470 | return $candidate->stringify if -f $candidate && -x _; | ||
| 471 | } | ||
| 472 | return; | ||
| 473 | } | ||
| 474 | |||
| 475 | sub _exit_code { | ||
| 476 | my ($self, $status) = @_; | ||
| 477 | return 127 if $status == -1; | ||
| 478 | my $signal = $status & 127; | ||
| 479 | return 128 + $signal if $signal; | ||
| 480 | return $status >> 8; | ||
| 481 | } | ||
| 482 | |||
| 483 | sub _log { | ||
| 484 | my ($self, @message) = @_; | ||
| 485 | print STDERR $LOG_PREFIX . join('', @message) . "\n"; | ||
| 486 | return; | ||
| 487 | } | ||
| 488 | |||
| 489 | 1; | ||
| 490 | |||
| 491 | __END__ | ||
| 492 | |||
| 493 | =pod | ||
| 494 | |||
| 495 | =encoding UTF-8 | ||
| 496 | |||
| 497 | =head1 NAME | ||
| 498 | |||
| 499 | Kubernetes::REST::CLI::Minikube - Bring up a minikube test cluster and run a command against it | ||
| 500 | |||
| 501 | =head1 VERSION | ||
| 502 | |||
| 503 | version 1.109 | ||
| 504 | |||
| 505 | =head1 SYNOPSIS | ||
| 506 | |||
| 507 | use Kubernetes::REST::CLI::Minikube; | ||
| 508 | |||
| 509 | my ($options, $command) = Kubernetes::REST::CLI::Minikube->split_argv(@ARGV); | ||
| 510 | @ARGV = @$options; | ||
| 511 | my $runner = Kubernetes::REST::CLI::Minikube->new_with_options; | ||
| 512 | exit $runner->run(@$command); | ||
| 513 | |||
| 514 | =head1 DESCRIPTION | ||
| 515 | |||
| 516 | L<MooX::Options>-based class that powers the L<kube_test_minikube> CLI tool. | ||
| 517 | It makes sure a C<minikube> binary is available (downloading a verified one | ||
| 518 | into a cache directory when there is none), brings up a minikube profile whose | ||
| 519 | kubeconfig lives in an isolated file, exports the live-test gate variables of | ||
| 520 | the Kubernetes::REST family of distributions and runs the command it was | ||
| 521 | given with that environment. | ||
| 522 | |||
| 523 | This class does not talk to the Kubernetes API and does not consume | ||
| 524 | L<Kubernetes::REST::CLI::Role::Connection>: it only orchestrates the | ||
| 525 | C<minikube> binary. Every process interaction goes through L</_run>, | ||
| 526 | L</_capture> and L</_download>, so tests can replace them without a real | ||
| 527 | minikube. | ||
| 528 | |||
| 529 | The C<KUBECONFIG> environment variable is set to the isolated file for every | ||
| 530 | C<minikube> invocation, so the user's F<~/.kube/config> is never read or | ||
| 531 | written. | ||
| 532 | |||
| 533 | =head2 profile | ||
| 534 | |||
| 535 | The minikube profile to bring up, passed as C<-p> to every C<minikube> | ||
| 536 | invocation. Default: C<kube-rest-test>. The profile also names the kubeconfig | ||
| 537 | context minikube writes. | ||
| 538 | |||
| 539 | Short option: C<-p> | ||
| 540 | |||
| 541 | =head2 driver | ||
| 542 | |||
| 543 | The minikube driver, passed as C<--driver=> to C<minikube start>. Default: | ||
| 544 | C<docker>. | ||
| 545 | |||
| 546 | Short option: C<-d> | ||
| 547 | |||
| 548 | =head2 kubernetes_version | ||
| 549 | |||
| 550 | Kubernetes version for the cluster, passed as C<--kubernetes-version=> to | ||
| 551 | C<minikube start> when set. Omit to let minikube pick its default. | ||
| 552 | |||
| 553 | Short option: C<-k> | ||
| 554 | |||
| 555 | =head2 minikube_version | ||
| 556 | |||
| 557 | Pin the minikube release, as tagged upstream (C<v1.38.1>). When set, the | ||
| 558 | binary in L</install_dir> is used if it reports exactly this version and | ||
| 559 | downloaded from the release directory of that version otherwise; a | ||
| 560 | C<minikube> found on C<PATH> is ignored. Without a pin a C<minikube> on | ||
| 561 | C<PATH> is used as-is, then one already in L</install_dir>, and only when | ||
| 562 | neither exists the latest release is downloaded. | ||
| 563 | |||
| 564 | =head2 cpus | ||
| 565 | |||
| 566 | Number of CPUs for the cluster, passed as C<--cpus=> to C<minikube start> | ||
| 567 | when set. | ||
| 568 | |||
| 569 | Short option: C<-c> | ||
| 570 | |||
| 571 | =head2 memory | ||
| 572 | |||
| 573 | Memory for the cluster in minikube's notation (C<4g>, C<4096mb>, or a plain | ||
| 574 | number of megabytes), passed as C<--memory=> to C<minikube start> when set. | ||
| 575 | |||
| 576 | Short option: C<-m> | ||
| 577 | |||
| 578 | =head2 container_runtime | ||
| 579 | |||
| 580 | Container runtime for the cluster, passed as C<--container-runtime=> to | ||
| 581 | C<minikube start> when set. On a Podman backend the rootless driver needs | ||
| 582 | C<--driver podman --container-runtime containerd --rootless>. | ||
| 583 | |||
| 584 | =head2 rootless | ||
| 585 | |||
| 586 | Pass C<--rootless> to C<minikube start>. Required by the rootless C<podman> | ||
| 587 | driver, which additionally needs C<--container-runtime containerd> (see | ||
| 588 | L</container_runtime>). | ||
| 589 | |||
| 590 | =head2 install_dir | ||
| 591 | |||
| 592 | Directory a downloaded C<minikube> binary is installed into, and where the | ||
| 593 | default L</kubeconfig> lives. Default: C<$XDG_CACHE_HOME/kube_test_minikube>, | ||
| 594 | or F<~/.cache/kube_test_minikube> when C<XDG_CACHE_HOME> is not set. | ||
| 595 | |||
| 596 | =head2 kubeconfig | ||
| 597 | |||
| 598 | Path of the isolated kubeconfig file. Default: | ||
| 599 | F<E<lt>install_dirE<gt>/E<lt>profileE<gt>.kubeconfig>. Every C<minikube> | ||
| 600 | invocation runs with C<KUBECONFIG> set to this file, and it is the value all | ||
| 601 | exported variables receive. Removed again by L</delete>. | ||
| 602 | |||
| 603 | =head2 env | ||
| 604 | |||
| 605 | Name of an additional environment variable to set to the kubeconfig path, | ||
| 606 | for distributions with a live-test gate of their own. Repeatable. | ||
| 607 | |||
| 608 | Short option: C<-e> | ||
| 609 | |||
| 610 | =head2 context | ||
| 611 | |||
| 612 | When set, C<TEST_KUBERNETES_REST_CONTEXT> is exported with this value as well. | ||
| 613 | Without it that variable is left alone. | ||
| 614 | |||
| 615 | =head2 stop | ||
| 616 | |||
| 617 | Run C<minikube stop> after the command has finished. Without a command, stop | ||
| 618 | the profile and exit without starting anything. | ||
| 619 | |||
| 620 | Short option: C<-s> | ||
| 621 | |||
| 622 | =head2 delete | ||
| 623 | |||
| 624 | Run C<minikube delete> after the command has finished and remove the | ||
| 625 | kubeconfig file. Without a command, delete the profile and exit without | ||
| 626 | starting anything. Takes precedence over L</stop>. | ||
| 627 | |||
| 628 | Short option: C<-D> | ||
| 629 | |||
| 630 | =head2 restart | ||
| 631 | |||
| 632 | Run C<minikube start> even when the profile already reports a running host | ||
| 633 | and API server. C<start> is idempotent on minikube's side; this makes it | ||
| 634 | re-check the node. | ||
| 635 | |||
| 636 | Short option: C<-r> | ||
| 637 | |||
| 638 | =head2 verbose | ||
| 639 | |||
| 640 | Print each C<minikube> command line to STDERR before it runs. | ||
| 641 | |||
| 642 | Short option: C<-v> | ||
| 643 | |||
| 644 | =head2 minikube_bin | ||
| 645 | |||
| 646 | Path of the C<minikube> binary in use. Resolved lazily as described under | ||
| 647 | L</minikube_version>, downloading and verifying a release when nothing usable | ||
| 648 | exists; pass it to the constructor to use a specific binary and skip the | ||
| 649 | lookup. | ||
| 650 | |||
| 651 | =head2 split_argv | ||
| 652 | |||
| 653 | my ($options, $command) = Kubernetes::REST::CLI::Minikube->split_argv(@ARGV); | ||
| 654 | |||
| 655 | Class method. Splits a command line into the tool's own options and the | ||
| 656 | command to run: the command starts at the first argument that is not an | ||
| 657 | option or the value of an option that takes one, or after a C<-->. Returns | ||
| 658 | two array references. L<MooX::Options> rewrites C<@ARGV> before parsing it | ||
| 659 | (a bundled C<-lr> becomes C<-l r>), so the command has to be taken out | ||
| 660 | before C<new_with_options> ever sees it - this is what C<bin/kube_test_minikube> | ||
| 661 | does. Unique prefixes of long option names are recognised the way | ||
| 662 | L<Getopt::Long> recognises them. | ||
| 663 | |||
| 664 | =head2 run | ||
| 665 | |||
| 666 | my $exit_code = $runner->run(@command); | ||
| 667 | |||
| 668 | Entry point called by C<bin/kube_test_minikube>. Makes sure the profile is | ||
| 669 | running (L</_ensure_running>), then runs C<@command> with the environment | ||
| 670 | from L</_env_for_child> and returns the command's exit status: its exit code, | ||
| 671 | or 128 plus the signal number when it died from a signal, or 127 when it | ||
| 672 | could not be started at all. L</stop> and L</delete> tear the profile down | ||
| 673 | after the command and never mask that status. | ||
| 674 | |||
| 675 | Without a command it prints one C<export NAME='value'> line per exported | ||
| 676 | variable to STDOUT, for C<eval "$(kube_test_minikube)">, and returns 0 - | ||
| 677 | unless L</stop> or L</delete> is set, in which case it only tears down and | ||
| 678 | returns minikube's exit status. | ||
| 679 | |||
| 680 | Dies when C<minikube start> fails, naming the profile and driver; the | ||
| 681 | process exit code is then minikube's. | ||
| 682 | |||
| 683 | =head2 _ensure_running | ||
| 684 | |||
| 685 | Runs C<minikube start> unless L</_running> reports the profile up and the | ||
| 686 | kubeconfig file exists, or always with L</restart>. Dies naming the profile | ||
| 687 | and driver when C<start> fails. | ||
| 688 | |||
| 689 | =head2 _running | ||
| 690 | |||
| 691 | Returns true when C<minikube status -o json> reports both the host and the | ||
| 692 | API server of the profile as C<Running>. A failing C<status> (unknown or | ||
| 693 | stopped profile) counts as not running. On a multi-node profile the | ||
| 694 | control-plane entry is the one consulted. | ||
| 695 | |||
| 696 | =head2 _teardown | ||
| 697 | |||
| 698 | Runs C<minikube delete> (and removes the kubeconfig file) when L</delete> is | ||
| 699 | set, otherwise C<minikube stop> when L</stop> is set, otherwise nothing. | ||
| 700 | Returns the raw C<$?> of the minikube command, 0 when nothing ran. | ||
| 701 | |||
| 702 | =head2 _env_for_child | ||
| 703 | |||
| 704 | Returns a hash reference of the environment variables exported to the | ||
| 705 | command: C<KUBECONFIG>, C<TEST_KUBERNETES_REST_KUBECONFIG>, | ||
| 706 | C<TEST_IO_K8S_KUBECONFIG> and every L</env> name, all set to the absolute | ||
| 707 | kubeconfig path; C<TEST_KUBERNETES_REST_CONTEXT> only when L</context> is | ||
| 708 | given; and C<PATH> with L</install_dir> prepended when the binary in use | ||
| 709 | lives there, so C<minikube> is reachable from inside the command. | ||
| 710 | |||
| 711 | =head2 _run | ||
| 712 | |||
| 713 | my $status = $runner->_run(@cmd); | ||
| 714 | |||
| 715 | Runs C<@cmd> without a shell, with STDIN, STDOUT and STDERR passed through, | ||
| 716 | and returns the raw C<$?> (-1 when it could not be started). The seam every | ||
| 717 | pass-through process interaction goes through; override it in tests. | ||
| 718 | |||
| 719 | =head2 _capture | ||
| 720 | |||
| 721 | my $stdout = $runner->_capture(@cmd); | ||
| 722 | |||
| 723 | Runs C<@cmd> without a shell and returns its STDOUT as a string (STDERR is | ||
| 724 | passed through). Dies when the command cannot be started or exits non-zero. | ||
| 725 | The seam every parsed process interaction goes through; override it in tests. | ||
| 726 | |||
| 727 | =head2 _download | ||
| 728 | |||
| 729 | $runner->_download($url, $file); | ||
| 730 | |||
| 731 | Fetches C<$url> straight into C<$file> (streamed to disk, never held in | ||
| 732 | memory) and dies on any HTTP failure. The seam for every HTTP interaction; | ||
| 733 | override it in tests. | ||
| 734 | |||
| 735 | =head1 SEE ALSO | ||
| 736 | |||
| 737 | =over | ||
| 738 | |||
| 739 | =item * L<kube_test_minikube> - The CLI tool built on this class | ||
| 740 | |||
| 741 | =item * L<Kubernetes::REST> - The client whose live tests this runs | ||
| 742 | |||
| 743 | =item * L<Kubernetes::REST::CLI::Watch> - The other L<MooX::Options> CLI class of this distribution | ||
| 744 | |||
| 745 | =back | ||
| 746 | |||
| 747 | =head1 SUPPORT | ||
| 748 | |||
| 749 | =head2 Issues | ||
| 750 | |||
| 751 | Please report bugs and feature requests on GitHub at | ||
| 752 | L<https://github.com/pplu/kubernetes-rest/issues>. | ||
| 753 | |||
| 754 | =head2 IRC | ||
| 755 | |||
| 756 | Join C<#kubernetes> on C<irc.perl.org> or message Getty directly. | ||
| 757 | |||
| 758 | =head1 CONTRIBUTING | ||
| 759 | |||
| 760 | Contributions are welcome! Please fork the repository and submit a pull request. | ||
| 761 | |||
| 762 | =head1 AUTHORS | ||
| 763 | |||
| 764 | =over 4 | ||
| 765 | |||
| 766 | =item * | ||
| 767 | |||
| 768 | Torsten Raudssus <getty@cpan.org> | ||
| 769 | |||
| 770 | =item * | ||
| 771 | |||
| 772 | Jose Luis Martinez Torres <jlmartin@cpan.org> | ||
| 773 | |||
| 774 | =back | ||
| 775 | |||
| 776 | =head1 COPYRIGHT AND LICENSE | ||
| 777 | |||
| 778 | This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>. | ||
| 779 | |||
| 780 | This is free software, licensed under: | ||
| 781 | |||
| 782 | The Apache License, Version 2.0, January 2004 | ||
| 783 | |||
| 784 | =cut |