NodeAdminStateUpdater { void adjustNodeAgentsToRunFromNodeRepository() { try { Map<String, NodeSpec> nodeSpecByHostname = nodeRepository.getNodes(hostHostname).stream() .collect(Collectors.toMap(NodeSpec::hostname, Function.identity())); Map<String, Acl> aclByHostname = Optional.of(cachedAclSupplier.get()) .filter(acls -> acls.keySet().containsAll(nodeSpecByHostname.keySet())) .orElseGet(cachedAclSupplier::invalidateAndGet); Set<NodeAgentContext> nodeAgentContexts = nodeSpecByHostname.keySet().stream() .map(hostname -> nodeAgentContextFactory.create( nodeSpecByHostname.get(hostname), aclByHostname.getOrDefault(hostname, Acl.EMPTY))) .collect(Collectors.toSet()); nodeAdmin.refreshContainersToRun(nodeAgentContexts); } catch (ConvergenceException e) { log.log(Level.WARNING, "Failed to update which containers should be running: " + Exceptions.toMessageString(e)); } catch (RuntimeException e) { log.log(Level.WARNING, "Failed to update which containers should be running", e); } } NodeAdminStateUpdater(
            NodeAgentContextFactory nodeAgentContextFactory,
            NodeRepository nodeRepository,
            Orchestrator orchestrator,
            NodeAdmin nodeAdmin,
            HostName hostHostname,
            Clock clock); void start(); void stop(); void converge(State wantedState);  }
@Test public void uses_cached_acl() { mockNodeRepo(NodeState.active, 1); mockAcl(Acl.EMPTY, 1); updater.adjustNodeAgentsToRunFromNodeRepository(); verify(nodeRepository, times(1)).getAcls(any()); clock.advance(Duration.ofSeconds(30)); updater.adjustNodeAgentsToRunFromNodeRepository(); clock.advance(Duration.ofSeconds(30)); updater.adjustNodeAgentsToRunFromNodeRepository(); clock.advance(Duration.ofSeconds(30)); verify(nodeRepository, times(1)).getAcls(any()); clock.advance(Duration.ofSeconds(30)); updater.adjustNodeAgentsToRunFromNodeRepository(); verify(nodeRepository, times(2)).getAcls(any()); }
@Test public void node_spec_and_acl_aligned() { Acl acl = new Acl.Builder().withTrustedPorts(22).build(); mockNodeRepo(NodeState.active, 3); mockAcl(acl, 1, 2, 3); updater.adjustNodeAgentsToRunFromNodeRepository(); updater.adjustNodeAgentsToRunFromNodeRepository(); updater.adjustNodeAgentsToRunFromNodeRepository(); verify(nodeAgentContextFactory, times(3)).create(argThat(spec -> spec.hostname().equals("host1.yahoo.com")), eq(acl)); verify(nodeAgentContextFactory, times(3)).create(argThat(spec -> spec.hostname().equals("host2.yahoo.com")), eq(acl)); verify(nodeAgentContextFactory, times(3)).create(argThat(spec -> spec.hostname().equals("host3.yahoo.com")), eq(acl)); verify(nodeRepository, times(3)).getNodes(eq(hostHostname.value())); verify(nodeRepository, times(1)).getAcls(eq(hostHostname.value())); }
@Test public void node_spec_and_acl_mismatch_missing_one_acl() { Acl acl = new Acl.Builder().withTrustedPorts(22).build(); mockNodeRepo(NodeState.active, 3); mockAcl(acl, 1, 2); updater.adjustNodeAgentsToRunFromNodeRepository(); mockNodeRepo(NodeState.active, 2); updater.adjustNodeAgentsToRunFromNodeRepository(); updater.adjustNodeAgentsToRunFromNodeRepository(); verify(nodeAgentContextFactory, times(3)).create(argThat(spec -> spec.hostname().equals("host1.yahoo.com")), eq(acl)); verify(nodeAgentContextFactory, times(3)).create(argThat(spec -> spec.hostname().equals("host2.yahoo.com")), eq(acl)); verify(nodeAgentContextFactory, times(1)).create(argThat(spec -> spec.hostname().equals("host3.yahoo.com")), eq(Acl.EMPTY)); verify(nodeRepository, times(3)).getNodes(eq(hostHostname.value())); verify(nodeRepository, times(2)).getAcls(eq(hostHostname.value())); }
@Test public void node_spec_and_acl_mismatch_additional_acl() { Acl acl = new Acl.Builder().withTrustedPorts(22).build(); mockNodeRepo(NodeState.active, 2); mockAcl(acl, 1, 2, 3); updater.adjustNodeAgentsToRunFromNodeRepository(); updater.adjustNodeAgentsToRunFromNodeRepository(); updater.adjustNodeAgentsToRunFromNodeRepository(); verify(nodeAgentContextFactory, times(3)).create(argThat(spec -> spec.hostname().equals("host1.yahoo.com")), eq(acl)); verify(nodeAgentContextFactory, times(3)).create(argThat(spec -> spec.hostname().equals("host2.yahoo.com")), eq(acl)); verify(nodeRepository, times(3)).getNodes(eq(hostHostname.value())); verify(nodeRepository, times(1)).getAcls(eq(hostHostname.value())); }