-
Notifications
You must be signed in to change notification settings - Fork 943
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
Changes from 1 commit
3e9ecc3
cf033aa
bafe1c1
cfe34af
0d03a39
4078fd1
8d04749
4b06c72
2fee23a
6b95e54
0b6d083
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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' | ||
; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ALTER TYPE provisioner_job_type | ||
ADD VALUE IF NOT EXISTS 'template_version_plan'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,6 +107,12 @@ type workspaceProvisionJob struct { | |
DryRun bool `json:"dry_run"` | ||
} | ||
|
||
// The input for a "template_version_plan" job. | ||
type templateVersionPlanJob struct { | ||
TemplateVersionID uuid.UUID `json:"template_version_id"` | ||
ParameterValues []database.ParameterValue `json:"parameter_values"` | ||
} | ||
|
||
// Implementation of the provisioner daemon protobuf server. | ||
type provisionerdServer struct { | ||
AccessURL *url.URL | ||
|
@@ -216,18 +222,15 @@ func (server *provisionerdServer) AcquireJob(ctx context.Context, _ *proto.Empty | |
if err != nil { | ||
return nil, failJob(fmt.Sprintf("compute parameters: %s", err)) | ||
} | ||
// Convert parameters to the protobuf type. | ||
protoParameters := make([]*sdkproto.ParameterValue, 0, len(parameters)) | ||
for _, computedParameter := range parameters { | ||
converted, err := convertComputedParameterValue(computedParameter) | ||
if err != nil { | ||
return nil, failJob(fmt.Sprintf("convert parameter: %s", err)) | ||
} | ||
protoParameters = append(protoParameters, converted) | ||
|
||
// Convert types to their corresponding protobuf types. | ||
protoParameters, err := convertComputedParameterValues(parameters) | ||
if err != nil { | ||
return nil, failJob(fmt.Sprintf("convert computed parameters to protobuf: %s", err)) | ||
} | ||
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)) | ||
} | ||
|
||
protoJob.Type = &proto.AcquiredJob_WorkspaceBuild_{ | ||
|
@@ -246,6 +249,45 @@ func (server *provisionerdServer) AcquireJob(ctx context.Context, _ *proto.Empty | |
}, | ||
}, | ||
} | ||
case database.ProvisionerJobTypeTemplateVersionPlan: | ||
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)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The inputs are slightly different, but the outputs are the same, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Effectively, yes There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
templateVersion, err := server.Database.GetTemplateVersionByID(ctx, input.TemplateVersionID) | ||
if err != nil { | ||
return nil, failJob(fmt.Sprintf("get template version: %s", err)) | ||
} | ||
|
||
// Compute parameters for the plan to consume. | ||
parameters, err := parameter.Compute(ctx, server.Database, parameter.ComputeScope{ | ||
TemplateImportJobID: templateVersion.JobID, | ||
OrganizationID: job.OrganizationID, | ||
TemplateID: templateVersion.TemplateID, | ||
UserID: user.ID, | ||
WorkspaceID: uuid.NullUUID{}, | ||
AdditionalParameterValues: input.ParameterValues, | ||
}, nil) | ||
if err != nil { | ||
return nil, failJob(fmt.Sprintf("compute parameters: %s", err)) | ||
} | ||
|
||
// Convert types to their corresponding protobuf types. | ||
protoParameters, err := convertComputedParameterValues(parameters) | ||
if err != nil { | ||
return nil, failJob(fmt.Sprintf("convert computed parameters to protobuf: %s", err)) | ||
} | ||
|
||
protoJob.Type = &proto.AcquiredJob_TemplatePlan_{ | ||
TemplatePlan: &proto.AcquiredJob_TemplatePlan{ | ||
ParameterValues: protoParameters, | ||
Metadata: &sdkproto.Provision_Metadata{ | ||
CoderUrl: server.AccessURL.String(), | ||
}, | ||
}, | ||
} | ||
case database.ProvisionerJobTypeTemplateVersionImport: | ||
protoJob.Type = &proto.AcquiredJob_TemplateImport_{ | ||
TemplateImport: &proto.AcquiredJob_TemplateImport{ | ||
|
@@ -716,6 +758,19 @@ func convertLogSource(logSource proto.LogSource) (database.LogSource, error) { | |
} | ||
} | ||
|
||
func convertComputedParameterValues(parameters []parameter.ComputedValue) ([]*sdkproto.ParameterValue, error) { | ||
protoParameters := make([]*sdkproto.ParameterValue, len(parameters)) | ||
for i, computedParameter := range parameters { | ||
converted, err := convertComputedParameterValue(computedParameter) | ||
if err != nil { | ||
return nil, xerrors.Errorf("convert parameter: %w", err) | ||
} | ||
protoParameters[i] = converted | ||
} | ||
|
||
return protoParameters, nil | ||
} | ||
|
||
func convertComputedParameterValue(param parameter.ComputedValue) (*sdkproto.ParameterValue, error) { | ||
var scheme sdkproto.ParameterDestination_Scheme | ||
switch param.DestinationScheme { | ||
|
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.
No wrapping? I guess this gets protobuffed in the end, right?
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.
Can't wrap with Sprintf, and these just get condensed to strings sadly :(