Skip to content

feat: Extract instance type when provisioning VMs #4839

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
Nov 1, 2022
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
3 changes: 2 additions & 1 deletion 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
@@ -0,0 +1,2 @@
ALTER TABLE workspace_resources
DROP COLUMN instance_type;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE workspace_resources
ADD COLUMN instance_type varchar(256);
17 changes: 9 additions & 8 deletions coderd/database/models.go

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

35 changes: 21 additions & 14 deletions coderd/database/queries.sql.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/queries/workspaceresources.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ SELECT * FROM workspace_resources WHERE created_at > $1;

-- name: InsertWorkspaceResource :one
INSERT INTO
workspace_resources (id, created_at, job_id, transition, type, name, hide, icon)
workspace_resources (id, created_at, job_id, transition, type, name, hide, icon, instance_type)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;
($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *;

-- name: GetWorkspaceResourceMetadataByResourceID :many
SELECT
Expand Down
4 changes: 4 additions & 0 deletions coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,10 @@ func insertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
Name: protoResource.Name,
Hide: protoResource.Hide,
Icon: protoResource.Icon,
InstanceType: sql.NullString{
String: protoResource.InstanceType,
Valid: protoResource.InstanceType != "",
},
})
if err != nil {
return xerrors.Errorf("insert provisioner job resource %q: %w", protoResource.Name, err)
Expand Down
18 changes: 10 additions & 8 deletions coderd/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,11 @@ func ConvertWorkspaceApp(app database.WorkspaceApp) WorkspaceApp {
// ConvertWorkspaceResource anonymizes a workspace resource.
func ConvertWorkspaceResource(resource database.WorkspaceResource) WorkspaceResource {
return WorkspaceResource{
ID: resource.ID,
JobID: resource.JobID,
Transition: resource.Transition,
Type: resource.Type,
ID: resource.ID,
JobID: resource.JobID,
Transition: resource.Transition,
Type: resource.Type,
InstanceType: resource.InstanceType.String,
}
}

Expand Down Expand Up @@ -667,10 +668,11 @@ type User struct {
}

type WorkspaceResource struct {
ID uuid.UUID `json:"id"`
JobID uuid.UUID `json:"job_id"`
Transition database.WorkspaceTransition `json:"transition"`
Type string `json:"type"`
ID uuid.UUID `json:"id"`
JobID uuid.UUID `json:"job_id"`
Transition database.WorkspaceTransition `json:"transition"`
Type string `json:"type"`
InstanceType string `json:"instance_type"`
}

type WorkspaceResourceMetadata struct {
Expand Down
38 changes: 32 additions & 6 deletions provisioner/terraform/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,12 +382,13 @@ func ConvertResources(module *tfjson.StateModule, rawGraph string) ([]*proto.Res
}

resources = append(resources, &proto.Resource{
Name: resource.Name,
Type: resource.Type,
Agents: agents,
Hide: resourceHidden[label],
Icon: resourceIcon[label],
Metadata: resourceMetadata[label],
Name: resource.Name,
Type: resource.Type,
Agents: agents,
Hide: resourceHidden[label],
Icon: resourceIcon[label],
Metadata: resourceMetadata[label],
InstanceType: applyInstanceType(resource),
})
}

Expand All @@ -405,6 +406,31 @@ type graphResource struct {
Depth uint
}

// applyInstanceType sets the instance type on an agent if it matches
// one of the special resource types that we track.
func applyInstanceType(resource *tfjson.StateResource) string {
key, isValid := map[string]string{
"google_compute_instance": "machine_type",
"aws_instance": "instance_type",
"aws_spot_instance_request": "instance_type",
"azurerm_linux_virtual_machine": "size",
"azurerm_windows_virtual_machine": "size",
}[resource.Type]
if !isValid {
return ""
}

instanceTypeRaw, isValid := resource.AttributeValues[key]
if !isValid {
return ""
}
instanceType, isValid := instanceTypeRaw.(string)
if !isValid {
return ""
}
return instanceType
}

// applyAutomaticInstanceID checks if the resource is one of a set of *magical* IDs
// that automatically index their identifier for automatic authentication.
func applyAutomaticInstanceID(resource *tfjson.StateResource, agents []*proto.Agent) {
Expand Down
52 changes: 52 additions & 0 deletions provisioner/terraform/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,58 @@ func TestAppSlugValidation(t *testing.T) {
require.ErrorContains(t, err, "duplicate app slug")
}

func TestInstanceTypeAssociation(t *testing.T) {
t.Parallel()
type tc struct {
ResourceType string
InstanceTypeKey string
}
for _, tc := range []tc{{
ResourceType: "google_compute_instance",
InstanceTypeKey: "machine_type",
}, {
ResourceType: "aws_instance",
InstanceTypeKey: "instance_type",
}, {
ResourceType: "aws_spot_instance_request",
InstanceTypeKey: "instance_type",
}, {
ResourceType: "azurerm_linux_virtual_machine",
InstanceTypeKey: "size",
}, {
ResourceType: "azurerm_windows_virtual_machine",
InstanceTypeKey: "size",
}} {
tc := tc
t.Run(tc.ResourceType, func(t *testing.T) {
t.Parallel()
instanceType, err := cryptorand.String(12)
require.NoError(t, err)
resources, err := terraform.ConvertResources(&tfjson.StateModule{
Resources: []*tfjson.StateResource{{
Address: tc.ResourceType + ".dev",
Type: tc.ResourceType,
Name: "dev",
Mode: tfjson.ManagedResourceMode,
AttributeValues: map[string]interface{}{
tc.InstanceTypeKey: instanceType,
},
}},
// This is manually created to join the edges.
}, `digraph {
compound = "true"
newrank = "true"
subgraph "root" {
"[root] `+tc.ResourceType+`.dev" [label = "`+tc.ResourceType+`.dev", shape = "box"]
}
}`)
require.NoError(t, err)
require.Len(t, resources, 1)
require.Equal(t, resources[0].GetInstanceType(), instanceType)
})
}
}

func TestInstanceIDAssociation(t *testing.T) {
t.Parallel()
type tc struct {
Expand Down
Loading