Skip to content

feat: archive template versions to hide them from the ui #10086

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

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
fc715fd
prune template versions
Emyrk Oct 5, 2023
611dac1
Implement fakes and mocks
Emyrk Oct 5, 2023
1de0a5d
Exclude template active versions
Emyrk Oct 5, 2023
3e5f91d
Delete versions are unusable
Emyrk Oct 5, 2023
9200779
Merge remote-tracking branch 'origin/main' into stevenmasley/soft_del…
Emyrk Oct 5, 2023
6e6fbc3
migration bump
Emyrk Oct 5, 2023
2e2c092
Spaces to tabs
Emyrk Oct 5, 2023
7f3d805
add template version prune command
Emyrk Oct 9, 2023
02aa085
Rename to archive
Emyrk Oct 9, 2023
9a416c3
Rename to "archive"
Emyrk Oct 9, 2023
c82ff9a
Merge remote-tracking branch 'origin/main' into stevenmasley/soft_del…
Emyrk Oct 9, 2023
c06d63a
Bump migration file
Emyrk Oct 9, 2023
460b0d2
Make update golden files
Emyrk Oct 9, 2023
d9bfea5
Fix swagger id
Emyrk Oct 9, 2023
655c906
Fix swagger accept json
Emyrk Oct 9, 2023
d4e54b2
Make gen
Emyrk Oct 9, 2023
938f256
Add unarchive
Emyrk Oct 9, 2023
7498c73
Add unarchive api
Emyrk Oct 9, 2023
685512a
Add cli command to unarchive a version
Emyrk Oct 9, 2023
f446ae5
Linting
Emyrk Oct 9, 2023
a9ff9d6
Update golden files
Emyrk Oct 9, 2023
b83a15e
Move cmd commands, allow archiuve deleted
Emyrk Oct 9, 2023
0ecbb79
update golden files
Emyrk Oct 9, 2023
bb3571b
Fix cli errors
Emyrk Oct 9, 2023
533e913
Move cmd
Emyrk Oct 9, 2023
02555b1
Implement fake
Emyrk Oct 9, 2023
1606b99
Linting and gen
Emyrk Oct 9, 2023
3818a57
fixup! Linting and gen
Emyrk Oct 9, 2023
48e44b6
fix: properly trim spaces so multi-line shebang executes (#10146)
kylecarbs Oct 9, 2023
3c97681
chore: reorganize storybook (#10144)
aslilac Oct 9, 2023
f01a4b3
chore: bump the golang-x group with 6 updates (#10128)
dependabot[bot] Oct 9, 2023
e9b4d15
chore: bump google.golang.org/api from 0.143.0 to 0.145.0 (#10130)
dependabot[bot] Oct 9, 2023
4236fc8
ci: bump the github-actions group with 2 updates (#10131)
dependabot[bot] Oct 9, 2023
8c86767
chore: add icons for popular programming languages (#10141)
aslilac Oct 9, 2023
02bcd20
chore: run `go mod tidy`
coadler Oct 9, 2023
91a2025
feat: add `external-auth` cli (#10052)
kylecarbs Oct 9, 2023
54509e5
feat: allow storing extra oauth token properties in the database (#10…
kylecarbs Oct 9, 2023
3fe8966
bump migration number:
Emyrk Oct 10, 2023
44bbbfe
Swagger annotations
Emyrk Oct 10, 2023
f0294c3
Merge remote-tracking branch 'origin/main' into stevenmasley/soft_del…
Emyrk Oct 10, 2023
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
Prev Previous commit
Next Next commit
Rename to archive
  • Loading branch information
Emyrk committed Oct 9, 2023
commit 02aa08524f632976fc54d405658d9f9747a565ad
2 changes: 1 addition & 1 deletion cli/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (r *RootCmd) templates() *clibase.Cmd {
r.templateVersions(),
r.templateDelete(),
r.templatePull(),
r.templatePrune(),
r.archiveTemplateVersions(),
},
}

Expand Down
21 changes: 10 additions & 11 deletions cli/templateprune.go → cli/templateversionarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,30 @@ import (
"strings"
"time"

"golang.org/x/xerrors"

"github.com/coder/pretty"
"golang.org/x/xerrors"

"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
)

func (r *RootCmd) templatePrune() *clibase.Cmd {
func (r *RootCmd) archiveTemplateVersions() *clibase.Cmd {
var (
all clibase.Bool
)
client := new(codersdk.Client)
cmd := &clibase.Cmd{
Use: "prune [name...]",
Short: "Prune unused failed template versions from a given template(s)",
Use: "archive [name...] ",
Short: "Archive unused failed template versions from a given template(s)",
Middleware: clibase.Chain(
r.InitClient(client),
),
Options: clibase.OptionSet{
cliui.SkipPromptOption(),
clibase.Option{
Name: "all",
Description: "Include all unused template versions. By default, only failed template versions are pruned.",
Description: "Include all unused template versions. By default, only failed template versions are archived.",
Flag: "all",
Value: &all,
},
Expand Down Expand Up @@ -67,9 +66,9 @@ func (r *RootCmd) templatePrune() *clibase.Cmd {
templateNames = append(templateNames, template.Name)
}

// Confirm prune of the template.
// Confirm archive of the template.
_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: fmt.Sprintf("Prune template versions of these templates: %s?", pretty.Sprint(cliui.DefaultStyles.Code, strings.Join(templateNames, ", "))),
Text: fmt.Sprintf("Archive template versions of these templates: %s?", pretty.Sprint(cliui.DefaultStyles.Code, strings.Join(templateNames, ", "))),
IsConfirm: true,
Default: cliui.ConfirmNo,
})
Expand All @@ -78,13 +77,13 @@ func (r *RootCmd) templatePrune() *clibase.Cmd {
}

for _, template := range templates {
resp, err := client.PruneTemplateVersions(ctx, template.ID, all.Value())
resp, err := client.ArchiveTemplateVersions(ctx, template.ID, all.Value())
if err != nil {
return xerrors.Errorf("delete template %q: %w", template.Name, err)
return xerrors.Errorf("archive template versions for %q: %w", template.Name, err)
}

_, _ = fmt.Fprintln(
inv.Stdout, fmt.Sprintf("Deleted %s versions from "+pretty.Sprint(cliui.DefaultStyles.Keyword, template.Name)+" at "+cliui.Timestamp(time.Now()), len(resp.DeletedIDs)),
inv.Stdout, fmt.Sprintf("Archive %s versions from "+pretty.Sprint(cliui.DefaultStyles.Keyword, template.Name)+" at "+cliui.Timestamp(time.Now()), len(resp.ArchivedIDs)),
)

if ok, _ := inv.ParsedFlags().GetBool("verbose"); err == nil && ok {
Expand Down
2 changes: 1 addition & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ func New(options *Options) *API {
r.Delete("/", api.deleteTemplate)
r.Patch("/", api.patchTemplateMeta)
r.Route("/versions", func(r chi.Router) {
r.Delete("/prune", api.deletePruneTemplateVersions)
r.Post("/archive", api.postArchiveTemplateVersions)
r.Get("/", api.templateVersionsByTemplate)
r.Patch("/", api.patchActiveTemplateVersion)
r.Get("/{templateversionname}", api.templateVersionByName)
Expand Down
22 changes: 11 additions & 11 deletions coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,17 @@ func (q *querier) AllUserIDs(ctx context.Context) ([]uuid.UUID, error) {
return q.db.AllUserIDs(ctx)
}

func (q *querier) ArchiveUnusedTemplateVersions(ctx context.Context, arg database.ArchiveUnusedTemplateVersionsParams) ([]uuid.UUID, error) {
tpl, err := q.db.GetTemplateByID(ctx, arg.TemplateID)
if err != nil {
return nil, err
}
if err := q.authorizeContext(ctx, rbac.ActionUpdate, tpl); err != nil {
return nil, err
}
return q.db.ArchiveUnusedTemplateVersions(ctx, arg)
}

func (q *querier) CleanTailnetCoordinators(ctx context.Context) error {
if err := q.authorizeContext(ctx, rbac.ActionDelete, rbac.ResourceTailnetCoordinator); err != nil {
return err
Expand Down Expand Up @@ -2242,17 +2253,6 @@ func (q *querier) InsertWorkspaceResourceMetadata(ctx context.Context, arg datab
return q.db.InsertWorkspaceResourceMetadata(ctx, arg)
}

func (q *querier) PruneUnusedTemplateVersions(ctx context.Context, arg database.PruneUnusedTemplateVersionsParams) ([]uuid.UUID, error) {
tpl, err := q.db.GetTemplateByID(ctx, arg.TemplateID)
if err != nil {
return nil, err
}
if err := q.authorizeContext(ctx, rbac.ActionUpdate, tpl); err != nil {
return nil, err
}
return q.db.PruneUnusedTemplateVersions(ctx, arg)
}

func (q *querier) RegisterWorkspaceProxy(ctx context.Context, arg database.RegisterWorkspaceProxyParams) (database.WorkspaceProxy, error) {
fetch := func(ctx context.Context, arg database.RegisterWorkspaceProxyParams) (database.WorkspaceProxy, error) {
return q.db.GetWorkspaceProxyByID(ctx, arg.ID)
Expand Down
134 changes: 67 additions & 67 deletions coderd/database/dbfake/dbfake.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,73 @@ func (q *FakeQuerier) AllUserIDs(_ context.Context) ([]uuid.UUID, error) {
return userIDs, nil
}

func (q *FakeQuerier) ArchiveUnusedTemplateVersions(_ context.Context, arg database.ArchiveUnusedTemplateVersionsParams) ([]uuid.UUID, error) {
err := validateDatabaseType(arg)
if err != nil {
return nil, err
}
q.mutex.Lock()
defer q.mutex.Unlock()
type latestBuild struct {
Number int32
Version uuid.UUID
}
latest := make(map[uuid.UUID]latestBuild)

for _, b := range q.workspaceBuilds {
v, ok := latest[b.WorkspaceID]
if ok || b.BuildNumber < v.Number {
// Not the latest
continue
}
latest[b.WorkspaceID] = latestBuild{
Number: b.BuildNumber,
Version: b.TemplateVersionID,
}
}

usedVersions := make(map[uuid.UUID]bool)
for _, l := range latest {
usedVersions[l.Version] = true
}
for _, tpl := range q.templates {
usedVersions[tpl.ActiveVersionID] = true
}

var deleted []uuid.UUID
for i, v := range q.templateVersions {
if v.Archived {
continue
}

if _, ok := usedVersions[v.ID]; !ok {
var job *database.ProvisionerJob
for i, j := range q.provisionerJobs {
if v.JobID == j.ID {
job = &q.provisionerJobs[i]
break
}
}

if arg.JobStatus.Valid {
if job.JobStatus != arg.JobStatus.ProvisionerJobStatus {
continue
}
}

if job.JobStatus == database.ProvisionerJobStatusRunning || job.JobStatus == database.ProvisionerJobStatusPending {
continue
}

v.Archived = true
q.templateVersions[i] = v
deleted = append(deleted, v.ID)
}
}

return deleted, nil
}

func (*FakeQuerier) CleanTailnetCoordinators(_ context.Context) error {
return ErrUnimplemented
}
Expand Down Expand Up @@ -5199,73 +5266,6 @@ func (q *FakeQuerier) InsertWorkspaceResourceMetadata(_ context.Context, arg dat
return metadata, nil
}

func (q *FakeQuerier) PruneUnusedTemplateVersions(_ context.Context, arg database.PruneUnusedTemplateVersionsParams) ([]uuid.UUID, error) {
err := validateDatabaseType(arg)
if err != nil {
return nil, err
}
q.mutex.Lock()
defer q.mutex.Unlock()
type latestBuild struct {
Number int32
Version uuid.UUID
}
latest := make(map[uuid.UUID]latestBuild)

for _, b := range q.workspaceBuilds {
v, ok := latest[b.WorkspaceID]
if ok || b.BuildNumber < v.Number {
// Not the latest
continue
}
latest[b.WorkspaceID] = latestBuild{
Number: b.BuildNumber,
Version: b.TemplateVersionID,
}
}

usedVersions := make(map[uuid.UUID]bool)
for _, l := range latest {
usedVersions[l.Version] = true
}
for _, tpl := range q.templates {
usedVersions[tpl.ActiveVersionID] = true
}

var deleted []uuid.UUID
for i, v := range q.templateVersions {
if v.Deleted {
continue
}

if _, ok := usedVersions[v.ID]; !ok {
var job *database.ProvisionerJob
for _, j := range q.provisionerJobs {
if v.JobID == j.ID {
job = &j
break
}
}

if arg.JobStatus.Valid {
if job.JobStatus != arg.JobStatus.ProvisionerJobStatus {
continue
}
}

if job.JobStatus == database.ProvisionerJobStatusRunning || job.JobStatus == database.ProvisionerJobStatusPending {
continue
}

v.Deleted = true
q.templateVersions[i] = v
deleted = append(deleted, v.ID)
}
}

return deleted, nil
}

func (q *FakeQuerier) RegisterWorkspaceProxy(_ context.Context, arg database.RegisterWorkspaceProxyParams) (database.WorkspaceProxy, error) {
q.mutex.Lock()
defer q.mutex.Unlock()
Expand Down
14 changes: 7 additions & 7 deletions coderd/database/dbmetrics/dbmetrics.go

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

12 changes: 6 additions & 6 deletions coderd/database/dbmock/dbmock.go

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

4 changes: 2 additions & 2 deletions coderd/database/dump.sql

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
Expand Up @@ -4,7 +4,7 @@ BEGIN;
DROP VIEW template_version_with_user;

ALTER TABLE template_versions
DROP COLUMN deleted;
DROP COLUMN archived;

-- Restore the old version of the template_version_with_user view.
CREATE VIEW
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ BEGIN;
-- The view will be rebuilt with the new column
DROP VIEW template_version_with_user;

-- Hard deleting can cause reference issues with audit logs,
-- so we only support soft deleting this resource at this time.
-- Archived template versions are not visible or usable by default.
ALTER TABLE template_versions
ADD COLUMN deleted BOOLEAN NOT NULL DEFAULT FALSE;
ADD COLUMN archived BOOLEAN NOT NULL DEFAULT FALSE;

-- Restore the old version of the template_version_with_user view.
CREATE VIEW
Expand All @@ -18,8 +17,8 @@ SELECT
coalesce(visible_users.username, '') AS created_by_username
FROM
template_versions
LEFT JOIN
visible_users
LEFT JOIN
visible_users
ON
template_versions.created_by = visible_users.id;

Expand Down
6 changes: 3 additions & 3 deletions coderd/database/models.go

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

Loading