Skip to content

Commit bf59a8b

Browse files
committed
Refactor PostgresProxy to resolve backend address for PostgreSQL and remove outdated Kubernetes deployment files
1 parent 2cddb3e commit bf59a8b

File tree

5 files changed

+215
-1
lines changed

5 files changed

+215
-1
lines changed

cmd/proxy/internal/proxy/postgresql/proxy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (p *PostgresProxy) HandleConnection(clientConn net.Conn) {
7676
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
7777
defer cancel()
7878

79-
backendAddr, err := p.Resolver.Resolve(ctx, metadata, core.DatabaseTypeMysql)
79+
backendAddr, err := p.Resolver.Resolve(ctx, metadata, core.DatabaseTypePostgresql)
8080
if err != nil {
8181
logger.Error("Resolution failed", "error", err, "remote_addr", clientConn.RemoteAddr())
8282
_ = p.sendErrorResponse(clientConn, &ErrorResponse{
File renamed without changes.
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: xdatabase-proxy
5+
---
6+
apiVersion: v1
7+
kind: ServiceAccount
8+
metadata:
9+
name: xdatabase-proxy-sa
10+
namespace: xdatabase-proxy
11+
---
12+
apiVersion: rbac.authorization.k8s.io/v1
13+
kind: ClusterRole
14+
metadata:
15+
name: xdatabase-proxy-role
16+
namespace: xdatabase-proxy
17+
rules:
18+
- apiGroups: [""]
19+
resources: ["pods", "services", "endpoints", "secrets", "configmaps"]
20+
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
21+
- apiGroups: [""]
22+
resources: ["events"]
23+
verbs: ["create", "patch"]
24+
- apiGroups: [""]
25+
resources: ["nodes"]
26+
verbs: ["list", "watch"]
27+
- apiGroups: ["apps"]
28+
resources: ["deployments", "daemonsets", "statefulsets", "replicasets"]
29+
verbs: ["get", "list", "watch"]
30+
- apiGroups: ["networking.k8s.io"]
31+
resources: ["ingresses"]
32+
verbs: ["get", "list", "watch"]
33+
- apiGroups: ["metrics.k8s.io"]
34+
resources: ["pods", "nodes"]
35+
verbs: ["get", "list", "watch"]
36+
---
37+
apiVersion: rbac.authorization.k8s.io/v1
38+
kind: ClusterRoleBinding
39+
metadata:
40+
name: xdatabase-proxy-role-binding
41+
namespace: xdatabase-proxy
42+
subjects:
43+
- kind: ServiceAccount
44+
name: xdatabase-proxy-sa
45+
namespace: xdatabase-proxy
46+
roleRef:
47+
kind: ClusterRole
48+
name: xdatabase-proxy-role
49+
apiGroup: rbac.authorization.k8s.io
50+
---
51+
apiVersion: apps/v1
52+
kind: DaemonSet
53+
metadata:
54+
name: xdatabase-proxy
55+
namespace: xdatabase-proxy
56+
spec:
57+
selector:
58+
matchLabels:
59+
app: xdatabase-proxy
60+
template:
61+
metadata:
62+
labels:
63+
app: xdatabase-proxy
64+
spec:
65+
serviceAccountName: xdatabase-proxy-sa
66+
tolerations:
67+
- key: node-role.kubernetes.io/control-plane
68+
operator: Exists
69+
effect: NoSchedule
70+
- key: node-role.kubernetes.io/master
71+
operator: Exists
72+
effect: NoSchedule
73+
containers:
74+
- name: xdatabase-proxy
75+
image: xdatabase-proxy-local-test:local-test
76+
imagePullPolicy: Never
77+
ports:
78+
- containerPort: 5432
79+
hostPort: 5432
80+
name: proxy-port
81+
env:
82+
- name: DATABASE_TYPE
83+
value: "postgresql"
84+
- name: RUNTIME
85+
value: "kubernetes"
86+
- name: NAMESPACE
87+
valueFrom:
88+
fieldRef:
89+
fieldPath: metadata.namespace
90+
- name: DISCOVERY_MODE
91+
value: "kubernetes"
92+
- name: PROXY_START_PORT
93+
value: "5432"
94+
- name: HEALTH_SERVER_PORT
95+
value: "8080"
96+
- name: TLS_ENABLED
97+
value: "true"
98+
- name: TLS_MODE
99+
value: "kubernetes"
100+
- name: TLS_SECRET_NAME
101+
value: "xdatabase-proxy-tls"
102+
- name: TLS_AUTO_GENERATE
103+
value: "true"
104+
- name: TLS_AUTO_RENEW
105+
value: "true"
106+
- name: TLS_RENEWAL_THRESHOLD_DAYS
107+
value: "30"
108+
- name: DEBUG
109+
value: "true"
110+
resources:
111+
requests:
112+
cpu: 100m
113+
memory: 128Mi
114+
limits:
115+
cpu: 1000m
116+
memory: 1Gi
117+
livenessProbe:
118+
httpGet:
119+
path: /health
120+
port: 8080
121+
scheme: HTTP
122+
initialDelaySeconds: 15
123+
periodSeconds: 20
124+
timeoutSeconds: 5
125+
failureThreshold: 3
126+
successThreshold: 1
127+
readinessProbe:
128+
httpGet:
129+
path: /ready
130+
port: 8080
131+
scheme: HTTP
132+
initialDelaySeconds: 5
133+
periodSeconds: 10
134+
timeoutSeconds: 3
135+
failureThreshold: 3
136+
successThreshold: 1
137+
---
138+
apiVersion: v1
139+
kind: Service
140+
metadata:
141+
name: xdatabase-proxy
142+
namespace: xdatabase-proxy
143+
spec:
144+
selector:
145+
app: xdatabase-proxy
146+
ports:
147+
- port: 5432
148+
targetPort: proxy-port
149+
name: proxy-port
150+
type: ClusterIP
File renamed without changes.

scripts/local-deploy/postgresql.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/bash
2+
3+
# Local build and deploy to Kubernetes cluster
4+
# This script builds the image locally and loads it directly to the cluster
5+
# without pulling from remote registry
6+
7+
set -e
8+
9+
IMAGE_TAG="xdatabase-proxy-local-test:local-test"
10+
11+
echo "🏗️ Building xdatabase-proxy locally..."
12+
echo "Environment: local-test"
13+
echo "Image: $IMAGE_TAG"
14+
15+
# Build the Docker image locally
16+
docker build -t "$IMAGE_TAG" -f Dockerfile .
17+
18+
echo "✅ Build complete!"
19+
20+
# Detect Kubernetes cluster type and load image
21+
if kubectl config current-context | grep -q "minikube"; then
22+
echo "📦 Loading image to Minikube..."
23+
minikube image load "$IMAGE_TAG"
24+
echo "✅ Image loaded to Minikube"
25+
elif kubectl config current-context | grep -q "kind"; then
26+
echo "📦 Loading image to Kind..."
27+
kind load docker-image "$IMAGE_TAG" --name general
28+
echo "✅ Image loaded to Kind"
29+
elif kubectl config current-context | grep -q "k3d"; then
30+
echo "📦 Loading image to k3d..."
31+
k3d image import "$IMAGE_TAG"
32+
echo "✅ Image loaded to k3d"
33+
elif kubectl config current-context | grep -q "docker-desktop"; then
34+
echo "📦 Docker Desktop detected - image is already available in local registry"
35+
echo "✅ Image available locally"
36+
else
37+
echo "⚠️ Unknown cluster type: $(kubectl config current-context)"
38+
echo "Assuming local Docker registry is shared with cluster..."
39+
echo "✅ Image should be available locally"
40+
fi
41+
42+
# Apply Kubernetes manifests
43+
echo "🚀 Deploying to Kubernetes..."
44+
kubectl apply -f "kubernetes/examples/local-test/postgresql.yaml"
45+
46+
# Wait a moment for the deployment to register
47+
sleep 2
48+
49+
# Restart the DaemonSet to use the new image
50+
echo "🔄 Restarting xdatabase-proxy DaemonSet..."
51+
kubectl rollout restart daemonset/xdatabase-proxy -n xdatabase-proxy
52+
53+
# Wait for rollout to complete
54+
echo "⏳ Waiting for rollout to complete..."
55+
kubectl rollout status daemonset/xdatabase-proxy -n xdatabase-proxy --timeout=120s
56+
57+
echo "✅ Deployment complete!"
58+
echo ""
59+
echo "📊 Pod status:"
60+
kubectl get pods -n xdatabase-proxy -l app=xdatabase-proxy
61+
62+
echo ""
63+
echo "📝 To view logs:"
64+
echo "kubectl logs -f -n xdatabase-proxy -l app=xdatabase-proxy"

0 commit comments

Comments
 (0)