Class: K8s::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/k8s/client.rb,
lib/k8s/client/version.rb

Constant Summary collapse

VERSION =
"0.3.1"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport, namespace: nil) ⇒ Client

Returns a new instance of Client.

Parameters:



23
24
25
26
27
28
# File 'lib/k8s/client.rb', line 23

def initialize(transport, namespace: nil)
  @transport = transport
  @namespace = namespace

  @api_clients = {}
end

Class Method Details

.config(config) ⇒ K8s::Client

Parameters:

  • config (Phraos::Kube::Config)

Returns:



13
14
15
# File 'lib/k8s/client.rb', line 13

def self.config(config)
  new(Transport.config(config))
end

.in_cluster_configK8s::Client

Returns:



18
19
20
# File 'lib/k8s/client.rb', line 18

def self.in_cluster_config
  new(Transport.in_cluster_config)
end

Instance Method Details

#api(api_version = 'v1') ⇒ APIClient

Parameters:

  • api_version (String) (defaults to: 'v1')

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

Returns:



40
41
42
# File 'lib/k8s/client.rb', line 40

def api(api_version = 'v1')
  @api_clients[api_version] ||= APIClient.new(@transport, api_version)
end

#api_groupsArray<String>

Cached /apis preferred group apiVersions

Returns:

  • (Array<String>)


56
57
58
# File 'lib/k8s/client.rb', line 56

def api_groups
  @api_groups || api_groups!
end

#api_groups!Array<String>

Force-update /apis cache. Required if creating new CRDs/apiservices.

Returns:

  • (Array<String>)


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

def api_groups!
  @api_groups = @transport.get('/apis',
    response_class: K8s::API::MetaV1::APIGroupList,
  ).groups.map{|api_group| api_group.preferredVersion.groupVersion }
end

#apis(api_versions = nil, prefetch_resources: false, skip_missing: false) ⇒ Array<APIClient>

Parameters:

  • api_versions (Array<String>) (defaults to: nil)

    defaults to all APIs

  • prefetch_resources (Boolean) (defaults to: false)

    prefetch any missing api_resources for each api_version

  • skip_missing (Boolean) (defaults to: false)

    return APIClient without api_resources? if 404

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/k8s/client.rb', line 64

def apis(api_versions = nil, prefetch_resources: false, skip_missing: false)
  api_versions ||= ['v1'] + self.api_groups

  if prefetch_resources
    # api groups that are missing their api_resources
    api_paths = api_versions
      .uniq
      .select{|api_version| !api(api_version).api_resources? }
      .map{|api_version| APIClient.path(api_version) }

    # load into APIClient.api_resources=
    @transport.gets(*api_paths, response_class: K8s::API::MetaV1::APIResourceList, skip_missing: skip_missing).each do |api_resource_list|
      api(api_resource_list.groupVersion).api_resources = api_resource_list.resources if api_resource_list
    end
  end

  api_versions.map{|api_version| api(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:



106
107
108
# File 'lib/k8s/client.rb', line 106

def client_for_resource(resource, namespace: nil)
  api(resource.apiVersion).client_for_resource(resource, namespace: namespace)
end

#create_resource(resource) ⇒ K8s::Resource

Parameters:

Returns:



112
113
114
# File 'lib/k8s/client.rb', line 112

def create_resource(resource)
  client_for_resource(resource).create_resource(resource)
end

#delete_resource(resource) ⇒ K8s::Resource

Parameters:

Returns:



158
159
160
# File 'lib/k8s/client.rb', line 158

def delete_resource(resource)
  client_for_resource(resource).delete_resource(resource)
end

#get_resource(resource) ⇒ K8s::Resource

Parameters:

Returns:



118
119
120
# File 'lib/k8s/client.rb', line 118

def get_resource(resource)
  client_for_resource(resource).get_resource(resource)
end

#get_resources(resources) ⇒ Array<K8s::Resource, nil>

Returns nils for any resources that do not exist. This includes custom resources that were not yet defined.

Parameters:

Returns:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/k8s/client.rb', line 127

def get_resources(resources)
  # prefetch api resources, skip missing APIs
  resource_apis = apis(resources.map{ |resource| resource.apiVersion }, prefetch_resources: true, skip_missing: true)

  # map each resource to excon request options, or nil if resource is not (yet) defined
  requests = resources.zip(resource_apis).map{ |resource, api_client|
    next nil unless api_client.api_resources?

    resource_client = api_client.client_for_resource(resource)

    {
      method: 'GET',
      path: resource_client.path(resource..name, namespace: resource..namespace),
      response_class: resource_client.resource_class,
    }
  }

  # map non-nil requests to response objects, or nil for nil request options
  Util.compact_map(requests) { |reqs|
    @transport.requests(*reqs, skip_missing: true)
  }
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:



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

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

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

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

Parameters:

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

Returns:



85
86
87
# File 'lib/k8s/client.rb', line 85

def resources(namespace: nil)
  apis(prefetch_resources: true).map { |api| api.resources(namespace: namespace) }.flatten
end

#update_resource(resource) ⇒ K8s::Resource

Parameters:

Returns:



152
153
154
# File 'lib/k8s/client.rb', line 152

def update_resource(resource)
  client_for_resource(resource).update_resource(resource)
end

#versionK8s::API::Version

Returns:

Raises:



32
33
34
35
36
# File 'lib/k8s/client.rb', line 32

def version
  @transport.get('/version',
    response_class: K8s::API::Version,
  )
end