Skip to content

Commit

Permalink
Copy improved ngrok setup from kubechecks
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Morrison committed Jul 26, 2023
1 parent 0467714 commit 1ba1942
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 115 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ build/
.env

go.work
go.work.sum
go.work.sum
ngrok.url
14 changes: 9 additions & 5 deletions Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dotenv()
config.define_bool("enable_gitlab")
config.define_bool("enable_github")
config.define_bool("live_debug")
config.define_string("ngrok_fqdn")
cfg = config.parse()

allow_k8s_contexts([
Expand All @@ -30,8 +31,13 @@ k8s_resource(
)
k8s_context=k8s_context()

# /////////////////////////////////////////////////////////////////////////////
# N G R O K
# /////////////////////////////////////////////////////////////////////////////

# Load NGROK Tiltfile
load('./localdev/ngrok/Tiltfile', 'get_ngrok_url')
load('./localdev/ngrok/Tiltfile', 'deploy_ngrok', 'get_ngrok_url')
deploy_ngrok(cfg)

def checkEnvSet(key):
if not os.getenv(key):
Expand All @@ -43,7 +49,7 @@ def checkEnvSet(key):
checkEnvSet("TFC_ORGANIZATION")
checkEnvSet("TFC_TOKEN")

ngrok_url=get_ngrok_url()
ngrok_url=get_ngrok_url(cfg)
org=str(os.getenv('TFC_ORGANIZATION'))

tfcOutputs=local_terraform_resource(
Expand All @@ -58,7 +64,7 @@ tfcOutputs=local_terraform_resource(
'localdev/terraform/*.tf',
],
labels=["tfc"],
resource_deps=['wait-ngrok-url']
resource_deps=[]
)

if tfcOutputs:
Expand Down Expand Up @@ -90,7 +96,6 @@ if cfg.get('enable_gitlab'):
],
resource_deps=[
'tf-tfc',
'wait-ngrok-url',
],
labels=['gitlab']
)
Expand All @@ -117,7 +122,6 @@ if cfg.get('enable_github'):
],
resource_deps=[
'tf-tfc',
'wait-ngrok-url',
],
labels=['github']
)
Expand Down
74 changes: 32 additions & 42 deletions localdev/ngrok/Tiltfile
Original file line number Diff line number Diff line change
@@ -1,48 +1,38 @@
load('ext://secret', 'secret_from_dict')

# /////////////////////////////////////////////////////////////////////////////
# N G R O K
# /////////////////////////////////////////////////////////////////////////////

def get_ngrok_url():
if config.tilt_subcommand == 'down':
check_cmd='/usr/bin/curl -v http://localhost:4040/api/tunnels | jq --raw-output ".tunnels[0].public_url"'

def get_ngrok_url(cfg):
if cfg.get("ngrok_fqdn"):
return "https://"+cfg.get("ngrok_fqdn")

elif config.tilt_subcommand == 'down':
local('rm ngrok.url || true')
return 'http://xyz.ngrok.io'

else:
return str(local(
'curl -s http://localhost:4040/api/tunnels \
| jq --raw-output ".tunnels[0].public_url"'.format(
k8s_namespace,
k8s_context
),
)).rstrip('\n')

# Read Auth token for ngrok (assumes ngrok has been installed and configured)
ngrok_config_file = str(local('ngrok config check', quiet=True)).lstrip('Valid configuration file at ').rstrip('\n')
ngrok_config = read_yaml(ngrok_config_file)
k8s_yaml(
secret_from_dict("ngrok-config", inputs = {
'NGROK_AUTHTOKEN' : ngrok_config['authtoken'],
})
)
k8s_resource(
objects=['ngrok-config:secret'],
labels=["ngrok"],
new_name='ngrok-config',
resource_deps=['k8s:namespace']
)

# Deploy ngrok proxy
k8s_yaml(
kustomize('./')
)
k8s_resource(
'ngrok',
resource_deps=['ngrok-config', 'k8s:namespace'],
labels=["ngrok"],
port_forwards=4040,
)
k8s_resource(
'wait-ngrok-url',
resource_deps=['k8s:namespace', 'ngrok'],
labels=["ngrok"]
)
url=str(read_file('ngrok.url', "")).rstrip('\n')

return url

def deploy_ngrok(cfg):

hostnameArg = ""
if cfg.get("ngrok_fqdn"):
hostnameArg = " --hostname {}".format(cfg.get("ngrok_fqdn"))

local_resource(
"ngrok",
serve_cmd="ngrok http localhost:8080 {}".format(hostnameArg),
readiness_probe=probe(
initial_delay_secs=5,
period_secs=10,
exec=exec_action(["bash", "-c", 'localdev/ngrok/update-url.sh'])
),
labels=["ngrok"],
links=["http://localhost:4040"]
)

watch_file('./ngrok.url')
31 changes: 0 additions & 31 deletions localdev/ngrok/deploy.yaml

This file was deleted.

16 changes: 0 additions & 16 deletions localdev/ngrok/job.yaml

This file was deleted.

7 changes: 0 additions & 7 deletions localdev/ngrok/kustomization.yaml

This file was deleted.

13 changes: 0 additions & 13 deletions localdev/ngrok/service.yaml

This file was deleted.

11 changes: 11 additions & 0 deletions localdev/ngrok/update-url.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
set -eu

url=$(curl -s http://localhost:4040/api/tunnels | jq --raw-output ".tunnels[0].public_url")

if grep -q $url ngrok.url; then
echo "URL already set"
else
echo "Updating ngrok.url"
echo -n $url > ngrok.url
fi

0 comments on commit 1ba1942

Please sign in to comment.