@Override
  protected Object getAgentResource(
      ProcessState state, ProcessInstance process, Object dataResource) {
    Instance instance = getInstance(state);
    if (!isPod(instance)) {
      return false;
    }
    Long accountId = instance.getAccountId();
    List<Long> agentIds =
        agentInstanceDao.getAgentProvider(
            SystemLabels.LABEL_AGENT_SERVICE_LABELS_PROVIDER, accountId);
    Long agentId = agentIds.size() == 0 ? null : agentIds.get(0);
    if ((instance instanceof Instance)
        && (agentIds.contains(instance.getAgentId()) || instance.getSystem())) {
      return null;
    }

    if (agentId == null) {
      throw new ExecutionException("Failed to find labels provider", instance);
    }

    return agentId;
  }
  @Override
  protected void postProcessEvent(
      EventVO<?> event,
      Event reply,
      ProcessState state,
      ProcessInstance process,
      Object eventResource,
      Object dataResource,
      Object agentResource) {
    Map<String, String> labels =
        CollectionUtils.toMap(
            CollectionUtils.getNestedValue(
                reply.getData(), "instance", "+data", "+fields", "+labels"));
    if (labels.size() == 0) {
      labels =
          CollectionUtils.toMap(
              CollectionUtils.getNestedValue(
                  reply.getData(), "instanceHostMap", "instance", "+data", "+fields", "+labels"));
    } else {
      CollectionUtils.setNestedValue(
          CollectionUtils.toMap(reply.getData()),
          labels,
          "instanceHostMap",
          "instance",
          "+data",
          "+fields",
          "+labels");
    }

    Instance instance = getInstance(state);

    for (Map.Entry<String, String> label : labels.entrySet()) {
      labelsService.createContainerLabel(
          instance.getAccountId(), instance.getId(), label.getKey(), label.getValue());
    }
  }
Пример #3
0
  @Override
  public List<DnsEntryData> getServiceDnsData(
      final Instance instance, final boolean isVIPProvider) {
    MultiRecordMapper<ServiceDnsEntryData> mapper =
        new MultiRecordMapper<ServiceDnsEntryData>() {
          @Override
          protected ServiceDnsEntryData map(List<Object> input) {
            Service clientService = (Service) input.get(0);
            Service targetService = (Service) input.get(1);
            ServiceConsumeMap consumeMap = (ServiceConsumeMap) input.get(2);
            ServiceDnsEntryData data =
                new ServiceDnsEntryData(clientService, targetService, consumeMap);
            return data;
          }
        };

    ServiceTable clientService = mapper.add(SERVICE);
    ServiceTable targetService = mapper.add(SERVICE);
    ServiceConsumeMapTable serviceConsumeMap = mapper.add(SERVICE_CONSUME_MAP);

    // there are 2 conditions below linked with OR clause
    // first condition - means to return all non-dns clientService + target service map within the
    // same stack
    // that are not linked
    // second condition - return only clientService + targetService with explicit links
    Condition condition =
        (clientService
                .KIND
                .ne(ServiceDiscoveryConstants.KIND.DNSSERVICE.name())
                .and(targetService.ENVIRONMENT_ID.eq(clientService.ENVIRONMENT_ID))
                .and(serviceConsumeMap.ID.isNull()))
            .or(
                serviceConsumeMap
                    .ID
                    .isNotNull()
                    .and(serviceConsumeMap.REMOVED.isNull())
                    .and(
                        serviceConsumeMap.STATE.in(
                            CommonStatesConstants.ACTIVATING, CommonStatesConstants.ACTIVE)));

    List<ServiceDnsEntryData> serviceDnsEntries =
        create()
            .select(mapper.fields())
            .from(clientService)
            .join(targetService)
            .on(targetService.ACCOUNT_ID.eq(clientService.ACCOUNT_ID))
            .leftOuterJoin(serviceConsumeMap)
            .on(
                serviceConsumeMap
                    .SERVICE_ID
                    .eq(clientService.ID)
                    .and(serviceConsumeMap.CONSUMED_SERVICE_ID.eq(targetService.ID))
                    .and(serviceConsumeMap.REMOVED.isNull()))
            .where(targetService.REMOVED.isNull())
            .and(clientService.REMOVED.isNull())
            .and(condition)
            .fetch()
            .map(mapper);

    Nic nic = ntwkDao.getPrimaryNic(instance.getId());
    long vnetId = nic.getVnetId();
    return convertToDnsEntryData(isVIPProvider, serviceDnsEntries, instance.getAccountId(), vnetId);
  }