Class: Kitchen::Driver::K8s

Inherits:
Base
  • Object
show all
Includes:
KitchenK8s::Helper, ShellOut
Defined in:
lib/kitchen/driver/k8s.rb

Overview

Driver for Kitchen

Author:

Instance Method Summary collapse

Methods included from KitchenK8s::Helper

#kube_options, #kubectl_command

Instance Method Details

#create(state) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/kitchen/driver/k8s.rb', line 109

def create(state)
  return if state[:pod_id]
  pod_id = config[:pod_name].downcase
  state[:pod_id] = pod_id
  state[:namespace] = config[:namespace]
  client = kubectl_client
  # Render pod
  pod = render_pod_info(pod_id)
  debug("Creating pod with YAML:\n#{pod}\n")
  client.create_pod(render_pod_info(pod_id))
  # Wait until pod is running
  status = nil
  start_time = Time.now
  while status != 'Running'
    if Time.now - start_time > 20
      info("Waiting for pod #{pod_id} to be running, currently #{status}")
    end
    sleep(1)
    status = client.get_pod(pod_id, config[:namespace]).status.phase
  end
  platform_dependencies.each do |cmd|
    run_command(kubectl_command('exec', '--tty', '--container=default', pod_id, '--', *Shellwords.split(cmd)))
  end
  if config[:provision_command]
    config[:provision_command].each do |cmd|
      run_command(kubectl_command('exec', '--tty', '--container=default', pod_id, '--', *Shellwords.split(cmd)))
    end
  end
  state[:pod_id] = pod_id
end

#default_platformObject



51
52
53
# File 'lib/kitchen/driver/k8s.rb', line 51

def default_platform
  instance.platform.name.split('-').first
end

#destroy(state) ⇒ Object



141
142
143
144
145
# File 'lib/kitchen/driver/k8s.rb', line 141

def destroy(state)
  return unless state[:pod_id]
  client = kubectl_client
  client.delete_pod(state[:pod_id], config[:namespace])
end

#finalize_config!(instance) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/kitchen/driver/k8s.rb', line 56

def finalize_config!(instance)
  super.tap do
    # Force the use of the Kubernetes transport since it isn't much use
    # without that.
    instance.transport = Kitchen::Transport::K8s.new(config)
    # Leave room for the possibility of other provisioners in the future,
    # but force some options we need.
    if instance.provisioner.is_a?(Kitchen::Provisioner::ChefBase)
      instance.provisioner.send(:config).update(
        require_chef_omnibus: false,
        product_name: nil,
        chef_omnibus_root: '/opt/chef',
        sudo: false,
      )
    end
    # Ditto to the above, other verifiers will need their own hacks, but
    # this is a start at least.
    if instance.verifier.is_a?(Kitchen::Verifier::Busser)
      instance.verifier.send(:config).update(
        root_path: '/tmp/kitchen/verifier',
        sudo: false,
      )
    elsif defined?(Kitchen::Verifier::Inspec) && instance.verifier.is_a?(Kitchen::Verifier::Inspec)
      # Monkeypatch kitchen-inspec to use a copy of the k8s train transport.
      require 'kitchen/verifier/k8s'
      _config = config # Because closure madness.
      old_runner_options = instance.verifier.method(:runner_options)
      instance.verifier.send(:define_singleton_method, :runner_options) do |transport, state = {}, platform = nil, suite = nil|
        if transport.is_a?(Kitchen::Transport::K8s)
          {
            "backend" => "k8s_hack",
            "logger" => logger,
            "pod" => state[:pod_id],
            "container" => "default",
            "kubectl_path" => _config[:binary],
            "context" => _config[:context],
          }.tap do |runner_options|
            # Copied directly from kitchen-inspec because there is no way not to. Sigh.
            runner_options["color"] = (config[:color].nil? ? true : config[:color])
            runner_options["format"] = config[:format] unless config[:format].nil?
            runner_options["output"] = config[:output] % { platform: platform, suite: suite } unless config[:output].nil?
            runner_options["profiles_path"] = config[:profiles_path] unless config[:profiles_path].nil?
            runner_options[:controls] = config[:controls]
          end
        else
          old_runner_options.call(transport, state, platform, suite)
        end
      end
    end
  end
end