Class: LanguageOperator::CLI::Commands::Cluster
- Inherits:
-
BaseCommand
- Object
- Thor
- BaseCommand
- LanguageOperator::CLI::Commands::Cluster
- Includes:
- Helpers::UxHelper, LanguageOperator::Constants
- Defined in:
- lib/language_operator/cli/commands/cluster.rb
Overview
Cluster management commands
Constant Summary
Constants included from LanguageOperator::Constants
LanguageOperator::Constants::ALL_MODE_ALIASES, LanguageOperator::Constants::EXECUTION_MODES, LanguageOperator::Constants::PRIMARY_MODES, LanguageOperator::Constants::RESOURCE_AGENT, LanguageOperator::Constants::RESOURCE_AGENT_VERSION, LanguageOperator::Constants::RESOURCE_MODEL, LanguageOperator::Constants::RESOURCE_PERSONA, LanguageOperator::Constants::RESOURCE_TOOL
Instance Method Summary collapse
Methods included from Helpers::UxHelper
#box, #highlight_ruby_code, #logo, #pastel, #prompt, #spinner, #table
Methods included from LanguageOperator::Constants
Instance Method Details
#create(name) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/language_operator/cli/commands/cluster.rb', line 22 def create(name) handle_command_error('create cluster') do kubeconfig = [:kubeconfig] context = [:context] org_id = [:organization_id] # Create Kubernetes client to find organization namespace k8s = Formatters::ProgressFormatter.with_spinner('Connecting to Kubernetes cluster') do Kubernetes::Client.new(kubeconfig: kubeconfig, context: context) end # Auto-detect organization if not provided unless org_id org_id = detect_organization_id(k8s) exit 1 unless org_id end # Find the organization namespace namespace = Formatters::ProgressFormatter.with_spinner("Finding namespace for organization #{org_id[0..7]}") do find_org_namespace(k8s, org_id) end unless namespace Formatters::ProgressFormatter.error("Organization namespace not found for ID: #{org_id}") puts "\nMake sure the organization exists and you have access to it." puts "Available organization namespaces:" list_org_namespaces(k8s) exit 1 end # Handle dry-run: output manifest and exit early if [:dry_run] # Create a mock client to pass org context for dry run mock_client = Object.new def mock_client.current_org_id; @org_id; end mock_client.instance_variable_set(:@org_id, org_id) resource = Kubernetes::ResourceBuilder.language_cluster(name, namespace: namespace, domain: [:domain], k8s_client: mock_client) puts resource.to_yaml return end # Check if cluster already exists in Kubernetes begin existing_cluster = k8s.get_resource('LanguageCluster', name, namespace) if existing_cluster Formatters::ProgressFormatter.error("Cluster '#{name}' already exists in namespace '#{namespace}'") exit 1 end rescue StandardError => e # If we get a 404 or API error, the cluster doesn't exist - that's fine unless e..include?('404') || e..include?('Not Found') warn "Warning: Could not check for existing cluster: #{e.}" if ENV['DEBUG'] end end # Check if operator is installed unless k8s.operator_installed? Formatters::ProgressFormatter.error('Language Operator not found in cluster') puts "\nInstall the operator first:" puts ' langop install' exit 1 end # Create namespace if it doesn't exist unless k8s.namespace_exists?(namespace) Formatters::ProgressFormatter.with_spinner("Creating namespace '#{namespace}'") do k8s.create_namespace(namespace, labels: Constants::KubernetesLabels.cluster_management_labels.merge( Constants::KubernetesLabels::CLUSTER_LABEL => name )) end end # Create LanguageCluster resource Formatters::ProgressFormatter.with_spinner('Creating LanguageCluster resource') do labels = { 'langop.io/organization-id' => org_id } resource = Kubernetes::ResourceBuilder.language_cluster(name, namespace: namespace, domain: [:domain], labels: labels, k8s_client: k8s) k8s.apply_resource(resource) resource end # Get the actual Kubernetes context being used actual_context = k8s.current_context # Save cluster to config Formatters::ProgressFormatter.with_spinner('Saving cluster configuration') do Config::ClusterConfig.add_cluster( name, namespace, kubeconfig || ENV.fetch('KUBECONFIG', LanguageOperator::Utils::SecurePath.('.kube/config')), actual_context ) end # Switch to new cluster if requested Config::ClusterConfig.set_current_cluster(name) if [:switch] puts format_cluster_details( name: name, namespace: namespace, context: actual_context, domain: [:domain], org_id: org_id, status: 'Ready', created: Time.now.strftime('%Y-%m-%dT%H:%M:%SZ') ) # Show usage instructions if not auto-switched unless [:switch] puts puts 'Switch to this cluster with:' puts pastel.dim(" langop use #{name}") end end end |
#delete(name) ⇒ Object
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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/language_operator/cli/commands/cluster.rb', line 217 def delete(name) handle_command_error('delete cluster') do unless Config::ClusterConfig.cluster_exists?(name) Formatters::ProgressFormatter.error("Cluster '#{name}' not found") exit 1 end cluster = Config::ClusterConfig.get_cluster(name) # Confirm deletion return if ![:force] && !confirm_deletion('cluster', name, name) # Delete LanguageCluster resource from Kubernetes (unless --force-local) if [:force_local] Formatters::ProgressFormatter.warn('Skipping Kubernetes resource deletion (--force-local specified)') else begin k8s = Helpers::ClusterValidator.kubernetes_client(name) Formatters::ProgressFormatter.with_spinner('Deleting LanguageCluster resource') do k8s.delete_resource('LanguageCluster', name, cluster[:namespace]) end rescue StandardError => e Formatters::ProgressFormatter.error("Failed to delete cluster resource: #{e.}") puts puts 'Cluster deletion failed. The LanguageCluster resource could not be removed from Kubernetes.' puts 'This may be due to:' puts ' • Network connectivity issues' puts ' • Insufficient permissions' puts ' • The cluster resource no longer exists' puts puts 'To force removal from local configuration only, use:' puts pastel.dim(" langop cluster delete #{name} --force-local") exit 1 end end # Remove from config only after successful Kubernetes deletion Formatters::ProgressFormatter.with_spinner('Removing cluster from configuration') do Config::ClusterConfig.remove_cluster(name) end # Clear current cluster if this was it Config::ClusterConfig.set_current_cluster(nil) if Config::ClusterConfig.current_cluster == name end end |
#inspect(name) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/language_operator/cli/commands/cluster.rb', line 265 def inspect(name) handle_command_error('inspect cluster') do unless Config::ClusterConfig.cluster_exists?(name) Formatters::ProgressFormatter.error("Cluster '#{name}' not found") exit 1 end cluster = Config::ClusterConfig.get_cluster(name) # Get detailed cluster info begin k8s = Helpers::ClusterValidator.kubernetes_client(name) # Get cluster resource cluster_resource = k8s.get_resource('LanguageCluster', name, cluster[:namespace]) status = cluster_resource.dig('status', 'phase') || 'Unknown' domain = cluster_resource.dig('spec', 'domain') # Main cluster information puts highlighted_box( title: 'LanguageCluster', rows: { 'Name' => pastel.white.bold(name), 'Namespace' => cluster[:namespace], 'Cluster' => name, 'Context' => cluster[:context] || 'default', 'Domain' => domain, 'Status' => status, 'Created' => cluster[:created] }.compact ) puts # Get agents agents = k8s.list_resources(RESOURCE_AGENT, namespace: cluster[:namespace]) agent_items = agents.map do |agent| { name: agent.dig('metadata', 'name'), status: agent.dig('status', 'phase') || 'Unknown' } end list_box(title: 'Agents', items: agent_items, style: :detailed) puts # Get tools tools = k8s.list_resources(RESOURCE_TOOL, namespace: cluster[:namespace]) tool_items = tools.map do |tool| { name: tool.dig('metadata', 'name') } end list_box(title: 'Tools', items: tool_items, style: :detailed) puts # Get models models = k8s.list_resources(RESOURCE_MODEL, namespace: cluster[:namespace]) model_items = models.map do |model| provider = model.dig('spec', 'provider') model_name = model.dig('spec', 'modelName') { name: model.dig('metadata', 'name'), meta: "#{provider}/#{model_name}" } end list_box(title: 'Models', items: model_items, style: :detailed) puts # Get personas personas = k8s.list_resources('LanguagePersona', namespace: cluster[:namespace]) persona_items = personas.map do |persona| { name: persona.dig('metadata', 'name'), meta: persona.dig('spec', 'tone') } end list_box(title: 'Personas', items: persona_items, style: :detailed) rescue StandardError => e Formatters::ProgressFormatter.error("Failed to get cluster details: #{e.}") raise if ENV['DEBUG'] end end end |
#list ⇒ Object
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/language_operator/cli/commands/cluster.rb', line 141 def list handle_command_error('list clusters') do # Query Kubernetes directly for all LanguageCluster resources (like kubectl get languageclusters -A) k8s = Kubernetes::Client.new # List all LanguageCluster resources across all namespaces language_clusters = k8s.list_resources('LanguageCluster', namespace: nil) if language_clusters.empty? Formatters::ProgressFormatter.info('No clusters found') puts "\nCreate a cluster with:" puts ' langop cluster create <name>' return end current = Config::ClusterConfig.current_cluster # Build table data from LanguageCluster resources table_data = language_clusters.map do |cluster_resource| begin name = cluster_resource.dig('metadata', 'name') namespace = cluster_resource.dig('metadata', 'namespace') status = cluster_resource.dig('status', 'phase') || 'Unknown' domain = cluster_resource.dig('spec', 'domain') # Get organization information from the cluster resource itself org_id = cluster_resource.dig('metadata', 'labels', 'langop.io/organization-id') organization = org_id ? org_id[0..7] : 'legacy' # Check if this cluster matches the current selection name_display = name name_display = "#{pastel.bold(name)} (selected)" if name == current # Get related resources in the same namespace agents = k8s.list_resources(RESOURCE_AGENT, namespace: namespace) tools = k8s.list_resources(RESOURCE_TOOL, namespace: namespace) models = k8s.list_resources(RESOURCE_MODEL, namespace: namespace) { name: name_display, namespace: namespace, organization: organization, agents: agents.count, tools: tools.count, models: models.count, status: status, domain: domain || '' } rescue StandardError => e # Handle any errors gracefully name = cluster_resource.dig('metadata', 'name') || 'unknown' namespace = cluster_resource.dig('metadata', 'namespace') || 'unknown' { name: name, namespace: namespace, organization: '?', agents: '?', tools: '?', models: '?', status: 'Error', domain: '?' } end end Formatters::TableFormatter.clusters(table_data) puts "\nNo cluster selected. Use 'langop use <cluster>' to select one." unless current end end |