Skip to content
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

chore: check task config policies against slots and max_slots #10015

Merged
merged 3 commits into from
Oct 3, 2024
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 master/internal/api_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (a *apiServer) getCommandLaunchParams(ctx context.Context, req *protoComman
// Check submitted config against task config policies.
valid, err := configpolicy.CheckNTSCConstraints(ctx, int(cmdSpec.Metadata.WorkspaceID), config, a.m.rm)
if !valid {
return nil, nil, err
return nil, nil, status.Errorf(codes.InvalidArgument, "failed constraint check: %v", err)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@amandavialva01 I had %w here at first, but the formatting was super weird. I was reminded of the conversation we had about %v vs. %w in fmt.Errorf. Eventually I'll (hopefully) keep it straight! 😆

Copy link
Contributor

Choose a reason for hiding this comment

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

Loll same! 😄 It's definitely a matter of trial and error 😆

}

token, err := getTaskSessionToken(ctx, userModel)
Expand Down
17 changes: 11 additions & 6 deletions master/internal/configpolicy/task_config_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,20 @@ func CheckNTSCConstraints(
if err == nil && constraints.PriorityLimit != nil && workloadConfig.Resources.Priority != nil {
if !priorityWithinLimit(*workloadConfig.Resources.Priority, *constraints.PriorityLimit, smallerHigher) {
return false, fmt.Errorf("requested priority [%d] exceeds limit set by admin [%d]: %w",
*constraints.PriorityLimit, *workloadConfig.Resources.Priority, errPriorityConstraintFailure)
*workloadConfig.Resources.Priority, *constraints.PriorityLimit, errPriorityConstraintFailure)
}
}

if constraints.ResourceConstraints != nil && constraints.ResourceConstraints.MaxSlots != nil &&
workloadConfig.Resources.MaxSlots != nil {
if *constraints.ResourceConstraints.MaxSlots < *workloadConfig.Resources.MaxSlots {
return false, fmt.Errorf("requested resources.max_slots [%d] exceeds limit set by admin [%d]: %w",
*constraints.ResourceConstraints.MaxSlots, *workloadConfig.Resources.MaxSlots, errResourceConstraintFailure)
if constraints.ResourceConstraints != nil && constraints.ResourceConstraints.MaxSlots != nil {
if workloadConfig.Resources.MaxSlots != nil {
if *constraints.ResourceConstraints.MaxSlots < *workloadConfig.Resources.MaxSlots {
return false, fmt.Errorf("requested resources.max_slots [%d] exceeds limit set by admin [%d]: %w",
*workloadConfig.Resources.MaxSlots, *constraints.ResourceConstraints.MaxSlots, errResourceConstraintFailure)
}
}
if *constraints.ResourceConstraints.MaxSlots < workloadConfig.Resources.Slots {
return false, fmt.Errorf("requested resources.slots [%d] exceeds limit set by admin [%d]: %w",
workloadConfig.Resources.Slots, *constraints.ResourceConstraints.MaxSlots, errResourceConstraintFailure)
}
}

Expand Down
18 changes: 18 additions & 0 deletions master/internal/configpolicy/task_config_policy_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ func TestValidateNTSCConstraints(t *testing.T) {
require.ErrorIs(t, err, errResourceConstraintFailure)
})

t.Run("exceeds slots - not ok", func(t *testing.T) {
constraints := DefaultConstraints()
w := model.Workspace{Name: uuid.NewString(), UserID: user.ID}
_, err := db.Bun().NewInsert().Model(&w).Exec(context.Background())
require.NoError(t, err)
addConstraints(t, user, &w.ID, *constraints)

resourceManager := mocks.ResourceManager{}
resourceManager.On("SmallerValueIsHigherPriority", mock.Anything).Return(true, nil)

config := defaultConfig()
config.Resources.Slots = *config.Resources.MaxSlots
config.Resources.MaxSlots = nil // ensure only slots is set
Comment on lines +125 to +126
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this setup! Makes the test function explicit

_, err = CheckNTSCConstraints(context.Background(), w.ID, config, &resourceManager)
require.Error(t, err)
require.ErrorIs(t, err, errResourceConstraintFailure)
})

t.Run("rm priority not supported - ok", func(t *testing.T) {
w := addWorkspacePriorityLimit(t, user, wkspPriorityLimit)
rm1 := mocks.ResourceManager{}
Expand Down
Loading