KubeConstraint implements SystemConstraint { @Override public Result evaluate(TaskRequest taskRequest, VirtualMachineCurrentState targetVM, TaskTrackerState taskTrackerState) { if (!mesosConfiguration.isKubeApiServerIntegrationEnabled()) { return VALID; } Optional<AgentInstance> instanceOpt = SchedulerUtils.findInstance(agentManagementService, schedulerConfiguration.getInstanceAttributeName(), targetVM); if (!instanceOpt.isPresent()) { return Failure.INSTANCE_NOT_FOUND.toResult(); } String instanceId = instanceOpt.get().getId(); V1Node node = kubeApiFacade.getNodeInformer().getIndexer().getByKey(instanceId); if (node == null) { return Failure.NODE_NOT_FOUND.toResult(); } Set<String> toleratedTaints = mesosConfiguration.getFenzoTaintTolerations(); if (toleratedTaints == null || toleratedTaints.isEmpty()) { return VALID; } if (node.getSpec() != null) { List<V1Taint> taints = node.getSpec().getTaints(); if (taints != null && !taints.isEmpty()) { for (V1Taint taint : taints) { String taintKey = taint.getKey(); if (!toleratedTaints.contains(taintKey)) { return Failure.TAINT_NOT_TOLERATED_IN_CONFIGURATION.toResult(); } } } } return VALID; } @Inject  KubeConstraint(SchedulerConfiguration schedulerConfiguration,
                          MesosConfiguration mesosConfiguration,
                          AgentManagementService agentManagementService,
                          KubeApiFacade kubeApiFacade); static boolean isKubeConstraintReason(String reason); @Override String getName(); @Override void prepare(); @Override Result evaluate(TaskRequest taskRequest, VirtualMachineCurrentState targetVM, TaskTrackerState taskTrackerState); static final String NAME; static final String INSTANCE_NOT_FOUND_REASON; static final String NODE_NOT_FOUND_REASON; static final String NODE_NOT_READY_REASON; static final String TAINT_NOT_TOLERATED_IN_CONFIGURATION_REASON; }
@Test public void instanceNotFound() { when(agentManagementService.findAgentInstance(any())).thenReturn(Optional.empty()); ConstraintEvaluator.Result result = kubeConstraint.evaluate( createTaskRequest(TASK_ID), createVirtualMachineCurrentStateMock(INSTANCE_ID, Collections.emptyList(), Collections.emptyList()), taskTrackerState); assertThat(result.isSuccessful()).isFalse(); assertThat(result.getFailureReason()).isEqualToIgnoringCase(KubeConstraint.INSTANCE_NOT_FOUND_REASON); }
@Test public void nodeNotFound() { AgentInstance agentInstance = createAgentInstance(INSTANCE_ID, INSTANCE_GROUP_ID); when(agentManagementService.findAgentInstance(INSTANCE_ID)).thenReturn(Optional.of(agentInstance)); when(indexer.getByKey(INSTANCE_ID)).thenReturn(null); ConstraintEvaluator.Result result = kubeConstraint.evaluate( createTaskRequest(TASK_ID), createVirtualMachineCurrentStateMock(INSTANCE_ID, Collections.emptyList(), Collections.emptyList()), taskTrackerState); assertThat(result.isSuccessful()).isFalse(); assertThat(result.getFailureReason()).isEqualToIgnoringCase(KubeConstraint.NODE_NOT_FOUND_REASON); }
@Test public void taintNotToleratedInConfiguration() { AgentInstance agentInstance = createAgentInstance(INSTANCE_ID, INSTANCE_GROUP_ID); when(agentManagementService.findAgentInstance(INSTANCE_ID)).thenReturn(Optional.of(agentInstance)); V1Taint taint = createTaint("not_tolerated_taint_key", "NoSchedule", "not_tolerated_taint_value"); V1Node node = createNode(INSTANCE_ID, true, Collections.singletonList(taint)); when(indexer.getByKey(INSTANCE_ID)).thenReturn(node); ConstraintEvaluator.Result result = kubeConstraint.evaluate( createTaskRequest(TASK_ID), createVirtualMachineCurrentStateMock(INSTANCE_ID, Collections.emptyList(), Collections.emptyList()), taskTrackerState); assertThat(result.isSuccessful()).isFalse(); assertThat(result.getFailureReason()).isEqualToIgnoringCase(KubeConstraint.TAINT_NOT_TOLERATED_IN_CONFIGURATION_REASON); }
@Test public void successfullyScheduled() { AgentInstance agentInstance = createAgentInstance(INSTANCE_ID, INSTANCE_GROUP_ID); when(agentManagementService.findAgentInstance(INSTANCE_ID)).thenReturn(Optional.of(agentInstance)); V1Taint taint = createTaint("tolerated_taint_key", "NoSchedule", "tolerated_taint_value"); V1Node node = createNode(INSTANCE_ID, true, Collections.singletonList(taint)); when(indexer.getByKey(INSTANCE_ID)).thenReturn(node); ConstraintEvaluator.Result result = kubeConstraint.evaluate( createTaskRequest(TASK_ID), createVirtualMachineCurrentStateMock(INSTANCE_ID, Collections.emptyList(), Collections.emptyList()), taskTrackerState); assertThat(result.isSuccessful()).isTrue(); }