|
| 1 | +#!/bin/bash |
| 2 | +# ============================================================================= |
| 3 | +# DocumentDB K8s Discovery Plugin — Test Environment Setup (WSL) |
| 4 | +# |
| 5 | +# Creates a kind cluster with the DocumentDB Kubernetes Operator and a |
| 6 | +# DocumentDB cluster for testing the VS Code extension discovery plugin. |
| 7 | +# |
| 8 | +# Prerequisites: WSL2 on Windows with Docker Desktop (WSL integration enabled) |
| 9 | +# OR Docker Engine installed directly in WSL. |
| 10 | +# |
| 11 | +# Tools installed automatically via curl/apt: kubectl, kind, helm. |
| 12 | +# ============================================================================= |
| 13 | +set -euo pipefail |
| 14 | + |
| 15 | +echo "=== DocumentDB K8s Discovery Plugin — Test Environment Setup (WSL) ===" |
| 16 | +echo "" |
| 17 | + |
| 18 | +# Verify we are running inside WSL |
| 19 | +if ! grep -qi microsoft /proc/version 2>/dev/null; then |
| 20 | + echo "WARNING: /proc/version does not indicate a WSL environment." |
| 21 | + echo "This script is intended for WSL2 on Windows. Proceeding anyway..." |
| 22 | +fi |
| 23 | + |
| 24 | +# 1. Verify Docker is available |
| 25 | +if ! command -v docker &>/dev/null; then |
| 26 | + echo "ERROR: 'docker' not found." |
| 27 | + echo "" |
| 28 | + echo "Options:" |
| 29 | + echo " A) Enable Docker Desktop WSL integration:" |
| 30 | + echo " Docker Desktop → Settings → Resources → WSL Integration → enable your distro" |
| 31 | + echo " B) Install Docker Engine in WSL:" |
| 32 | + echo " curl -fsSL https://get.docker.com | sh" |
| 33 | + echo " sudo usermod -aG docker \$USER && newgrp docker" |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +if ! docker info &>/dev/null; then |
| 38 | + echo "ERROR: Docker daemon is not running or the current user has no access." |
| 39 | + echo "" |
| 40 | + echo "If using Docker Desktop, make sure it is running and WSL integration is enabled." |
| 41 | + echo "If using Docker Engine, start the daemon: sudo service docker start" |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +echo "Docker: OK ($(docker version --format '{{.Server.Version}}' 2>/dev/null || echo 'version unknown'))" |
| 46 | + |
| 47 | +# 2. Install kubectl |
| 48 | +if ! command -v kubectl &>/dev/null; then |
| 49 | + echo "Installing kubectl..." |
| 50 | + KUBECTL_VERSION=$(curl -fsSL https://dl.k8s.io/release/stable.txt) |
| 51 | + curl -fsSL "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl" \ |
| 52 | + -o /tmp/kubectl |
| 53 | + chmod +x /tmp/kubectl |
| 54 | + sudo mv /tmp/kubectl /usr/local/bin/kubectl |
| 55 | + echo "kubectl installed: $(kubectl version --client --short 2>/dev/null || kubectl version --client)" |
| 56 | +fi |
| 57 | + |
| 58 | +# 3. Install kind |
| 59 | +if ! command -v kind &>/dev/null; then |
| 60 | + echo "Installing kind..." |
| 61 | + KIND_VERSION=$(curl -fsSL https://api.github.com/repos/kubernetes-sigs/kind/releases/latest \ |
| 62 | + | grep '"tag_name"' | sed -E 's/.*"v([^"]+)".*/\1/') |
| 63 | + curl -fsSL "https://github.com/kubernetes-sigs/kind/releases/download/v${KIND_VERSION}/kind-linux-amd64" \ |
| 64 | + -o /tmp/kind |
| 65 | + chmod +x /tmp/kind |
| 66 | + sudo mv /tmp/kind /usr/local/bin/kind |
| 67 | + echo "kind installed: $(kind version)" |
| 68 | +fi |
| 69 | + |
| 70 | +# 4. Install helm |
| 71 | +if ! command -v helm &>/dev/null; then |
| 72 | + echo "Installing helm..." |
| 73 | + curl -fsSL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash |
| 74 | + echo "helm installed: $(helm version --short)" |
| 75 | +fi |
| 76 | + |
| 77 | +echo "" |
| 78 | +echo "All prerequisites satisfied." |
| 79 | +echo "" |
| 80 | + |
| 81 | +# 5. Delete existing cluster if present |
| 82 | +if kind get clusters 2>/dev/null | grep -q '^documentdb-dev$'; then |
| 83 | + echo "Deleting existing 'documentdb-dev' cluster..." |
| 84 | + kind delete cluster --name documentdb-dev |
| 85 | +fi |
| 86 | + |
| 87 | +# 6. Create kind cluster (K8s 1.35 required for ImageVolume GA) |
| 88 | +echo "Creating kind cluster 'documentdb-dev'..." |
| 89 | +kind create cluster --name documentdb-dev --image kindest/node:v1.35.0 |
| 90 | + |
| 91 | +# 7. Install cert-manager |
| 92 | +echo "Installing cert-manager..." |
| 93 | +helm repo add jetstack https://charts.jetstack.io 2>/dev/null || true |
| 94 | +helm repo update |
| 95 | +helm install cert-manager jetstack/cert-manager \ |
| 96 | + --namespace cert-manager \ |
| 97 | + --create-namespace \ |
| 98 | + --set installCRDs=true \ |
| 99 | + --wait |
| 100 | + |
| 101 | +# 8. Install DocumentDB operator |
| 102 | +echo "Installing DocumentDB operator..." |
| 103 | +helm repo add documentdb https://documentdb.github.io/documentdb-kubernetes-operator 2>/dev/null || true |
| 104 | +helm repo update |
| 105 | +helm install documentdb-operator documentdb/documentdb-operator \ |
| 106 | + --namespace documentdb-operator \ |
| 107 | + --create-namespace \ |
| 108 | + --wait |
| 109 | + |
| 110 | +# 9. Deploy DocumentDB cluster |
| 111 | +echo "Deploying DocumentDB cluster..." |
| 112 | +cat <<EOF | kubectl apply -f - |
| 113 | +apiVersion: v1 |
| 114 | +kind: Namespace |
| 115 | +metadata: |
| 116 | + name: documentdb-ns |
| 117 | +--- |
| 118 | +apiVersion: v1 |
| 119 | +kind: Secret |
| 120 | +metadata: |
| 121 | + name: documentdb-credentials |
| 122 | + namespace: documentdb-ns |
| 123 | +type: Opaque |
| 124 | +stringData: |
| 125 | + username: dev_user |
| 126 | + password: DevPassword123 |
| 127 | +--- |
| 128 | +apiVersion: documentdb.io/preview |
| 129 | +kind: DocumentDB |
| 130 | +metadata: |
| 131 | + name: my-documentdb |
| 132 | + namespace: documentdb-ns |
| 133 | +spec: |
| 134 | + nodeCount: 1 |
| 135 | + instancesPerNode: 1 |
| 136 | + documentDbCredentialSecret: documentdb-credentials |
| 137 | + resource: |
| 138 | + storage: |
| 139 | + pvcSize: 10Gi |
| 140 | + exposeViaService: |
| 141 | + serviceType: ClusterIP |
| 142 | +EOF |
| 143 | + |
| 144 | +# 10. Wait for DocumentDB to be healthy |
| 145 | +echo "Waiting for DocumentDB cluster to become healthy..." |
| 146 | +if ! kubectl wait --for=jsonpath='{.status.status}'='Cluster in healthy state' \ |
| 147 | + documentdb/my-documentdb -n documentdb-ns --timeout=300s; then |
| 148 | + echo "ERROR: DocumentDB cluster did not become healthy before timeout." |
| 149 | + kubectl get documentdb my-documentdb -n documentdb-ns |
| 150 | + exit 1 |
| 151 | +fi |
| 152 | + |
| 153 | +echo "" |
| 154 | +echo "=== Test Environment Ready ===" |
| 155 | +echo "" |
| 156 | +echo "Kubeconfig context: kind-documentdb-dev" |
| 157 | +echo "" |
| 158 | +echo "DocumentDB cluster:" |
| 159 | +echo " Namespace: documentdb-ns" |
| 160 | +echo " Service: documentdb-service-my-documentdb (ClusterIP :10260)" |
| 161 | +echo " Credentials: dev_user / DevPassword123 (auto-resolved from K8s Secret)" |
| 162 | +echo "" |
| 163 | +echo "To test in the extension:" |
| 164 | +echo " 1. Build: npm run build" |
| 165 | +echo " 2. Open VS Code via Remote - WSL (or use 'code .' from WSL terminal)" |
| 166 | +echo " 3. Press F5 in VS Code" |
| 167 | +echo " 4. Service Discovery → '+' → 'Kubernetes'" |
| 168 | +echo " 5. Manage Credentials → select 'kind-documentdb-dev'" |
| 169 | +echo " 6. Expand: documentdb-ns → documentdb-service-my-documentdb" |
| 170 | +echo " 7. Right-click → 'Add to Connections View'" |
| 171 | +echo " 8. Credentials should auto-resolve — no username prompt!" |
| 172 | +echo "" |
| 173 | +echo "Manual port-forward test:" |
| 174 | +echo " kubectl port-forward svc/documentdb-service-my-documentdb 10260:10260 -n documentdb-ns" |
| 175 | +echo " mongosh 'mongodb://dev_user:DevPassword123@127.0.0.1:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0'" |
| 176 | +echo "" |
| 177 | +echo "To tear down: ./scripts/k8s-test-teardown-wsl.sh" |
0 commit comments