2025-04-17 09:29:29 -04:00
|
|
|
package prebuilds
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-04-24 09:39:38 -04:00
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"golang.org/x/xerrors"
|
2025-04-28 12:28:56 +02:00
|
|
|
|
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
2025-05-14 14:52:22 +02:00
|
|
|
sdkproto "github.com/coder/coder/v2/provisionersdk/proto"
|
2025-04-17 09:29:29 -04:00
|
|
|
)
|
|
|
|
|
|
2025-04-28 16:37:41 -04:00
|
|
|
var (
|
|
|
|
|
ErrNoClaimablePrebuiltWorkspaces = xerrors.New("no claimable prebuilt workspaces found")
|
|
|
|
|
ErrAGPLDoesNotSupportPrebuiltWorkspaces = xerrors.New("prebuilt workspaces functionality is not supported under the AGPL license")
|
|
|
|
|
)
|
2025-04-24 09:39:38 -04:00
|
|
|
|
2025-04-17 09:29:29 -04:00
|
|
|
// ReconciliationOrchestrator manages the lifecycle of prebuild reconciliation.
|
|
|
|
|
// It runs a continuous loop to check and reconcile prebuild states, and can be stopped gracefully.
|
|
|
|
|
type ReconciliationOrchestrator interface {
|
|
|
|
|
Reconciler
|
|
|
|
|
|
2025-04-25 11:07:15 +02:00
|
|
|
// Run starts a continuous reconciliation loop that periodically calls ReconcileAll
|
2025-04-17 09:29:29 -04:00
|
|
|
// to ensure all prebuilds are in their desired states. The loop runs until the context
|
|
|
|
|
// is canceled or Stop is called.
|
2025-04-25 11:07:15 +02:00
|
|
|
Run(ctx context.Context)
|
2025-04-17 09:29:29 -04:00
|
|
|
|
|
|
|
|
// Stop gracefully shuts down the orchestrator with the given cause.
|
|
|
|
|
// The cause is used for logging and error reporting.
|
|
|
|
|
Stop(ctx context.Context, cause error)
|
2025-05-14 14:52:22 +02:00
|
|
|
|
|
|
|
|
// TrackResourceReplacement handles a pathological situation whereby a terraform resource is replaced due to drift,
|
|
|
|
|
// which can obviate the whole point of pre-provisioning a prebuilt workspace.
|
|
|
|
|
// See more detail at https://coder.com/docs/admin/templates/extending-templates/prebuilt-workspaces#preventing-resource-replacement.
|
|
|
|
|
TrackResourceReplacement(ctx context.Context, workspaceID, buildID uuid.UUID, replacements []*sdkproto.ResourceReplacement)
|
2025-04-17 09:29:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Reconciler interface {
|
2025-04-28 12:28:56 +02:00
|
|
|
StateSnapshotter
|
|
|
|
|
|
2025-04-17 09:29:29 -04:00
|
|
|
// ReconcileAll orchestrates the reconciliation of all prebuilds across all templates.
|
|
|
|
|
// It takes a global snapshot of the system state and then reconciles each preset
|
|
|
|
|
// in parallel, creating or deleting prebuilds as needed to reach their desired states.
|
|
|
|
|
ReconcileAll(ctx context.Context) error
|
|
|
|
|
}
|
2025-04-24 09:39:38 -04:00
|
|
|
|
2025-04-28 12:28:56 +02:00
|
|
|
// StateSnapshotter defines the operations necessary to capture workspace prebuilds state.
|
|
|
|
|
type StateSnapshotter interface {
|
|
|
|
|
// SnapshotState captures the current state of all prebuilds across templates.
|
|
|
|
|
// It creates a global database snapshot that can be viewed as a collection of PresetSnapshots,
|
|
|
|
|
// each representing the state of prebuilds for a specific preset.
|
|
|
|
|
// MUST be called inside a repeatable-read transaction.
|
|
|
|
|
SnapshotState(ctx context.Context, store database.Store) (*GlobalSnapshot, error)
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-24 09:39:38 -04:00
|
|
|
type Claimer interface {
|
|
|
|
|
Claim(ctx context.Context, userID uuid.UUID, name string, presetID uuid.UUID) (*uuid.UUID, error)
|
|
|
|
|
Initiator() uuid.UUID
|
|
|
|
|
}
|