- Notifications
You must be signed in to change notification settings - Fork948
feat: add managed agent license limit checks#18937
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -42,6 +42,7 @@ type Executor struct { | ||
templateScheduleStore *atomic.Pointer[schedule.TemplateScheduleStore] | ||
accessControlStore *atomic.Pointer[dbauthz.AccessControlStore] | ||
auditor *atomic.Pointer[audit.Auditor] | ||
buildUsageChecker *atomic.Pointer[wsbuilder.UsageChecker] | ||
log slog.Logger | ||
tick <-chan time.Time | ||
statsCh chan<- Stats | ||
@@ -65,7 +66,7 @@ type Stats struct { | ||
} | ||
// New returns a new wsactions executor. | ||
func NewExecutor(ctx context.Context, db database.Store, ps pubsub.Pubsub, fc *files.Cache, reg prometheus.Registerer, tss *atomic.Pointer[schedule.TemplateScheduleStore], auditor *atomic.Pointer[audit.Auditor], acs *atomic.Pointer[dbauthz.AccessControlStore],buildUsageChecker *atomic.Pointer[wsbuilder.UsageChecker],log slog.Logger, tick <-chan time.Time, enqueuer notifications.Enqueuer, exp codersdk.Experiments) *Executor { | ||
factory := promauto.With(reg) | ||
le := &Executor{ | ||
//nolint:gocritic // Autostart has a limited set of permissions. | ||
@@ -78,6 +79,7 @@ func NewExecutor(ctx context.Context, db database.Store, ps pubsub.Pubsub, fc *f | ||
log: log.Named("autobuild"), | ||
auditor: auditor, | ||
accessControlStore: acs, | ||
buildUsageChecker: buildUsageChecker, | ||
notificationsEnqueuer: enqueuer, | ||
reg: reg, | ||
experiments: exp, | ||
@@ -279,7 +281,7 @@ func (e *Executor) runOnce(t time.Time) Stats { | ||
} | ||
if nextTransition != "" { | ||
builder := wsbuilder.New(ws, nextTransition, *e.buildUsageChecker.Load()). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This could panic; can you add some protection for it? | ||
SetLastWorkspaceBuildInTx(&latestBuild). | ||
SetLastWorkspaceBuildJobInTx(&latestJob). | ||
Experiments(e.experiments). | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2193,6 +2193,14 @@ func (q *querier) GetLogoURL(ctx context.Context) (string, error) { | ||
return q.db.GetLogoURL(ctx) | ||
} | ||
func (q *querier) GetManagedAgentCount(ctx context.Context, arg database.GetManagedAgentCountParams) (int64, error) { | ||
// Must be able to read all workspaces to check usage. | ||
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceWorkspace); err != nil { | ||
return 0, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Please wrap err; could get confusing if there's an authz error on a seemingly unrelated resource. | ||
} | ||
return q.db.GetManagedAgentCount(ctx, arg) | ||
} | ||
func (q *querier) GetNotificationMessagesByStatus(ctx context.Context, arg database.GetNotificationMessagesByStatusParams) ([]database.NotificationMessage, error) { | ||
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceNotificationMessage); err != nil { | ||
return nil, err | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -35,3 +35,21 @@ DELETE | ||
FROM licenses | ||
WHERE id = $1 | ||
RETURNING id; | ||
-- name: GetManagedAgentCount :one | ||
-- This isn't strictly a license query, but it's related to license enforcement. | ||
SELECT | ||
COUNT(DISTINCT wb.id) AS count | ||
FROM | ||
workspace_builds AS wb | ||
JOIN | ||
provisioner_jobs AS pj | ||
ON | ||
wb.job_id = pj.id | ||
WHERE | ||
wb.transition = 'start'::workspace_transition | ||
AND wb.has_ai_task = true | ||
-- Exclude failed builds since they can't use AI managed agents anyway. | ||
AND pj.job_status NOT IN ('canceled'::provisioner_job_status, 'failed'::provisioner_job_status) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Maybe it'll be more future-proof to whitelist instead of blacklist statuses... | ||
-- Jobs are counted when they are created. | ||
AND wb.created_at BETWEEN @start_time::timestamptz AND @end_time::timestamptz; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -335,7 +335,7 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) { | ||
return | ||
} | ||
builder:=wsbuilder.New(workspace,database.WorkspaceTransition(createBuild.Transition),*api.BuildUsageChecker.Load()). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Can panic | ||
Initiator(apiKey.UserID). | ||
RichParameterValues(createBuild.RichParameterValues). | ||
LogLevel(string(createBuild.LogLevel)). | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -56,6 +56,7 @@ type Builder struct { | ||
logLevelstring | ||
deploymentValues*codersdk.DeploymentValues | ||
experiments codersdk.Experiments | ||
usageCheckerUsageChecker | ||
richParameterValues []codersdk.WorkspaceBuildParameter | ||
initiator uuid.UUID | ||
@@ -89,7 +90,24 @@ type Builder struct { | ||
verifyNoLegacyParametersOncebool | ||
} | ||
typeUsageCheckerinterface { | ||
CheckBuildUsage(ctx context.Context,store database.Store,templateVersion*database.TemplateVersion) (UsageCheckResponse,error) | ||
} | ||
typeUsageCheckResponsestruct { | ||
Permittedbool | ||
Messagestring | ||
} | ||
typeNoopUsageCheckerstruct{} | ||
var_UsageChecker=NoopUsageChecker{} | ||
func (NoopUsageChecker)CheckBuildUsage(_ context.Context,_ database.Store,_*database.TemplateVersion) (UsageCheckResponse,error) { | ||
returnUsageCheckResponse{ | ||
Permitted:true, | ||
},nil | ||
} | ||
// versionTarget expresses how to determine the template version for the build. | ||
// | ||
@@ -121,8 +139,8 @@ type stateTarget struct { | ||
explicit*[]byte | ||
} | ||
funcNew(w database.Workspace,t database.WorkspaceTransition,ucUsageChecker)Builder { | ||
returnBuilder{workspace:w,trans:t,usageChecker:uc} | ||
} | ||
// Methods that customize the build are public, have a struct receiver and return a new Builder. | ||
@@ -321,6 +339,10 @@ func (b *Builder) buildTx(authFunc func(action policy.Action, object rbac.Object | ||
iferr!=nil { | ||
returnnil,nil,nil,err | ||
} | ||
err=b.checkUsage() | ||
iferr!=nil { | ||
returnnil,nil,nil,err | ||
} | ||
err=b.checkRunningBuild() | ||
iferr!=nil { | ||
returnnil,nil,nil,err | ||
@@ -1247,6 +1269,23 @@ func (b *Builder) checkTemplateJobStatus() error { | ||
returnnil | ||
} | ||
func (b*Builder)checkUsage()error { | ||
templateVersion,err:=b.getTemplateVersion() | ||
iferr!=nil { | ||
returnBuildError{http.StatusInternalServerError,"failed to fetch template version",err} | ||
} | ||
resp,err:=b.usageChecker.CheckBuildUsage(b.ctx,b.store,templateVersion) | ||
iferr!=nil { | ||
returnBuildError{http.StatusInternalServerError,"failed to check build usage",err} | ||
} | ||
if!resp.Permitted { | ||
returnBuildError{http.StatusForbidden,"Build is not permitted: "+resp.Message,nil} | ||
Comment on lines +1280 to +1283 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Nit: capitalisation consistency | ||
} | ||
returnnil | ||
} | ||
func (b*Builder)checkRunningBuild()error { | ||
job,err:=b.getLastBuildJob() | ||
ifxerrors.Is(err,sql.ErrNoRows) { | ||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.