Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docker-build): failing container build with Podman should fail properly #1882

Merged
merged 2 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ class HcChunkedResponseHandlerWrapper implements ResponseHandler<Object> {

@Override
public Object handleResponse(HttpResponse response) throws IOException {
if (!isJson(response) && !hasNoContentTypeAsForPodman(response)) {
throw new IllegalArgumentException(
"Docker daemon returned an unexpected content type while trying to build the Dockerfile. Content type must be `application/json` or no content type defined.");
}

try (InputStream stream = response.getEntity().getContent()) {
// Parse text as json
if (isJson(response)) {
EntityStreamReaderUtil.processJsonStream(handler, stream);
}
EntityStreamReaderUtil.processJsonStream(handler, stream);
}
return null;
}
Expand All @@ -45,4 +48,9 @@ private static boolean isJson(HttpResponse response) {
.filter(h -> h.getName().equalsIgnoreCase("Content-Type"))
.anyMatch(h -> h.getValue().toLowerCase().startsWith("application/json"));
}

private static boolean hasNoContentTypeAsForPodman(HttpResponse response) {
return Stream.of(response.getAllHeaders())
.noneMatch(h -> h.getName().equalsIgnoreCase("Content-Type"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package org.eclipse.jkube.kit.build.service.docker.access.hc;

import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;

import org.eclipse.jkube.kit.build.service.docker.access.chunked.EntityStreamReaderUtil;
Expand Down Expand Up @@ -54,15 +56,16 @@ void handleResponseWithJsonResponse() throws IOException {
@Test
void handleResponseWithTextPlainResponse() throws IOException {
givenResponseHeaders(new BasicHeader("Content-Type", "text/plain"));
hcChunkedResponseHandlerWrapper.handleResponse(response);
verifyProcessJsonStream(0);
assertThrows(IllegalArgumentException.class, () -> {
hcChunkedResponseHandlerWrapper.handleResponse(response);
});
}

@Test
void handleResponseWithNoContentType() throws IOException {
void handleResponseWithNoContentTypeAsForPodman() throws IOException {
givenResponseHeaders();
hcChunkedResponseHandlerWrapper.handleResponse(response);
verifyProcessJsonStream(0);
verifyProcessJsonStream(1);
}

private void givenResponseHeaders(Header... headers) {
Expand Down