diff --git a/.changelog/16921.txt b/.changelog/16921.txt new file mode 100644 index 000000000..d37937711 --- /dev/null +++ b/.changelog/16921.txt @@ -0,0 +1,3 @@ +```release-note:bug +client: Prevent a panic when an allocation has a legacy task-level bridge network and uses a driver that does not create a network namespace +``` diff --git a/client/allocrunner/network_manager_linux.go b/client/allocrunner/network_manager_linux.go index 77d0f9192..f86f39d62 100644 --- a/client/allocrunner/network_manager_linux.go +++ b/client/allocrunner/network_manager_linux.go @@ -93,7 +93,7 @@ func newNetworkManager(alloc *structs.Allocation, driverManager drivermanager.Ma nm = netManager networkInitiator = task.Name - } else if tg.Networks[0].Hostname != "" { + } else if len(tg.Networks) > 0 && tg.Networks[0].Hostname != "" { // TODO jrasell: remove once the default linux network manager // supports setting the hostname in bridged mode. This currently // indicates only Docker supports this, which is true unless a diff --git a/client/allocrunner/network_manager_linux_test.go b/client/allocrunner/network_manager_linux_test.go index 56beba8d4..c7a565cbc 100644 --- a/client/allocrunner/network_manager_linux_test.go +++ b/client/allocrunner/network_manager_linux_test.go @@ -256,6 +256,32 @@ func TestNewNetworkManager(t *testing.T) { err: true, errContains: "hostname is not currently supported on driver group1", }, + { + name: "legacy task network using exec and bridge", + alloc: &structs.Allocation{ + TaskGroup: "group", + Job: &structs.Job{ + TaskGroups: []*structs.TaskGroup{ + { + Name: "group", + Tasks: []*structs.Task{ + { + Name: "task1", + Driver: "group1", + Resources: &structs.Resources{ + Networks: []*structs.NetworkResource{ + {Mode: "bridge"}, + }, + }, + }, + }, + }, + }, + }, + }, + mustInit: false, + err: false, + }, } { t.Run(tc.name, func(t *testing.T) { require := require.New(t)