Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: run a terraform plan before creating workspaces with the given template parameters #1732

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 11 commits into from
Jun 1, 2022

Conversation

deansheather
Copy link
Member

@deansheather deansheather commented May 25, 2022

Adds a new provisioner job type template_version_plan which is for running a plan against a template version with custom parameters, but without having to make a workspace first. This will be used in coder create so we can verify that the workspace creation will start successfully and not fail quickly due to bad parameter values (leaving an inconsolable workspace state).

Subtasks

  • Add template_version_plan provisioner job type to database
  • Add POST /templateversions/:template_version_id/plan endpoint and codersdk methods
  • Add endpoints to monitor template version plan logs and cancel early (talking to Kyle in Slack about what to do here to avoid duplication)
  • Add tests for template plan endpoint
  • Add provisionerd protobuf types for TemplateVersionPlan
  • Dispatch template_version_plan provisioner jobs => TemplateVersionPlan protobuf jobs
  • Add provisionerd job handling for TemplateVersionPlan jobs
  • Add tests for provisionerd job handling
  • Add template version plan to coder create before creating the workspace
    • Add the plan
    • Wait for the plan
    • Handle logs and cancellation
    • Check for parameter validation errors and reprompt for parameters
  • Add tests for coder create plan
  • Add workspace name to the plan for name validation checks

Future:

  • Detect parameter validation errors and reprompt users instead of making them run the command again
  • Integrate the plan endpoint into the frontend

Screenshots

Successful validation:
Code_4UyXd6vUMP

Unsuccessful validation/plan:
Code_TPxhFaf7Vv

Fixes #831

@deansheather deansheather self-assigned this May 25, 2022
@deansheather deansheather requested a review from kylecarbs May 27, 2022 09:58
@deansheather deansheather marked this pull request as ready for review May 27, 2022 19:50
@deansheather deansheather requested a review from a team May 27, 2022 19:50
@deansheather deansheather requested a review from a team as a code owner May 27, 2022 20:45
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

I haven't tested this out myself, but it looks solid to me.

Comment on lines 173 to 174
// We use the workspace RBAC check since we don't want to allow plans if the
// user can't create workspaces.
Copy link
Member

Choose a reason for hiding this comment

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

Technically correct - the best kind of correct.

Comment on lines 1 to 9
-- It's not possible to drop enum values from enum types, so the UP has "IF NOT
-- EXISTS".

-- Delete all jobs that use the new enum value.
DELETE FROM
provisioner_jobs
WHERE
provisioner_job_type = 'template_version_plan'
;
Copy link
Member

Choose a reason for hiding this comment

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

⚠️ reminder: rename me ⚠️

Comment on lines 1 to 2
ALTER TYPE provisioner_job_type
ADD VALUE IF NOT EXISTS 'template_version_plan';
Copy link
Member

Choose a reason for hiding this comment

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

⚠️ reminder: rename me ⚠️

Comment on lines +197 to +198
// TODO (Dean): reprompt for parameter values if we deem it to
// be a validation error
Copy link
Member

Choose a reason for hiding this comment

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

TODO: open an issue

Copy link
Member Author

Choose a reason for hiding this comment

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

Opened two issues:

#1872 - for integrating with frontend
#1873 - for reprompting for values in CLI

}
transition, err := convertWorkspaceTransition(workspaceBuild.Transition)
if err != nil {
return nil, failJob(fmt.Sprint("convert workspace transition: %w", err))
return nil, failJob(fmt.Sprintf("convert workspace transition: %s", err))
Copy link
Member

Choose a reason for hiding this comment

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

No wrapping? I guess this gets protobuffed in the end, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Can't wrap with Sprintf, and these just get condensed to strings sadly :(

@deansheather deansheather changed the title feat: add new provisioner job type template_version_plan feat: run a terraform plan before creating workspaces with the given template parameters May 27, 2022
@deansheather deansheather requested review from a team and removed request for a team May 28, 2022 10:21
Comment on lines 180 to 183
var req codersdk.CreateTemplateVersionPlanRequest
if !httpapi.Read(rw, r, &req) {
return
}
Copy link
Member

Choose a reason for hiding this comment

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

As a user, I could spam this ~1000 times and take down a Coder deployment for the day. The same could be said for workspaces, but we have issues tracking quotas for those.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK so I'll just add details to those issues that whatever fix we do needs to apply to this endpoint too

Copy link
Member Author

Choose a reason for hiding this comment

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

Could you link the issue? I can't seem to find it after searching "rate limit" and "quota"

Copy link
Member

Choose a reason for hiding this comment

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

Ahh my bad for being slow here.

Can we limit the parallelization here? This is a remarkably easy attack vector that we don't seem to have a solid plan to fix.

Copy link
Member Author

Choose a reason for hiding this comment

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

That seems out of scope for this PR because we have the same issue with regular workspace builds and template imports which also needs to be fixed. It'd make sense to come up with a good solution that covers all of these job types

Copy link
Member Author

Choose a reason for hiding this comment

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

In that case, what's the mechanism we use to block multiple workspace builds so we can implement here

Copy link
Member

Choose a reason for hiding this comment

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

Multiple builds cannot occur for the same workspace in parallel because they are stateful.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do we have any protection across multiple builds across multiple workspaces on the same user?

Copy link
Member

Choose a reason for hiding this comment

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

Nope, but it's less of a problem because that'd be extremely easy to detect malice via our current API. An admin could list workspaces to easily detect who's the malicious actor.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's fair, but I still think that a global solution should be applied to all jobs simultaneously. You said earlier there was a ticket about this, could you link it so I can add context to it? I will try to tackle it before community MVP

Comment on lines 257 to 261
var input templateVersionPlanJob
err = json.Unmarshal(job.Input, &input)
if err != nil {
return nil, failJob(fmt.Sprintf("unmarshal job input %q: %s", job.Input, err))
}
Copy link
Member

Choose a reason for hiding this comment

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

It scares me to add another job with such similar scope to the others, but I understand why.

It'd be helpful if you explained why we couldn't use the workspace_build job to do similarly. I believe it'd just require not being attached to a literal workspace build, which seems like a reasonable change anyways.

Copy link
Member Author

Choose a reason for hiding this comment

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

We need to have a separate job for this because the job endpoints have different schemas and different endpoints to access the job details, not a unified /jobs endpoint. You've said in the past that you'd like to split these different job types apart more in the future (so they don't share DB tables for metadata etc.), so merging these two jobs together would be counterintuitive to that goal

Copy link
Member

Choose a reason for hiding this comment

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

The inputs are slightly different, but the outputs are the same, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Effectively, yes

Copy link
Member

Choose a reason for hiding this comment

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

It's probably worth opening a ticket considering merging those job types, or at least stating what the impl would look like to merge them. I'm always nervous about adding more job types.

@deansheather deansheather requested a review from kylecarbs May 31, 2022 17:35
return err
return xerrors.Errorf("begin workspace dry-run: %w", err)
}
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "Planning workspace...")
Copy link
Member Author

Choose a reason for hiding this comment

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

I kept the language "Planning workspace" because I couldn't come up with something that made sense with the word "dry-run"....

"Dry-running workspace"

"Running workspace dry-run"

etc.

@deansheather deansheather merged commit 6be8a37 into main Jun 1, 2022
@deansheather deansheather deleted the template-version-plan branch June 1, 2022 14:44
kylecarbs pushed a commit that referenced this pull request Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Workspaces can be created with invalid values, then cannot be deleted
4 participants