Skip to content

Commit 479d42c

Browse files
authored
feat(dev): support configurable ports and hosts for multi-workspace and cloudtop (#983)
* feat(dev): support configurable ports and hosts for multi-workspace and cloudtop Support configuring ports and hosts via environment variables for KHI development servers and test runners. - Makefile: allow overriding WEB_HOST, WEB_PORT, BACKEND_HOST, BACKEND_PORT, STORYBOOK_HOST, STORYBOOK_PORT, and KARMA_PORT. Add --allowed-hosts flag automatically when WEB_HOST is 0.0.0.0. - Angular proxy: forward to BACKEND_HOST and BACKEND_PORT, normalizing 0.0.0.0 to 127.0.0.1 for HTTP proxy target. - Karma: configure port from KARMA_PORT, bind to 127.0.0.1, and set --remote-debugging-port=0 to avoid debugging port collisions in headless Chrome. * fix(dev): update karma beginsPattern regex to match 127.0.0.1 in tasks.json
1 parent 7d363d0 commit 479d42c

6 files changed

Lines changed: 31 additions & 13 deletions

File tree

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@
5050
"name": "Launch Karma (Chrome)",
5151
"type": "chrome",
5252
"request": "launch",
53-
"url": "http://localhost:9876",
53+
"url": "http://127.0.0.1:9876",
5454
"webRoot": "${workspaceFolder}/web",
5555
"preLaunchTask": "Start Karma",
5656
"sourceMaps": true,
5757
"sourceMapPathOverrides": {
58-
"http://localhost:9876/base/*": "${webRoot}/*"
58+
"http://127.0.0.1:9876/base/*": "${webRoot}/*"
5959
}
6060
}
6161
]

.vscode/tasks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"background": {
6868
"activeOnStart": true,
6969
"beginsPattern": {
70-
"regexp": ".*server started at http://localhost:9876.*"
70+
"regexp": ".*server started at http://(localhost|127\\.0\\.0\\.1):[0-9]+.*"
7171
},
7272
"endsPattern": {
7373
"regexp": ".*Executed .* FAILURE.*|.*Executed .* SUCCESS.*"

scripts/make/build.mk

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@
22
# This file contains make tasks for building.
33

44

5+
WEB_HOST ?= localhost
6+
WEB_PORT ?= 4200
7+
WEB_ALLOWED_HOSTS_FLAG ?= $(if $(filter 0.0.0.0,$(WEB_HOST)),--allowed-hosts,)
8+
STORYBOOK_HOST ?= localhost
9+
STORYBOOK_PORT ?= 6006
10+
KARMA_PORT ?= 9876
11+
BACKEND_PORT ?= $(or $(PORT),8080)
12+
BACKEND_HOST ?= $(if $(filter 0.0.0.0,$(HOST)),127.0.0.1,$(or $(HOST),127.0.0.1))
13+
514
.PHONY: watch-web
615
watch-web: $(GENERATE_FRONTEND_DUMMY) ## Run frontend development server
7-
cd web && npx ng serve -c dev
16+
cd web && BACKEND_PORT=$(BACKEND_PORT) BACKEND_HOST=$(BACKEND_HOST) npx ng serve -c dev --host $(WEB_HOST) --port $(WEB_PORT) $(WEB_ALLOWED_HOSTS_FLAG)
817

918
$(FRONTEND_ARTIFACT_FILES_DUMMY): $(GENERATE_FRONTEND_DUMMY) $(FRONTEND_SOURCE_FILES) $(FRONTEND_GENERATED_SRCS)## Build frontend for production
1019
cd web && npx ng build --output-path ../pkg/server/dist -c prod
@@ -15,15 +24,15 @@ build-web: $(FRONTEND_ARTIFACT_FILES_DUMMY) ## Build frontend for production
1524

1625
.PHONY: watch-storybook
1726
watch-storybook: $(GENERATE_FRONTEND_DUMMY) ## Run storybook development server
18-
cd web && npm run storybook
27+
cd web && npm run storybook -- --host $(STORYBOOK_HOST) --port $(STORYBOOK_PORT)
1928

2029
.PHONY: build-storybook
2130
build-storybook: $(GENERATE_FRONTEND_DUMMY) ## Build storybook
2231
cd web && npm run build-storybook
2332

2433
.PHONY: watch-karma
2534
watch-karma: $(GENERATE_FRONTEND_DUMMY) ## Run karma test server
26-
cd web && npm run test
35+
cd web && KARMA_PORT=$(KARMA_PORT) npm run test
2736

2837
khi: $(GENERATE_BACKEND_DUMMY) $(FRONTEND_ARTIFACT_FILES_DUMMY) $(BACKEND_SRCS)
2938
CGO_ENABLED=0 go build -ldflags="-s -w -X github.com/GoogleCloudPlatform/khi/pkg/common/constants.VERSION=$(shell cat ./VERSION)" -o ./khi ./cmd/kubernetes-history-inspector/...

scripts/make/testing.mk

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
# testing.mk
22
# This file contains make tasks related to testing.
33

4+
KARMA_PORT ?= 9876
5+
46
.PHONY: test-web
57
test-web: $(GENERATE_FRONTEND_DUMMY) $(FRONTEND_SOURCE_FILES)## Run frontend tests
6-
cd web && npx ng test --browsers ChromeHeadlessNoSandbox --watch=false
8+
cd web && KARMA_PORT=$(KARMA_PORT) npx ng test --browsers ChromeHeadlessNoSandbox --watch=false
79

810
.PHONY: watch-test-web
911
watch-test-web: $(GENERATE_FRONTEND_DUMMY) ## Run frontend tests in watch mode
10-
cd web && npx ng test
12+
cd web && KARMA_PORT=$(KARMA_PORT) npx ng test
1113

1214
.PHONY: test-go
1315
test-go: $(GENERATE_BACKEND_DUMMY) $(BACKEND_TEST_SRCS) $(FRONTEND_ARTIFACT_FILES_DUMMY) ## Run backend tests
1416
go test ./...
1517

1618
.PHONY: coverage-web
1719
coverage-web: $(GENERATE_FRONTEND_DUMMY) $(FRONTEND_SOURCE_FILES)## Run frontend tests and generate coverage report
18-
cd web && npx ng test --code-coverage --browsers ChromeHeadlessNoSandbox --watch false --progress false
20+
cd web && KARMA_PORT=$(KARMA_PORT) npx ng test --code-coverage --browsers ChromeHeadlessNoSandbox --watch false --progress false
1921

2022
.PHONY: coverage-go
2123
coverage-go: $(GENERATE_BACKEND_DUMMY) $(BACKEND_TEST_SRCS) $(FRONTEND_ARTIFACT_FILES_DUMMY)## Run backend tests and generate coverage report

web/karma.conf.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ module.exports = function (config) {
5353
]
5454
},
5555
reporters: ['progress', 'kjhtml'],
56-
port: 9876,
56+
port: parseInt(process.env.KARMA_PORT, 10) || 9876,
57+
hostname: '127.0.0.1',
58+
listenAddress: '127.0.0.1',
5759
colors: true,
5860
logLevel: config.LOG_INFO,
5961
autoWatch: true,
@@ -71,7 +73,8 @@ module.exports = function (config) {
7173
'--no-sandbox',
7274
'--enable-unsafe-swiftshader',
7375
'--enable-webgl',
74-
'--disable-gpu'
76+
'--disable-gpu',
77+
'--remote-debugging-port=0'
7578
]
7679
}
7780
},

web/proxy.conf.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818
* Angular Proxy Configuration
1919
*
2020
* This file defines the proxy configuration for the Angular development server.
21-
* During development, requests to the /api/ path are forwarded to localhost:8080.
21+
* During development, requests to the /api/ path are forwarded to the backend server.
2222
*/
2323

24+
const backendPort = process.env.BACKEND_PORT || process.env.PORT || "8080";
25+
const rawHost = process.env.BACKEND_HOST || process.env.HOST || "127.0.0.1";
26+
const backendHost = rawHost === "0.0.0.0" ? "127.0.0.1" : rawHost;
27+
2428
export default {
2529
"/api": {
26-
target: "http://127.0.0.1:8080",
30+
target: `http://${backendHost}:${backendPort}`,
2731
secure: false,
2832
changeOrigin: true,
2933
logLevel: "debug",

0 commit comments

Comments
 (0)