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.



55
56
57
58
59
60
61
62
# File 'lib/dapp/kube/kubernetes/client.rb', line 55

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.



50
51
52
# File 'lib/dapp/kube/kubernetes/client.rb', line 50

def config
  @config
end

#contextObject (readonly)

Returns the value of attribute context.



51
52
53
# File 'lib/dapp/kube/kubernetes/client.rb', line 51

def context
  @context
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



52
53
54
# File 'lib/dapp/kube/kubernetes/client.rb', line 52

def namespace
  @namespace
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



53
54
55
# File 'lib/dapp/kube/kubernetes/client.rb', line 53

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



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/dapp/kube/kubernetes/client.rb', line 162

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



153
154
155
# File 'lib/dapp/kube/kubernetes/client.rb', line 153

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

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



157
158
159
# File 'lib/dapp/kube/kubernetes/client.rb', line 157

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

#event_list(**query_parameters) ⇒ Object



198
199
200
# File 'lib/dapp/kube/kubernetes/client.rb', line 198

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

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

Returns:

  • (Boolean)


149
150
151
# File 'lib/dapp/kube/kubernetes/client.rb', line 149

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

#namespace_list(**query_parameters) ⇒ Object



145
146
147
# File 'lib/dapp/kube/kubernetes/client.rb', line 145

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

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/dapp/kube/kubernetes/client.rb', line 176

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



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

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



67
68
69
70
71
72
73
74
75
# File 'lib/dapp/kube/kubernetes/client.rb', line 67

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

#with_query(query, &blk) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/dapp/kube/kubernetes/client.rb', line 77

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