Skip to content

Commit

Permalink
runtime: fail early when starting docker container with FC
Browse files Browse the repository at this point in the history
FC does not support network device hotplug. Let's add a check to fail
early when starting containers created by docker.

Signed-off-by: Peng Tao <bergwolf@hyper.sh>
(cherry picked from commit 21204ca)
Signed-off-by: Greg Kurz <groug@kaod.org>
  • Loading branch information
bergwolf authored and gkurz committed Oct 18, 2023
1 parent 7e6f801 commit c17cbd3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/runtime/virtcontainers/sandbox.go
Expand Up @@ -933,6 +933,17 @@ func (s *Sandbox) createNetwork(ctx context.Context) error {
return nil
}

// docker container needs the hypervisor process ID to find out the container netns,
// which means that the hypervisor has to support network device hotplug so that docker
// can use the prestart hooks to set up container netns.
caps := s.hypervisor.Capabilities(ctx)
if !caps.IsNetworkDeviceHotplugSupported() {
spec := s.GetPatchedOCISpec()
if utils.IsDockerContainer(spec) {
return errors.New("docker container needs network device hotplug but the configured hypervisor does not support it")
}
}

span, ctx := katatrace.Trace(ctx, s.Logger(), "createNetwork", sandboxTracingTags, map[string]string{"sandbox_id": s.id})
defer span.End()
katatrace.AddTags(span, "network", s.network, "NetworkConfig", s.config.NetworkConfig)
Expand Down
20 changes: 20 additions & 0 deletions src/runtime/virtcontainers/utils/utils.go
Expand Up @@ -12,9 +12,11 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"

"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"

Expand Down Expand Up @@ -494,3 +496,21 @@ func RevertBytes(num uint64) uint64 {
}
return 1024*RevertBytes(a) + b
}

// IsDockerContainer returns if the container is managed by docker
// This is done by checking the prestart hook for `libnetwork` arguments.
func IsDockerContainer(spec *specs.Spec) bool {
if spec == nil || spec.Hooks == nil {
return false
}

for _, hook := range spec.Hooks.Prestart {
for _, arg := range hook.Args {
if strings.HasPrefix(arg, "libnetwork") {
return true
}
}
}

return false
}
23 changes: 23 additions & 0 deletions src/runtime/virtcontainers/utils/utils_test.go
Expand Up @@ -16,6 +16,7 @@ import (
"syscall"
"testing"

"github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -580,3 +581,25 @@ func TestRevertBytes(t *testing.T) {
num := RevertBytes(testNum)
assert.Equal(expectedNum, num)
}

func TestIsDockerContainer(t *testing.T) {
assert := assert.New(t)

ociSpec := &specs.Spec{
Hooks: &specs.Hooks{
Prestart: []specs.Hook{
{
Args: []string{
"haha",
},
},
},
},
}
assert.False(IsDockerContainer(ociSpec))

ociSpec.Hooks.Prestart = append(ociSpec.Hooks.Prestart, specs.Hook{
Args: []string{"libnetwork-xxx"},
})
assert.True(IsDockerContainer(ociSpec))
}

0 comments on commit c17cbd3

Please sign in to comment.