#!/bin/bash # fix-portainer-service.sh — Create the missing headless Service for Portainer agent # Usage: ./hack/fix-portainer-service.sh # # The Portainer agent does a DNS lookup for "portainer-agent" to discover peers. # Without a Service, this lookup fails and the agent crashes. set -euo pipefail KUBECONFIG_URL="http://localhost:8080" echo "==> Fetching kubeconfig from $KUBECONFIG_URL..." KUBECONFIG_FILE=$(mktemp) trap 'rm -f "$KUBECONFIG_FILE"' EXIT curl -s "$KUBECONFIG_URL" > "$KUBECONFIG_FILE" if [ ! -s "$KUBECONFIG_FILE" ]; then echo "ERROR: Failed to fetch kubeconfig. Is the VM running?" exit 1 fi echo "==> Creating headless Service for portainer-agent..." kubectl --kubeconfig "$KUBECONFIG_FILE" apply -f - <<'EOF' apiVersion: v1 kind: Service metadata: name: portainer-agent namespace: portainer spec: clusterIP: None selector: app: portainer-agent ports: - name: agent port: 9001 targetPort: 9001 protocol: TCP EOF echo "==> Restarting portainer-agent deployment..." kubectl --kubeconfig "$KUBECONFIG_FILE" rollout restart -n portainer deployment/portainer-agent echo "==> Waiting for rollout..." kubectl --kubeconfig "$KUBECONFIG_FILE" rollout status -n portainer deployment/portainer-agent --timeout=120s echo "==> Done. Checking pod status:" kubectl --kubeconfig "$KUBECONFIG_FILE" get pods -n portainer