Skip to content

feat: persist generated coder_app id #18487

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 3 commits into from
Jun 23, 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 coderd/agentapi/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (a *AppsAPI) BatchUpdateAppHealths(ctx context.Context, req *agentproto.Bat
Health: app.Health,
})
if err != nil {
return nil, xerrors.Errorf("update workspace app health for app %q (%q): %w", err, app.ID, app.Slug)
return nil, xerrors.Errorf("update workspace app health for app %q (%q): %w", app.ID, app.Slug, err)
}
}

Expand Down
13 changes: 12 additions & 1 deletion coderd/provisionerdserver/provisionerdserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2595,8 +2595,19 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
openIn = database.WorkspaceAppOpenInSlimWindow
}

var appID string
if app.Id == "" || app.Id == uuid.Nil.String() {
appID = uuid.NewString()
} else {
appID = app.Id
}
id, err := uuid.Parse(appID)
if err != nil {
return xerrors.Errorf("parse app uuid: %w", err)
}

dbApp, err := db.InsertWorkspaceApp(ctx, database.InsertWorkspaceAppParams{
ID: uuid.New(),
ID: id,
CreatedAt: dbtime.Now(),
AgentID: dbAgent.ID,
Slug: slug,
Expand Down
12 changes: 12 additions & 0 deletions provisioner/terraform/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/awalterschulze/gographviz"
"github.com/google/uuid"
tfjson "github.com/hashicorp/terraform-json"
"github.com/mitchellh/mapstructure"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -93,6 +94,7 @@ type agentDisplayAppsAttributes struct {

// A mapping of attributes on the "coder_app" resource.
type agentAppAttributes struct {
ID string `mapstructure:"id"`
AgentID string `mapstructure:"agent_id"`
// Slug is required in terraform, but to avoid breaking existing users we
// will default to the resource name if it is not specified.
Expand Down Expand Up @@ -522,7 +524,17 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
continue
}

id := attrs.ID
if id == "" {
// This should never happen since the "id" attribute is set on creation:
// https://github.com/coder/terraform-provider-coder/blob/cfa101df4635e405e66094fa7779f9a89d92f400/provider/app.go#L37
logger.Warn(ctx, "coder_app's id was unexpectedly empty", slog.F("name", attrs.Name))

id = uuid.NewString()
}

agent.Apps = append(agent.Apps, &proto.App{
Id: id,
Slug: attrs.Slug,
DisplayName: attrs.DisplayName,
Command: attrs.Command,
Expand Down
6 changes: 6 additions & 0 deletions provisioner/terraform/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,9 @@ func TestConvertResources(t *testing.T) {
if agent.GetInstanceId() != "" {
agent.Auth = &proto.Agent_InstanceId{}
}
for _, app := range agent.Apps {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For test reproducibility; see above.

app.Id = ""
}
}
}

Expand Down Expand Up @@ -1037,6 +1040,9 @@ func TestConvertResources(t *testing.T) {
if agent.GetInstanceId() != "" {
agent.Auth = &proto.Agent_InstanceId{}
}
for _, app := range agent.Apps {
app.Id = ""
}
}
}
// Convert expectedNoMetadata and resources into a
Expand Down
1 change: 1 addition & 0 deletions provisionerd/proto/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import "github.com/coder/coder/v2/apiversion"
// - Add new field named `scheduling` to `Prebuild`, with fields for timezone
// and schedule rules to define cron-based scaling of prebuilt workspace
// instances based on time patterns.
// - Added new field named `id` to `App`, which transports the ID generated by the coder_app provider to be persisted.
const (
CurrentMajor = 1
CurrentMinor = 7
Expand Down
13 changes: 11 additions & 2 deletions provisionersdk/proto/provisioner.pb.go

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

1 change: 1 addition & 0 deletions provisionersdk/proto/provisioner.proto
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ message App {
bool hidden = 11;
AppOpenIn open_in = 12;
string group = 13;
string id = 14; // If nil, new UUID will be generated.
}

// Healthcheck represents configuration for checking for app readiness.
Expand Down
5 changes: 5 additions & 0 deletions site/e2e/provisionerGenerated.ts

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

1 change: 1 addition & 0 deletions site/e2e/tests/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ test("app", async ({ context, page }) => {
token,
apps: [
{
id: randomUUID(),
url: `http://localhost:${addr.port}`,
displayName: appName,
order: 0,
Expand Down
Loading