public void populateInstances()

in teamcity-kubernetes-plugin-server/src/main/java/jetbrains/buildServer/clouds/kubernetes/KubeCloudImageImpl.java [211:268]


    public void populateInstances(){
        try{
            final Collection<Pod> pods = myApiConnector.listPods(CollectionsUtil.asMap(
              KubeTeamCityLabels.TEAMCITY_CLOUD_IMAGE, myImageData.getId(),
              KubeTeamCityLabels.TEAMCITY_CLOUD_PROFILE, myImageData.getProfileId()
                                                                 ));
            final Set<String> keys = new HashSet<>(myIdToInstanceMap.keySet());
            final List<Pod> newPods = new ArrayList<>();
            for (Pod pod : pods){
                if (pod.getMetadata() == null){
                    LOG.debug("Found pod without metadata...");
                    continue;
                }
                final String podName = pod.getMetadata().getName();
                if (keys.remove(podName)){
                    LOG.debug(String.format("Found known pod '%s'", podName));
                    final KubeCloudInstance instance = myIdToInstanceMap.get(podName);
                    if (instance == null){
                        LOG.warn(String.format("Instance '%s' was removed?!", podName));
                        continue;
                    }
                    instance.updateState(pod);
                } else {
                    LOG.debug(String.format("Found new pod '%s'", podName));
                    newPods.add(pod);
                }
            }
            keys.removeIf(instanceId->{
                KubeCloudInstance instance = myIdToInstanceMap.get(instanceId);
                return  instance != null && instance.getStatus() ==InstanceStatus.SCHEDULED_TO_START;
            });
            if (keys.size() > 0) {
                LOG.info(String.format("The following %d %s %s deleted: %s",
                                       keys.size(), StringUtil.pluralize( "pod",keys.size()),
                                       keys.size() == 1? "was" : "were",
                                       String.join(", ", keys))
                );
                keys.forEach(myIdToInstanceMap::remove);
            }
            if (newPods.size() > 0){
                final List<String> podNames = newPods.stream().map(pod -> pod.getMetadata().getName()).collect(Collectors.toList());
                LOG.info(String.format(
                  "Found %d new %s: %s",
                  newPods.size(),
                  StringUtil.pluralize("pod", newPods.size()),
                  String.join(", ", podNames)
                ));
                newPods.forEach(pod->{
                    final String podName = pod.getMetadata().getName();
                    myIdToInstanceMap.putIfAbsent(podName, new KubeCloudInstanceImpl(this, pod));
                });
            }
            myCurrentError = null;
        } catch (KubernetesClientException ex){
            myCurrentError = new CloudErrorInfo("Failed populate instances", ex.getMessage(), ex);
            throw ex;
        }
    }