-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[prebuilds] Fix prebuild updates received by ws-manager-bridge #10042
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--require ts-node/register | ||
--require reflect-metadata/Reflect | ||
--require source-map-support/register | ||
--reporter spec | ||
--watch-extensions ts | ||
--exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
components/ws-manager-bridge/src/prebuild-state-mapper.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { suite, test } from "mocha-typescript"; | ||
import * as chai from "chai"; | ||
import { PrebuildStateMapper } from "./prebuild-state-mapper"; | ||
import { WorkspaceConditionBool, WorkspacePhase, WorkspaceStatus } from "@gitpod/ws-manager/lib"; | ||
import { PrebuiltWorkspace } from "@gitpod/gitpod-protocol"; | ||
|
||
const expect = chai.expect; | ||
|
||
@suite | ||
class TestPrebuildStateMapper { | ||
@test public testAll() { | ||
const id = "12345"; | ||
const snapshot = "some-valid-snapshot"; | ||
const failed = "some system error"; | ||
const headlessTaskFailed = "some user/content error"; | ||
|
||
const table: { | ||
name: string; | ||
status: Pick<WorkspaceStatus.AsObject, "id" | "phase"> & { | ||
conditions: Partial<WorkspaceStatus.AsObject["conditions"]>; | ||
}; | ||
expected: (Omit<Partial<PrebuiltWorkspace>, "error"> & { hasError?: boolean }) | undefined; | ||
}[] = [ | ||
{ | ||
name: "STOPPED", | ||
expected: undefined, | ||
status: { | ||
id, | ||
phase: WorkspacePhase.STOPPED, | ||
conditions: { | ||
snapshot, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "failed", | ||
expected: { | ||
state: "failed", | ||
hasError: true, | ||
}, | ||
status: { | ||
id, | ||
phase: WorkspacePhase.STOPPING, | ||
conditions: { | ||
failed, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "failed - no snapshot", | ||
expected: { | ||
state: "failed", | ||
hasError: true, | ||
}, | ||
status: { | ||
id, | ||
phase: WorkspacePhase.STOPPING, | ||
conditions: { | ||
snapshot: "", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "aborted", | ||
expected: { | ||
state: "aborted", | ||
hasError: true, | ||
}, | ||
status: { | ||
id, | ||
phase: WorkspacePhase.STOPPING, | ||
conditions: { | ||
stoppedByRequest: WorkspaceConditionBool.TRUE, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "user/content error", | ||
expected: { | ||
state: "available", | ||
hasError: true, | ||
snapshot, | ||
}, | ||
status: { | ||
id, | ||
phase: WorkspacePhase.STOPPING, | ||
conditions: { | ||
headlessTaskFailed, | ||
snapshot, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "available and no error", | ||
expected: { | ||
state: "available", | ||
hasError: false, | ||
snapshot, | ||
}, | ||
status: { | ||
id, | ||
phase: WorkspacePhase.STOPPING, | ||
conditions: { | ||
snapshot, | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
for (const test of table) { | ||
const cut = new PrebuildStateMapper(); | ||
const actual = cut.mapWorkspaceStatusToPrebuild(test.status as WorkspaceStatus.AsObject); | ||
expect(!!actual?.update.error, test.name + ": hasError").to.be.equal(!!test.expected?.hasError); | ||
delete actual?.update.error; | ||
delete test.expected?.hasError; | ||
expect(actual?.update, test.name).to.deep.equal(test.expected); | ||
} | ||
} | ||
} | ||
module.exports = new TestPrebuildStateMapper(); // Only to circumvent no usage warning :-/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { HeadlessWorkspaceEventType, PrebuiltWorkspace } from "@gitpod/gitpod-protocol"; | ||
import { log } from "@gitpod/gitpod-protocol/lib/util/logging"; | ||
import { WorkspacePhase, WorkspaceStatus } from "@gitpod/ws-manager/lib"; | ||
import { injectable } from "inversify"; | ||
|
||
export interface PrebuildUpdate { | ||
type: HeadlessWorkspaceEventType; | ||
update: Partial<PrebuiltWorkspace>; | ||
} | ||
|
||
@injectable() | ||
export class PrebuildStateMapper { | ||
mapWorkspaceStatusToPrebuild(status: WorkspaceStatus.AsObject): PrebuildUpdate | undefined { | ||
if (status.phase === WorkspacePhase.STOPPED) { | ||
// Ideally, we'd love to hande STOPPED identical to STOPPING, because we want to assume that all conditions are stable. | ||
// For reliabilies sake we don't do it because experiences shows that unstable conditions are one of the most common sources of errors. | ||
return undefined; | ||
} | ||
|
||
if (status.phase === WorkspacePhase.STOPPING) { | ||
if (!!status.conditions!.timeout) { | ||
return { | ||
type: HeadlessWorkspaceEventType.AbortedTimedOut, | ||
update: { | ||
state: "timeout", | ||
error: status.conditions!.timeout, | ||
}, | ||
}; | ||
} else if (!!status.conditions!.failed) { | ||
return { | ||
type: HeadlessWorkspaceEventType.Failed, | ||
update: { | ||
state: "failed", | ||
error: status.conditions!.failed, | ||
}, | ||
}; | ||
} else if (!!status.conditions!.stoppedByRequest) { | ||
return { | ||
type: HeadlessWorkspaceEventType.Aborted, | ||
update: { | ||
state: "aborted", | ||
error: "Cancelled", | ||
}, | ||
}; | ||
} else if (!!status.conditions!.headlessTaskFailed) { | ||
const result: PrebuildUpdate = { | ||
type: HeadlessWorkspaceEventType.FinishedButFailed, | ||
update: { | ||
state: "available", | ||
snapshot: status.conditions!.snapshot, | ||
error: status.conditions!.headlessTaskFailed, | ||
}, | ||
}; | ||
return result; | ||
} else if (!!status.conditions!.snapshot) { | ||
return { | ||
type: HeadlessWorkspaceEventType.FinishedSuccessfully, | ||
update: { | ||
state: "available", | ||
snapshot: status.conditions!.snapshot, | ||
}, | ||
}; | ||
} else if (!status.conditions!.snapshot) { | ||
// STOPPING && no snapshot? Definitely an error case | ||
return { | ||
type: HeadlessWorkspaceEventType.Failed, | ||
update: { | ||
state: "failed", | ||
error: "error while taking snapshot", | ||
}, | ||
}; | ||
} else { | ||
log.error({ instanceId: status.id }, "unhandled prebuild status update", { | ||
phase: status.phase, | ||
conditions: status.phase, | ||
}); | ||
return undefined; | ||
} | ||
} | ||
|
||
return { | ||
type: HeadlessWorkspaceEventType.Started, | ||
update: { | ||
state: "building", | ||
}, | ||
}; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙈