Skip to content

feat: notify on workspace update #15979

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 10 commits into from
Jan 2, 2025
Merged

Conversation

DanielleMaywood
Copy link
Contributor

@DanielleMaywood DanielleMaywood commented Dec 30, 2024

Relates to #15845

When the /workspace/<name>/builds endpoint is hit, we check if the requested template version is different to the previously used template version. If these values differ, we can assume that the workspace has been manually updated and send the appropriate notification. Automatic updates happen in the lifecycle executor and bypasses this endpoint entirely.

@DanielleMaywood DanielleMaywood marked this pull request as ready for review December 31, 2024 10:05
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Looks good for the most part, although I'd suggest using a transaction. Feel free to consider all other comments optional 👍🏻.

@@ -327,6 +328,16 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
return
}

previousWorkspaceBuild, err := api.Database.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspace.ID)
Copy link
Member

Choose a reason for hiding this comment

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

This is racy since we're not in a transaction. There could be an intermediate build between this one and the actual build (unlikely).

This and builder.Build could be moved closer to each other and wrapped inside a tx to eliminate the edge case, though (the same is done in createWorkspace so should be fine). Or since the builder already fetches the previous build, perhaps the deduction of "template changed" can be moved in there and exposed by some means.

(Not looking to complicate this so feel free to ignore, but yet another alternative is to move the notification logic to wsbuilder, although that may need to rely on initiator vs the signal of using this endpoint. The initiator could be you, or it could be an admin, etc.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the likelihood of a race is very minimal, but even if it should happen the notifier would handle de-duplication anyways.

I also think moving the notification logic into wsbuilder is at the wrong abstraction level.

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with not moving the notification logic, but why would we not remove the race, even if there's only a minimal chance? From my POV there's two factors I want to protect against:

  1. Potentially missing a notification because of the race (yes, minimal and unlikely, but still).
  2. Now that we're introduced previousWorkspaceBuild data into this endpoint, future changes may rely upon it in ways we haven't thought of here, this consideration may be important there as well and less likely to be spotted.

@@ -428,6 +445,79 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
httpapi.Write(ctx, rw, http.StatusCreated, apiBuild)
}

func (api *API) notifyWorkspaceUpdated(ctx context.Context, workspaceID uuid.UUID) {
Copy link
Member

Choose a reason for hiding this comment

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

It seems a bit unfortunate that this method needs to redo a bunch of queries just to get the message across, AFAICT we already have most of the data available in the endpoint calling out to this method.

Not going to enforce a change here, but this could be made more specific for the use-case (for the time-being).

(Additionally, if moved inside wsbuilder I believe we would have pretty much all of the data available.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I totally agree, I've tried to reduce the amount of queries it makes.

@@ -327,6 +328,16 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
return
}

previousWorkspaceBuild, err := api.Database.GetLatestWorkspaceBuildByWorkspaceID(ctx, workspace.ID)
Copy link
Member

Choose a reason for hiding this comment

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

I'm not convinced if this is the right place to send the notification. We should send it after the successful update, so I would target the provisioner server?

Copy link
Member

@mafredri mafredri Dec 31, 2024

Choose a reason for hiding this comment

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

Good point. I think we can tweak this via wording.

For instance, this would be pretty flexible and let us keep the notification here:

A new workspace build to update your workspace to template version 'boo' has been manually created/initiated.

Personally I think it's more valuable here as you know immediately this has been triggered and since the update happens here whether or not the build is successful.

Copy link
Member

Choose a reason for hiding this comment

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

Personally I think it's more valuable here as you know immediately this has been triggered and since the update happens here whether or not the build is successful.

Fair 👍 A user could cancel the request.

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

A few minor comments/suggestions but LGTM.

@DanielleMaywood DanielleMaywood merged commit f3fe3bc into main Jan 2, 2025
30 checks passed
@DanielleMaywood DanielleMaywood deleted the dm-workspace-update-notify branch January 2, 2025 12:19
@github-actions github-actions bot locked and limited conversation to collaborators Jan 2, 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.

3 participants