Class: LanguageOperator::CLI::Helpers::KubeconfigValidator
- Inherits:
-
Object
- Object
- LanguageOperator::CLI::Helpers::KubeconfigValidator
- Defined in:
- lib/language_operator/cli/helpers/kubeconfig_validator.rb
Overview
Validates kubeconfig and cluster connectivity
Class Method Summary collapse
-
.detect_kubeconfig ⇒ Object
Detect kubeconfig path.
-
.kubeconfig_exists? ⇒ Boolean
Check if kubeconfig exists.
-
.validate ⇒ Object
Validate kubeconfig exists and cluster is accessible Returns [valid, error_message].
-
.validate! ⇒ Object
Validate and exit with error if invalid.
Class Method Details
.detect_kubeconfig ⇒ Object
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
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 |
.validate ⇒ Object
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_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, ] unless k8s.operator_installed? [true, nil] rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT => e [false, (e)] rescue K8s::Error::Unauthorized => e [false, (e)] rescue StandardError => e [false, (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, = validate return if valid Formatters::ProgressFormatter.error() exit 1 end |