Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions deploy/helm/kubeview/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ podSecurityContext: {}
securityContext: {}

# -- Run multiple instances of the Kubeview pod
# NOTE: With more than one replica, clients need sticky sessions (session affinity) on the
# Service or ingress, so a browser's SSE event stream and its API calls reach the same pod.
# Without them live updates will not work, see https://github.com/benc-uk/kubeview/issues/185
replicaCount: 1

# -- This is for changing the resource limits and requests for the Kubeview pods
Expand Down
32 changes: 22 additions & 10 deletions frontend/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,30 @@ Alpine.data('mainApp', () => ({

let data
let res
try {
res = await fetch(`api/fetch/${this.namespace}?clientID=${getClientId()}`)
if (!res.ok) throw new Error(`HTTP error ${res.status}: ${res.statusText}`)
data = await res.json()

this.isLoading = false
this.showWelcome = false
} catch (err) {
this.showError(`Failed to fetch namespace data: ${err.message}`, res)
return
const maxAttempts = 5
for (let attempt = 1; ; attempt++) {
try {
res = await fetch(`api/fetch/${this.namespace}?clientID=${getClientId()}`)
// 409 means the client's SSE stream is connected to a different pod, which can happen
// with multiple replicas and no sticky sessions. Retrying should eventually land the
// fetch on the same pod as the SSE stream. See https://github.com/benc-uk/kubeview/issues/185
if (res.status === 409 && attempt < maxAttempts) {
console.warn(`⚠️ SSE stream not on this pod, retrying fetch (${attempt}/${maxAttempts})`)
await new Promise((resolve) => setTimeout(resolve, 250))
continue
}
if (!res.ok) throw new Error(`HTTP error ${res.status}: ${res.statusText}`)
data = await res.json()
break
} catch (err) {
this.showError(`Failed to fetch namespace data: ${err.message}`, res)
return
}
}

this.isLoading = false
this.showWelcome = false

if (this.cfg.debug) {
console.log('📦 Fetched data:', data)
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ require (
sigs.k8s.io/structured-merge-diff/v6 v6.4.2 // indirect
sigs.k8s.io/yaml v1.6.0 // indirect
)

replace github.com/benc-uk/go-rest-api => github.com/davidgrldo/go-rest-api v1.1.1-fix.185
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/benc-uk/go-rest-api v1.1.0 h1:bLyUPPl/R52cpjzu8EavQxnNtUlaXt0ad5WmBiMjOUk=
github.com/benc-uk/go-rest-api v1.1.0/go.mod h1:l6SGcra0vweKQ7XHC7vooGnMAr8QjRdeP+gVwUObX24=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
Expand All @@ -8,6 +6,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davidgrldo/go-rest-api v1.1.1-fix.185 h1:tMrHp7uGpEZZwR21tZXjUMHo61OnDw13vDel42kXQVw=
github.com/davidgrldo/go-rest-api v1.1.1-fix.185/go.mod h1:l6SGcra0vweKQ7XHC7vooGnMAr8QjRdeP+gVwUObX24=
github.com/elastic/go-sysinfo v1.15.5 h1:fCVUDmjHgljLUQCygherMnsRRJ9AkuAQIywTL7dEH28=
github.com/elastic/go-sysinfo v1.15.5/go.mod h1:ZBVXmqS368dOn/jvijV/zHLfakWTYHBZPk3G244lHrU=
github.com/elastic/go-windows v1.0.2 h1:yoLLsAsV5cfg9FLhZ9EXZ2n2sQFKeDYrHenkcivY4vI=
Expand Down
15 changes: 15 additions & 0 deletions server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"net/http"
"regexp"
"slices"
"strconv"

"github.com/benc-uk/go-rest-api/pkg/problem"
Expand Down Expand Up @@ -139,6 +140,20 @@ func (s *KubeviewAPI) handleFetchData(w http.ResponseWriter, r *http.Request) {
return
}

// Guard: with multiple replicas and no sticky sessions, the browser's SSE
// stream may be connected to a different pod than this request landed on.
// Joining the group here would create a group member with no SSE channel on
// this pod, so the client would never receive any events. Reject the
// request instead, so the frontend can retry and land on the right pod.
// See https://github.com/benc-uk/kubeview/issues/185
if !slices.Contains(s.eventBroker.GetClients(), clientID) {
log.Printf("⚡ Client %s has no SSE connection on this pod, rejecting fetch", strconv.Quote(clientID))
problem.Wrap(409, r.RequestURI, "client not connected",
errors.New("clientID has no SSE connection to this pod, retry the request")).Send(w)

return
}

// Critical: Puts the client in the correct SSE group for this namespace
// Events are sent to this group, so the client will receive updates ONLY for this namespace
s.eventBroker.RemoveFromAllGroups(clientID)
Expand Down