diff --git a/cli/cliui/resources.go b/cli/cliui/resources.go index 324d0f452ac67..de60466f3e472 100644 --- a/cli/cliui/resources.go +++ b/cli/cliui/resources.go @@ -44,7 +44,7 @@ func WorkspaceResources(writer io.Writer, resources []codersdk.WorkspaceResource if resource.Transition != database.WorkspaceTransitionStop { continue } - addressOnStop[resource.Address] = resource + addressOnStop[resource.Type+"."+resource.Name] = resource } // Displayed stores whether a resource has already been shown. // Resources can be stored with numerous states, which we @@ -75,24 +75,25 @@ func WorkspaceResources(writer io.Writer, resources []codersdk.WorkspaceResource // callers to hide resources eventually. continue } - if _, shown := displayed[resource.Address]; shown { + resourceAddress := resource.Type + "." + resource.Name + if _, shown := displayed[resourceAddress]; shown { // The same resource can have multiple transitions. continue } - displayed[resource.Address] = struct{}{} + displayed[resourceAddress] = struct{}{} // Sort agents by name for consistent output. sort.Slice(resource.Agents, func(i, j int) bool { return resource.Agents[i].Name < resource.Agents[j].Name }) - _, existsOnStop := addressOnStop[resource.Address] + _, existsOnStop := addressOnStop[resourceAddress] resourceState := "ephemeral" if existsOnStop { resourceState = "persistent" } // Display a line for the resource. tableWriter.AppendRow(table.Row{ - Styles.Bold.Render(resource.Type + "." + resource.Name), + Styles.Bold.Render(resourceAddress), Styles.Placeholder.Render(resourceState), "", }) diff --git a/cli/cliui/resources_test.go b/cli/cliui/resources_test.go index 95a7d7cb01864..571b49bb67746 100644 --- a/cli/cliui/resources_test.go +++ b/cli/cliui/resources_test.go @@ -42,17 +42,14 @@ func TestWorkspaceResources(t *testing.T) { disconnected := database.Now().Add(-4 * time.Second) go func() { err := cliui.WorkspaceResources(ptty.Output(), []codersdk.WorkspaceResource{{ - Address: "disk", Transition: database.WorkspaceTransitionStart, Type: "google_compute_disk", Name: "root", }, { - Address: "disk", Transition: database.WorkspaceTransitionStop, Type: "google_compute_disk", Name: "root", }, { - Address: "another", Transition: database.WorkspaceTransitionStart, Type: "google_compute_instance", Name: "dev", diff --git a/cmd/cliui/main.go b/cmd/cliui/main.go index 18612b63ba11a..d02b7cd60c1da 100644 --- a/cmd/cliui/main.go +++ b/cmd/cliui/main.go @@ -188,17 +188,14 @@ func main() { RunE: func(cmd *cobra.Command, args []string) error { disconnected := database.Now().Add(-4 * time.Second) return cliui.WorkspaceResources(cmd.OutOrStdout(), []codersdk.WorkspaceResource{{ - Address: "disk", Transition: database.WorkspaceTransitionStart, Type: "google_compute_disk", Name: "root", }, { - Address: "disk", Transition: database.WorkspaceTransitionStop, Type: "google_compute_disk", Name: "root", }, { - Address: "another", Transition: database.WorkspaceTransitionStart, Type: "google_compute_instance", Name: "dev", diff --git a/coderd/database/databasefake/databasefake.go b/coderd/database/databasefake/databasefake.go index 45f652d038653..8d227f7e6f9a7 100644 --- a/coderd/database/databasefake/databasefake.go +++ b/coderd/database/databasefake/databasefake.go @@ -1024,7 +1024,6 @@ func (q *fakeQuerier) InsertWorkspaceResource(_ context.Context, arg database.In CreatedAt: arg.CreatedAt, JobID: arg.JobID, Transition: arg.Transition, - Address: arg.Address, Type: arg.Type, Name: arg.Name, } diff --git a/coderd/database/dump.sql b/coderd/database/dump.sql index fe6b026abf19a..fb8621e2f2f3d 100644 --- a/coderd/database/dump.sql +++ b/coderd/database/dump.sql @@ -270,7 +270,6 @@ CREATE TABLE workspace_resources ( created_at timestamp with time zone NOT NULL, job_id uuid NOT NULL, transition workspace_transition NOT NULL, - address character varying(256) NOT NULL, type character varying(192) NOT NULL, name character varying(64) NOT NULL ); diff --git a/coderd/database/migrations/000004_jobs.up.sql b/coderd/database/migrations/000004_jobs.up.sql index 379857ed3abbd..bc1679ea4bae8 100644 --- a/coderd/database/migrations/000004_jobs.up.sql +++ b/coderd/database/migrations/000004_jobs.up.sql @@ -66,7 +66,6 @@ CREATE TABLE workspace_resources ( created_at timestamptz NOT NULL, job_id uuid NOT NULL REFERENCES provisioner_jobs (id) ON DELETE CASCADE, transition workspace_transition NOT NULL, - address varchar(256) NOT NULL, type varchar(192) NOT NULL, name varchar(64) NOT NULL, PRIMARY KEY (id) diff --git a/coderd/database/models.go b/coderd/database/models.go index 55dc014d82e66..a8d311194139e 100644 --- a/coderd/database/models.go +++ b/coderd/database/models.go @@ -438,7 +438,6 @@ type WorkspaceResource struct { CreatedAt time.Time `db:"created_at" json:"created_at"` JobID uuid.UUID `db:"job_id" json:"job_id"` Transition WorkspaceTransition `db:"transition" json:"transition"` - Address string `db:"address" json:"address"` Type string `db:"type" json:"type"` Name string `db:"name" json:"name"` } diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index b37c1e53c7ac8..97d1aa3a30d93 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -2475,7 +2475,7 @@ func (q *sqlQuerier) UpdateWorkspaceBuildByID(ctx context.Context, arg UpdateWor const getWorkspaceResourceByID = `-- name: GetWorkspaceResourceByID :one SELECT - id, created_at, job_id, transition, address, type, name + id, created_at, job_id, transition, type, name FROM workspace_resources WHERE @@ -2490,7 +2490,6 @@ func (q *sqlQuerier) GetWorkspaceResourceByID(ctx context.Context, id uuid.UUID) &i.CreatedAt, &i.JobID, &i.Transition, - &i.Address, &i.Type, &i.Name, ) @@ -2499,7 +2498,7 @@ func (q *sqlQuerier) GetWorkspaceResourceByID(ctx context.Context, id uuid.UUID) const getWorkspaceResourcesByJobID = `-- name: GetWorkspaceResourcesByJobID :many SELECT - id, created_at, job_id, transition, address, type, name + id, created_at, job_id, transition, type, name FROM workspace_resources WHERE @@ -2520,7 +2519,6 @@ func (q *sqlQuerier) GetWorkspaceResourcesByJobID(ctx context.Context, jobID uui &i.CreatedAt, &i.JobID, &i.Transition, - &i.Address, &i.Type, &i.Name, ); err != nil { @@ -2539,17 +2537,9 @@ func (q *sqlQuerier) GetWorkspaceResourcesByJobID(ctx context.Context, jobID uui const insertWorkspaceResource = `-- name: InsertWorkspaceResource :one INSERT INTO - workspace_resources ( - id, - created_at, - job_id, - transition, - address, - type, - name - ) + workspace_resources (id, created_at, job_id, transition, type, name) VALUES - ($1, $2, $3, $4, $5, $6, $7) RETURNING id, created_at, job_id, transition, address, type, name + ($1, $2, $3, $4, $5, $6) RETURNING id, created_at, job_id, transition, type, name ` type InsertWorkspaceResourceParams struct { @@ -2557,7 +2547,6 @@ type InsertWorkspaceResourceParams struct { CreatedAt time.Time `db:"created_at" json:"created_at"` JobID uuid.UUID `db:"job_id" json:"job_id"` Transition WorkspaceTransition `db:"transition" json:"transition"` - Address string `db:"address" json:"address"` Type string `db:"type" json:"type"` Name string `db:"name" json:"name"` } @@ -2568,7 +2557,6 @@ func (q *sqlQuerier) InsertWorkspaceResource(ctx context.Context, arg InsertWork arg.CreatedAt, arg.JobID, arg.Transition, - arg.Address, arg.Type, arg.Name, ) @@ -2578,7 +2566,6 @@ func (q *sqlQuerier) InsertWorkspaceResource(ctx context.Context, arg InsertWork &i.CreatedAt, &i.JobID, &i.Transition, - &i.Address, &i.Type, &i.Name, ) diff --git a/coderd/database/queries/workspaceresources.sql b/coderd/database/queries/workspaceresources.sql index 869f4a6311880..c120cf41a8d57 100644 --- a/coderd/database/queries/workspaceresources.sql +++ b/coderd/database/queries/workspaceresources.sql @@ -16,14 +16,6 @@ WHERE -- name: InsertWorkspaceResource :one INSERT INTO - workspace_resources ( - id, - created_at, - job_id, - transition, - address, - type, - name - ) + workspace_resources (id, created_at, job_id, transition, type, name) VALUES - ($1, $2, $3, $4, $5, $6, $7) RETURNING *; + ($1, $2, $3, $4, $5, $6) RETURNING *; diff --git a/coderd/provisionerdaemons.go b/coderd/provisionerdaemons.go index 4499bbbc3d155..e4832aa06b9bb 100644 --- a/coderd/provisionerdaemons.go +++ b/coderd/provisionerdaemons.go @@ -27,7 +27,6 @@ import ( "github.com/coder/coder/coderd/httpapi" "github.com/coder/coder/coderd/parameter" "github.com/coder/coder/provisionerd/proto" - "github.com/coder/coder/provisionersdk" sdkproto "github.com/coder/coder/provisionersdk/proto" ) @@ -475,18 +474,14 @@ func (server *provisionerdServer) CompleteJob(ctx context.Context, completed *pr database.WorkspaceTransitionStart: jobType.TemplateImport.StartResources, database.WorkspaceTransitionStop: jobType.TemplateImport.StopResources, } { - addresses, err := provisionersdk.ResourceAddresses(resources) - if err != nil { - return nil, xerrors.Errorf("compute resource addresses: %w", err) - } - for index, resource := range resources { + for _, resource := range resources { server.Logger.Info(ctx, "inserting template import job resource", slog.F("job_id", job.ID.String()), slog.F("resource_name", resource.Name), slog.F("resource_type", resource.Type), slog.F("transition", transition)) - err = insertWorkspaceResource(ctx, server.Database, jobID, transition, resource, addresses[index]) + err = insertWorkspaceResource(ctx, server.Database, jobID, transition, resource) if err != nil { return nil, xerrors.Errorf("insert resource: %w", err) } @@ -540,13 +535,9 @@ func (server *provisionerdServer) CompleteJob(ctx context.Context, completed *pr if err != nil { return xerrors.Errorf("update workspace build: %w", err) } - addresses, err := provisionersdk.ResourceAddresses(jobType.WorkspaceBuild.Resources) - if err != nil { - return xerrors.Errorf("compute resource addresses: %w", err) - } // This could be a bulk insert to improve performance. - for index, protoResource := range jobType.WorkspaceBuild.Resources { - err = insertWorkspaceResource(ctx, db, job.ID, workspaceBuild.Transition, protoResource, addresses[index]) + for _, protoResource := range jobType.WorkspaceBuild.Resources { + err = insertWorkspaceResource(ctx, db, job.ID, workspaceBuild.Transition, protoResource) if err != nil { return xerrors.Errorf("insert provisioner job: %w", err) } @@ -578,13 +569,12 @@ func (server *provisionerdServer) CompleteJob(ctx context.Context, completed *pr return &proto.Empty{}, nil } -func insertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.UUID, transition database.WorkspaceTransition, protoResource *sdkproto.Resource, address string) error { +func insertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.UUID, transition database.WorkspaceTransition, protoResource *sdkproto.Resource) error { resource, err := db.InsertWorkspaceResource(ctx, database.InsertWorkspaceResourceParams{ ID: uuid.New(), CreatedAt: database.Now(), JobID: jobID, Transition: transition, - Address: address, Type: protoResource.Type, Name: protoResource.Name, }) diff --git a/coderd/workspacebuilds.go b/coderd/workspacebuilds.go index b0e21b5381f22..c9161a1933bc4 100644 --- a/coderd/workspacebuilds.go +++ b/coderd/workspacebuilds.go @@ -112,7 +112,6 @@ func convertWorkspaceResource(resource database.WorkspaceResource, agents []code CreatedAt: resource.CreatedAt, JobID: resource.JobID, Transition: resource.Transition, - Address: resource.Address, Type: resource.Type, Name: resource.Name, Agents: agents, diff --git a/codersdk/workspaceresources.go b/codersdk/workspaceresources.go index 9f9169f58480c..56024e756113e 100644 --- a/codersdk/workspaceresources.go +++ b/codersdk/workspaceresources.go @@ -25,7 +25,6 @@ type WorkspaceResource struct { CreatedAt time.Time `json:"created_at"` JobID uuid.UUID `json:"job_id"` Transition database.WorkspaceTransition `json:"workspace_transition"` - Address string `json:"address"` Type string `json:"type"` Name string `json:"name"` Agents []WorkspaceAgent `json:"agents,omitempty"` diff --git a/provisionersdk/resources.go b/provisionersdk/resources.go deleted file mode 100644 index b84786d889e0e..0000000000000 --- a/provisionersdk/resources.go +++ /dev/null @@ -1,49 +0,0 @@ -package provisionersdk - -import ( - "fmt" - - "golang.org/x/xerrors" - - "github.com/coder/coder/provisionersdk/proto" -) - -// ResourceAddresses returns an index-matching slice of unique addresses -// to access resources. -func ResourceAddresses(resources []*proto.Resource) ([]string, error) { - resourcesByHost := map[string]*proto.Resource{} - for _, resource := range resources { - otherByName, exists := resourcesByHost[resource.Name] - if !exists { - resourcesByHost[resource.Name] = resource - continue - } - // If we have conflicting names, to reduce confusion we prepend the types. - delete(resourcesByHost, otherByName.Name) - otherAddress := fmt.Sprintf("%s.%s", otherByName.Type, otherByName.Name) - resourcesByHost[otherAddress] = otherByName - address := fmt.Sprintf("%s.%s", resource.Type, resource.Name) - _, exists = resourcesByHost[address] - if !exists { - resourcesByHost[address] = resource - continue - } - return nil, xerrors.Errorf("found resource with conflicting address %q", otherAddress) - } - - addresses := make([]string, 0, len(resources)) - for _, resource := range resources { - found := false - for host, other := range resourcesByHost { - if resource != other { - continue - } - found = true - addresses = append(addresses, host) - } - if !found { - panic(fmt.Sprintf("dev error: resource %s.%s wasn't given an address", resource.Type, resource.Name)) - } - } - return addresses, nil -} diff --git a/provisionersdk/resources_test.go b/provisionersdk/resources_test.go deleted file mode 100644 index a379389f8ba83..0000000000000 --- a/provisionersdk/resources_test.go +++ /dev/null @@ -1,63 +0,0 @@ -package provisionersdk_test - -import ( - "testing" - - "github.com/stretchr/testify/require" - - "github.com/coder/coder/provisionersdk" - "github.com/coder/coder/provisionersdk/proto" -) - -func TestResourceAddresses(t *testing.T) { - t.Parallel() - t.Run("Single", func(t *testing.T) { - t.Parallel() - addresses, err := provisionersdk.ResourceAddresses([]*proto.Resource{{ - Type: "google_compute_instance", - Name: "dev", - }}) - require.NoError(t, err) - require.Len(t, addresses, 1) - require.Equal(t, addresses[0], "dev") - }) - t.Run("Multiple", func(t *testing.T) { - t.Parallel() - addresses, err := provisionersdk.ResourceAddresses([]*proto.Resource{{ - Type: "google_compute_instance", - Name: "linux", - }, { - Type: "google_compute_instance", - Name: "windows", - }}) - require.NoError(t, err) - require.Len(t, addresses, 2) - require.Equal(t, addresses[0], "linux") - require.Equal(t, addresses[1], "windows") - }) - t.Run("ConflictingDifferent", func(t *testing.T) { - t.Parallel() - addresses, err := provisionersdk.ResourceAddresses([]*proto.Resource{{ - Type: "google_compute_instance", - Name: "dev", - }, { - Type: "kubernetes_pod", - Name: "dev", - }}) - require.NoError(t, err) - require.Len(t, addresses, 2) - require.Equal(t, addresses[0], "google_compute_instance.dev") - require.Equal(t, addresses[1], "kubernetes_pod.dev") - }) - t.Run("ConflictingSame", func(t *testing.T) { - t.Parallel() - _, err := provisionersdk.ResourceAddresses([]*proto.Resource{{ - Type: "google_compute_instance", - Name: "dev", - }, { - Type: "google_compute_instance", - Name: "dev", - }}) - require.Error(t, err) - }) -}