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

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

Defined Under Namespace

Modules: Error, Resource

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.



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

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

Instance Method Details

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



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

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

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



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

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

#event_list(**query_parameters) ⇒ Object



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

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

#namespaceObject



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

def namespace
  @namespace || kube_context_config['context']['namespace'] || 'default'
end

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

Returns:

  • (Boolean)


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

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

#namespace_list(**query_parameters) ⇒ Object



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

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

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



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

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 из конструктора.



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

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

#with_query(query, &blk) ⇒ Object



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

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