debian-mirror-gitlab/lib/gitlab/kubernetes/node.rb

86 lines
2 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module Gitlab
module Kubernetes
class Node
def initialize(cluster)
@cluster = cluster
end
def all
2020-10-24 23:57:45 +05:30
{
nodes: metadata.presence,
node_connection_error: nodes_from_cluster[:connection_error],
metrics_connection_error: nodes_metrics_from_cluster[:connection_error]
}.compact
2020-07-28 23:09:34 +05:30
end
private
attr_reader :cluster
2020-10-24 23:57:45 +05:30
def metadata
nodes.map do |node|
base_data(node).merge(node_metrics(node))
end
end
2020-07-28 23:09:34 +05:30
def nodes_from_cluster
2020-10-24 23:57:45 +05:30
@nodes_from_cluster ||= graceful_request { cluster.kubeclient.get_nodes }
2020-07-28 23:09:34 +05:30
end
def nodes_metrics_from_cluster
2020-10-24 23:57:45 +05:30
@nodes_metrics_from_cluster ||= graceful_request { cluster.kubeclient.metrics_client.get_nodes }
2020-07-28 23:09:34 +05:30
end
def nodes
@nodes ||= nodes_from_cluster[:response].to_a
end
def nodes_metrics
@nodes_metrics ||= nodes_metrics_from_cluster[:response].to_a
end
def node_metrics_from_node(node)
nodes_metrics.find do |node_metric|
node_metric.metadata.name == node.metadata.name
end
end
def graceful_request(&block)
::Gitlab::Kubernetes::KubeClient.graceful_request(cluster.id, &block)
end
2020-10-24 23:57:45 +05:30
def base_data(node)
2020-07-28 23:09:34 +05:30
{
'metadata' => {
'name' => node.metadata.name
},
'status' => {
'capacity' => {
'cpu' => node.status.capacity.cpu,
'memory' => node.status.capacity.memory
},
'allocatable' => {
'cpu' => node.status.allocatable.cpu,
'memory' => node.status.allocatable.memory
}
}
}
end
def node_metrics(node)
node_metrics = node_metrics_from_node(node)
return {} unless node_metrics
{
'usage' => {
'cpu' => node_metrics.usage.cpu,
'memory' => node_metrics.usage.memory
}
}
end
end
end
end