-
Notifications
You must be signed in to change notification settings - Fork 2
feat(runtime): add ContainerRuntime abstraction interface #281
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
Merged
skevetter
merged 4 commits into
main
from
skevetter/devsy-098-container-runtime-abstraction
May 14, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2c354b2
feat(runtime): add ContainerRuntime abstraction interface
skevetter 2c6113b
fix(lint): use runtime constants instead of string literals in tests
skevetter eb8db47
fix(lint): replace remaining string literals with runtime constants
skevetter 18afe35
fix(runtime): address code review feedback
skevetter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| package docker | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os/exec" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| // RuntimeName identifies a container runtime. | ||
| type RuntimeName string | ||
|
|
||
| const ( | ||
| RuntimeDocker RuntimeName = "docker" | ||
| RuntimePodman RuntimeName = "podman" | ||
| RuntimeNerdctl RuntimeName = "nerdctl" | ||
| ) | ||
|
|
||
| // ContainerRuntime describes the capabilities of a container runtime (Docker, Podman, nerdctl). | ||
| // Rather than checking IsPodman()/IsNerdctl() at every call site, callers query capability | ||
| // methods that express *why* the code branches. | ||
| type ContainerRuntime interface { | ||
| Name() RuntimeName | ||
| SupportsInternalBuildKit() bool | ||
| SupportsSignalProxy() bool | ||
| SupportsMountConsistency() bool | ||
| NeedsUserNamespaceArgs() bool | ||
| GPUAvailable(ctx context.Context, helper *DockerHelper) (bool, error) | ||
| } | ||
|
|
||
| type dockerRuntime struct{} | ||
|
|
||
| func (dockerRuntime) Name() RuntimeName { return RuntimeDocker } | ||
| func (dockerRuntime) SupportsInternalBuildKit() bool { return true } | ||
| func (dockerRuntime) SupportsSignalProxy() bool { return true } | ||
| func (dockerRuntime) SupportsMountConsistency() bool { return true } | ||
| func (dockerRuntime) NeedsUserNamespaceArgs() bool { return false } | ||
|
|
||
| func (dockerRuntime) GPUAvailable(ctx context.Context, h *DockerHelper) (bool, error) { | ||
| out, err := h.buildCmd(ctx, "info", "-f", "{{.Runtimes.nvidia}}").Output() | ||
| if err != nil { | ||
| return false, nil | ||
| } | ||
| return strings.Contains(string(out), "nvidia-container-runtime"), nil | ||
| } | ||
|
|
||
| type podmanRuntime struct{} | ||
|
|
||
| func (podmanRuntime) Name() RuntimeName { return RuntimePodman } | ||
| func (podmanRuntime) SupportsInternalBuildKit() bool { return false } | ||
| func (podmanRuntime) SupportsSignalProxy() bool { return true } | ||
| func (podmanRuntime) SupportsMountConsistency() bool { return true } | ||
| func (podmanRuntime) NeedsUserNamespaceArgs() bool { return true } | ||
|
|
||
| func (podmanRuntime) GPUAvailable(ctx context.Context, h *DockerHelper) (bool, error) { | ||
| out, err := h.buildCmd(ctx, "info", "-f", "{{.Host.CDIDevices}}").Output() | ||
| if err != nil { | ||
| return false, nil | ||
| } | ||
| return strings.Contains(strings.ToLower(string(out)), "nvidia"), nil | ||
| } | ||
|
|
||
| type nerdctlRuntime struct{} | ||
|
|
||
| func (nerdctlRuntime) Name() RuntimeName { return RuntimeNerdctl } | ||
| func (nerdctlRuntime) SupportsInternalBuildKit() bool { return true } | ||
| func (nerdctlRuntime) SupportsSignalProxy() bool { return false } | ||
| func (nerdctlRuntime) SupportsMountConsistency() bool { return false } | ||
| func (nerdctlRuntime) NeedsUserNamespaceArgs() bool { return false } | ||
|
|
||
| func (nerdctlRuntime) GPUAvailable(ctx context.Context, h *DockerHelper) (bool, error) { | ||
| out, err := h.buildCmd(ctx, "info", "-f", "{{.Runtimes.nvidia}}").Output() | ||
| if err != nil { | ||
| return false, nil | ||
| } | ||
| return strings.Contains(string(out), "nvidia-container-runtime"), nil | ||
| } | ||
|
|
||
| // DetectRuntime probes the binary at dockerCommand to determine which runtime it is. | ||
| // The result is cached per binary path. | ||
| func DetectRuntime(dockerCommand string) ContainerRuntime { | ||
| return runtimeCache.get(dockerCommand) | ||
| } | ||
|
|
||
| // RuntimeFromName returns a ContainerRuntime for the given explicit name. | ||
| // Empty string defaults to Docker. Unknown non-empty names return an error | ||
| // to catch typos in provider configuration. | ||
| func RuntimeFromName(name string) (ContainerRuntime, error) { | ||
| switch RuntimeName(strings.ToLower(strings.TrimSpace(name))) { | ||
| case "", RuntimeDocker: | ||
| return dockerRuntime{}, nil | ||
| case RuntimePodman: | ||
| return podmanRuntime{}, nil | ||
| case RuntimeNerdctl: | ||
| return nerdctlRuntime{}, nil | ||
| default: | ||
| return nil, fmt.Errorf("unknown container runtime %q", name) | ||
| } | ||
| } | ||
|
|
||
| const detectTimeout = 5 * time.Second | ||
|
|
||
| var runtimeCache = &runtimeDetectionCache{entries: make(map[string]ContainerRuntime)} | ||
|
|
||
| type runtimeDetectionCache struct { | ||
| mu sync.Mutex | ||
| entries map[string]ContainerRuntime | ||
| } | ||
|
|
||
| func (c *runtimeDetectionCache) get(dockerCommand string) ContainerRuntime { | ||
| c.mu.Lock() | ||
| if rt, ok := c.entries[dockerCommand]; ok { | ||
| c.mu.Unlock() | ||
| return rt | ||
| } | ||
| c.mu.Unlock() | ||
|
|
||
| rt := detect(dockerCommand) | ||
|
|
||
| c.mu.Lock() | ||
| c.entries[dockerCommand] = rt | ||
| c.mu.Unlock() | ||
| return rt | ||
| } | ||
|
|
||
| func detect(dockerCommand string) ContainerRuntime { | ||
| ctx, cancel := context.WithTimeout(context.Background(), detectTimeout) | ||
| defer cancel() | ||
|
|
||
| out, err := exec.CommandContext(ctx, dockerCommand, "--version").Output() | ||
| if err != nil { | ||
| return dockerRuntime{} | ||
| } | ||
| lower := strings.ToLower(string(out)) | ||
| if strings.Contains(lower, "podman") { | ||
| return podmanRuntime{} | ||
| } | ||
| if strings.Contains(lower, "nerdctl") { | ||
| return nerdctlRuntime{} | ||
| } | ||
| return dockerRuntime{} | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package docker | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestRuntimeFromName(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| expected RuntimeName | ||
| }{ | ||
| {"docker", string(RuntimeDocker), RuntimeDocker}, | ||
| {"podman", string(RuntimePodman), RuntimePodman}, | ||
| {"nerdctl", string(RuntimeNerdctl), RuntimeNerdctl}, | ||
| {"Docker-uppercase", "Docker", RuntimeDocker}, | ||
| {"Podman-uppercase", "Podman", RuntimePodman}, | ||
| {"NERDCTL-uppercase", "NERDCTL", RuntimeNerdctl}, | ||
| {"empty", "", RuntimeDocker}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| rt, err := RuntimeFromName(tt.input) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.expected, rt.Name()) | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| func TestRuntimeFromNameUnknown(t *testing.T) { | ||
| _, err := RuntimeFromName("unknown") | ||
| assert.Error(t, err) | ||
| assert.Contains(t, err.Error(), "unknown container runtime") | ||
| } | ||
|
|
||
| func TestDockerRuntimeCapabilities(t *testing.T) { | ||
| rt, err := RuntimeFromName(string(RuntimeDocker)) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, RuntimeDocker, rt.Name()) | ||
| assert.True(t, rt.SupportsInternalBuildKit()) | ||
| assert.True(t, rt.SupportsSignalProxy()) | ||
| assert.True(t, rt.SupportsMountConsistency()) | ||
| assert.False(t, rt.NeedsUserNamespaceArgs()) | ||
| } | ||
|
|
||
| func TestPodmanRuntimeCapabilities(t *testing.T) { | ||
| rt, err := RuntimeFromName(string(RuntimePodman)) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, RuntimePodman, rt.Name()) | ||
| assert.False(t, rt.SupportsInternalBuildKit()) | ||
| assert.True(t, rt.SupportsSignalProxy()) | ||
| assert.True(t, rt.SupportsMountConsistency()) | ||
| assert.True(t, rt.NeedsUserNamespaceArgs()) | ||
| } | ||
|
|
||
| func TestNerdctlRuntimeCapabilities(t *testing.T) { | ||
| rt, err := RuntimeFromName(string(RuntimeNerdctl)) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, RuntimeNerdctl, rt.Name()) | ||
| assert.True(t, rt.SupportsInternalBuildKit()) | ||
| assert.False(t, rt.SupportsSignalProxy()) | ||
| assert.False(t, rt.SupportsMountConsistency()) | ||
| assert.False(t, rt.NeedsUserNamespaceArgs()) | ||
| } | ||
|
|
||
| func TestDockerHelperGetRuntimeFallback(t *testing.T) { | ||
| h := &DockerHelper{DockerCommand: "nonexistent-binary-xyz"} | ||
| rt := h.GetRuntime() | ||
| assert.Equal(t, RuntimeDocker, rt.Name(), "should fall back to docker for unknown binary") | ||
| } | ||
|
|
||
| func TestDockerHelperGetRuntimeExplicit(t *testing.T) { | ||
| h := &DockerHelper{ | ||
| DockerCommand: string(RuntimeDocker), | ||
| Runtime: podmanRuntime{}, | ||
| } | ||
| rt := h.GetRuntime() | ||
| assert.Equal(t, RuntimePodman, rt.Name(), "should use explicitly set runtime") | ||
| } | ||
|
|
||
| func TestDockerHelperIsPodmanDelegates(t *testing.T) { | ||
| h := &DockerHelper{ | ||
| DockerCommand: string(RuntimeDocker), | ||
| Runtime: podmanRuntime{}, | ||
| } | ||
| assert.True(t, h.IsPodman()) | ||
| assert.False(t, h.IsNerdctl()) | ||
| } | ||
|
|
||
| func TestDockerHelperIsNerdctlDelegates(t *testing.T) { | ||
| h := &DockerHelper{ | ||
| DockerCommand: string(RuntimeDocker), | ||
| Runtime: nerdctlRuntime{}, | ||
| } | ||
| assert.False(t, h.IsPodman()) | ||
| assert.True(t, h.IsNerdctl()) | ||
| } | ||
|
|
||
| func TestDetectRuntimeCaching(t *testing.T) { | ||
| rt1 := DetectRuntime("nonexistent-binary-abc") | ||
| rt2 := DetectRuntime("nonexistent-binary-abc") | ||
| assert.Equal(t, rt1, rt2, "same binary should return same cached runtime") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.