Method: KubernetesCLI#get_objects

Defined in:
lib/kubernetes-cli.rb

#get_objects(type, namespace, match_labels = {}) ⇒ Object

T::Sig::WithoutRuntime.sig

params(
  type: String,
  namespace: T.any(String, Symbol),
  match_labels: T::Hash[String, String]
).returns(
  T::Array[T.untyped]
)

[View source]

237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/kubernetes-cli.rb', line 237

def get_objects(type, namespace, match_labels = {})
  cmd = [executable, '--kubeconfig', kubeconfig_path, 'get', type]

  if namespace == :all
    cmd << '--all-namespaces'
  elsif namespace
    cmd += ['-n', namespace.to_s]
  end

  unless match_labels.empty?
    cmd += ['--selector', match_labels.map { |key, value| "#{key}=#{value}" }.join(',')]
  end

  cmd += ['-o', 'json']

  result = backticks(cmd)

  on_last_status_failure do |last_status|
    raise GetResourceError, "couldn't get resources of type '#{type}' "\
      "in namespace #{namespace}: kubectl exited with status code #{last_status.exitstatus}"
  end

  begin
    JSON.parse(result)['items']
  rescue JSON::ParserError
    raise GetResourceError, "json parsing error"
  end
end