Skip to content

Use prebuild workspace class for regular workspaces #11912

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

Merged
merged 5 commits into from
Aug 11, 2022
Merged
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
1 change: 1 addition & 0 deletions components/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@jmondi/oauth2-server": "^2.2.2",
"@octokit/rest": "18.6.1",
"@probot/get-private-key": "^1.1.1",
"@types/jaeger-client": "^3.18.3",
"amqplib": "^0.8.0",
"base-64": "^1.0.0",
"bitbucket": "^2.7.0",
Expand Down
91 changes: 91 additions & 0 deletions components/server/src/workspace/workspace-classes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* 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 { WorkspaceClassesConfig, WorkspaceClasses } from "./workspace-classes";
import * as chai from "chai";
const expect = chai.expect;

let config: WorkspaceClassesConfig = [
{
id: "g1-standard",
isDefault: true,
category: "GENERAL PURPOSE",
displayName: "Standard",
description: "Up to 4 vCPU, 8 GB memory, 30GB storage",
powerups: 1,
deprecated: false,
},
];

config.push({
id: "g1-large",
isDefault: false,
category: "GENERAL PURPOSE",
displayName: "Large",
description: "Up to 8 vCPU, 16 GB memory, 50GB storage",
powerups: 2,
deprecated: false,
marker: {
moreResources: true,
},
});

config.push({
id: "g1-deprecated",
isDefault: false,
category: "GENERAL PURPOSE",
displayName: "Large",
description: "Up to 8 vCPU, 16 GB memory, 50GB storage",
powerups: 2,
deprecated: true,
marker: {
moreResources: true,
},
});

describe("workspace-classes", function () {
describe("can substitute", function () {
it("classes are the same", function () {
const classId = WorkspaceClasses.selectClassForRegular("g1-large", "g1-large", config);
expect(classId).to.be.equal("g1-large");
});

it("prebuild has more resources, substitute has not", function () {
const classId = WorkspaceClasses.selectClassForRegular("g1-large", "g1-standard", config);
expect(classId).to.be.equal("g1-large");
});

it("prebuild has more resources, substitute also has more resources", function () {
const classId = WorkspaceClasses.selectClassForRegular("g1-large", "g1-large", config);
expect(classId).to.be.equal("g1-large");
});

it("prebuild has more resources, substitute has not, prebuild is deprecated", function () {
const classId = WorkspaceClasses.selectClassForRegular("g1-deprecated", "g1-standard", config);
expect(classId).to.be.equal("g1-large");
});

it("prebuild has more resources, substitute has not, prebuild not deprecated", function () {
const classId = WorkspaceClasses.selectClassForRegular("g1-large", "g1-standard", config);
expect(classId).to.be.equal("g1-large");
});

it("prebuild does not have more resources, return substitute", function () {
const classId = WorkspaceClasses.selectClassForRegular("g1-standard", "g1-large", config);
expect(classId).to.be.equal("g1-large");
});

it("prebuild does not have more resources, substitute unknown", function () {
const classId = WorkspaceClasses.selectClassForRegular("g1-standard", "g1-unknown", config);
expect(classId).to.be.equal("g1-standard");
});

it("substitute is not acceptable", function () {
const classId = WorkspaceClasses.selectClassForRegular("g1-large", "g1-standard", config);
expect(classId).to.be.equal("g1-large");
});
});
});
93 changes: 93 additions & 0 deletions components/server/src/workspace/workspace-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
* See License-AGPL.txt in the project root for license information.
*/

import { WorkspaceDB } from "@gitpod/gitpod-db/lib";
import { User, Workspace } from "@gitpod/gitpod-protocol";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
import { EntitlementService } from "../billing/entitlement-service";

export type WorkspaceClassesConfig = [WorkspaceClassConfig];

Expand Down Expand Up @@ -111,4 +115,93 @@ export namespace WorkspaceClasses {
);
}
}

/**
* Gets the workspace class of the prebuild
* If the class is not supported anymore undefined will be returned
* @param ctx
* @param workspace
* @param db
* @param classes
*/
export async function getFromPrebuild(
ctx: TraceContext,
workspace: Workspace,
db: WorkspaceDB,
): Promise<string | undefined> {
const span = TraceContext.startSpan("getFromPrebuild", ctx);
try {
if (!workspace.basedOnPrebuildId) {
return undefined;
}

const prebuild = await db.findPrebuildByID(workspace.basedOnPrebuildId);
if (!prebuild) {
return undefined;
}

const buildWorkspaceInstance = await db.findCurrentInstance(prebuild.buildWorkspaceId);
return buildWorkspaceInstance?.workspaceClass;
} finally {
span.finish();
}
}

/**
* @param user
* @param classes
* @param entitlementService
*/
export async function getConfiguredOrUpgradeFromLegacy(
user: User,
classes: WorkspaceClassesConfig,
entitlementService: EntitlementService,
): Promise<string> {
if (user.additionalData?.workspaceClasses?.regular) {
return user.additionalData?.workspaceClasses?.regular;
}

let workspaceClass = WorkspaceClasses.getDefaultId(classes);
if (await entitlementService.userGetsMoreResources(user)) {
workspaceClass = WorkspaceClasses.getMoreResourcesIdOrDefault(classes);
}

return workspaceClass;
}

/**
* @param currentClassId
* @param substituteClassId
* @param classes
*/
export function selectClassForRegular(
currentClassId: string,
substituteClassId: string | undefined,
classes: WorkspaceClassesConfig,
): string {
if (currentClassId === substituteClassId) {
return currentClassId;
}

const current = classes.find((c) => c.id === currentClassId);
let substitute = classes.find((c) => c.id === substituteClassId);

if (current?.marker?.moreResources) {
if (substitute?.marker?.moreResources) {
return substitute?.id;
} else {
if (current.deprecated) {
return getMoreResourcesIdOrDefault(classes);
} else {
return current.id;
}
}
} else {
if (substitute?.id) {
return substitute.id;
} else {
return getDefaultId(classes);
}
}
}
}
Loading