-
Notifications
You must be signed in to change notification settings - Fork 894
feat: add agent exec pkg #15577
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
feat: add agent exec pkg #15577
Changes from 1 commit
90281bd
538c6c3
00c9cd7
196c8a9
97e68f4
712b328
5f633e1
05702c3
0132fb3
ae30643
986e18e
b158919
7235bfb
521956f
471ea8d
5076cf0
b8725f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,12 @@ package agentexec | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
"strconv" | ||
"strings" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
@@ -25,8 +25,7 @@ const ( | |
// is returned. All instances of exec.Cmd should flow through this function to ensure | ||
// proper resource constraints are applied to the child process. | ||
func CommandContext(ctx context.Context, cmd string, args ...string) (*exec.Cmd, error) { | ||
environ := os.Environ() | ||
_, enabled := envVal(environ, EnvProcPrioMgmt) | ||
_, enabled := os.LookupEnv(EnvProcPrioMgmt) | ||
if runtime.GOOS != "linux" || !enabled { | ||
return exec.CommandContext(ctx, cmd, args...), nil | ||
} | ||
|
@@ -41,14 +40,24 @@ func CommandContext(ctx context.Context, cmd string, args ...string) (*exec.Cmd, | |
return nil, xerrors.Errorf("eval symlinks: %w", err) | ||
} | ||
|
||
args = append([]string{"agent-exec", cmd}, args...) | ||
return exec.CommandContext(ctx, bin, args...), nil | ||
execArgs := []string{"agent-exec"} | ||
if score, ok := envValInt(EnvProcOOMScore); ok { | ||
execArgs = append(execArgs, oomScoreArg(score)) | ||
} | ||
|
||
if score, ok := envValInt(EnvProcNiceScore); ok { | ||
execArgs = append(execArgs, niceScoreArg(score)) | ||
} | ||
execArgs = append(execArgs, "--", cmd) | ||
execArgs = append(execArgs, args...) | ||
|
||
return exec.CommandContext(ctx, bin, execArgs...), nil | ||
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. Do we do any filtering on agent env vars like 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. That should be handled by the calling code imo, unless it was an oversight that we want to start accounting for now. |
||
} | ||
|
||
// envValInt searches for a key in a list of environment variables and parses it to an int. | ||
// If the key is not found or cannot be parsed, returns 0 and false. | ||
func envValInt(env []string, key string) (int, bool) { | ||
val, ok := envVal(env, key) | ||
func envValInt(key string) (int, bool) { | ||
val, ok := os.LookupEnv(key) | ||
if !ok { | ||
return 0, false | ||
} | ||
|
@@ -60,14 +69,15 @@ func envValInt(env []string, key string) (int, bool) { | |
return i, true | ||
} | ||
|
||
// envVal searches for a key in a list of environment variables and returns its value. | ||
// If the key is not found, returns empty string and false. | ||
func envVal(env []string, key string) (string, bool) { | ||
prefix := key + "=" | ||
for _, e := range env { | ||
if strings.HasPrefix(e, prefix) { | ||
return strings.TrimPrefix(e, prefix), true | ||
} | ||
} | ||
return "", false | ||
const ( | ||
niceArg = "coder-nice" | ||
oomArg = "coder-oom" | ||
) | ||
|
||
func niceScoreArg(score int) string { | ||
return fmt.Sprintf("--%s=%d", niceArg, score) | ||
} | ||
|
||
func oomScoreArg(score int) string { | ||
return fmt.Sprintf("--%s=%d", oomArg, score) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.