Class: LanguageOperator::CLI::Helpers::HealthChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/language_operator/cli/helpers/health_checker.rb

Overview

Health checker for Language Operator components

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(k8s_client, namespace, cluster_name = nil, cluster_namespace = nil) ⇒ HealthChecker

Returns a new instance of HealthChecker.



12
13
14
15
16
17
# File 'lib/language_operator/cli/helpers/health_checker.rb', line 12

def initialize(k8s_client, namespace, cluster_name = nil, cluster_namespace = nil)
  @k8s = k8s_client
  @namespace = namespace
  @cluster_name = cluster_name
  @cluster_namespace = cluster_namespace
end

Instance Attribute Details

#cluster_nameObject (readonly)

Returns the value of attribute cluster_name.



10
11
12
# File 'lib/language_operator/cli/helpers/health_checker.rb', line 10

def cluster_name
  @cluster_name
end

#cluster_namespaceObject (readonly)

Returns the value of attribute cluster_namespace.



10
11
12
# File 'lib/language_operator/cli/helpers/health_checker.rb', line 10

def cluster_namespace
  @cluster_namespace
end

#k8sObject (readonly)

Returns the value of attribute k8s.



10
11
12
# File 'lib/language_operator/cli/helpers/health_checker.rb', line 10

def k8s
  @k8s
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



10
11
12
# File 'lib/language_operator/cli/helpers/health_checker.rb', line 10

def namespace
  @namespace
end

Instance Method Details

#check_clickhouse_healthObject

Check ClickHouse health and authentication



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/language_operator/cli/helpers/health_checker.rb', line 60

def check_clickhouse_health
  # First check if service exists
  begin
    @k8s.client.api('v1')
               .resource('services', namespace: @namespace)
               .get('language-operator-clickhouse')
  rescue K8s::Error::NotFound
    return { healthy: false, error: 'ClickHouse service not found' }
  end

  # Try to connect and authenticate using port-forward
  require 'net/http'
  require 'uri'
  require 'open3'

  port = find_available_port
  pf_pid = nil

  begin
    # Start port-forward in background
    pf_command = "kubectl port-forward -n #{@namespace} service/language-operator-clickhouse #{port}:8123"
    pf_stdin, pf_stdout, pf_stderr, pf_thread = Open3.popen3(pf_command)

    # Give port-forward time to establish
    sleep(2)

    # Test connection with credentials
    uri = URI("http://localhost:#{port}/ping")
    
    http = Net::HTTP.new(uri.host, uri.port)
    http.read_timeout = 5
    http.open_timeout = 5

    # Try with auth (default ClickHouse credentials for Language Operator)
    request = Net::HTTP::Get.new(uri)
    request.basic_auth('langop', 'langop')
    
    response = http.request(request)
    
    {
      healthy: response.code == '200',
      auth_works: response.code == '200',
      response_code: response.code
    }
  rescue StandardError => e
    { healthy: false, error: e.message }
  ensure
    # Clean up port-forward
    if pf_thread&.alive?
      Process.kill('TERM', pf_thread.pid) rescue nil
      pf_thread.join(1)
    end
  end
end

#check_cluster_existsObject

Check if the selected cluster exists in Kubernetes



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/language_operator/cli/helpers/health_checker.rb', line 172

def check_cluster_exists
  return { healthy: true, note: 'No cluster specified' } unless @cluster_name && @cluster_namespace

  cluster_resource = @k8s.get_resource('LanguageCluster', @cluster_name, @cluster_namespace)

  status = cluster_resource.dig('status', 'phase') || 'Unknown'
  {
    healthy: true,
    status: status,
    exists: true
  }
rescue K8s::Error::NotFound
  { healthy: false, error: "Cluster '#{@cluster_name}' not found in namespace '#{@cluster_namespace}'" }
rescue StandardError => e
  { healthy: false, error: e.message }
end

#check_dashboard_healthObject

Check dashboard deployment health



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/language_operator/cli/helpers/health_checker.rb', line 20

def check_dashboard_health
  deployment = @k8s.client.api('apps/v1')
                       .resource('deployments', namespace: @namespace)
                       .get('language-operator-dashboard')

  ready_replicas = deployment.dig('status', 'readyReplicas') || 0
  desired_replicas = deployment.dig('spec', 'replicas') || 0

  {
    healthy: ready_replicas == desired_replicas && desired_replicas > 0,
    ready_replicas: ready_replicas,
    desired_replicas: desired_replicas
  }
rescue K8s::Error::NotFound
  { healthy: false, error: 'Dashboard deployment not found' }
rescue StandardError => e
  { healthy: false, error: e.message }
end

#check_operator_healthObject

Check operator deployment health



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/language_operator/cli/helpers/health_checker.rb', line 40

def check_operator_health
  deployment = @k8s.client.api('apps/v1')
                       .resource('deployments', namespace: @namespace)
                       .get('language-operator')

  ready_replicas = deployment.dig('status', 'readyReplicas') || 0
  desired_replicas = deployment.dig('spec', 'replicas') || 0

  {
    healthy: ready_replicas == desired_replicas && desired_replicas > 0,
    ready_replicas: ready_replicas,
    desired_replicas: desired_replicas
  }
rescue K8s::Error::NotFound
  { healthy: false, error: 'Operator deployment not found' }
rescue StandardError => e
  { healthy: false, error: e.message }
end

#check_postgres_healthObject

Check PostgreSQL health and authentication



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/language_operator/cli/helpers/health_checker.rb', line 116

def check_postgres_health
  # First check if service exists
  begin
    @k8s.client.api('v1')
               .resource('services', namespace: @namespace)
               .get('language-operator-dashboard-postgresql')
  rescue K8s::Error::NotFound
    return { healthy: false, error: 'PostgreSQL service not found' }
  end

  # Try to connect and authenticate using port-forward and pg_isready
  require 'open3'

  port = find_available_port
  pf_pid = nil

  begin
    # Start port-forward in background
    pf_command = "kubectl port-forward -n #{@namespace} service/language-operator-dashboard-postgresql #{port}:5432"
    pf_stdin, pf_stdout, pf_stderr, pf_thread = Open3.popen3(pf_command)

    # Give port-forward time to establish
    sleep(2)

    # Test connection with pg_isready (if available) or simple TCP connection
    if system('which pg_isready > /dev/null 2>&1')
      # Use pg_isready for proper PostgreSQL health check
      test_output, test_status = Open3.capture2e("pg_isready -h localhost -p #{port} -U postgres -d langop_dashboard")
      {
        healthy: test_status.success?,
        auth_works: test_status.success?,
        output: test_output.strip
      }
    else
      # Fallback to basic TCP connection test
      require 'socket'
      begin
        socket = TCPSocket.new('localhost', port)
        socket.close
        { healthy: true, auth_works: false, note: 'pg_isready not available - basic TCP test only' }
      rescue StandardError => e
        { healthy: false, error: e.message }
      end
    end
  rescue StandardError => e
    { healthy: false, error: e.message }
  ensure
    # Clean up port-forward
    if pf_thread&.alive?
      Process.kill('TERM', pf_thread.pid) rescue nil
      pf_thread.join(1)
    end
  end
end

#run_all_checksObject

Run all health checks with progress indicators



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/language_operator/cli/helpers/health_checker.rb', line 190

def run_all_checks
  results = {}

  # Cluster existence check (first)
  if @cluster_name && @cluster_namespace
    begin
      results[:cluster] = Formatters::ProgressFormatter.with_spinner(
        "Verifying cluster '#{@cluster_name}' exists"
      ) { check_cluster_exists }
    rescue StandardError => e
      results[:cluster] = { healthy: false, error: e.message }
    end
  end

  # Dashboard health check
  begin
    results[:dashboard] = Formatters::ProgressFormatter.with_spinner(
      'Verifying language-operator-dashboard deployment'
    ) { check_dashboard_health }
  rescue StandardError => e
    results[:dashboard] = { healthy: false, error: e.message }
  end

  # Operator health check  
  begin
    results[:operator] = Formatters::ProgressFormatter.with_spinner(
      'Verifying language-operator deployment'
    ) { check_operator_health }
  rescue StandardError => e
    results[:operator] = { healthy: false, error: e.message }
  end

  # ClickHouse health check
  begin
    results[:clickhouse] = Formatters::ProgressFormatter.with_spinner(
      'Verifying ClickHouse health and authentication'
    ) { check_clickhouse_health }
  rescue StandardError => e
    results[:clickhouse] = { healthy: false, error: e.message }
  end

  # PostgreSQL health check
  begin
    results[:postgres] = Formatters::ProgressFormatter.with_spinner(
      'Verifying PostgreSQL health and authentication'
    ) { check_postgres_health }
  rescue StandardError => e
    results[:postgres] = { healthy: false, error: e.message }
  end

  results
end