Skip to content

Commit 9e4e7e5

Browse files
authored
Merge pull request #135 from Kong/api-summit-24
WIP: Adding ephemeral test env for routes team. Summit demo
2 parents 8bf59f2 + ac64cac commit 9e4e7e5

14 files changed

+357
-32
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ kong-quickstart.*
66
kong.env
77
kong.yaml
88
*.pid
9+
tls*/
10+
*.env
11+
*.log

base.mk

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ PORTS_FILE := $(BASE_MAKEFILE_DIR)PORTS.env
99

1010
echo_fail = printf "\e[31m✘ \033\e[0m$(1)\n"
1111
echo_pass = printf "\e[32m✔ \033\e[0m$(1)\n"
12+
echo_info = printf "\e[33mℹ \033\e[0m$(1)\n"
13+
echo_bull = printf "\e[34m• \033\e[0m$1\n"
1214

1315
check-dependency = $(if $(shell command -v $(1)),$(call echo_pass,found $(1)),$(call echo_fail,$(1) not installed);exit 1)
1416

flight-data/routes/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
routes
2+
3+
tls.*

flight-data/routes/Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ RUN CGO_ENABLED=0 go build -o /go/bin/app
1212
FROM gcr.io/distroless/static-debian11
1313

1414
COPY --from=build /go/bin/app /
15-
EXPOSE 8080
16-
CMD ["/app"]
15+
EXPOSE 8081
16+
CMD ["/app", "8081"]

flight-data/routes/Makefile

+114-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,124 @@
11
include ../../base.mk
22

3+
routes_mkfile_dir := $(CURRDIR)
4+
35
check-dependencies:
46
@$(call check-dependency,go)
7+
@$(call check-dependency,jq)
8+
@$(call check-dependency,deck)
9+
@$(call check-dependency,cloudflared)
10+
@$(call check-dependency,docker)
511

612
test: check-dependencies
7-
go test -v ./...
13+
@go test -v ./...
814

915
build: check-dependencies
10-
go build .
16+
@go build .
17+
18+
build-docker:
19+
@docker build -t kong-air-routes-svc:dev .
1120

1221
run: check-dependencies build
13-
./routes ${KONG_AIR_ROUTES_PORT}
22+
@./routes ${KONG_AIR_ROUTES_PORT}
23+
24+
docker: build-docker
25+
@docker run -d --name kong-air-routes-svc -p ${KONG_AIR_ROUTES_PORT}:${KONG_AIR_ROUTES_PORT} kong-air-routes-svc:dev
26+
27+
kill-docker:
28+
-@docker stop kong-air-routes-svc
29+
-@docker rm kong-air-routes-svc
30+
@if [ $$? -ne 0 ]; then $(call echo_fail,Failed to kill the docker containers); exit 1; else $(call echo_pass,Killed the docker container); fi
31+
32+
###############################################################################
33+
# DEV Environment setup
34+
TLS_KEY := $(CURDIR)/dev/kong/konnect/tf/tls.key
35+
TLS_CRT := $(CURDIR)/dev/kong/konnect/tf/tls.crt
36+
TF_OUT_LOG := $(CURDIR)/dev/kong/konnect/tf/.terraform.out.log
37+
TF_ERR_LOG := $(CURDIR)/dev/kong/konnect/tf/.terraform.err.log
38+
39+
$(TLS_KEY) $(TLS_CRT):
40+
@openssl req -new -x509 -nodes -newkey rsa:2048 -subj "/CN=kongdp/C=US" -keyout $(TLS_KEY) -out $(TLS_CRT) > /dev/null 2>&1
41+
42+
konnect-dev-cert: $(TLS_KEY) $(TLS_CRT)
43+
@if [ $$? -ne 0 ]; then $(call echo_fail,Failed to create Konnect certificate); exit 1; else $(call echo_pass,Created Konnect certificate); fi
44+
45+
kill-konnect-dev-cert:
46+
@rm -f $(TLS_KEY) $(TLS_CRT)
47+
@if [ $$? -ne 0 ]; then $(call echo_fail,Failed to remove Konnect certificate); exit 1; else $(call echo_pass,Removed Konnect certificate); fi
48+
49+
konnect-dev-tf-init:
50+
@cd $(CURDIR)/dev/kong/konnect/tf && terraform init > $(TF_OUT_LOG) 2> $(TF_ERR_LOG)
51+
@if [ $$? -ne 0 ]; then $(call echo_fail,terraform failed to initialize); exit 1; else $(call echo_pass,terraform initialized); fi
52+
53+
konnect-dev-tf-apply:
54+
@cd $(CURDIR)/dev/kong/konnect/tf && KONNECT_TOKEN=`cat ~/.konnect/routes-dev.pat` terraform apply -auto-approve > $(TF_OUT_LOG) 2> $(TF_ERR_LOG) && \
55+
echo "KONG_GATEWAY_URL=$$(terraform output -json | jq -r .gateway_endpoint.value)" > $(CURDIR)/dev/kong/konnect/.kong-air-routes-svc.env && \
56+
echo "KONNECT_CONTROL_PLANE_NAME=$$(terraform output -json | jq -r .control_plane_name.value)" >> $(CURDIR)/dev/kong/konnect/.kong-air-routes-svc.env
57+
@if [ $$? -ne 0 ]; then $(call echo_fail,Failed to apply terraform manifests); exit 1; else $(call echo_pass,Konnect resources available); fi
58+
59+
konnect-dev-tf-destroy:
60+
@cd $(CURDIR)/dev/kong/konnect/tf && KONNECT_TOKEN=`cat ~/.konnect/routes-dev.pat` terraform destroy -auto-approve > $(TF_OUT_LOG) 2> $(TF_ERR_LOG)
61+
@if [ $$? -ne 0 ]; then $(call echo_fail,Failed to destroy terraform resources); exit 1; else $(call echo_pass,Destroyed Konnect resources); fi
62+
63+
konnect-dev-env: konnect-dev-cert konnect-dev-tf-init konnect-dev-tf-apply
64+
65+
kill-konnect-dev-env: konnect-dev-tf-destroy kill-konnect-dev-cert
66+
67+
cloudflare-tunnel:
68+
@cloudflared tunnel run routes > ~/Library/Logs/com.cloudflare.cloudflared.out.log 2> ~/Library/Logs/com.cloudflare.cloudflared.err.log & \
69+
echo $$! > $(CURDIR)/.cloudflared.pid
70+
@if [ $$? -ne 0 ]; then $(call echo_fail,Failed to run cloudflare tunnel); exit 1; \
71+
else $(call echo_pass,cloudflare tunnel running at PID $$(cat $(CURDIR)/.cloudflared.pid)); fi
72+
73+
kill-cloudflare-tunnel:
74+
@if [ -f $(CURDIR)/.cloudflared.pid ]; then \
75+
kill $$(cat $(CURDIR)/.cloudflared.pid) || true; \
76+
rm $(CURDIR)/.cloudflared.pid; \
77+
$(call echo_pass,Killed the cloudflare tunnel); \
78+
fi
79+
80+
routes-svc-kong-config:
81+
@deck file openapi2kong -s openapi.yaml | \
82+
deck file patch --output-file $(CURDIR)/dev/kong/.kong-routes-svc.yaml $(CURDIR)/dev/kong/patches.yaml
83+
@if [ $$? -ne 0 ]; then $(call echo_fail,Failed to create Kong configuration); exit 1; \
84+
else $(call echo_pass,Wrote Kong configuration to dev/kong/.kong-routes-svc.yaml); fi
85+
86+
kill-route-svc-kong-config:
87+
@rm -f $(CURDIR)/dev/kong/.kong-routes-svc.yaml
88+
89+
sync-routes-svc-kong-config:
90+
@source $(CURDIR)/dev/kong/konnect/.kong-air-routes-svc.env && \
91+
deck gateway sync \
92+
--konnect-token-file ~/.konnect/routes-dev.pat \
93+
--konnect-control-plane-name $$KONNECT_CONTROL_PLANE_NAME \
94+
$(CURDIR)/dev/kong/.kong-routes-svc.yaml
95+
@if [ $$? -ne 0 ]; then $(call echo_fail,Failed to sync Kong configuration); exit 1; \
96+
else $(call echo_pass,Synced Kong configuration); fi
97+
98+
.PHONY: dev-env
99+
dev-env: check-dependencies
100+
@$(call echo_bull,Creating a kong-air routes team dev environment)
101+
@$(MAKE) docker
102+
@$(MAKE) konnect-dev-env
103+
@$(MAKE) cloudflare-tunnel
104+
@$(MAKE) routes-svc-kong-config
105+
@$(MAKE) sync-routes-svc-kong-config
106+
@echo
107+
@echo "Run the following command to load the dev environment variables"
108+
@echo "source dev/kong/konnect/.kong-air-routes-svc.env"
109+
@echo
110+
@echo "The Kong Gateway proxy endpoint is available in the variable KONG_GATEWAY_URL"
111+
@echo "Route a request to Kong Gateway with:"
112+
@echo " curl `source $(CURDIR)/dev/kong/konnect/.kong-air-routes-svc.env && echo $$KONG_GATEWAY_URL`"
113+
@echo
114+
@echo "You can also route directly to your service from the Cloudflare public endpoint:"
115+
@echo " curl https://routes.kong-air.dev/routes"
116+
117+
.PHONY: kill-dev-env
118+
kill-dev-env: check-dependencies
119+
@$(MAKE) kill-route-svc-kong-config
120+
@$(MAKE) kill-cloudflare-tunnel
121+
@$(MAKE) kill-konnect-dev-env
122+
@$(MAKE) kill-docker
123+
@echo "Killed the dev environment"
124+
###############################################################################

flight-data/routes/api/routes.go

+40-27
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,63 @@ package api
66
import (
77
"github.com/Kong/KongAir/flight-data/routes/api/models"
88
"github.com/labstack/echo/v4"
9-
"net/http"
9+
"net/http"
1010
)
1111

1212
type RouteService struct {
13-
Routes []models.Route
13+
Routes []models.Route
14+
PrivateRoutes []models.Route
1415
}
1516

1617
func NewRouteService() *RouteService {
1718
rv := RouteService{}
1819
rv.Routes = []models.Route{
19-
{Id: "LHR-JFK", Origin: "LHR", Destination: "JFK", AvgDuration: 470},
20-
{Id: "LHR-SFO", Origin: "LHR", Destination: "SFO", AvgDuration: 660},
21-
{Id: "LHR-DXB", Origin: "LHR", Destination: "DXB", AvgDuration: 420},
22-
{Id: "LHR-HKG", Origin: "LHR", Destination: "HKG", AvgDuration: 745},
23-
{Id: "LHR-BOM", Origin: "LHR", Destination: "BOM", AvgDuration: 540},
24-
{Id: "LHR-HND", Origin: "LHR", Destination: "HND", AvgDuration: 830},
25-
{Id: "LHR-CPT", Origin: "LHR", Destination: "CPT", AvgDuration: 700},
26-
{Id: "LHR-SYD", Origin: "LHR", Destination: "SYD", AvgDuration: 1320},
27-
{Id: "LHR-SIN", Origin: "LHR", Destination: "SIN", AvgDuration: 800},
28-
{Id: "LHR-LAX", Origin: "LHR", Destination: "LAX", AvgDuration: 675},
20+
{Id: "LHR-JFK", Origin: "LHR", Destination: "JFK", AvgDuration: 470},
21+
{Id: "LHR-SFO", Origin: "LHR", Destination: "SFO", AvgDuration: 660},
22+
{Id: "LHR-DXB", Origin: "LHR", Destination: "DXB", AvgDuration: 420},
23+
{Id: "LHR-HKG", Origin: "LHR", Destination: "HKG", AvgDuration: 745},
24+
{Id: "LHR-BOM", Origin: "LHR", Destination: "BOM", AvgDuration: 540},
25+
{Id: "LHR-HND", Origin: "LHR", Destination: "HND", AvgDuration: 830},
26+
{Id: "LHR-CPT", Origin: "LHR", Destination: "CPT", AvgDuration: 700},
27+
{Id: "LHR-SYD", Origin: "LHR", Destination: "SYD", AvgDuration: 1320},
28+
{Id: "LHR-SIN", Origin: "LHR", Destination: "SIN", AvgDuration: 800},
29+
{Id: "LHR-LAX", Origin: "LHR", Destination: "LAX", AvgDuration: 675},
30+
}
31+
rv.PrivateRoutes = []models.Route{
32+
{Id: "VIP-LHR-JFK", Origin: "LHR", Destination: "VIP-JFK", AvgDuration: 430},
33+
{Id: "VIP-LHR-SFO", Origin: "LHR", Destination: "VIP-SFO", AvgDuration: 620},
34+
{Id: "VIP-LHR-DXB", Origin: "LHR", Destination: "VIP-DXB", AvgDuration: 390},
35+
{Id: "VIP-LHR-HKG", Origin: "LHR", Destination: "VIP-HKG", AvgDuration: 645},
2936
}
3037
return &rv
3138
}
3239

3340
func (s *RouteService) GetHealth(ctx echo.Context) error {
34-
return ctx.JSON(http.StatusOK, map[string]string{"status": "OK"})
41+
return ctx.JSON(http.StatusOK, map[string]string{"status": "OK"})
3542
}
3643

3744
func (s *RouteService) GetRoutes(ctx echo.Context, params models.GetRoutesParams) error {
38-
err := ctx.JSON(200, s.Routes)
39-
if err != nil {
40-
return err
45+
if ctx.Request().Header.Get("x-vip") == "true" {
46+
allRoutes := append(s.Routes, s.PrivateRoutes...)
47+
return ctx.JSON(200, allRoutes)
4148
}
42-
return nil
49+
50+
return ctx.JSON(200, s.Routes)
4351
}
52+
4453
func (s *RouteService) GetRoute(ctx echo.Context, id string) error {
45-
for _, route := range s.Routes {
46-
if route.Id == id {
47-
err := ctx.JSON(200, route)
48-
if err != nil {
49-
return err
50-
}
51-
return nil
52-
}
53-
}
54-
return ctx.JSON(404, nil)
54+
routes := s.Routes
55+
if ctx.Request().Header.Get("x-vip") == "true" {
56+
routes = append(routes, s.PrivateRoutes...)
57+
}
58+
for _, route := range routes {
59+
if route.Id == id {
60+
err := ctx.JSON(200, route)
61+
if err != nil {
62+
return err
63+
}
64+
return nil
65+
}
66+
}
67+
return ctx.JSON(404, nil)
5568
}

flight-data/routes/dev/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.kong-routes-svc.yaml

flight-data/routes/dev/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
# Steps
3+
4+
## Initialize Konnect Resources
5+
1. `terraform init`
6+
1. `openssl req -new -x509 -nodes -newkey rsa:2048 -subj "/CN=kongdp/C=US" -keyout ./tls.key -out ./tls.crt`
7+
1. `KONNECT_TOKEN=$(cat ~/.konnect/routes-dev.pat ) terraform apply -auto-approve`
8+
9+
## Run the application
10+
1. `make run`
11+
12+
## Run the cloudflared tunnel
13+
14+
Requires some initial setup including an authenticated cloudflared installation and local configuration. See
15+
https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/get-started/create-local-tunnel/
16+
17+
1. cloudflared tunnel login
18+
1. cloudflared tunnel create routes
19+
1. cloudflared tunnel route dns 55f23fbd-9104-4b47-a14b-d068f9b980aa routes
20+
1. Setup a configuration file: `~/.cloudflared/config.yml`
21+
22+
```yaml
23+
url: http://localhost:8081
24+
tunnel: 55f23fbd-9104-4b47-a14b-d068f9b980aa
25+
credentials-file: $HOME/.cloudflared/55f23fbd-9104-4b47-a14b-d068f9b980aa.json
26+
```
27+
28+
## Run the tunnel
29+
1. `cloudflared tunnel run routes`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# Crash log files
9+
crash.log
10+
crash.*.log
11+
12+
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
13+
# password, private keys, and other secrets. These should not be part of version
14+
# control as they are data points which are potentially sensitive and subject
15+
# to change depending on the environment.
16+
*.tfvars
17+
*.tfvars.json
18+
19+
# Ignore override files as they are usually used to override resources locally and so
20+
# are not checked in
21+
override.tf
22+
override.tf.json
23+
*_override.tf
24+
*_override.tf.json
25+
26+
# Ignore transient lock info files created by terraform apply
27+
.terraform.tfstate.lock.info
28+
29+
# Include override files you do wish to add to version control using negated pattern
30+
# !example_override.tf
31+
32+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
33+
# example: *tfplan*
34+
35+
# Ignore CLI configuration files
36+
.terraformrc
37+
terraform.rc

flight-data/routes/dev/kong/konnect/tf/.terraform.lock.hcl

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
terraform {
2+
required_providers {
3+
konnect = {
4+
source = "kong/konnect"
5+
version = "0.6.0"
6+
}
7+
}
8+
}
9+
10+
provider "konnect" {
11+
server_url = "https://us.api.konghq.com"
12+
}

0 commit comments

Comments
 (0)