Skip to content

Commit

Permalink
fix: Docker push works with Podman REST API
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Oct 27, 2020
1 parent fb1c43c commit 9f1ef1d
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Usage:
* Fix 440: Added quickstart for MicroProfile running on OpenLiberty
* Fix: Valid content type for REST docker API requests with body
* Fix #448: Service.spec.type added from config if existing Service fragment doesn't specify it
* Fix: Docker push works with Podman REST API

### 1.0.1 (2020-10-05)
* Fix #381: Remove root as default user in AssemblyConfigurationUtils#getAssemblyConfigurationOrCreateDefault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.eclipse.jkube.kit.build.service.docker.access.DockerAccessException;
import org.eclipse.jkube.kit.build.service.docker.access.UrlBuilder;
import org.eclipse.jkube.kit.build.service.docker.access.chunked.BuildJsonResponseHandler;
import org.eclipse.jkube.kit.build.service.docker.access.chunked.EntityStreamReaderUtil;
import org.eclipse.jkube.kit.build.service.docker.access.chunked.PullOrPushResponseJsonHandler;
import org.eclipse.jkube.kit.build.service.docker.access.hc.http.HttpClientBuilder;
import org.eclipse.jkube.kit.build.service.docker.access.hc.unix.UnixSocketClientBuilder;
Expand Down Expand Up @@ -174,7 +173,7 @@ public void startExecContainer(String containerId, LogOutputSpec outputSpec) thr
}
}

private ResponseHandler<Object> createExecResponseHandler(LogOutputSpec outputSpec) throws FileNotFoundException {
private ResponseHandler<Object> createExecResponseHandler(LogOutputSpec outputSpec) {
final LogCallback callback = new DefaultLogCallback(outputSpec);
return new ResponseHandler<Object>() {
@Override
Expand Down Expand Up @@ -707,25 +706,6 @@ private static boolean isSSL(String url) {
return url != null && url.toLowerCase().startsWith("https");
}

// Preparation for performing requests
private static class HcChunkedResponseHandlerWrapper implements ResponseHandler<Object> {

private EntityStreamReaderUtil.JsonEntityResponseHandler handler;

HcChunkedResponseHandlerWrapper(EntityStreamReaderUtil.JsonEntityResponseHandler handler) {
this.handler = handler;
}

@Override
public Object handleResponse(HttpResponse response) throws IOException {
try (InputStream stream = response.getEntity().getContent()) {
// Parse text as json
EntityStreamReaderUtil.processJsonStream(handler, stream);
}
return null;
}
}

public String fetchApiVersionFromServer(String baseUrl, ApacheHttpClientDelegate delegate) throws IOException {
HttpGet get = new HttpGet(baseUrl + (baseUrl.endsWith("/") ? "" : "/") + "version");
get.addHeader(HttpHeaders.ACCEPT, "*/*");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.build.service.docker.access.hc;

import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.eclipse.jkube.kit.build.service.docker.access.chunked.EntityStreamReaderUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.stream.Stream;

class HcChunkedResponseHandlerWrapper implements ResponseHandler<Object> {

private final EntityStreamReaderUtil.JsonEntityResponseHandler handler;

HcChunkedResponseHandlerWrapper(EntityStreamReaderUtil.JsonEntityResponseHandler handler) {
this.handler = handler;
}

@Override
public Object handleResponse(HttpResponse response) throws IOException {
try (InputStream stream = response.getEntity().getContent()) {
// Parse text as json
if (isJson(response)) {
EntityStreamReaderUtil.processJsonStream(handler, stream);
}
}
return null;
}

private static boolean isJson(HttpResponse response) {
return Stream.of(response.getAllHeaders())
.filter(h -> h.getName().equalsIgnoreCase("Content-Type"))
.anyMatch(h -> h.getValue().toLowerCase().startsWith("application/json"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.build.service.docker.access.hc;

import java.io.IOException;

import org.eclipse.jkube.kit.build.service.docker.access.chunked.EntityStreamReaderUtil;

import mockit.Expectations;
import mockit.Mocked;
import mockit.Verifications;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.message.BasicHeader;
import org.junit.Before;
import org.junit.Test;

@SuppressWarnings("unused")
public class HcChunkedResponseHandlerWrapperTest {

@Mocked
private EntityStreamReaderUtil.JsonEntityResponseHandler handler;
@Mocked
private HttpResponse response;
@Mocked
private EntityStreamReaderUtil entityStreamReaderUtil;

private Header[] headers;
private HcChunkedResponseHandlerWrapper hcChunkedResponseHandlerWrapper;

@Before
public void setUp() {
hcChunkedResponseHandlerWrapper = new HcChunkedResponseHandlerWrapper(handler);

}

@Test
public void handleResponseWithJsonResponse() throws IOException {
givenResponseHeaders(new BasicHeader("ConTenT-Type", "application/json; charset=UTF-8"));
hcChunkedResponseHandlerWrapper.handleResponse(response);
verifyProcessJsonStream(1);
}

@Test
public void handleResponseWithTextPlainResponse() throws IOException {
givenResponseHeaders(new BasicHeader("Content-Type", "text/plain"));
hcChunkedResponseHandlerWrapper.handleResponse(response);
verifyProcessJsonStream(0);
}

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

private void givenResponseHeaders(Header... headers) {
// @formatter:off
new Expectations() {{
response.getAllHeaders(); result = headers;
}};
// @formatter:on
}

@SuppressWarnings("AccessStaticViaInstance")
private void verifyProcessJsonStream(int timesCalled) throws IOException {
// @formatter:off
new Verifications() {{
entityStreamReaderUtil.processJsonStream(handler, response.getEntity().getContent()); times = timesCalled;
}};
// @formatter:on
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void build(ImageConfiguration imageConfig) throws JKubeServiceException {
// Assume we always want to tag
jKubeServiceHub.getDockerServiceHub().getBuildService().tagImage(imageConfig.getName(), imageConfig);
} catch (IOException ex) {
throw new JKubeServiceException("Error while trying to build the image", ex);
throw new JKubeServiceException("Error while trying to build the image: " + ex.getMessage(), ex);
}
}

Expand All @@ -59,7 +59,7 @@ public void push(Collection<ImageConfiguration> imageConfigs, int retries, Regis
jKubeServiceHub.getDockerServiceHub().getRegistryService()
.pushImages(imageConfigs, retries, registryConfig, skipTag);
} catch (IOException ex) {
throw new JKubeServiceException("Error while trying to push the image", ex);
throw new JKubeServiceException("Error while trying to push the image: " + ex.getMessage(), ex);
}
}

Expand Down

0 comments on commit 9f1ef1d

Please sign in to comment.