Skip to content

Commit e4f9936

Browse files
committed
[+] updated README with detailed environment variable descriptions
[+] improved Kubernetes labels section for clarity [+] added examples for service definitions with connection pooling [+] modified connection string format section for better readability [+] updated note regarding multi-node cluster setup [+] enhanced error handling in main.go for KUBE_CONTEXT retrieval [+] set default KUBE_CONTEXT to 'local-test' if not provided
1 parent 7fd1b28 commit e4f9936

File tree

2 files changed

+36
-35
lines changed

2 files changed

+36
-35
lines changed

README.md

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,24 @@ go build -o xdatabase-proxy apps/proxy/main.go
5151

5252
### Environment Variables
5353

54-
| Variable | Description | Default Value |
55-
| ---------------------------- | ------------------------------------ | ------------- |
56-
| KUBE_CONTEXT | Kubernetes context name | local-test |
57-
| POSTGRESQL_PROXY_ENABLED | Enable PostgreSQL proxy | - |
58-
| POSTGRESQL_PROXY_START_PORT | Starting port for PostgreSQL proxy | - |
59-
| NAMESPACE | Namespace for certificate storage | - |
54+
| Variable | Description | Required | Default | Example Value |
55+
| --------------------------- | ----------------------------------------------------------------------------- | -------- | ---------- | --------------- |
56+
| KUBE_CONTEXT | Kubernetes context name (only used in development/test mode, ignored in prod) | No | local-test | local-test |
57+
| POSTGRESQL_PROXY_ENABLED | Enable PostgreSQL proxy. Must be set to 'true' to activate the proxy. | Yes | - | true |
58+
| POSTGRESQL_PROXY_START_PORT | Starting port for PostgreSQL proxy. Must be set. | Yes | 5432 | 5432 |
59+
| NAMESPACE | Namespace where the proxy runs and self-signed certs are stored. | Yes | - | xdatabase-proxy |
6060

6161
### Kubernetes Labels
6262

6363
The following labels are required for the proxy to identify database services:
6464

65-
| Label | Description | Example Value |
66-
| ----------------------------- | ------------------------------------------------------------ | --------------- |
67-
| xdatabase-proxy-enabled | Whether the service should be managed by the proxy | true |
68-
| xdatabase-proxy-deployment-id | Database deployment ID | db-deployment-1 |
69-
| xdatabase-proxy-database-type | Database type | postgresql |
70-
| xdatabase-proxy-pooled | Whether this is a connection pooling service | true/false |
71-
| xdatabase-proxy-destination-port | Target port for the database connection | 5432 |
65+
| Label | Description | Example Value |
66+
| -------------------------------- | -------------------------------------------------- | --------------- |
67+
| xdatabase-proxy-enabled | Whether the service should be managed by the proxy | true |
68+
| xdatabase-proxy-deployment-id | Database deployment ID | db-deployment-1 |
69+
| xdatabase-proxy-database-type | Database type | postgresql |
70+
| xdatabase-proxy-pooled | Whether this is a connection pooling service | true/false |
71+
| xdatabase-proxy-destination-port | Target port for the database connection | 5432 |
7272

7373
> **Important**: This proxy is designed to be tool-agnostic. You don't need to use any specific pooling or cluster management solution. Simply add the appropriate labels to any service, and the proxy will route connections accordingly based on those labels.
7474
@@ -77,11 +77,13 @@ The following labels are required for the proxy to identify database services:
7777
The proxy supports three connection scenarios:
7878

7979
1. **Direct Connection**
80+
8081
- Client → PostgreSQL
8182
- Simple, direct connection to a single PostgreSQL instance
8283
- Use when connection pooling is not needed
8384

8485
2. **Connection Pooling**
86+
8587
- Client → Connection Pooler → PostgreSQL
8688
- Efficient connection management
8789
- Recommended for applications with many connections
@@ -98,6 +100,7 @@ The proxy supports three connection scenarios:
98100
### Service Definition Examples
99101

100102
#### 1. Direct PostgreSQL Service
103+
101104
```yaml
102105
apiVersion: v1
103106
kind: Service
@@ -107,15 +110,16 @@ metadata:
107110
xdatabase-proxy-enabled: "true"
108111
xdatabase-proxy-deployment-id: "db-deployment-1"
109112
xdatabase-proxy-database-type: "postgresql"
110-
xdatabase-proxy-pooled: "false" # Direct PostgreSQL connection
111-
xdatabase-proxy-destination-port: "5432" # Target PostgreSQL port
113+
xdatabase-proxy-pooled: "false" # Direct PostgreSQL connection
114+
xdatabase-proxy-destination-port: "5432" # Target PostgreSQL port
112115
spec:
113116
ports:
114117
- port: 5432
115118
name: postgresql
116119
```
117120
118121
#### 2. Connection Pooling Service (Example with PgBouncer)
122+
119123
```yaml
120124
apiVersion: v1
121125
kind: Service
@@ -125,15 +129,16 @@ metadata:
125129
xdatabase-proxy-enabled: "true"
126130
xdatabase-proxy-deployment-id: "db-deployment-1"
127131
xdatabase-proxy-database-type: "postgresql"
128-
xdatabase-proxy-pooled: "true" # This indicates it's a connection pooling service
129-
xdatabase-proxy-destination-port: "6432" # Target pooler port
132+
xdatabase-proxy-pooled: "true" # This indicates it's a connection pooling service
133+
xdatabase-proxy-destination-port: "6432" # Target pooler port
130134
spec:
131135
ports:
132136
- port: 6432
133137
name: postgresql
134138
```
135139
136140
#### 3. Multi-Node Cluster Setup (Example with Pgpool-II)
141+
137142
```yaml
138143
# Connection Pooler Service (Required for multi-node)
139144
apiVersion: v1
@@ -144,35 +149,24 @@ metadata:
144149
xdatabase-proxy-enabled: "true"
145150
xdatabase-proxy-deployment-id: "db-deployment-1"
146151
xdatabase-proxy-database-type: "postgresql"
147-
xdatabase-proxy-pooled: "true" # Required for multi-node setup
152+
xdatabase-proxy-pooled: "true" # Required for multi-node setup
153+
xdatabase-proxy-destination-port: "6432" # Target cluster manager port (Same as the connection pooler port)
148154
spec:
149155
ports:
150156
- port: 6432
151157
name: postgresql
152-
---
153-
# Cluster Manager Service
154-
apiVersion: v1
155-
kind: Service
156-
metadata:
157-
name: cluster-manager
158-
labels:
159-
xdatabase-proxy-enabled: "true"
160-
xdatabase-proxy-deployment-id: "db-deployment-1"
161-
xdatabase-proxy-database-type: "postgresql"
162-
xdatabase-proxy-pooled: "true" # Must be true for cluster manager
163-
spec:
164-
ports:
165-
- port: 9999 # Target port for your cluster manager
166-
name: postgresql
167158
```
168159
160+
> **Note:** For multi-node clusters (e.g., Pgpool-II, Patroni, etc.), you only need to define the connection pooler service as shown above. There is no need for a separate cluster manager service definition; the proxy will automatically handle routing based on labels.
161+
169162
### Connection String Format
170163
171164
```
172165
postgresql://username.deployment_id[.pool]@proxy-host:port/dbname
173166
```
174167

175168
Examples:
169+
176170
```
177171
# 1. Direct PostgreSQL Connection
178172
postgresql://myuser.db-deployment-1@localhost:3001/mydb
@@ -233,4 +227,6 @@ kubectl apply -f https://raw.githubusercontent.com/hasirciogli/xdatabase-proxy/m
233227

234228
---
235229

236-
*(Note: A Turkish version of this README is planned and will be added soon.)*
230+
_(Note: A Turkish version of this README is planned and will be added soon.)_
231+
232+
---

apps/proxy/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ func startPostgresProxy(ctx context.Context, wg *sync.WaitGroup) {
6969
panic("POSTGRESQL_PROXY_START_PORT is not set (REQUIRED)")
7070
}
7171

72-
proxy, err := postgresql.NewPostgresProxy("local-test")
72+
kubeContext := os.Getenv("KUBE_CONTEXT")
73+
if kubeContext == "" {
74+
kubeContext = "local-test"
75+
}
76+
77+
proxy, err := postgresql.NewPostgresProxy(kubeContext)
7378
if err != nil {
7479
log.Fatalf("Failed to create PostgreSQL proxy: %v", err)
7580
}

0 commit comments

Comments
 (0)