Module: VagrantK8s::Kubeconfig

Defined in:
lib/vagrant-k8s/kubeconfig.rb

Class Method Summary collapse

Class Method Details

.kubectl_command(machine, extra_args, namespace: nil, context: nil) ⇒ Array<String>

Builds a kubectl command line from cluster config and per-call overrides.

A namespace or context passed by the caller takes precedence over the k8s block, so provisioners can target a different context without forcing that override onto every other command.

Parameters:

  • machine (Vagrant::Machine)

    Machine whose k8s config supplies defaults.

  • extra_args (Array<String>)

    Arguments appended after the global flags.

  • namespace (String, nil) (defaults to: nil)

    Namespace override.

  • context (String, nil) (defaults to: nil)

    Context override.

Returns:

  • (Array<String>)

    Complete kubectl command line.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vagrant-k8s/kubeconfig.rb', line 18

def kubectl_command(machine, extra_args, namespace: nil, context: nil)
  config = machine.config.k8s
  command = [config.kubectl]
  kubeconfig = resolve_path(machine, config.kubeconfig)
  command += ['--kubeconfig', kubeconfig] if kubeconfig
  selected_context = context || config.context
  selected_namespace = namespace || config.namespace
  command += ['--context', selected_context] if present?(selected_context)
  command += ['--namespace', selected_namespace] if present?(selected_namespace)
  command + extra_args
end

.present?(value) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/vagrant-k8s/kubeconfig.rb', line 37

def present?(value)
  value.is_a?(String) && !value.empty?
end

.resolve_path(machine, path) ⇒ Object



30
31
32
33
34
35
# File 'lib/vagrant-k8s/kubeconfig.rb', line 30

def resolve_path(machine, path)
  return nil unless present?(path)
  return path if Pathname.new(path).absolute?

  File.expand_path(path, machine.env.root_path.to_s)
end