Skip to content

Commit 8617439

Browse files
author
Max Azatian
committed
fixes 2 - for CI, took another version of setup-k8s.sh
1 parent 00db7c8 commit 8617439

File tree

1 file changed

+9
-83
lines changed

1 file changed

+9
-83
lines changed

cert-generator/setup-k8s.sh

Lines changed: 9 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,13 @@ else
1616
fi
1717
echo "--- End Debug Info ---"
1818

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-
4919
if [ "$CI" = "true" ] && [ -f /root/.kube/config ]; then
5020
echo "CI environment detected. Creating a patched kubeconfig..."
5121
# Read from the read-only original and write the patched version to our new file
5222
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"
6626

6727
echo "Kubeconfig patched and new config is at ${KUBECONFIG}."
6828
echo "--- Patched Kubeconfig Contents ---"
@@ -112,38 +72,6 @@ echo "Self-signed certificate created and copied."
11272
# --- Generate Kubeconfig ---
11373
if [ -d /backend ]; then
11474
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-
14775
if ! kubectl config view --raw -o jsonpath='{.clusters[0].cluster.certificate-authority-data}' > /dev/null 2>&1; then
14876
echo "ERROR: kubectl is not configured to connect to a cluster."
14977
exit 1
@@ -187,15 +115,13 @@ roleRef:
187115
EOF
188116
TOKEN=$(kubectl create token integr8scode-sa -n default --duration=24h)
189117
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}"
198123
fi
124+
199125
cat > /backend/kubeconfig.yaml <<EOF
200126
apiVersion: v1
201127
kind: Config

0 commit comments

Comments
 (0)