diff --git a/client/allocrunner/networking_bridge_linux.go b/client/allocrunner/networking_bridge_linux.go index 75e9612d1fea..ed7aeb60d5ee 100644 --- a/client/allocrunner/networking_bridge_linux.go +++ b/client/allocrunner/networking_bridge_linux.go @@ -148,7 +148,7 @@ func (b *bridgeNetworkConfigurator) Setup(ctx context.Context, alloc *structs.Al return fmt.Errorf("failed to initialize table forwarding rules: %v", err) } - if err := b.cni.Load(cni.WithConfListBytes(b.buildNomadNetConfig())); err != nil { + if err := b.ensureCNIInitialized(); err != nil { return err } @@ -176,9 +176,20 @@ func (b *bridgeNetworkConfigurator) Setup(ctx context.Context, alloc *structs.Al // Teardown calls the CNI plugins with the delete action func (b *bridgeNetworkConfigurator) Teardown(ctx context.Context, alloc *structs.Allocation, spec *drivers.NetworkIsolationSpec) error { + if err := b.ensureCNIInitialized(); err != nil { + return err + } + return b.cni.Remove(ctx, alloc.ID, spec.Path, cni.WithCapabilityPortMap(getPortMapping(alloc))) } +func (b *bridgeNetworkConfigurator) ensureCNIInitialized() error { + if b.cni.Status() == cni.ErrCNINotInitialized { + return b.cni.Load(cni.WithConfListBytes(b.buildNomadNetConfig())) + } + return nil +} + // getPortMapping builds a list of portMapping structs that are used as the // portmapping capability arguments for the portmap CNI plugin func getPortMapping(alloc *structs.Allocation) []cni.PortMapping {