Class: Kdep::ClusterHealth

Inherits:
Object
  • Object
show all
Defined in:
lib/kdep/cluster_health.rb

Constant Summary collapse

CPU_WARN =
80
MEMORY_WARN =
85

Instance Method Summary collapse

Instance Method Details

#checkObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kdep/cluster_health.rb', line 6

def check
  begin
    output = Kdep::Kubectl.run("top", "nodes", "--no-headers")
  rescue Kdep::Kubectl::Error
    return ["Cluster health metrics unavailable (metrics-server may not be installed)"]
  end

  warnings = []

  output.each_line do |line|
    parts = line.strip.split(/\s+/)
    next if parts.length < 5

    name = parts[0]
    cpu_percent = parts[2].sub("%", "").to_i
    mem_percent = parts[4].sub("%", "").to_i

    if cpu_percent > CPU_WARN
      warnings << "Node #{name}: CPU at #{cpu_percent}% (threshold: #{CPU_WARN}%)"
    end

    if mem_percent > MEMORY_WARN
      warnings << "Node #{name}: Memory at #{mem_percent}% (threshold: #{MEMORY_WARN}%)"
    end
  end

  warnings
end

#report(ui) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/kdep/cluster_health.rb', line 35

def report(ui)
  warnings = check
  if warnings.empty?
    ui.success("Cluster health: OK")
  else
    warnings.each { |w| ui.warn(w) }
  end
end