Skip to content

Commit b89309f

Browse files
committed
support remote use
listen on all interfaces if K_LISTEN_ON_ALL_INTERFACES is set to "true" to allow remote access, for example for kibana
1 parent f97dc6c commit b89309f

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"
@@ -594,12 +595,19 @@ def kibana
594595
# kibana.k8s.elastic.co/name=mynewsdesk-funnel
595596

596597
require "socket"
598+
if ENV["K_LISTEN_ON_ALL_INTERFACES"] == "true"
599+
kubectl_address_flag = " --address 0.0.0.0"
600+
hostname = Socket.gethostname
601+
else
602+
kubectl_address_flag = ""
603+
hostname = "localhost"
604+
end
597605

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

602-
puts "Making kibana accessible at: http://localhost:#{port}/app/monitoring#/overview"
610+
puts "Making kibana accessible at: http://#{hostname}:#{port}/app/monitoring#/overview"
603611
if system "which pbcopy > /dev/null 2>&1"
604612
puts "Storing kibana password in clipboard..."
605613
puts ""
@@ -612,7 +620,7 @@ def kibana
612620
puts "Login with username 'elastic' and paste the password '#{password}'"
613621
end
614622
puts ""
615-
kubectl "port-forward service/#{application}-kb-http #{port}:5601"
623+
kubectl "port-forward service/#{application}-kb-http #{port}:5601#{kubectl_address_flag}"
616624
end
617625
end
618626

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)