Skip to content

feat(agent/agentcontainers): add ContainerEnvInfoer#16623

Merged
johnstcn merged 9 commits into
mainfrom
cj/agent-docker-envinfo
Feb 24, 2025
Merged

feat(agent/agentcontainers): add ContainerEnvInfoer#16623
johnstcn merged 9 commits into
mainfrom
cj/agent-docker-envinfo

Conversation

@johnstcn

@johnstcn johnstcn commented Feb 19, 2025

Copy link
Copy Markdown
Member

This PR adds an alternative implementation of EnvInfo (#16603) that reads information from a running container.

NOTE: the new tests aren't run in CI by default; you'll have to just "trust" me that they work or run them yourself.

@johnstcn johnstcn self-assigned this Feb 19, 2025

@mafredri mafredri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only real issue I spotted is the env parsing. Both the premise (which I question) and also multiline handling. Otherwise looks solid.

Comment thread agent/agentcontainers/containers_dockercli.go Outdated
Comment thread agent/agentcontainers/containers_dockercli.go
Comment thread agent/agentcontainers/containers_dockercli.go Outdated
Comment thread agent/agentcontainers/containers_dockercli.go Outdated
Comment thread agent/agentcontainers/containers_dockercli.go Outdated
Comment thread agent/agentcontainers/containers_dockercli.go Outdated
Comment thread agent/agentcontainers/containers_dockercli.go Outdated
Comment thread agent/agentcontainers/containers_dockercli.go Outdated
Comment thread agent/agentcontainers/containers_dockercli.go Outdated
Comment thread agent/agentcontainers/containers_dockercli.go Outdated
}

// EnvInfo returns information about the environment of a container.
func EnvInfo(ctx context.Context, execer agentexec.Execer, container, containerUser string) (*ContainerEnvInfoer, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Where will this be called? Right now it's only used in tests.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

See here: #16638

Comment thread agent/agentcontainers/containers_dockercli.go Outdated

func (cei *ContainerEnvInfoer) CurrentUser() (*user.User, error) {
// Clone the user so that the caller can't modify it
u := *cei.user

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: this is technically just a shallow clone, but it's OK because user.User does not have any reference type fields: https://pkg.go.dev/os/user#User

Comment thread agent/agentcontainers/containers_dockercli.go Outdated
Comment thread agent/agentcontainers/containers_dockercli.go Outdated
if assert.Len(t, foundContainer.Volumes, 1) {
assert.Equal(t, testTempDir, foundContainer.Volumes[testTempDir])
}
// Test that EnvInfo is able to correctly modify a command to be

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tasty 👍

@mafredri mafredri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Noticed one potential issue? Otherwise LGTM once those are fixed 👍🏻

Comment on lines +131 to +142
func (*DockerEnvInfoer) Environ() []string {
// Return a clone of the environment so that the caller can't modify it
return os.Environ()
}

func (*DockerEnvInfoer) UserHomeDir() (string, error) {
// We default the working directory of the command to the user's home
// directory. Since this came from inside the container, we cannot guarantee
// that this exists on the host. Return the "real" home directory of the user
// instead.
return os.UserHomeDir()
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are we intentionally using os here now? IIRC these were accessing dei.user before. I'm guessing the env and home dir may be different between host and container.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah I found that we still need to use os here otherwise you won't have e.g. $PATH set correctly, which will cause all kinds of issues. Given that both the implementations are doing the same thing now I could take it out of the equation, but it's also nice to have this abstraction in place IMO.

@mafredri mafredri Feb 24, 2025

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, if that's the case, I suppose I don't understand how this is supposed to be used.

Essentially setting cmd.Env is no-op for docker exec commands. We need to serialize the container env as docker exec -e ENV1=val1 ... where this is needed (or an env file or some such).

In general I like the abstraction, but as used it seems confusing to me?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I suppose a better implementation would be to have DockerEnvInfoer embed systemEnvInfoer and just override the required methods. That way you keep the abstraction but it's clear then that this implementation does nothing different.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is going to need a little refactoring so I'll merge as-is and address this in a follow-up.

Comment thread agent/agentcontainers/containers_dockercli.go
@johnstcn johnstcn merged commit 304007b into main Feb 24, 2025
@johnstcn johnstcn deleted the cj/agent-docker-envinfo branch February 24, 2025 15:05
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants