Skip to content

Commit 3508a09

Browse files
committed
[ws-proxy] Do not stomp on newer workspaces when handling events
1 parent a49bd32 commit 3508a09

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

components/ws-proxy/pkg/proxy/infoprovider.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ type WorkspaceInfo struct {
7878
// (parsed from URL)
7979
IDEPublicPort string
8080

81-
Ports []PortInfo
82-
Auth *wsapi.WorkspaceAuthentication
81+
Ports []PortInfo
82+
Auth *wsapi.WorkspaceAuthentication
83+
StartedAt time.Time
8384
}
8485

8586
// PortInfo contains all information ws-proxy needs to know about a workspace port
@@ -265,7 +266,7 @@ func (p *RemoteWorkspaceInfoProvider) listen(client wsapi.WorkspaceManagerClient
265266
}
266267

267268
if status.Phase == wsapi.WorkspacePhase_STOPPED {
268-
p.cache.Delete(status.Metadata.MetaId)
269+
p.cache.Delete(status.Metadata.MetaId, status.Metadata.StartedAt.AsTime())
269270
} else {
270271
info := mapWorkspaceStatusToInfo(status)
271272
p.cache.Insert(info)
@@ -309,6 +310,7 @@ func mapWorkspaceStatusToInfo(status *wsapi.WorkspaceStatus) *WorkspaceInfo {
309310
IDEPublicPort: getPortStr(status.Spec.Url),
310311
Ports: portInfos,
311312
Auth: status.Auth,
313+
StartedAt: status.Metadata.StartedAt.AsTime(),
312314
}
313315
}
314316

@@ -400,6 +402,18 @@ func (c *workspaceInfoCache) Insert(info *WorkspaceInfo) {
400402
}
401403

402404
func (c *workspaceInfoCache) doInsert(info *WorkspaceInfo) {
405+
existing := c.infos[info.WorkspaceID]
406+
if existing != nil {
407+
// Do not insert if the current workspace is newer
408+
// This works around issues when a workspace is deleted then restarted in quick succession
409+
// and the stopping event occurs after the replocement pod is started
410+
if !existing.StartedAt.IsZero() && !info.StartedAt.IsZero() {
411+
if existing.StartedAt.After(info.StartedAt) {
412+
log.WithField("workspaceID", existing.WorkspaceID).Debug("ignoring insert of older workspace")
413+
return
414+
}
415+
}
416+
}
403417
c.infos[info.WorkspaceID] = info
404418
c.coordsByPublicPort[info.IDEPublicPort] = &WorkspaceCoords{
405419
ID: info.WorkspaceID,
@@ -413,14 +427,23 @@ func (c *workspaceInfoCache) doInsert(info *WorkspaceInfo) {
413427
}
414428
}
415429

416-
func (c *workspaceInfoCache) Delete(workspaceID string) {
430+
func (c *workspaceInfoCache) Delete(workspaceID string, startedAt time.Time) {
417431
c.cond.L.Lock()
418432
defer c.cond.L.Unlock()
419433

420434
info, present := c.infos[workspaceID]
421435
if !present || info == nil {
422436
return
423437
}
438+
// Do not delete if the current workspace info is newer
439+
// This works around issues when a workspace is deleted then restarted in quick succession
440+
// and the delete event occurs after the replocement pod is started
441+
if !startedAt.IsZero() && !info.StartedAt.IsZero() {
442+
if info.StartedAt.After(startedAt) {
443+
log.WithField("workspaceID", workspaceID).WithField("startedAt", startedAt).WithField("info", info).Debug("ignoring delete of older workspace")
444+
return
445+
}
446+
}
424447
delete(c.coordsByPublicPort, info.IDEPublicPort)
425448
delete(c.infos, workspaceID)
426449
}

components/ws-proxy/pkg/proxy/infoprovider_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/google/go-cmp/cmp"
1616
"github.com/google/go-cmp/cmp/cmpopts"
1717
"google.golang.org/grpc"
18+
"google.golang.org/protobuf/types/known/timestamppb"
1819

1920
wsapi "github.com/gitpod-io/gitpod/ws-manager/api"
2021
wsmock "github.com/gitpod-io/gitpod/ws-manager/api/mock"
@@ -178,10 +179,12 @@ func TestRemoteInfoProvider(t *testing.T) {
178179
}
179180

180181
var (
182+
startedAt = time.Date(2021, 1, 1, 00, 00, 00, 00, time.UTC)
181183
testWorkspaceStatus = &wsapi.WorkspaceStatus{
182184
Id: "e63cb5ff-f4e4-4065-8554-b431a32c0000",
183185
Metadata: &wsapi.WorkspaceMetadata{
184-
MetaId: "e63cb5ff-f4e4-4065-8554-b431a32c2714",
186+
MetaId: "e63cb5ff-f4e4-4065-8554-b431a32c2714",
187+
StartedAt: timestamppb.New(startedAt),
185188
},
186189
Auth: &wsapi.WorkspaceAuthentication{
187190
Admission: wsapi.AdmissionLevel_ADMIT_OWNER_ONLY,
@@ -216,5 +219,6 @@ var (
216219
},
217220
URL: testWorkspaceStatus.Spec.Url,
218221
WorkspaceID: testWorkspaceStatus.Metadata.MetaId,
222+
StartedAt: startedAt,
219223
}
220224
)

0 commit comments

Comments
 (0)