Skip to content
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

feat: add migrations and queries to support prebuilds #16891

Merged
merged 77 commits into from
Apr 3, 2025
Merged

Conversation

SasSwart
Copy link
Contributor

@SasSwart SasSwart commented Mar 12, 2025

Depends on #16916 (change base to main once it is merged)

Closes coder/internal#514

This is one of several PRs to decompose the dk/prebuilds feature branch into separate PRs to merge into main.

SasSwart

This comment was marked as outdated.

@@ -12,6 +12,8 @@ const (
LockIDDBPurge
LockIDNotificationsReportGenerator
LockIDCryptoKeyRotation
LockIDReconcileTemplatePrebuilds
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need two separate locks because we'll have a wider reconciliation loop which must hold a global lock, and an inner lock which is used to protect the piece where we determine the state. The latter can be called outside of a reconciliation loop (as in the metrics collection).

@dannykopping dannykopping changed the base branch from main to prebuilds-system-user March 13, 2025 21:01
@dannykopping dannykopping force-pushed the prebuilds-db branch 2 times, most recently from 2024110 to 390a1fd Compare March 13, 2025 21:22
@evgeniy-scherbina
Copy link
Contributor

Do we want to add unit-tests on DB level in this PR to make sure VIEWs and SQL queries are correct?

@dannykopping
Copy link
Contributor

Do we want to add unit-tests on DB level in this PR to make sure VIEWs and SQL queries are correct?

Feels like overkill to me, since we have tests which rely on the views indirectly, but if you feel it'd add value then go ahead 👍

@SasSwart SasSwart force-pushed the prebuilds-db branch 3 times, most recently from 70ecdcc to 7e062e2 Compare March 17, 2025 08:14
@dannykopping dannykopping force-pushed the prebuilds-system-user branch from c92fb1b to e7e9c27 Compare March 17, 2025 12:12
Copy link
Member

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 on the RBAC checks. @spikecurtis no ResourceSystem is now used.

Resource<Type>.All() is used in almost all of these because there is no filter for orgs or a user. The reconciliation loop queries across all orgs.

The queries should succeed or fail as an entire set, rather then be filtered. Based on how they are used. In the future, if we add more views for prebuilds, we can update these queries to filter and apply the appropriate checks. Similar to how we filter workspaces & templates today.

Comment on lines +1148 to +1160
workspaceObject := rbac.ResourceWorkspace.WithOwner(arg.NewUserID.String()).InOrg(preset.OrganizationID)
err = q.authorizeContext(ctx, policy.ActionCreate, workspaceObject.RBACObject())
if err != nil {
return empty, err
}

tpl, err := q.GetTemplateByID(ctx, preset.TemplateID.UUID)
if err != nil {
return empty, xerrors.Errorf("verify template by id: %w", err)
}
if err := q.authorizeContext(ctx, policy.ActionUse, tpl); err != nil {
return empty, xerrors.Errorf("use template for workspace: %w", err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just leave a comment this should match InsertWorkspace

Comment on lines +1186 to +1191
func (q *querier) CountInProgressPrebuilds(ctx context.Context) ([]database.CountInProgressPrebuildsRow, error) {
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceWorkspace.All()); err != nil {
return nil, err
}
return q.db.CountInProgressPrebuilds(ctx)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This query spans all organizations, and prebuilds are just workspaces 👍

Comment on lines +2200 to +2206
func (q *querier) GetPresetsBackoff(ctx context.Context, lookback time.Time) ([]database.GetPresetsBackoffRow, error) {
// GetPresetsBackoff returns a list of template version presets along with metadata such as the number of failed prebuilds.
if err := q.authorizeContext(ctx, policy.ActionViewInsights, rbac.ResourceTemplate.All()); err != nil {
return nil, err
}
return q.db.GetPresetsBackoff(ctx, lookback)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spikecurtis we decided to just lump in the preset metrics with template insights. Since template insights are metrics related to a template, prebuild metrics metadata is essentially the same imo.

@SasSwart SasSwart merged commit 99c6f23 into main Apr 3, 2025
34 checks passed
@SasSwart SasSwart deleted the prebuilds-db branch April 3, 2025 08:58
@github-actions github-actions bot locked and limited conversation to collaborators Apr 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for prebuilds to the database
5 participants