-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-k8s.sh
More file actions
executable file
Ā·242 lines (201 loc) Ā· 7.82 KB
/
run-k8s.sh
File metadata and controls
executable file
Ā·242 lines (201 loc) Ā· 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/bash
set -e
# Source shared utilities
if [ -z "$SCRIPT_DIR" ]; then
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
fi
# If SCRIPT_DIR is the root directory, we need to source from scripts subdirectory
if [ "$(basename "$SCRIPT_DIR")" = "scripts" ]; then
source "$SCRIPT_DIR/utils.sh"
source "$SCRIPT_DIR/config.sh"
else
source "$SCRIPT_DIR/scripts/utils.sh"
source "$SCRIPT_DIR/scripts/config.sh"
fi
# Function to show usage
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -r, --replicas NUM Number of replicas for router and subgraphs (default: 2)"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 # Deploy Apollo Supergraph with 2 replicas (default)"
echo " $0 --replicas 3 # Deploy Apollo Supergraph with 3 replicas"
echo " $0 -r 1 # Deploy Apollo Supergraph with 1 replica"
echo " $0 --help # Show this help message"
}
# Default values
NAMESPACE=$(get_k8s_namespace)
SERVICE_TYPE="ClusterIP"
REPLICAS=2
PORT_FORWARD_PID=""
# Cleanup function to stop port forwarding
cleanup() {
if [ -n "$PORT_FORWARD_PID" ] && kill -0 "$PORT_FORWARD_PID" 2>/dev/null; then
print_status "Stopping port forwarding (PID: $PORT_FORWARD_PID)..."
kill "$PORT_FORWARD_PID" 2>/dev/null || true
fi
}
# Set trap to cleanup on script exit
trap cleanup EXIT
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-r|--replicas)
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then
REPLICAS="$2"
shift 2
else
print_error "Replicas must be a positive integer"
show_usage
exit 1
fi
;;
-h|--help)
show_usage
exit 0
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
show_script_header "Apollo Supergraph Kubernetes Deployment" "Deploying Apollo Supergraph (router + subgraphs) to minikube"
# Validate required tools
if ! validate_required_tools; then
exit 1
fi
# Check if minikube is running
if ! minikube_is_running; then
print_error "Minikube is not running. Please start minikube first:"
echo " minikube start"
echo " or run: ./setup-minikube.sh"
exit 1
fi
print_success "Minikube is running"
# Source environment variables from .env file
if file_exists "router/.env"; then
print_status "Loading environment variables from router/.env"
# Load environment variables from .env file (simple key=value format)
set -a # automatically export all variables
source router/.env
set +a # stop automatically exporting
else
print_error "router/.env file not found. Please create it with APOLLO_KEY and APOLLO_GRAPH_REF"
print_error "Run the following command to create it safely:"
print_error " ./setup-env.sh"
print_error "Then edit router/.env with your actual Apollo Studio credentials"
exit 1
fi
# Verify required environment variables are set
if [ -z "$APOLLO_KEY" ] || [ -z "$APOLLO_GRAPH_REF" ]; then
print_error "APOLLO_KEY and APOLLO_GRAPH_REF must be set in router/.env"
print_error "Make sure your .env file contains simple key=value pairs (no export statements)"
exit 1
fi
print_status "Using APOLLO_GRAPH_REF: $APOLLO_GRAPH_REF"
# Build subgraphs Docker image
# Check if Docker is available in minikube
if ! minikube docker-env > /dev/null 2>&1; then
print_error "Cannot get minikube Docker environment"
exit 1
fi
# Set Docker environment to use minikube's Docker daemon
eval $(minikube docker-env)
print_status "Building subgraphs Docker image..."
cd subgraphs
docker build -t subgraphs:latest .
cd ..
print_success "Subgraphs Docker image built successfully"
# Create namespace
print_status "Creating namespace: $NAMESPACE"
kubectl apply -f k8s/namespace.yaml
# Set deployment environment variables
export NAMESPACE
export SERVICE_TYPE
export ROUTER_REPLICAS=$REPLICAS
export SUBGRAPHS_REPLICAS=$REPLICAS
# Create ConfigMaps from router files (single source of truth)
print_status "Creating ConfigMaps from router files..."
# Create router-config ConfigMap from router/router.yaml
kubectl create configmap router-config \
--from-file=config.yaml=router/router.yaml \
--namespace=$NAMESPACE \
--dry-run=client -o yaml | kubectl apply -f -
# Generate supergraph with Kubernetes URLs and create ConfigMap
print_status "Generating supergraph with Kubernetes URLs..."
cd router
# Generate supergraph with localhost URLs first
./compose.sh
# Create a temporary copy with Kubernetes URLs
sed "s|http://localhost:$SUBGRAPHS_PORT|http://$(get_subgraphs_service_name).$(get_k8s_namespace).svc.cluster.local:$SUBGRAPHS_PORT|g" supergraph.graphql > supergraph-k8s.graphql
cd ..
# Create supergraph-schema ConfigMap from the Kubernetes version
kubectl create configmap supergraph-schema \
--from-file=supergraph.graphql=router/supergraph-k8s.graphql \
--namespace=$NAMESPACE \
--dry-run=client -o yaml | kubectl apply -f -
# Clean up temporary file
rm router/supergraph-k8s.graphql
# Deploy subgraphs
print_status "Deploying subgraphs..."
NAMESPACE=$NAMESPACE SUBGRAPHS_REPLICAS=$SUBGRAPHS_REPLICAS envsubst < k8s/subgraphs-deployment-clusterip.yaml | kubectl apply -f -
# Wait for subgraphs to be ready
wait_for_deployment "subgraphs" "$NAMESPACE"
# Deploy Apollo Router
print_status "Deploying Apollo Router..."
NAMESPACE=$NAMESPACE ROUTER_REPLICAS=$ROUTER_REPLICAS APOLLO_GRAPH_REF=$APOLLO_GRAPH_REF APOLLO_KEY=$APOLLO_KEY envsubst < k8s/router-deployment-clusterip.yaml | kubectl apply -f -
# Wait for router to be ready
wait_for_deployment "apollo-router" "$NAMESPACE"
# Apply Ingress
print_status "Applying Ingress configuration..."
kubectl apply -f k8s/ingress.yaml
# Wait for ingress to be ready
print_status "Waiting for Ingress to be ready..."
sleep 10
# Get minikube IP
MINIKUBE_IP=$(get_minikube_ip)
# Start port forwarding automatically
print_status "Starting port forwarding for Apollo Router..."
print_status "Running: ./scripts/port-forward-utils.sh"
source ./scripts/port-forward-utils.sh && start_router_port_forward
# Start port forwarding for subgraphs
print_status "Starting port forwarding for subgraphs..."
source ./scripts/port-forward-utils.sh && start_subgraphs_port_forward
# Wait a moment for port forward to establish
sleep 3
# Source test utilities for health check
source "./scripts/test-utils.sh"
# Check if port forward is working
if test_router_health > /dev/null 2>&1; then
print_success "Port forwarding established successfully!"
else
print_warning "Port forwarding may still be establishing. Please wait a moment and try accessing $(get_router_graphql_url)"
fi
print_success "Deployment completed successfully!"
echo ""
echo "š Deployment Summary:"
echo " - Namespace: $NAMESPACE"
echo " - Service Type: $SERVICE_TYPE"
echo " - Subgraphs: ${SUBGRAPHS_REPLICAS} replica(s)"
echo " - Apollo Router: ${ROUTER_REPLICAS} replica(s)"
echo ""
echo "š Access your applications:"
echo " - Apollo Router: $(get_router_graphql_url) (port forwarding active)"
echo " - Router Health: $(get_router_health_url)"
echo " - Subgraphs: $(get_subgraphs_url) (port forwarding active)"
echo ""
echo "š Useful commands:"
echo " - View pods: kubectl get pods -n $NAMESPACE"
echo " - View services: kubectl get svc -n $NAMESPACE"
echo " - View router logs: kubectl logs -f deployment/apollo-router -n $NAMESPACE"
echo " - View subgraphs logs: kubectl logs -f deployment/subgraphs -n $NAMESPACE"
echo " - Stop port forwarding: ./scripts/port-forward-utils.sh stop"
echo " - Restart port forwarding: ./scripts/port-forward-utils.sh start"
echo " - Test router: ./test-router.sh"
echo " - Check status: ./status-k8s.sh"
show_script_footer "Apollo Supergraph Deployment"