Repository: GoogleContainerTools/jib
File: jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/PullBaseImageStep.java (Java)

Code before:

      ContainerConfigurationTemplate containerConfig =
          Verify.verifyNotNull(manifestsAndConfigs.get(0).getConfig());
      PlatformChecker.checkManifestPlatform(buildContext, containerConfig);

      return Collections.singletonList(
          JsonToImageTranslator.toImage(
              (BuildableManifestTemplate) Verify.verifyNotNull(manifest), containerConfig));
    }

    // Manifest list cached. Identify matching platforms and check if all of them are cached.
    ImmutableList.Builder<Image> images = ImmutableList.builder();
    for (Platform platform : buildContext.getContainerConfiguration().getPlatforms()) {
      String manifestDigest =
          lookUpPlatformSpecificImageManifest((V22ManifestListTemplate) manifestList, platform);

      Optional<ManifestAndConfigTemplate> manifestAndConfigFound =
          manifestsAndConfigs.stream()
              .filter(entry -> manifestDigest.equals(entry.getManifestDigest()))
              .findFirst();
      if (!manifestAndConfigFound.isPresent()) {
        return Collections.emptyList();
      }

      ManifestTemplate manifest = Verify.verifyNotNull(manifestAndConfigFound.get().getManifest());
      // Verify all layers described in manifest are present in cache
      if (!baseImageLayersCache.areAllLayersCached(manifest)) {
        return Collections.emptyList();
      }

      ContainerConfigurationTemplate containerConfig =
          Verify.verifyNotNull(manifestAndConfigFound.get().getConfig());
      images.add(
          JsonToImageTranslator.toImage((BuildableManifestTemplate) manifest, containerConfig));
    }
    return images.build();
  }
}


Review comment (refactor):
(optional) there was quite a bit of code duplication in this method even in the past between the section that deals with general and platform-based manifest processing. Could there be an opportunity for refactoring common logic out into a helper method?

Code after:
      String manifestDigest =
          lookUpPlatformSpecificImageManifest((V22ManifestListTemplate) manifestList, platform);

      Optional<ManifestAndConfigTemplate> manifestAndConfigFound =
          manifestsAndConfigs.stream()
              .filter(entry -> manifestDigest.equals(entry.getManifestDigest()))
              .findFirst();
      if (!manifestAndConfigFound.isPresent()) {
        return Collections.emptyList();
      }
      Optional<Image> cachedImage =
          getBaseImageIfAllLayersCached(manifestAndConfigFound.get(), false);
      if (!cachedImage.isPresent()) {
        return Collections.emptyList();
      }
      images.add(cachedImage.get());
    }
    return images.build();
  }

  /**
   * Helper method to retrieve a base image from cache given manifest and container config. Does not
   * return image if any layers of base image are missing in cache.
   *
   * @param manifestAndConfig stores an image manifest and a container config
   * @param isSingleManifest true if base image is not a manifest list
   * @return the single cached {@link Image} found, or {@code Optional#empty()} if the base image
   *     not found in cache with all layers present.
   * @throws BadContainerConfigurationFormatException if the container configuration is in a bad
   *     format
   * @throws PlatformNotFoundInBaseImageException if build target platform is not found in the base
   *     image
   * @throws LayerCountMismatchException LayerCountMismatchException if the manifest and
   *     configuration contain conflicting layer information
   */
  private Optional<Image> getBaseImageIfAllLayersCached(
      ManifestAndConfigTemplate manifestAndConfig, boolean isSingleManifest)
      throws BadContainerConfigurationFormatException, PlatformNotFoundInBaseImageException,
          LayerCountMismatchException {
    Cache baseImageLayersCache = buildContext.getBaseImageLayersCache();
    ManifestTemplate manifest = Verify.verifyNotNull(manifestAndConfig.getManifest());

    // Verify all layers described in manifest are present in cache
    if (!baseImageLayersCache.areAllLayersCached(manifest)) {
      return Optional.empty();
    }
    if (manifest instanceof V21ManifestTemplate) {
      return Optional.of(JsonToImageTranslator.toImage((V21ManifestTemplate) manifest));
    }
    ContainerConfigurationTemplate containerConfig =
        Verify.verifyNotNull(manifestAndConfig.getConfig());