ContainerOperationsImpl implements ContainerOperations { @Override public CommandResult executeCommandInNetworkNamespace(NodeAgentContext context, String... command) { int containerPid = containerEngine.getContainer(context.containerName()) .filter(container -> container.state.isRunning()) .orElseThrow(() -> new RuntimeException( "Found no running container named " + context.containerName().asString())) .pid; return terminal.newCommandLine(context) .add("nsenter", String.format("--net=/proc/%d/ns/net", containerPid), "--") .add(command) .executeSilently(); } ContainerOperationsImpl(ContainerEngine containerEngine, Terminal terminal, IPAddresses ipAddresses, FileSystem fileSystem); @Override void createContainer(NodeAgentContext context, ContainerData containerData, ContainerResources containerResources); @Override void startContainer(NodeAgentContext context); @Override void removeContainer(NodeAgentContext context, Container container); @Override void updateContainer(NodeAgentContext context, ContainerResources containerResources); @Override Optional<Container> getContainer(NodeAgentContext context); @Override boolean pullImageAsyncIfNeeded(DockerImage dockerImage); @Override ProcessResult executeCommandInContainerAsRoot(NodeAgentContext context, Long timeoutSeconds, String... command); @Override ProcessResult executeCommandInContainerAsRoot(NodeAgentContext context, String... command); @Override CommandResult executeCommandInNetworkNamespace(NodeAgentContext context, String... command); @Override void resumeNode(NodeAgentContext context); @Override void suspendNode(NodeAgentContext context); @Override void restartVespa(NodeAgentContext context); @Override void startServices(NodeAgentContext context); @Override void stopServices(NodeAgentContext context); @Override Optional<ContainerStats> getContainerStats(NodeAgentContext context); @Override boolean noManagedContainersRunning(); @Override boolean deleteUnusedContainerImages(List<DockerImage> excludes, Duration minImageAgeToDelete);  }
@Test public void runsCommandInNetworkNamespace() { NodeAgentContext context = new NodeAgentContextImpl.Builder("container-42.domain.tld").build(); makeContainer("container-42", Container.State.RUNNING, 42); terminal.expectCommand("nsenter --net=/proc/42/ns/net -- iptables -nvL 2>&1"); dockerOperations.executeCommandInNetworkNamespace(context, "iptables", "-nvL"); }