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

Instance Attribute Summary collapse

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)



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

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

Instance Attribute Details

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

Cached APIResources

Returns:



53
54
55
# File 'lib/k8s/api_client.rb', line 53

def api_resources
  @api_resources || api_resources!
end

#api_versionString (readonly)

Returns:

  • (String)


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

def 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)


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

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_resources!Array<K8s::API::MetaV1::APIResource>

Force-update APIResources

Returns:



45
46
47
48
# File 'lib/k8s/api_client.rb', line 45

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

#api_resources?Bool

Returns loaded yet?.

Returns:

  • (Bool)

    loaded yet?



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

def api_resources?
  !!@api_resources
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
# 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

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

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

#find_api_resource(resource_name) ⇒ K8s::API::MetaV1::APIResource

Parameters:

  • resource_name (String)

Returns:

Raises:



60
61
62
63
64
65
66
# File 'lib/k8s/api_client.rb', line 60

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

  found_resource
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:



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

def list_resources(resources = nil, **options)
  resources ||= self.resources.select(&:list?)

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

#path(*path) ⇒ String

Parameters:

  • path (Array<String>)

    join path from parts

Returns:

  • (String)


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

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:



72
73
74
# File 'lib/k8s/api_client.rb', line 72

def resource(resource_name, namespace: nil)
  ResourceClient.new(@transport, self, find_api_resource(resource_name), 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:



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

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