Class: K8s::APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/k8s/api_client.rb

Overview

Per-APIGroup/version client.

Offers access to ResourceClient instances for the APIResource types defined in this apigroup/version

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport, api_version) ⇒ APIClient

Returns a new instance of APIClient.

Parameters:

  • transport (K8s::Transport)
  • api_version (String)

    “group/version” or “version” (core)



19
20
21
22
# File 'lib/k8s/api_client.rb', line 19

def initialize(transport, api_version)
  @transport = transport
  @api_version = api_version
end

Class Method Details

.path(api_version) ⇒ String

Parameters:

  • api_version (String)

    either core version (v1) or apigroup/apiversion (apps/v1)

Returns:

  • (String)


8
9
10
11
12
13
14
15
# File 'lib/k8s/api_client.rb', line 8

def self.path(api_version)
  if api_version.include? '/'
    File.join('/apis', api_version)
  else
    File.join('/api', api_version)
  end

end

Instance Method Details

#api_resourcesArray<K8s::API::MetaV1::APIResource>

Cached APIResources

Returns:



57
58
59
# File 'lib/k8s/api_client.rb', line 57

def api_resources
  @api_resources || api_resources!
end

#api_resources!Array<K8s::API::MetaV1::APIResource>

Force-update APIResources

Returns:



48
49
50
51
52
# File 'lib/k8s/api_client.rb', line 48

def api_resources!
  @api_resources = @transport.get(self.path,
    response_class: K8s::API::MetaV1::APIResourceList,
  ).resources
end

#api_resources=(api_resources) ⇒ Object

Parameters:



41
42
43
# File 'lib/k8s/api_client.rb', line 41

def api_resources=(api_resources)
  @api_resources = api_resources
end

#api_resources?Bool

Returns loaded yet?.

Returns:

  • (Bool)

    loaded yet?



36
37
38
# File 'lib/k8s/api_client.rb', line 36

def api_resources?
  !!@api_resources
end

#api_versionString

Returns:

  • (String)


25
26
27
# File 'lib/k8s/api_client.rb', line 25

def api_version
  @api_version
end

#client_for_resource(resource, namespace: nil) ⇒ K8s::ResourceClient

Parameters:

  • resource (K8s::Resource)
  • namespace (String, nil) (defaults to: nil)

    default if resource is missing namespace

Returns:

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/k8s/api_client.rb', line 80

def client_for_resource(resource, namespace: nil)
  unless @api_version == resource.apiVersion
    raise K8s::Error::UndefinedResource, "Invalid apiVersion=#{resource.apiVersion} for #{@api_version} client"
  end

  unless api_resource = api_resources.find{ |api_resource| api_resource.kind == resource.kind }
    raise K8s::Error::UndefinedResource, "Unknown resource kind=#{resource.kind} for #{@api_version}"
  end

  ResourceClient.new(@transport, self, api_resource,
    namespace: resource..namespace || namespace,
  )
end

#list_resources(resources = nil, **options) ⇒ Array<K8s::Resource>

Pipeline list requests for multiple resource types.

Returns flattened array with mixed resource kinds.

Parameters:

  • resources (Array<K8s::ResourceClient>) (defaults to: nil)

    default is all listable resources for api

  • options

    @see [K8s::ResourceClient#list]

Returns:



111
112
113
114
115
# File 'lib/k8s/api_client.rb', line 111

def list_resources(resources = nil, **options)
  resources ||= self.resources.select{|resource| resource.list? }

  ResourceClient.list(resource, @transport, **options)
end

#path(*path) ⇒ String

Parameters:

  • path (Array<String>)

    join path from parts

Returns:

  • (String)


31
32
33
# File 'lib/k8s/api_client.rb', line 31

def path(*path)
  @transport.path(self.class.path(@api_version), *path)
end

#resource(resource_name, namespace: nil) ⇒ K8s::ResourceClient

Parameters:

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

Returns:

Raises:



65
66
67
68
69
70
71
72
73
# File 'lib/k8s/api_client.rb', line 65

def resource(resource_name, namespace: nil)
  unless api_resource = api_resources.find{ |api_resource| api_resource.name == resource_name }
    raise K8s::Error::UndefinedResource, "Unknown resource #{resource_name} for #{@api_version}"
  end

  ResourceClient.new(@transport, self, api_resource,
    namespace: namespace,
  )
end

#resources(namespace: nil) ⇒ Array<K8s::ResourceClient>

TODO: skip non-namespaced resources if namespace is given, or ignore namespace?

Parameters:

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

Returns:



98
99
100
101
102
# File 'lib/k8s/api_client.rb', line 98

def resources(namespace: nil)
  api_resources.map{ |api_resource| ResourceClient.new(@transport, self, api_resource,
    namespace: namespace,
  ) }
end