Skip to content

Commit 7df191f

Browse files
authored
Merge pull request #18 from p-wall/dev-host-override
support remote use
2 parents 9a96889 + b89309f commit 7df191f

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

k

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# KAIL_PATH - path to kail executable, defaults to "kail"
66
# KUBESEAL_PATH - path to kubeseal executable, defaults to "kubeseal"
77
# YQ_PATH - path to yq executable, defaults to "yq"
8+
# K_LISTEN_ON_ALL_INTERFACES - if set to "true", listen on 0.0.0.0 for local forwards
89

910
require "date" # to protect against YAML bug present in certain psych versions: https://github.com/ruby/psych/pull/695
1011
require "yaml"
@@ -605,12 +606,19 @@ def kibana
605606
# kibana.k8s.elastic.co/name=mynewsdesk-funnel
606607

607608
require "socket"
609+
if ENV["K_LISTEN_ON_ALL_INTERFACES"] == "true"
610+
kubectl_address_flag = " --address 0.0.0.0"
611+
hostname = Socket.gethostname
612+
else
613+
kubectl_address_flag = ""
614+
hostname = "localhost"
615+
end
608616

609617
5601.upto(5700) do |port|
610618
# If we can connect to the port another kibana is already active and we skip to the next port
611619
next if TCPSocket.new("127.0.0.1", port).close.nil? rescue false # rubocop:disable Style/RescueModifier
612620

613-
puts "Making kibana accessible at: http://localhost:#{port}/app/monitoring#/overview"
621+
puts "Making kibana accessible at: http://#{hostname}:#{port}/app/monitoring#/overview"
614622
if system "which pbcopy > /dev/null 2>&1"
615623
puts "Storing kibana password in clipboard..."
616624
puts ""
@@ -623,7 +631,7 @@ def kibana
623631
puts "Login with username 'elastic' and paste the password '#{password}'"
624632
end
625633
puts ""
626-
kubectl "port-forward service/#{application}-kb-http #{port}:5601"
634+
kubectl "port-forward service/#{application}-kb-http #{port}:5601#{kubectl_address_flag}"
627635
end
628636
end
629637

k_pg_proxy

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ PROXY_PORT = 10_000
1111
THREADS = 10
1212
CONTEXT = ARGV.first || `kubectl config current-context`.strip
1313

14+
if ENV["K_LISTEN_ON_ALL_INTERFACES"] == "true"
15+
BIND_HOST = "0.0.0.0"
16+
KUBECTL_ADDRESS_FLAG = " --address 0.0.0.0"
17+
HOSTNAME = Socket.gethostname
18+
else
19+
BIND_HOST = "127.0.0.1"
20+
KUBECTL_ADDRESS_FLAG = ""
21+
HOSTNAME = "localhost"
22+
end
23+
1424
def gray(string)
1525
$stdout.tty? ? "\e[0;90;49m#{string}\e[0m" : string
1626
end
@@ -240,7 +250,7 @@ def handle_connection(client_socket, connection_number)
240250

241251
port_forward_port = PROXY_PORT + connection_number
242252
port_forward_pid = spawn(
243-
"kubectl --context #{CONTEXT} port-forward #{primary_pod} #{port_forward_port}:5432",
253+
"kubectl --context #{CONTEXT} port-forward #{primary_pod} #{port_forward_port}:5432#{KUBECTL_ADDRESS_FLAG}",
244254
err: File::NULL,
245255
)
246256
Process.detach(port_forward_pid)
@@ -294,12 +304,12 @@ ensure
294304
pg_socket&.close
295305
end
296306

297-
puts "Listening for Postgres connections on localhost:#{PROXY_PORT}"
307+
puts "Listening for Postgres connections on #{HOSTNAME}:#{PROXY_PORT}"
298308
puts "Just pass the name of the kubernetes database and leave the rest to me!"
299309
puts ""
300310
puts "EXAMPLE:"
301-
puts "psql -h localhost -p #{PROXY_PORT} -d mynewsdesk-staging"
302-
server = TCPServer.new(PROXY_PORT)
311+
puts "psql -h #{HOSTNAME} -p #{PROXY_PORT} -d mynewsdesk-staging"
312+
server = TCPServer.new(BIND_HOST, PROXY_PORT)
303313
connection_number = 0
304314

305315
loop do

0 commit comments

Comments
 (0)