Class: OodCore::Job::Adapters::Kubernetes::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/ood_core/job/adapters/kubernetes/batch.rb

Overview

Utility class for the Kubernetes adapter to interact with the Kuberenetes APIs.

Defined Under Namespace

Classes: Error, NotFoundError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Batch

Returns a new instance of Batch.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 21

def initialize(options = {})
  options = options.to_h.symbolize_keys

  @config_file = options.fetch(:config_file, self.class.default_config_file)
  @bin = options.fetch(:bin, '/usr/bin/kubectl')
  @cluster = options.fetch(:cluster, 'open-ondemand')
  @mounts = options.fetch(:mounts, []).map { |m| m.to_h.symbolize_keys }
  @all_namespaces = options.fetch(:all_namespaces, false)
  @username_prefix = options.fetch(:username_prefix, '')
  @namespace_prefix = options.fetch(:namespace_prefix, '')
  @auto_supplemental_groups = options.fetch(:auto_supplemental_groups, false)

  tmp_ctx = options.fetch(:context, nil)
  @context = tmp_ctx.nil? && oidc_auth?(options.fetch(:auth, {}).symbolize_keys) ? @cluster : tmp_ctx

  @helper = OodCore::Job::Adapters::Kubernetes::Helper.new
end

Instance Attribute Details

#all_namespacesObject (readonly)

Returns the value of attribute all_namespaces.



17
18
19
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 17

def all_namespaces
  @all_namespaces
end

#auto_supplemental_groupsObject (readonly)

Returns the value of attribute auto_supplemental_groups.



19
20
21
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 19

def auto_supplemental_groups
  @auto_supplemental_groups
end

#binObject (readonly)

Returns the value of attribute bin.



16
17
18
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 16

def bin
  @bin
end

#clusterObject (readonly)

Returns the value of attribute cluster.



16
17
18
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 16

def cluster
  @cluster
end

#config_fileObject (readonly)

Returns the value of attribute config_file.



16
17
18
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 16

def config_file
  @config_file
end

#contextObject (readonly)

Returns the value of attribute context.



16
17
18
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 16

def context
  @context
end

#helperObject (readonly)

Returns the value of attribute helper.



17
18
19
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 17

def helper
  @helper
end

#mountsObject (readonly)

Returns the value of attribute mounts.



16
17
18
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 16

def mounts
  @mounts
end

#namespace_prefixObject (readonly)

Returns the value of attribute namespace_prefix.



18
19
20
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 18

def namespace_prefix
  @namespace_prefix
end

#username_prefixObject (readonly)

Returns the value of attribute username_prefix.



18
19
20
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 18

def username_prefix
  @username_prefix
end

Class Method Details

.configure_kube!(config) ⇒ Object



135
136
137
138
139
140
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 135

def configure_kube!(config)
  k = self.new(config)
  # TODO: probably shouldn't be using send here
  k.send(:set_cluster, config.fetch(:server, default_server).to_h.symbolize_keys)
  k.send(:configure_auth, config.fetch(:auth, default_auth).to_h.symbolize_keys)
end

.default_authObject



122
123
124
125
126
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 122

def default_auth
  {
    type: 'managed'
  }.symbolize_keys
end

.default_config_fileObject



118
119
120
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 118

def default_config_file
  (ENV['KUBECONFIG'] || "#{Dir.home}/.kube/config")
end

.default_serverObject



128
129
130
131
132
133
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 128

def default_server
  {
    endpoint: 'https://localhost:8080',
    cert_authority_file: nil
  }.symbolize_keys
end

Instance Method Details

#delete(id) ⇒ Object



110
111
112
113
114
115
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 110

def delete(id)
  safe_call("delete", "pod", id)
  safe_call("delete", "service", service_name(id))
  safe_call("delete", "secret", secret_name(id))
  safe_call("delete", "configmap", configmap_name(id))
end

#generate_id(name) ⇒ Object



55
56
57
58
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 55

def generate_id(name)
  # 2_821_109_907_456 = 36**8
  name.downcase.tr(' ', '-') + '-' + rand(2_821_109_907_456).to_s(36)
end

#info(id) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 96

def info(id)
  pod_json = safe_call('get', 'pod', id)
  return OodCore::Job::Info.new(**{ id: id, status: 'completed' }) if pod_json.empty?

  service_json = safe_call('get', 'service', service_name(id))
  secret_json = safe_call('get', 'secret', secret_name(id))

  helper.info_from_json(pod_json: pod_json, service_json: service_json, secret_json: secret_json)
end

#info_all(attrs: nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 60

def info_all(attrs: nil)
  cmd = if @all_namespaces
          "#{base_cmd} -o json get pods --all-namespaces"
        else
          "#{namespaced_cmd} -o json get pods"
        end

  output = call(cmd)
  all_pods_to_info(output)
end

#info_all_each(attrs: nil) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 80

def info_all_each(attrs: nil)
  return to_enum(:info_all_each, attrs: attrs) unless block_given?

  info_all(attrs: attrs).each do |job|
    yield job
  end
end

#info_where_owner(owner, attrs: nil) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 71

def info_where_owner(owner, attrs: nil)
  owner = Array.wrap(owner).map(&:to_s)

  # must at least have job_owner to filter by job_owner
  attrs = Array.wrap(attrs) | [:job_owner] unless attrs.nil?

  info_all(attrs: attrs).select { |info| owner.include? info.job_owner }
end

#info_where_owner_each(owner, attrs: nil) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 88

def info_where_owner_each(owner, attrs: nil)
  return to_enum(:info_where_owner_each, owner, attrs: attrs) unless block_given?

  info_where_owner(owner, attrs: attrs).each do |job|
    yield job
  end
end

#resource_file(resource_type = 'pod') ⇒ Object



39
40
41
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 39

def resource_file(resource_type = 'pod')
  File.dirname(__FILE__) + "/templates/#{resource_type}.yml.erb"
end

#status(id) ⇒ Object



106
107
108
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 106

def status(id)
  info(id).status
end

#submit(script, after: [], afterok: [], afternotok: [], afterany: []) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ood_core/job/adapters/kubernetes/batch.rb', line 43

def submit(script, after: [], afterok: [], afternotok: [], afterany: [])
  raise ArgumentError, 'Must specify the script' if script.nil?

  resource_yml, id = generate_id_yml(script)
  if !script.workdir.nil? && Dir.exist?(script.workdir)
    File.open(File.join(script.workdir, 'pod.yml'), 'w') { |f| f.write resource_yml }
  end
  call("#{formatted_ns_cmd} create -f -", stdin: resource_yml)

  id
end