{
  "id": "endpoint-fix-task",
  "title": "Fix a Broken Service Selector",
  "category": "Troubleshooting",
  "difficulty": "Medium",
  "type": "task",
  "weight": 7,
  "description": "## Fix a Broken Service Selector\n\nA service `my-svc` has been created but no traffic reaches the pods. The pods are labelled `app=backend`, but the service selector is misconfigured.\n\n**Your task:**\n\nFix the `my-svc` service selector so it correctly targets the `backend` pods.\n\n```bash\n# Investigate:\nkubectl describe service my-svc\nkubectl get endpoints my-svc\nkubectl get pods --show-labels\n```",
  "hints": [
    {
      "title": "Identify the mismatch",
      "body": "Check what label the pods actually carry, then compare it against the service selector.",
      "command": "kubectl get pods --show-labels && kubectl get service my-svc -o jsonpath='{.spec.selector}'"
    },
    {
      "title": "Patch the service selector",
      "body": "The pods carry `app=backend` but the service selector says `app=wrong-label`. Use `kubectl patch` to point the selector at the correct label.",
      "command": "kubectl patch service my-svc --type=json -p='[{\"op\":\"replace\",\"path\":\"/spec/selector/app\",\"value\":\"backend\"}]'"
    },
    {
      "title": "Verify endpoints are populated",
      "body": "After fixing the selector, Kubernetes will automatically update the endpoint slice. Confirm the service now has live pod addresses.",
      "command": "kubectl get endpoints my-svc"
    }
  ],
  "setup_commands": [
    {
      "command": "kubectl create deployment backend --image=nginx:alpine --replicas=2 2>/dev/null || true"
    },
    {
      "command": "kubectl apply -f - <<EOF\napiVersion: v1\nkind: Service\nmetadata:\n  name: my-svc\nspec:\n  selector:\n    app: wrong-label\n  ports:\n  - port: 80\n    targetPort: 80\nEOF"
    }
  ],
  "validation": {
    "commands": [
      {
        "description": "Service 'my-svc' selector is app=backend",
        "command": "kubectl get service my-svc -o jsonpath='{.spec.selector.app}'",
        "expected_output": "backend",
        "match": "exact"
      },
      {
        "description": "Endpoints exist for my-svc",
        "command": "kubectl get endpoints my-svc -o jsonpath='{.subsets[0].addresses[0].ip}' 2>/dev/null | grep -v '^$' || echo empty",
        "expected_output": "empty",
        "match": "not_contains"
      }
    ]
  },
  "default_namespace": "default",
  "teardown_commands": [
    {
      "command": "kubectl delete deployment backend --ignore-not-found"
    },
    {
      "command": "kubectl delete svc my-svc --ignore-not-found"
    }
  ]
}
