从云中的一个注册表中拉取镜像并推送到云中的另一个注册表

时间:2021-06-07 00:45:37

标签: java containers jib

我正在尝试利用 jib-core 以编程方式构建和推送图像。我希望实现的是从一个容器云注册中心拉取镜像,并将其推送到另一个云注册中心。我相信代码应该是这样的:

/*
* Generate random text - we use this random generated text to change the sha-256 digest to signify that its a new image
* */
private void generateRandomText() {
    //generate random 4 KB of data and write them to a file
    String uid = UUID.randomUUID().toString();
    try (RandomAccessFile f = new RandomAccessFile(RANDOM_TEXT, "rw")) {
        // an UUID is 36 bytes times 113 makes 4KB
        f.writeBytes(String.join("", Collections.nCopies(113, uid)));
    } catch (IOException e) {
        e.printStackTrace();
    }
}


/**
 * pull and push an image from one registry to another
 * @param fromRegistry - the registry we will be pushing the image from
 * @param toRegistry - the registry we will push the image to
 * */
public void pullAndPushImageToRegistry(String fromRegistry, String toRegistry) {
    generateRandomText();
    try {
        Jib.from(ImageReference.parse(fromRegistry))
                .addLayer(Arrays.asList(Paths.get(RANDOM_TEXT)), AbsoluteUnixPath.get("/"))
                .containerize(
                        Containerizer.to(RegistryImage.named(toRegistry)
                                .addCredential("username","password")));
    } catch (InterruptedException | RegistryException | IOException | CacheDirectoryCreationException | ExecutionException | InvalidImageReferenceException e) {
        LOG.error("Failed to push image " + e.getMessage());
        //e.printStackTrace();
    }
}

对于 pullAndPushImageToRegistry(otherregistry.azurecr.io/samples/nginx, myregistry.azurecr.io/samples/nginx) 的此类示例,我收到此错误:

Failed to push image java.lang.NoSuchMethodError: com.google.api.client.http.HttpRequest.setUseRawRedirectUrls(Z)Lcom/google/api/client/http/HttpRequest

并且不确定如何解决此问题。

1 个答案:

答案 0 :(得分:2)

您的项目正在引入 Jib 所依赖的旧版 Google HTTP 客户端库。截至目前,related dependencies and their versions

  • com.google.http-client:google-http-client:1.38.1
  • com.google.http-client:google-http-client-apache-v2:1.38.1
  • com.google.auth:google-auth-library-oauth2-http:0.22.2(不是 Google HTTP 客户端库的一部分,但这取决于它)

可能其他一些项目库也依赖于 Google HTTP 客户端库(直接或传递),而构建系统(无论是 Gradle 还是 Maven)最终会引入旧版本。

在 Gradle 和 Maven 中,有不同的方式和方法来解决这种依赖冲突,这里是 Maven 中的 example

相关问题