Class: LanguageOperator::CLI::Helpers::KubeconfigValidator

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

Overview

Validates kubeconfig and cluster connectivity

Class Method Summary collapse

Class Method Details

.detect_kubeconfigObject

Detect kubeconfig path



50
51
52
# File 'lib/language_operator/cli/helpers/kubeconfig_validator.rb', line 50

def detect_kubeconfig
  ENV.fetch('KUBECONFIG', nil) || default_kubeconfig_path
end

.kubeconfig_exists?Boolean

Check if kubeconfig exists

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/language_operator/cli/helpers/kubeconfig_validator.rb', line 55

def kubeconfig_exists?
  path = detect_kubeconfig
  path && File.exist?(path)
end

.validateObject

Validate kubeconfig exists and cluster is accessible Returns [valid, error_message]



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/language_operator/cli/helpers/kubeconfig_validator.rb', line 15

def validate
  # Check if kubeconfig file exists
  kubeconfig_path = detect_kubeconfig
  return [false, kubeconfig_missing_message(kubeconfig_path)] unless kubeconfig_path && File.exist?(kubeconfig_path)

  # Try to connect to cluster
  begin
    k8s = Kubernetes::Client.new(kubeconfig: kubeconfig_path)

    # Test connectivity by listing namespaces
    k8s.client.api('v1').resource('namespaces').list

    # Check if operator is installed
    return [false, operator_missing_message] unless k8s.operator_installed?

    [true, nil]
  rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT => e
    [false, connection_failed_message(e)]
  rescue K8s::Error::Unauthorized => e
    [false, auth_failed_message(e)]
  rescue StandardError => e
    [false, generic_error_message(e)]
  end
end

.validate!Object

Validate and exit with error if invalid



41
42
43
44
45
46
47
# File 'lib/language_operator/cli/helpers/kubeconfig_validator.rb', line 41

def validate!
  valid, error_message = validate
  return if valid

  Formatters::ProgressFormatter.error(error_message)
  exit 1
end