lib/seira/pods.rb in seira-0.4.7 vs lib/seira/pods.rb in seira-0.4.8

- old
+ new

@@ -80,72 +80,91 @@ else puts "Warning: Unrecognized argument #{arg}" end end - # If a pod name is specified, connect to that pod - # If a tier is specified, connect to a random pod from that tier - # Otherwise connect to a terminal pod - target_pod = pod_name || Helpers.fetch_pods(context: context, filters: { tier: tier || 'terminal' }).sample - if target_pod.nil? - puts 'Could not find pod to connect to' - exit(1) - end + # If a pod name is specified, find the pod with that name + existing_pod = Helpers.fetch_pod(pod_name, context: context) unless pod_name.to_s.empty? + # Use existing pod if found and not --dedicated + # Otherwise, connect to a random pod from the specified tier or the 'terminal' tier if unspecified + target_pod = if existing_pod && dedicated + puts "Cannot create new dedicated pod with name: #{pod_name}" + puts "A pod with this name already exists" + exit(1) + elsif existing_pod + existing_pod + elsif dedicated || pod_name.to_s.empty? + Helpers.fetch_pods(context: context, filters: { tier: tier || 'terminal' }).sample + else + puts 'Could not find pod to connect to' + exit(1) + end + if dedicated - # Create a dedicated temp pod to run in - # This is useful if you would like to have a persistent connection that doesn't get killed - # when someone updates the terminal deployment, or if you want to avoid noisy neighbors - # connected to the same pod. - temp_name = "temp-#{Random.unique_name}" + new_pod = if pod_name.nil? + create_dedicated_pod(target_pod) + else + create_dedicated_pod(target_pod, pod_name) + end - # Construct a spec for the temp pod - spec = target_pod['spec'] - temp_pod = { - apiVersion: target_pod['apiVersion'], - kind: 'Pod', - spec: spec, - metadata: { - name: temp_name, - annotations: { - owner: Helpers.shell_username - } + connect_to_pod(new_pod, command) + clean_up_pod(new_pod) + else + # If we don't need a dedicated pod, it's way easier - just connect to the already running one + connect_to_pod(target_pod.dig('metadata', 'name'), command) + end + end + + # Create a dedicated temp pod to run in + # This is useful if you would like to have a persistent connection that doesn't get killed + # when someone updates the terminal deployment, or if you want to avoid noisy neighbors + # connected to the same pod. + def create_dedicated_pod(target_pod, pod_name = "temp-#{Random.unique_name}") + spec = target_pod['spec'] + temp_pod = { + apiVersion: target_pod['apiVersion'], + kind: 'Pod', + spec: spec, + metadata: { + name: pod_name, + annotations: { + owner: Helpers.shell_username } } - # Don't restart the pod when it dies - spec['restartPolicy'] = 'Never' - # Overwrite container commands with something that times out, so if the client disconnects - # there's a limited amount of time that the temp pod is still taking up resources - # Note that this will break a pods which depends on containers running real commands, but - # for a simple terminal pod it's fine - spec['containers'].each do |container| - container['command'] = ['sleep', '86400'] # 86400 seconds = 24 hours - end + } + # Don't restart the pod when it dies + spec['restartPolicy'] = 'Never' + # Overwrite container commands with something that times out, so if the client disconnects + # there's a limited amount of time that the temp pod is still taking up resources + # Note that this will break a pods which depends on containers running real commands, but + # for a simple terminal pod it's fine + spec['containers'].each do |container| + container['command'] = %w[sleep 86400] # 86400 seconds = 24 hours + end - puts 'Creating dedicated pod...' - unless system("kubectl --namespace=#{app} create -f - <<JSON\n#{temp_pod.to_json}\nJSON") - puts 'Failed to create dedicated pod' - exit(1) - end + puts 'Creating dedicated pod...' + unless system("kubectl --namespace=#{app} create -f - <<JSON\n#{temp_pod.to_json}\nJSON") + puts 'Failed to create dedicated pod' + exit(1) + end - print 'Waiting for dedicated pod to start...' - loop do - pod = JSON.parse(kubectl("get pods/#{temp_name} -o json", context: context, return_output: true)) - break if pod['status']['phase'] == 'Running' - print '.' - sleep 1 - end - print "\n" + print 'Waiting for dedicated pod to start...' + loop do + pod = JSON.parse(kubectl("get pods/#{pod_name} -o json", context: context, return_output: true)) + break if pod['status']['phase'] == 'Running' + print '.' + sleep 1 + end + print "\n" - connect_to_pod(temp_name, command) + pod_name + end - # Clean up on disconnect so temp pod isn't taking up resources - unless kubectl("delete pods/#{temp_name}", context: context) - puts 'Failed to delete temp pod' - end - else - # If we don't need a dedicated pod, it's way easier - just connect to the already running one - connect_to_pod(target_pod.dig('metadata', 'name'), command) + # Clean up pod so it isn't taking up resources. Usually only for dedicated pods + def clean_up_pod(pod_name) + unless kubectl("delete pods/#{pod_name}", context: context) + puts 'Failed to delete temp pod' end end def connect_to_pod(name, command = 'sh') puts "Connecting to #{name}..."