Skip to content

feat(coderd): add filters and fix template for provisioner daemons #16558

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 1 commit into from
Feb 14, 2025
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
2 changes: 1 addition & 1 deletion cli/testdata/coder_provisioner_list_--output_json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"previous_job": {
"id": "======[workspace build job ID]======",
"status": "succeeded",
"template_name": "",
"template_name": "test-template",
"template_icon": "",
"template_display_name": ""
},
Expand Down
37 changes: 37 additions & 0 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 68 additions & 8 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3931,7 +3931,7 @@ func (q *FakeQuerier) GetProvisionerDaemonsByOrganization(_ context.Context, arg
return daemons, nil
}

func (q *FakeQuerier) GetProvisionerDaemonsWithStatusByOrganization(_ context.Context, arg database.GetProvisionerDaemonsWithStatusByOrganizationParams) ([]database.GetProvisionerDaemonsWithStatusByOrganizationRow, error) {
func (q *FakeQuerier) GetProvisionerDaemonsWithStatusByOrganization(ctx context.Context, arg database.GetProvisionerDaemonsWithStatusByOrganizationParams) ([]database.GetProvisionerDaemonsWithStatusByOrganizationRow, error) {
err := validateDatabaseType(arg)
if err != nil {
return nil, err
Expand Down Expand Up @@ -3981,6 +3981,31 @@ func (q *FakeQuerier) GetProvisionerDaemonsWithStatusByOrganization(_ context.Co
status = database.ProvisionerDaemonStatusIdle
}
}
var currentTemplate database.Template
if currentJob.ID != uuid.Nil {
var input codersdk.ProvisionerJobInput
err := json.Unmarshal(currentJob.Input, &input)
if err != nil {
return nil, err
}
if input.WorkspaceBuildID != nil {
b, err := q.getWorkspaceBuildByIDNoLock(ctx, *input.WorkspaceBuildID)
if err != nil {
return nil, err
}
input.TemplateVersionID = &b.TemplateVersionID
}
if input.TemplateVersionID != nil {
v, err := q.getTemplateVersionByIDNoLock(ctx, *input.TemplateVersionID)
if err != nil {
return nil, err
}
currentTemplate, err = q.getTemplateByIDNoLock(ctx, v.TemplateID.UUID)
if err != nil {
return nil, err
}
}
}

var previousJob database.ProvisionerJob
for _, job := range q.provisionerJobs {
Expand All @@ -3997,6 +4022,31 @@ func (q *FakeQuerier) GetProvisionerDaemonsWithStatusByOrganization(_ context.Co
}
}
}
var previousTemplate database.Template
if previousJob.ID != uuid.Nil {
var input codersdk.ProvisionerJobInput
err := json.Unmarshal(previousJob.Input, &input)
if err != nil {
return nil, err
}
if input.WorkspaceBuildID != nil {
b, err := q.getWorkspaceBuildByIDNoLock(ctx, *input.WorkspaceBuildID)
if err != nil {
return nil, err
}
input.TemplateVersionID = &b.TemplateVersionID
}
if input.TemplateVersionID != nil {
v, err := q.getTemplateVersionByIDNoLock(ctx, *input.TemplateVersionID)
if err != nil {
return nil, err
}
previousTemplate, err = q.getTemplateByIDNoLock(ctx, v.TemplateID.UUID)
if err != nil {
return nil, err
}
}
}

// Get the provisioner key name
var keyName string
Expand All @@ -4008,20 +4058,30 @@ func (q *FakeQuerier) GetProvisionerDaemonsWithStatusByOrganization(_ context.Co
}

rows = append(rows, database.GetProvisionerDaemonsWithStatusByOrganizationRow{
ProvisionerDaemon: daemon,
Status: status,
KeyName: keyName,
CurrentJobID: uuid.NullUUID{UUID: currentJob.ID, Valid: currentJob.ID != uuid.Nil},
CurrentJobStatus: database.NullProvisionerJobStatus{ProvisionerJobStatus: currentJob.JobStatus, Valid: currentJob.ID != uuid.Nil},
PreviousJobID: uuid.NullUUID{UUID: previousJob.ID, Valid: previousJob.ID != uuid.Nil},
PreviousJobStatus: database.NullProvisionerJobStatus{ProvisionerJobStatus: previousJob.JobStatus, Valid: previousJob.ID != uuid.Nil},
ProvisionerDaemon: daemon,
Status: status,
KeyName: keyName,
CurrentJobID: uuid.NullUUID{UUID: currentJob.ID, Valid: currentJob.ID != uuid.Nil},
CurrentJobStatus: database.NullProvisionerJobStatus{ProvisionerJobStatus: currentJob.JobStatus, Valid: currentJob.ID != uuid.Nil},
CurrentJobTemplateName: currentTemplate.Name,
CurrentJobTemplateDisplayName: currentTemplate.DisplayName,
CurrentJobTemplateIcon: currentTemplate.Icon,
PreviousJobID: uuid.NullUUID{UUID: previousJob.ID, Valid: previousJob.ID != uuid.Nil},
PreviousJobStatus: database.NullProvisionerJobStatus{ProvisionerJobStatus: previousJob.JobStatus, Valid: previousJob.ID != uuid.Nil},
PreviousJobTemplateName: previousTemplate.Name,
PreviousJobTemplateDisplayName: previousTemplate.DisplayName,
PreviousJobTemplateIcon: previousTemplate.Icon,
})
}

slices.SortFunc(rows, func(a, b database.GetProvisionerDaemonsWithStatusByOrganizationRow) int {
return a.ProvisionerDaemon.CreatedAt.Compare(b.ProvisionerDaemon.CreatedAt)
})

if arg.Limit.Valid && arg.Limit.Int32 > 0 && len(rows) > int(arg.Limit.Int32) {
rows = rows[:arg.Limit.Int32]
}

return rows, nil
}

Expand Down
2 changes: 2 additions & 0 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 46 additions & 21 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 23 additions & 6 deletions coderd/database/queries/provisionerdaemons.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ SELECT
current_job.job_status AS current_job_status,
previous_job.id AS previous_job_id,
previous_job.job_status AS previous_job_status,
COALESCE(tmpl.name, ''::text) AS current_job_template_name,
COALESCE(tmpl.display_name, ''::text) AS current_job_template_display_name,
COALESCE(tmpl.icon, ''::text) AS current_job_template_icon
COALESCE(current_template.name, ''::text) AS current_job_template_name,
COALESCE(current_template.display_name, ''::text) AS current_job_template_display_name,
COALESCE(current_template.icon, ''::text) AS current_job_template_icon,
COALESCE(previous_template.name, ''::text) AS previous_job_template_name,
COALESCE(previous_template.display_name, ''::text) AS previous_job_template_display_name,
COALESCE(previous_template.icon, ''::text) AS previous_job_template_icon
FROM
provisioner_daemons pd
JOIN
Expand All @@ -72,16 +75,30 @@ LEFT JOIN
LIMIT 1
)
)
-- Current job information.
LEFT JOIN
template_versions version ON version.id = (current_job.input->>'template_version_id')::uuid
workspace_builds current_build ON current_build.id = CASE WHEN current_job.input ? 'workspace_build_id' THEN (current_job.input->>'workspace_build_id')::uuid END
LEFT JOIN
templates tmpl ON tmpl.id = version.template_id
-- We should always have a template version, either explicitly or implicitly via workspace build.
template_versions current_version ON current_version.id = CASE WHEN current_job.input ? 'template_version_id' THEN (current_job.input->>'template_version_id')::uuid ELSE current_build.template_version_id END
LEFT JOIN
templates current_template ON current_template.id = current_version.template_id
-- Previous job information.
LEFT JOIN
workspace_builds previous_build ON previous_build.id = CASE WHEN previous_job.input ? 'workspace_build_id' THEN (previous_job.input->>'workspace_build_id')::uuid END
LEFT JOIN
-- We should always have a template version, either explicitly or implicitly via workspace build.
template_versions previous_version ON previous_version.id = CASE WHEN previous_job.input ? 'template_version_id' THEN (previous_job.input->>'template_version_id')::uuid ELSE previous_build.template_version_id END
LEFT JOIN
templates previous_template ON previous_template.id = previous_version.template_id
Comment on lines +78 to +93
Copy link
Member Author

@mafredri mafredri Feb 13, 2025

Choose a reason for hiding this comment

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

Review: I completed this because it was missing some things. But I wonder if we need to take RBAC into consideration now?

The endpoint previously returned a job ID/status only, now it also returns information regarding the template that job is associated with.

This has some implications considering regular members are allowed to view provisioner daemon resources. WDYT @johnstcn?

I'm not particularly fond of it, but we could add the same authorization to the API endpoint as for jobs:

	// For now, only owners and template admins can access provisioner jobs.
	if !api.Authorize(r, policy.ActionRead, rbac.ResourceProvisionerJobs.InOrg(org.ID)) {
		httpapi.ResourceNotFound(rw)
		return nil, false
	}

Copy link
Member

Choose a reason for hiding this comment

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

I don't know how sensitive template or template_version fields can be, but a red flag for me would be if this could cross org boundaries.

I think some further questions we need to answer before we can even update the RBAC rules are:

  • Who can view a template import job not linked to any workspace? (Suggestion: anyone who can read the linked template)
  • Who can view a workspace build job? (Suggestion: anyone who can read the linked workspace)

To be safe, I think we should promote the same level of access control as for provisioner jobs for now. We should add a follow-up issue to take provisioner job ownership and template-level ACLs into account.

Does that sound reasonable to you?

WHERE
pd.organization_id = @organization_id::uuid
AND (COALESCE(array_length(@ids::uuid[], 1), 0) = 0 OR pd.id = ANY(@ids::uuid[]))
AND (@tags::tagset = 'null'::tagset OR provisioner_tagset_contains(pd.tags::tagset, @tags::tagset))
ORDER BY
pd.created_at ASC;
pd.created_at ASC
LIMIT
sqlc.narg('limit')::int;

-- name: DeleteOldProvisionerDaemons :exec
-- Delete provisioner daemons that have been created at least a week ago
Expand Down
Loading
Loading