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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::YAML

yaml_load, yaml_load_file

Constructor Details

#initialize(config, context, namespace, timeout: nil) ⇒ Client

Returns a new instance of Client.



85
86
87
88
89
90
91
92
# File 'lib/dapp/kube/kubernetes/client.rb', line 85

def initialize(config, context, namespace, timeout: nil)
  @config = config
  @context = context
  @namespace = namespace
  @timeout = timeout
  @query_parameters = {}
  @cluster_version
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



80
81
82
# File 'lib/dapp/kube/kubernetes/client.rb', line 80

def config
  @config
end

#contextObject (readonly)

Returns the value of attribute context.



81
82
83
# File 'lib/dapp/kube/kubernetes/client.rb', line 81

def context
  @context
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



82
83
84
# File 'lib/dapp/kube/kubernetes/client.rb', line 82

def namespace
  @namespace
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



83
84
85
# File 'lib/dapp/kube/kubernetes/client.rb', line 83

def timeout
  @timeout
end

Instance Method Details

#cluster_version(**query_parameters) ⇒ Object

minikube returns empty major and minor. Fallback to stable only apis for minikube setup



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/dapp/kube/kubernetes/client.rb', line 192

def cluster_version(**query_parameters)
  version_obj = request!(:get, "/version", **query_parameters)
  @cluster_version ||= begin
    major = version_obj['major']
    minor = version_obj['minor'].gsub(/\+$/,'')
    k8s_version = "#{major}.#{minor}"
    if K8S_API_ENDPOINTS.has_key?(k8s_version)
      k8s_version
    else
      "stable"
    end
  end
end

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



183
184
185
# File 'lib/dapp/kube/kubernetes/client.rb', line 183

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

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



187
188
189
# File 'lib/dapp/kube/kubernetes/client.rb', line 187

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

#event_list(**query_parameters) ⇒ Object



228
229
230
# File 'lib/dapp/kube/kubernetes/client.rb', line 228

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

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

Returns:

  • (Boolean)


179
180
181
# File 'lib/dapp/kube/kubernetes/client.rb', line 179

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

#namespace_list(**query_parameters) ⇒ Object



175
176
177
# File 'lib/dapp/kube/kubernetes/client.rb', line 175

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

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



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/dapp/kube/kubernetes/client.rb', line 206

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

#resource_endpoint_path(resource) ⇒ Object

NOTICE: Название метода аналогично kind’у выдаваемого результата. NOTICE: В данном случае в результате kind=DeploymentList. NOTICE: Методы создания/обновления/удаления сущностей kubernetes заканчиваются на ‘!’. Например, create_deployment!. В каждом методе происходит выбор api на основе версии кластера



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

def resource_endpoint_path(resource)
  K8S_API_ENDPOINTS[cluster_version()].map do |path, resources|
    resources.include?(resource) ? path : nil
  end.compact.first
end

#with_namespace(namespace, &blk) ⇒ Object

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



97
98
99
100
101
102
103
104
105
# File 'lib/dapp/kube/kubernetes/client.rb', line 97

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

#with_query(query, &blk) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/dapp/kube/kubernetes/client.rb', line 107

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