Skip to content

Commit 9d5c663

Browse files
committed
Generating short ID for prebuilds
Also dropped unnecessary CASTs Signed-off-by: Danny Kopping <danny@coder.com>
1 parent c227bb8 commit 9d5c663

File tree

3 files changed

+63
-44
lines changed

3 files changed

+63
-44
lines changed

coderd/database/queries.sql.go

+20-24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/prebuilds.sql

+19-19
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,25 @@ WITH
3838
GROUP BY wpb.template_version_id, wpb.transition)
3939
SELECT t.template_id,
4040
t.template_version_id,
41-
t.using_active_version AS is_active,
42-
CAST(COALESCE(MAX(CASE WHEN p.template_version_id = t.template_version_id THEN p.ids END),
43-
'') AS TEXT) AS running_prebuild_ids,
44-
CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN p.count ELSE 0 END),
45-
0) AS INT) AS actual, -- running prebuilds for active version
46-
CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN t.desired_instances ELSE 0 END),
47-
0) AS INT) AS desired, -- we only care about the active version's desired instances
48-
CAST(COALESCE(MAX(CASE
49-
WHEN p.template_version_id = t.template_version_id AND t.using_active_version = false
50-
THEN p.count END),
51-
0) AS INT) AS extraneous, -- running prebuilds for inactive version
52-
CAST(COALESCE(MAX(CASE WHEN pip.transition = 'start'::workspace_transition THEN pip.count ELSE 0 END),
53-
0) AS INT) AS starting,
54-
CAST(COALESCE(MAX(CASE WHEN pip.transition = 'stop'::workspace_transition THEN pip.count ELSE 0 END),
55-
0) AS INT) AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know
56-
CAST(COALESCE(MAX(CASE WHEN pip.transition = 'delete'::workspace_transition THEN pip.count ELSE 0 END),
57-
0) AS INT) AS deleting,
58-
t.deleted AS template_deleted,
59-
t.deprecated AS template_deprecated
41+
t.using_active_version AS is_active,
42+
COALESCE(MAX(CASE WHEN p.template_version_id = t.template_version_id THEN p.ids END),
43+
'')::text AS running_prebuild_ids,
44+
COALESCE(MAX(CASE WHEN t.using_active_version THEN p.count ELSE 0 END),
45+
0)::int AS actual, -- running prebuilds for active version
46+
COALESCE(MAX(CASE WHEN t.using_active_version THEN t.desired_instances ELSE 0 END),
47+
0)::int AS desired, -- we only care about the active version's desired instances
48+
COALESCE(MAX(CASE
49+
WHEN p.template_version_id = t.template_version_id AND t.using_active_version = false
50+
THEN p.count END),
51+
0)::int AS extraneous, -- running prebuilds for inactive version
52+
COALESCE(MAX(CASE WHEN pip.transition = 'start'::workspace_transition THEN pip.count ELSE 0 END),
53+
0)::int AS starting,
54+
COALESCE(MAX(CASE WHEN pip.transition = 'stop'::workspace_transition THEN pip.count ELSE 0 END),
55+
0)::int AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know
56+
COALESCE(MAX(CASE WHEN pip.transition = 'delete'::workspace_transition THEN pip.count ELSE 0 END),
57+
0)::int AS deleting,
58+
t.deleted AS template_deleted,
59+
t.deprecated AS template_deprecated
6060
FROM templates_with_prebuilds t
6161
LEFT JOIN running_prebuilds p ON p.template_version_id = t.template_version_id
6262
LEFT JOIN prebuilds_in_progress pip ON pip.template_version_id = t.template_version_id

coderd/prebuilds/controller.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package prebuilds
22

33
import (
44
"context"
5+
"crypto/rand"
6+
"encoding/base32"
57
"fmt"
68
"github.com/coder/coder/v2/coderd/audit"
79
"github.com/coder/coder/v2/coderd/database/dbtime"
@@ -279,7 +281,10 @@ func (c Controller) reconcileTemplate(ctx context.Context, template database.Tem
279281
}
280282

281283
func (c Controller) createPrebuild(ctx context.Context, db database.Store, prebuildID uuid.UUID, template database.Template) error {
282-
name := fmt.Sprintf("prebuild-%s", prebuildID)
284+
name, err := generateName()
285+
if err != nil {
286+
return xerrors.Errorf("failed to generate unique prebuild ID: %w", err)
287+
}
283288

284289
now := dbtime.Now()
285290
// Workspaces are created without any versions.
@@ -355,3 +360,21 @@ func (c Controller) provision(ctx context.Context, db database.Store, prebuildID
355360
func (c Controller) Stop() {
356361
c.closeCh <- struct{}{}
357362
}
363+
364+
// generateName generates a 20-byte prebuild name which should safe to use without truncation in most situations.
365+
// UUIDs may be too long for a resource name in cloud providers (since this ID will be used in the prebuild's name).
366+
//
367+
// We're generating a 9-byte suffix (72 bits of entry):
368+
// 1 - e^(-1e9^2 / (2 * 2^72)) = ~0.01% likelihood of collision in 1 billion IDs.
369+
// See https://en.wikipedia.org/wiki/Birthday_attack.
370+
func generateName() (string, error) {
371+
b := make([]byte, 9)
372+
373+
_, err := rand.Read(b)
374+
if err != nil {
375+
return "", err
376+
}
377+
378+
// Encode the bytes to Base32 (A-Z2-7), strip any '=' padding
379+
return fmt.Sprintf("prebuild-%s", base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(b)), nil
380+
}

0 commit comments

Comments
 (0)