Skip to content

Commit 6294a42

Browse files
Merge pull request #1143 from RakhithaRR/gateway-org
2 parents bc3bb16 + be00e17 commit 6294a42

20 files changed

Lines changed: 522 additions & 125 deletions

File tree

.github/workflows/operator-integration-test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ jobs:
292292
# Graceful shutdown timeout
293293
shutdown_timeout: 15s
294294
295+
# Unique identifier for the gateway instance
296+
gateway_id: "platform-gateway-id"
297+
295298
# Policy xDS Server configuration
296299
policy_server:
297300
# Enable or disable the policy xDS server

gateway/configs/config-template.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ api_port = 9090
99
xds_port = 18000
1010
# Graceful shutdown timeout
1111
shutdown_timeout = "15s"
12+
# Unique identifier for the gateway instance (used in persistent storage)
13+
# It is recommended to use a uuid_v7 for this to improve db efficiency.
14+
gateway_id = "platform-gateway-id"
1215

1316
[controller.admin_server]
1417
# Dedicated admin/debug HTTP server for config dump and xDS sync endpoints

gateway/configs/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ gateway_host = "*"
1313
[router.access_logs]
1414
enabled = true
1515

16+
[controller.server]
17+
gateway_id = "platform-gateway-id"
18+
1619
[controller.auth.basic]
1720
enabled = true
1821

gateway/gateway-controller/cmd/controller/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func toBackendConfig(cfg *config.Config) storage.BackendConfig {
6161
ConnMaxIdleTime: pg.ConnMaxIdleTime,
6262
ApplicationName: pg.ApplicationName,
6363
},
64+
GatewayID: cfg.Controller.Server.GatewayID,
6465
}
6566
}
6667

gateway/gateway-controller/pkg/config/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ type ServerConfig struct {
176176
APIPort int `koanf:"api_port"`
177177
XDSPort int `koanf:"xds_port"`
178178
ShutdownTimeout time.Duration `koanf:"shutdown_timeout"`
179+
GatewayID string `koanf:"gateway_id"`
179180
}
180181

181182
// AdminServerConfig holds controller admin HTTP server configuration.
@@ -457,6 +458,7 @@ func defaultConfig() *Config {
457458
APIPort: 9090,
458459
XDSPort: 18000,
459460
ShutdownTimeout: 15 * time.Second,
461+
GatewayID: constants.PlatformGatewayId,
460462
},
461463
AdminServer: AdminServerConfig{
462464
Enabled: true,
@@ -810,6 +812,10 @@ func (c *Config) Validate() error {
810812
return fmt.Errorf("server.xds_port must be between 1 and 65535, got: %d", c.Controller.Server.XDSPort)
811813
}
812814

815+
if strings.TrimSpace(c.Controller.Server.GatewayID) == "" {
816+
return fmt.Errorf("server.gateway_id is required and cannot be empty")
817+
}
818+
813819
if c.Controller.AdminServer.Enabled {
814820
if c.Controller.AdminServer.Port < 1 || c.Controller.AdminServer.Port > 65535 {
815821
return fmt.Errorf("admin_server.port must be between 1 and 65535, got: %d", c.Controller.AdminServer.Port)

gateway/gateway-controller/pkg/config/config_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ func validConfig() *Config {
3333
return &Config{
3434
Controller: Controller{
3535
Server: ServerConfig{
36-
APIPort: 8080,
37-
XDSPort: 18000,
36+
APIPort: 8080,
37+
XDSPort: 18000,
38+
GatewayID: constants.PlatformGatewayId,
3839
},
3940
Storage: StorageConfig{
4041
Type: "memory",

gateway/gateway-controller/pkg/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package constants
2020

2121
const (
22+
PlatformGatewayId = "platform-gateway-id"
2223
// XDS/Envoy Constants
2324
TransportSocketPrefix = "ts"
2425
LoadBalancerIDKey = "lb_id"

gateway/gateway-controller/pkg/storage/factory.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type BackendConfig struct {
3131
Type string
3232
SQLitePath string
3333
Postgres PostgresConnectionConfig
34+
GatewayID string
3435
}
3536

3637
// NewStorage creates the configured persistent storage backend.
@@ -45,7 +46,7 @@ func NewStorage(cfg BackendConfig, logger *slog.Logger) (Storage, error) {
4546
return nil, err
4647
}
4748

48-
store := newSQLStore(backend.db, backend.logger, "sqlite")
49+
store := newSQLStore(backend.db, backend.logger, "sqlite", cfg.GatewayID)
4950
store.rebindQuery = func(query string) string { return query }
5051
store.isConfigUniqueViolation = isUniqueConstraintError
5152
store.isCertificateUniqueViolation = isCertificateUniqueConstraintError
@@ -59,7 +60,7 @@ func NewStorage(cfg BackendConfig, logger *slog.Logger) (Storage, error) {
5960
return nil, err
6061
}
6162

62-
store := newSQLStore(backend.db, backend.logger, "postgres")
63+
store := newSQLStore(backend.db, backend.logger, "postgres", cfg.GatewayID)
6364
store.rebindQuery = func(query string) string { return sqlx.Rebind(sqlx.DOLLAR, query) }
6465
store.isConfigUniqueViolation = isPostgresUniqueConstraintError
6566
store.isCertificateUniqueViolation = isPostgresCertificateUniqueConstraintError

gateway/gateway-controller/pkg/storage/gateway-controller-db.postgres.sql

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,48 @@
11
-- PostgreSQL Schema for Gateway-Controller API Configurations
2-
-- Version: 6
2+
-- Version: 8
33

44
-- Main table for deployments
55
CREATE TABLE IF NOT EXISTS deployments (
66
id TEXT PRIMARY KEY,
7+
gateway_id TEXT NOT NULL DEFAULT 'platform-gateway-id',
78
display_name TEXT NOT NULL,
89
version TEXT NOT NULL,
910
context TEXT NOT NULL,
1011
kind TEXT NOT NULL,
11-
handle TEXT NOT NULL UNIQUE,
12+
handle TEXT NOT NULL,
1213
status TEXT NOT NULL CHECK(status IN ('pending', 'deployed', 'failed')),
1314
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
1415
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
1516
deployed_at TIMESTAMPTZ,
1617
deployed_version BIGINT NOT NULL DEFAULT 0,
17-
UNIQUE(display_name, version)
18+
UNIQUE(display_name, version, gateway_id),
19+
UNIQUE(handle, gateway_id)
1820
);
1921

20-
CREATE INDEX IF NOT EXISTS idx_name_version ON deployments(display_name, version);
2122
CREATE INDEX IF NOT EXISTS idx_status ON deployments(status);
2223
CREATE INDEX IF NOT EXISTS idx_context ON deployments(context);
2324
CREATE INDEX IF NOT EXISTS idx_kind ON deployments(kind);
25+
CREATE INDEX IF NOT EXISTS idx_deployments_gateway_id ON deployments(gateway_id);
2426

2527
-- Table for custom TLS certificates
2628
CREATE TABLE IF NOT EXISTS certificates (
2729
id TEXT PRIMARY KEY,
28-
name TEXT NOT NULL UNIQUE,
30+
gateway_id TEXT NOT NULL DEFAULT 'platform-gateway-id',
31+
name TEXT NOT NULL,
2932
certificate BYTEA NOT NULL,
3033
subject TEXT NOT NULL,
3134
issuer TEXT NOT NULL,
3235
not_before TIMESTAMPTZ NOT NULL,
3336
not_after TIMESTAMPTZ NOT NULL,
3437
cert_count INTEGER NOT NULL DEFAULT 1,
3538
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
36-
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
39+
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
40+
UNIQUE(name, gateway_id)
3741
);
3842

3943
CREATE INDEX IF NOT EXISTS idx_cert_name ON certificates(name);
4044
CREATE INDEX IF NOT EXISTS idx_cert_expiry ON certificates(not_after);
45+
CREATE INDEX IF NOT EXISTS idx_certificates_gateway_id ON certificates(gateway_id);
4146

4247
-- Table for deployment-specific configurations
4348
CREATE TABLE IF NOT EXISTS deployment_configs (
@@ -50,17 +55,21 @@ CREATE TABLE IF NOT EXISTS deployment_configs (
5055
-- LLM Provider Templates table
5156
CREATE TABLE IF NOT EXISTS llm_provider_templates (
5257
id TEXT PRIMARY KEY,
53-
handle TEXT NOT NULL UNIQUE,
58+
gateway_id TEXT NOT NULL DEFAULT 'platform-gateway-id',
59+
handle TEXT NOT NULL,
5460
configuration TEXT NOT NULL,
5561
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
56-
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
62+
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
63+
UNIQUE(handle, gateway_id)
5764
);
5865

5966
CREATE INDEX IF NOT EXISTS idx_template_handle ON llm_provider_templates(handle);
67+
CREATE INDEX IF NOT EXISTS idx_llm_provider_templates_gateway_id ON llm_provider_templates(gateway_id);
6068

6169
-- Table for API keys
6270
CREATE TABLE IF NOT EXISTS api_keys (
6371
id TEXT PRIMARY KEY,
72+
gateway_id TEXT NOT NULL DEFAULT 'platform-gateway-id',
6473
name TEXT NOT NULL,
6574
api_key TEXT NOT NULL UNIQUE,
6675
masked_api_key TEXT NOT NULL,
@@ -78,7 +87,7 @@ CREATE TABLE IF NOT EXISTS api_keys (
7887
index_key TEXT NULL,
7988
display_name TEXT NOT NULL DEFAULT '',
8089
FOREIGN KEY (apiId) REFERENCES deployments(id) ON DELETE CASCADE,
81-
UNIQUE (apiId, name)
90+
UNIQUE (apiId, name, gateway_id)
8291
);
8392

8493
CREATE INDEX IF NOT EXISTS idx_api_key ON api_keys(api_key);
@@ -89,10 +98,83 @@ CREATE INDEX IF NOT EXISTS idx_created_by ON api_keys(created_by);
8998
CREATE INDEX IF NOT EXISTS idx_api_key_source ON api_keys(source);
9099
CREATE INDEX IF NOT EXISTS idx_api_key_external_ref ON api_keys(external_ref_id);
91100
CREATE INDEX IF NOT EXISTS idx_api_key_index_key ON api_keys(index_key);
101+
CREATE INDEX IF NOT EXISTS idx_api_keys_gateway_id ON api_keys(gateway_id);
92102
CREATE UNIQUE INDEX IF NOT EXISTS idx_unique_external_api_key
93103
ON api_keys(apiId, index_key)
94104
WHERE source = 'external' AND index_key IS NOT NULL;
95105

106+
-- Migration-safe column additions for existing deployments
107+
ALTER TABLE deployments ADD COLUMN IF NOT EXISTS gateway_id TEXT NOT NULL DEFAULT 'platform-gateway-id';
108+
ALTER TABLE certificates ADD COLUMN IF NOT EXISTS gateway_id TEXT NOT NULL DEFAULT 'platform-gateway-id';
109+
ALTER TABLE llm_provider_templates ADD COLUMN IF NOT EXISTS gateway_id TEXT NOT NULL DEFAULT 'platform-gateway-id';
110+
ALTER TABLE api_keys ADD COLUMN IF NOT EXISTS gateway_id TEXT NOT NULL DEFAULT 'platform-gateway-id';
111+
112+
ALTER TABLE deployments DROP CONSTRAINT IF EXISTS deployments_display_name_version_key;
113+
ALTER TABLE deployments DROP CONSTRAINT IF EXISTS deployments_handle_key;
114+
ALTER TABLE certificates DROP CONSTRAINT IF EXISTS certificates_name_key;
115+
ALTER TABLE llm_provider_templates DROP CONSTRAINT IF EXISTS llm_provider_templates_handle_key;
116+
ALTER TABLE api_keys DROP CONSTRAINT IF EXISTS api_keys_apiid_name_key;
117+
118+
DO $$
119+
BEGIN
120+
IF NOT EXISTS (
121+
SELECT 1 FROM pg_constraint WHERE conname = 'deployments_display_name_version_gateway_id_key'
122+
) THEN
123+
ALTER TABLE deployments
124+
ADD CONSTRAINT deployments_display_name_version_gateway_id_key
125+
UNIQUE (display_name, version, gateway_id);
126+
END IF;
127+
END $$;
128+
129+
DO $$
130+
BEGIN
131+
IF NOT EXISTS (
132+
SELECT 1 FROM pg_constraint WHERE conname = 'deployments_handle_gateway_id_key'
133+
) THEN
134+
ALTER TABLE deployments
135+
ADD CONSTRAINT deployments_handle_gateway_id_key
136+
UNIQUE (handle, gateway_id);
137+
END IF;
138+
END $$;
139+
140+
DO $$
141+
BEGIN
142+
IF NOT EXISTS (
143+
SELECT 1 FROM pg_constraint WHERE conname = 'certificates_name_gateway_id_key'
144+
) THEN
145+
ALTER TABLE certificates
146+
ADD CONSTRAINT certificates_name_gateway_id_key
147+
UNIQUE (name, gateway_id);
148+
END IF;
149+
END $$;
150+
151+
DO $$
152+
BEGIN
153+
IF NOT EXISTS (
154+
SELECT 1 FROM pg_constraint WHERE conname = 'llm_provider_templates_handle_gateway_id_key'
155+
) THEN
156+
ALTER TABLE llm_provider_templates
157+
ADD CONSTRAINT llm_provider_templates_handle_gateway_id_key
158+
UNIQUE (handle, gateway_id);
159+
END IF;
160+
END $$;
161+
162+
DO $$
163+
BEGIN
164+
IF NOT EXISTS (
165+
SELECT 1 FROM pg_constraint WHERE conname = 'api_keys_apiid_name_gateway_id_key'
166+
) THEN
167+
ALTER TABLE api_keys
168+
ADD CONSTRAINT api_keys_apiid_name_gateway_id_key
169+
UNIQUE (apiId, name, gateway_id);
170+
END IF;
171+
END $$;
172+
173+
CREATE INDEX IF NOT EXISTS idx_deployments_gateway_id ON deployments(gateway_id);
174+
CREATE INDEX IF NOT EXISTS idx_certificates_gateway_id ON certificates(gateway_id);
175+
CREATE INDEX IF NOT EXISTS idx_llm_provider_templates_gateway_id ON llm_provider_templates(gateway_id);
176+
CREATE INDEX IF NOT EXISTS idx_api_keys_gateway_id ON api_keys(gateway_id);
177+
96178
-- Schema migration metadata
97179
CREATE TABLE IF NOT EXISTS schema_migrations (
98180
id INTEGER PRIMARY KEY,

0 commit comments

Comments
 (0)