|
16 | 16 | fi |
17 | 17 | echo "--- End Debug Info ---" |
18 | 18 |
|
19 | | -# Function to detect the correct host IP for k3s access from Docker |
20 | | -detect_docker_host_ip() { |
21 | | - # First check if K3S_HOST_IP environment variable is explicitly set |
22 | | - if [ -n "$K3S_HOST_IP" ]; then |
23 | | - echo "$K3S_HOST_IP" |
24 | | - return |
25 | | - fi |
26 | | - |
27 | | - # Try host.docker.internal first (works on Docker Desktop for Mac/Windows) |
28 | | - if getent hosts host.docker.internal > /dev/null 2>&1; then |
29 | | - echo "host.docker.internal" |
30 | | - return |
31 | | - fi |
32 | | - |
33 | | - # If host.docker.internal doesn't work, try to ping it to see if it resolves |
34 | | - if ping -c 1 host.docker.internal > /dev/null 2>&1; then |
35 | | - echo "host.docker.internal" |
36 | | - return |
37 | | - fi |
38 | | - |
39 | | - # Check if we're using host networking (no .dockerenv means host network) |
40 | | - if [ ! -f /.dockerenv ]; then |
41 | | - echo "127.0.0.1" |
42 | | - return |
43 | | - fi |
44 | | - |
45 | | - # Fall back to Docker bridge gateway (typically Linux) |
46 | | - echo "172.17.0.1" |
47 | | -} |
48 | | - |
49 | 19 | if [ "$CI" = "true" ] && [ -f /root/.kube/config ]; then |
50 | 20 | echo "CI environment detected. Creating a patched kubeconfig..." |
51 | 21 | # Read from the read-only original and write the patched version to our new file |
52 | 22 | sed 's|server: https://127.0.0.1:6443|server: https://host.docker.internal:6443|g' /root/.kube/config > "${WRITABLE_KUBECONFIG_DIR}/config" |
53 | | -elif [ -f /root/.kube/config ] && grep -q "server:" /root/.kube/config; then |
54 | | - echo "Detected kubeconfig with k3s/k8s. Patching for Docker access..." |
55 | | - # For non-CI environments, we need to ensure k3s is accessible from within Docker |
56 | | - # Try to detect if we're in Docker and k3s is on the host |
57 | | - if [ -f /.dockerenv ]; then |
58 | | - # We're in a Docker container - need to patch the config |
59 | | - DOCKER_HOST_IP=$(detect_docker_host_ip) |
60 | | - echo "Detected Docker host IP: ${DOCKER_HOST_IP}" |
61 | | - |
62 | | - sed "s|server: https://[^:]*:6443|server: https://${DOCKER_HOST_IP}:6443|g" /root/.kube/config > "${WRITABLE_KUBECONFIG_DIR}/config" |
63 | | - echo "Patched kubeconfig to use ${DOCKER_HOST_IP} for k3s access from Docker" |
64 | | - export KUBECONFIG="${WRITABLE_KUBECONFIG_DIR}/config" |
65 | | - fi |
| 23 | + |
| 24 | + # Point the KUBECONFIG variable to our new, writable, and patched file |
| 25 | + export KUBECONFIG="${WRITABLE_KUBECONFIG_DIR}/config" |
66 | 26 |
|
67 | 27 | echo "Kubeconfig patched and new config is at ${KUBECONFIG}." |
68 | 28 | echo "--- Patched Kubeconfig Contents ---" |
@@ -112,38 +72,6 @@ echo "Self-signed certificate created and copied." |
112 | 72 | # --- Generate Kubeconfig --- |
113 | 73 | if [ -d /backend ]; then |
114 | 74 | echo "Ensuring kubeconfig is up to date" |
115 | | - |
116 | | - # In CI without a real K8s cluster, create a dummy kubeconfig |
117 | | - if [ "$CI" = "true" ] && ! kubectl cluster-info > /dev/null 2>&1; then |
118 | | - echo "CI detected without K8s cluster - creating dummy kubeconfig for testing" |
119 | | - cat > /backend/kubeconfig.yaml <<EOF |
120 | | -apiVersion: v1 |
121 | | -kind: Config |
122 | | -clusters: |
123 | | -- name: docker-desktop |
124 | | - cluster: |
125 | | - server: https://127.0.0.1:6443 |
126 | | - certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJkekNDQVIyZ0F3SUJBZ0lCQURBS0JnZ3Foa2pPUFFRREFqQWpNU0V3SHdZRFZRUUREQmhyTTNNdGMyVnkKZG1WeUxXTmhRREUyTnpjeU5UazVPVGN3SGhjTk1qSXhNakU1TVRVMU9UTTNXaGNOTXpJeE1qRTJNVFUxT1RNMwpXakFqTVNFd0h3WURWUVFEREJock0zTXRjMlZ5ZG1WeUxXTmhRREUyTnpjeU5UazVPVGN3V1RBVEJnY3Foa2pPClBRSUJCZ2dxaGtqT1BRTUJCd05DQUFSVnNKeWlqc3hJOGl6cGFQRVlIcEo0WGdFTG9xbVlLMXkwSytNMWlTMUwKa1d2d2JkcGZ0MXAwUFRLMTU0K2xia0JnbHVBdG9vSFJJUTg4MjZpcENLMDhvMEl3UURBT0JnTlZIUThCQWY4RQpCQU1DQXFRd0R3WURWUjBUQVFIL0JBVXdBd0VCL3pBZEJnTlZIUTRFRmdRVTlXRUpWNGcvWGh5YkpBWUhIQXVOCldJNnYvNll3Q2dZSUtvWkl6ajBFQXdJRFNBQXdSUUloQU1wNFRtakg5NWRYQnBGNmtCcFdKaWsxT3BYV0tMNzYKaHJKdVFYRXJJOGZlQWlBWk8rL2NsVklrd0Yvb0VuSEhZeHJCRGxHQzR2ekxIa2k2SFMvMUFCWkV3Zz09Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K |
127 | | -users: |
128 | | -- name: integr8scode-sa |
129 | | - user: |
130 | | - token: "dummy-token-for-ci-testing" |
131 | | -contexts: |
132 | | -- name: integr8scode |
133 | | - context: |
134 | | - cluster: docker-desktop |
135 | | - user: integr8scode-sa |
136 | | -current-context: integr8scode |
137 | | -EOF |
138 | | - chmod 644 /backend/kubeconfig.yaml |
139 | | - echo "Dummy kubeconfig.yaml created for CI testing." |
140 | | - echo "Setup completed successfully." |
141 | | - |
142 | | -# Create a setup-complete file to indicate success |
143 | | -touch /backend/setup-complete || true |
144 | | - exit 0 |
145 | | - fi |
146 | | - |
147 | 75 | if ! kubectl config view --raw -o jsonpath='{.clusters[0].cluster.certificate-authority-data}' > /dev/null 2>&1; then |
148 | 76 | echo "ERROR: kubectl is not configured to connect to a cluster." |
149 | 77 | exit 1 |
@@ -187,15 +115,13 @@ roleRef: |
187 | 115 | EOF |
188 | 116 | TOKEN=$(kubectl create token integr8scode-sa -n default --duration=24h) |
189 | 117 | K8S_SERVER=$(kubectl config view --raw -o jsonpath='{.clusters[0].cluster.server}') |
190 | | - # When running in Docker, need to use appropriate host IP |
191 | | - if [ -f /.dockerenv ]; then |
192 | | - DOCKER_HOST_IP=$(detect_docker_host_ip) |
193 | | - K8S_SERVER=$(echo "$K8S_SERVER" | sed "s|https://[^:]*:|https://${DOCKER_HOST_IP}:|") |
194 | | - echo "Running in Docker, using ${DOCKER_HOST_IP} for k3s server" |
195 | | - else |
196 | | - # Otherwise use localhost |
197 | | - K8S_SERVER=$(echo "$K8S_SERVER" | sed 's|https://[^:]*:|https://127.0.0.1:|') |
| 118 | + |
| 119 | + # In CI, ensure the generated kubeconfig also uses host.docker.internal |
| 120 | + if [ "$CI" = "true" ]; then |
| 121 | + K8S_SERVER=$(echo "$K8S_SERVER" | sed 's|https://127.0.0.1:|https://host.docker.internal:|') |
| 122 | + echo "CI: Patched K8S_SERVER to ${K8S_SERVER}" |
198 | 123 | fi |
| 124 | + |
199 | 125 | cat > /backend/kubeconfig.yaml <<EOF |
200 | 126 | apiVersion: v1 |
201 | 127 | kind: Config |
|
0 commit comments