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.



61
62
63
64
65
66
67
68
# File 'lib/dapp/kube/kubernetes/client.rb', line 61

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.



56
57
58
# File 'lib/dapp/kube/kubernetes/client.rb', line 56

def config
  @config
end

#contextObject (readonly)

Returns the value of attribute context.



57
58
59
# File 'lib/dapp/kube/kubernetes/client.rb', line 57

def context
  @context
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



58
59
60
# File 'lib/dapp/kube/kubernetes/client.rb', line 58

def namespace
  @namespace
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



59
60
61
# File 'lib/dapp/kube/kubernetes/client.rb', line 59

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



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/dapp/kube/kubernetes/client.rb', line 168

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



159
160
161
# File 'lib/dapp/kube/kubernetes/client.rb', line 159

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

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



163
164
165
# File 'lib/dapp/kube/kubernetes/client.rb', line 163

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

#event_list(**query_parameters) ⇒ Object



204
205
206
# File 'lib/dapp/kube/kubernetes/client.rb', line 204

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

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

Returns:

  • (Boolean)


155
156
157
# File 'lib/dapp/kube/kubernetes/client.rb', line 155

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

#namespace_list(**query_parameters) ⇒ Object



151
152
153
# File 'lib/dapp/kube/kubernetes/client.rb', line 151

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

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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/dapp/kube/kubernetes/client.rb', line 182

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 на основе версии кластера



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

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



73
74
75
76
77
78
79
80
81
# File 'lib/dapp/kube/kubernetes/client.rb', line 73

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

#with_query(query, &blk) ⇒ Object



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

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