Class: Dapp::Kube::Kubernetes::Client

Inherits:
Object
  • Object
show all
Extended by:
Helper::YAML
Includes:
Helper::YAML
Defined in:
lib/dapp/kube/kubernetes/client.rb

Defined Under Namespace

Modules: Error, Resource

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper::YAML

yaml_load, yaml_load_file

Constructor Details

#initialize(namespace: nil) ⇒ Client

Returns a new instance of Client.



10
11
12
13
# File 'lib/dapp/kube/kubernetes/client.rb', line 10

def initialize(namespace: nil)
  @namespace = namespace
  @query_parameters = {}
end

Class Method Details

.kube_cluster_config(kube_config, cluster_name) ⇒ Object



277
278
279
# File 'lib/dapp/kube/kubernetes/client.rb', line 277

def kube_cluster_config(kube_config, cluster_name)
  kube_config.fetch('clusters', []).find {|cluster| cluster['name'] == cluster_name}
end

.kube_config(kube_config_path) ⇒ Object



256
257
258
# File 'lib/dapp/kube/kubernetes/client.rb', line 256

def kube_config(kube_config_path)
  yaml_load_file(kube_config_path) if File.exist?(kube_config_path)
end

.kube_config_pathObject



250
251
252
253
254
# File 'lib/dapp/kube/kubernetes/client.rb', line 250

def kube_config_path
  kube_config_path = ENV['KUBECONFIG']
  kube_config_path = File.join(ENV['HOME'], '.kube/config') unless kube_config_path
  kube_config_path
end

.kube_context_config(kube_config, kube_context_name) ⇒ Object



269
270
271
# File 'lib/dapp/kube/kubernetes/client.rb', line 269

def kube_context_config(kube_config, kube_context_name)
  kube_config.fetch('contexts', []).find {|context| context['name'] == kube_context_name}
end

.kube_context_name(kube_config) ⇒ Object



260
261
262
263
264
265
266
267
# File 'lib/dapp/kube/kubernetes/client.rb', line 260

def kube_context_name(kube_config)
  kube_config['current-context'] || begin
    if (context = kube_config.fetch('contexts', []).first)
      warn "[WARN] .kube/config current-context is not set, using context '#{context['name']}'"
      context['name']
    end
  end
end

.kube_context_namespace(kube_context_config) ⇒ Object



281
282
283
# File 'lib/dapp/kube/kubernetes/client.rb', line 281

def kube_context_namespace(kube_context_config)
  kube_context_config['context']['namespace']
end

.kube_user_config(kube_config, user_name) ⇒ Object



273
274
275
# File 'lib/dapp/kube/kubernetes/client.rb', line 273

def kube_user_config(kube_config, user_name)
  kube_config.fetch('users', []).find {|user| user['name'] == user_name}
end

Instance Method Details

#create_namespace!(name, **query_parameters) ⇒ Object



94
95
96
# File 'lib/dapp/kube/kubernetes/client.rb', line 94

def create_namespace!(name, **query_parameters)
  request!(:post, '/api/v1/namespaces', body: { metadata: { name: name } }, **query_parameters)
end

#delete_namespace!(name, **query_parameters) ⇒ Object



98
99
100
# File 'lib/dapp/kube/kubernetes/client.rb', line 98

def delete_namespace!(name, **query_parameters)
  request!(:delete, "/api/v1/namespaces/#{name}", **query_parameters)
end

#event_list(**query_parameters) ⇒ Object



124
125
126
# File 'lib/dapp/kube/kubernetes/client.rb', line 124

def event_list(**query_parameters)
  request!(:get, "/api/v1/namespaces/#{namespace}/events", **query_parameters)
end

#namespaceObject



15
16
17
# File 'lib/dapp/kube/kubernetes/client.rb', line 15

def namespace
  @namespace || self.class.kube_context_namespace(kube_context_config) || "default"
end

#namespace?(name, **query_parameters) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/dapp/kube/kubernetes/client.rb', line 90

def namespace?(name, **query_parameters)
  namespace_list(**query_parameters)['items'].map { |item| item['metadata']['name'] }.include?(name)
end

#namespace_list(**query_parameters) ⇒ Object



86
87
88
# File 'lib/dapp/kube/kubernetes/client.rb', line 86

def namespace_list(**query_parameters)
  request!(:get, '/api/v1/namespaces', **query_parameters)
end

#pod_log(name, follow: false, **query_parameters, &blk) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dapp/kube/kubernetes/client.rb', line 102

def pod_log(name, follow: false, **query_parameters, &blk)
  excon_parameters = follow ? { response_block: blk } : {}
  request!(:get,
           "/api/v1/namespaces/#{namespace}/pods/#{name}/log",
           excon_parameters: excon_parameters,
           response_body_parameters: {json: false},
           **{ follow: follow }.merge(query_parameters))
rescue Excon::Error::Timeout
  raise Error::Timeout
rescue Error::Base => err
  if err.net_status[:code] == :bad_request and err.net_status[:data][:response_body]
    msg = err.net_status[:data][:response_body]['message']
    if msg.end_with? 'ContainerCreating'
      raise Error::Pod::ContainerCreating, data: err.net_status[:data]
    elsif msg.end_with? 'PodInitializing'
      raise Error::Pod::PodInitializing, data: err.net_status[:data]
    end
  end

  raise
end

#with_namespace(namespace, &blk) ⇒ Object

Чтобы не перегружать методы явной передачей namespace. Данный метод может пригодиться только в ситуации, когда надо указать другой namespace, в большинстве случаев используется namespace из конструктора.



22
23
24
25
26
27
28
29
30
# File 'lib/dapp/kube/kubernetes/client.rb', line 22

def with_namespace(namespace, &blk)
  old_namespace = @namespace
  begin
    @namespace = namespace
    return yield
  ensure
    @namespace = old_namespace
  end
end

#with_query(query, &blk) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/dapp/kube/kubernetes/client.rb', line 32

def with_query(query, &blk)
  old_query = @query_parameters
  begin
    @query_parameters = query
    return yield
  ensure
    @query_parameters = old_query
  end
end