forked from eclipse-jkube/jkube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor (jkube-kit/resource/helm) : Add HelmUploaderManager to manag…
…e different Helm repository uploaders HelmUploaderManager would use PluginServiceFactory to detect applicable Helm repository uploaders Signed-off-by: Rohan Kumar <rohaan@redhat.com>
- Loading branch information
1 parent
adb4dd8
commit c354872
Showing
12 changed files
with
346 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
jkube-kit/common/src/test/java/org/eclipse/jkube/kit/common/util/AsyncUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* 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.common.util; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; | ||
|
||
class AsyncUtilTest { | ||
@Test | ||
void get_whenFuturePresent_thenReturnValue() { | ||
// Given | ||
CompletableFuture<String> completableFuture = CompletableFuture.completedFuture("foo"); | ||
|
||
// When | ||
String result = AsyncUtil.get(completableFuture, Duration.ofMinutes(1)); | ||
|
||
// Then | ||
assertThat(result).isEqualTo("foo"); | ||
} | ||
|
||
@Test | ||
void get_whenFutureCompletedExceptionally_then() { | ||
// Given | ||
CompletableFuture<String> completableFuture = new CompletableFuture<>(); | ||
completableFuture.completeExceptionally(new IOException("io exception")); | ||
|
||
// When | ||
// Then | ||
assertThatIllegalStateException() | ||
.isThrownBy(() -> AsyncUtil.get(completableFuture, Duration.ofMinutes(1))) | ||
.withMessageContaining("io exception"); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../src/main/java/org/eclipse/jkube/kit/resource/helm/ArtifactoryHelmRepositoryUploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* 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.resource.helm; | ||
|
||
import org.eclipse.jkube.kit.common.KitLogger; | ||
|
||
import java.io.File; | ||
|
||
public class ArtifactoryHelmRepositoryUploader extends StandardRepositoryUploader { | ||
|
||
public ArtifactoryHelmRepositoryUploader(KitLogger logger) { | ||
super("PUT", logger, HelmRepository.HelmRepoType.ARTIFACTORY); | ||
} | ||
|
||
@Override | ||
public String url(File helmChart, HelmRepository repository) { | ||
return formatRepositoryURL(helmChart, repository); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../src/main/java/org/eclipse/jkube/kit/resource/helm/ChartMuseumHelmRepositoryUploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* 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.resource.helm; | ||
|
||
import org.eclipse.jkube.kit.common.KitLogger; | ||
|
||
import java.io.File; | ||
|
||
public class ChartMuseumHelmRepositoryUploader extends StandardRepositoryUploader { | ||
|
||
public ChartMuseumHelmRepositoryUploader(KitLogger logger) { | ||
super("POST", logger, HelmRepository.HelmRepoType.CHARTMUSEUM); | ||
} | ||
|
||
@Override | ||
public String url(File helmChart, HelmRepository repository) { | ||
return repository.getUrl(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
.../resource/helm/src/main/java/org/eclipse/jkube/kit/resource/helm/HelmUploaderManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* 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.resource.helm; | ||
|
||
import org.eclipse.jkube.kit.common.KitLogger; | ||
import org.eclipse.jkube.kit.common.util.PluginServiceFactory; | ||
|
||
import java.util.List; | ||
|
||
public class HelmUploaderManager { | ||
private static final String[] SERVICE_PATHS = new String[] { | ||
"META-INF/jkube/helm-uploaders" | ||
}; | ||
|
||
private final List<HelmUploader> helmUploaderList; | ||
|
||
public HelmUploaderManager(KitLogger log) { | ||
this.helmUploaderList = new PluginServiceFactory<>(log).createServiceObjects(SERVICE_PATHS); | ||
} | ||
|
||
public HelmUploader getHelmUploader(HelmRepository.HelmRepoType type) { | ||
for (HelmUploader helmUploader : helmUploaderList) { | ||
if (helmUploader.getType().equals(type)) { | ||
return helmUploader; | ||
} | ||
} | ||
throw new IllegalStateException("Could not find Helm Uploader for type " + type); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...e/helm/src/main/java/org/eclipse/jkube/kit/resource/helm/NexusHelmRepositoryUploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* 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.resource.helm; | ||
|
||
import org.eclipse.jkube.kit.common.KitLogger; | ||
|
||
import java.io.File; | ||
|
||
public class NexusHelmRepositoryUploader extends StandardRepositoryUploader { | ||
public NexusHelmRepositoryUploader(KitLogger logger) { | ||
super("PUT", logger, HelmRepository.HelmRepoType.NEXUS); | ||
} | ||
|
||
@Override | ||
public String url(File helmChart, HelmRepository repository) { | ||
String url = formatRepositoryURL(helmChart, repository); | ||
if (url.endsWith(".tar.gz")) { | ||
url = url.replaceAll("tar.gz$", "tgz"); | ||
} | ||
return url; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...ce/helm/src/main/java/org/eclipse/jkube/kit/resource/helm/StandardRepositoryUploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* 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.resource.helm; | ||
|
||
import io.fabric8.kubernetes.client.RequestConfigBuilder; | ||
import io.fabric8.kubernetes.client.http.HttpClient; | ||
import io.fabric8.kubernetes.client.http.HttpRequest; | ||
import io.fabric8.kubernetes.client.http.HttpResponse; | ||
import io.fabric8.kubernetes.client.utils.HttpClientUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.eclipse.jkube.kit.common.KitLogger; | ||
import org.eclipse.jkube.kit.common.util.Base64Util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.time.Duration; | ||
|
||
import static org.eclipse.jkube.kit.common.util.AsyncUtil.get; | ||
|
||
public abstract class StandardRepositoryUploader implements HelmUploader { | ||
private final String method; | ||
private final KitLogger logger; | ||
private final HelmRepository.HelmRepoType type; | ||
private static final long HELM_UPLOAD_TIMEOUT_MINUTES = 30; | ||
|
||
protected StandardRepositoryUploader(String method, KitLogger logger, HelmRepository.HelmRepoType type) { | ||
this.method = method; | ||
this.logger = logger; | ||
this.type = type; | ||
} | ||
|
||
public abstract String url(File helmChart, HelmRepository repository); | ||
|
||
@Override | ||
public HelmRepository.HelmRepoType getType() { | ||
return type; | ||
} | ||
|
||
@Override | ||
public void uploadSingle(File file, HelmRepository repository) throws IOException, BadUploadException { | ||
String uploadUrl = url(file, repository); | ||
|
||
try (HttpClient httpClient = HttpClientUtils.getHttpClientFactory().newBuilder().tag(new RequestConfigBuilder().withRequestRetryBackoffLimit(0).build()).build()) { | ||
HttpRequest.Builder httpRequestBuilder = httpClient.newHttpRequestBuilder(); | ||
httpRequestBuilder.uri(uploadUrl); | ||
// At this point username and password are always populated since this is requirement in HelmService | ||
httpRequestBuilder.header("Authorization", String.format("Basic %s", Base64Util.encodeToString(repository.getUsername() + ":" + repository.getPassword()))); | ||
httpRequestBuilder.method(method, "application/gzip", Files.newInputStream(file.toPath()), file.length()); | ||
HttpResponse<byte[]> response = get(httpClient.sendAsync(httpRequestBuilder.build(), byte[].class), Duration.ofMinutes(HELM_UPLOAD_TIMEOUT_MINUTES)); | ||
handleHttpResponse(response); | ||
} | ||
} | ||
|
||
private void handleHttpResponse(HttpResponse<byte[]> response) throws BadUploadException { | ||
if (!response.isSuccessful()) { | ||
String responseStr; | ||
if (response.body() != null) { | ||
responseStr = new String(response.body()); | ||
} else if (StringUtils.isNotBlank(response.message())) { | ||
responseStr = response.message(); | ||
} else { | ||
responseStr = "No details provided"; | ||
} | ||
throw new BadUploadException(responseStr); | ||
} else { | ||
String message = Integer.toString(response.code()); | ||
if (response.body() != null) { | ||
message += " - " + new String(response.body()); | ||
} | ||
logger.info(message); | ||
} | ||
} | ||
|
||
protected String formatRepositoryURL(File file, HelmRepository repository) { | ||
return String.format("%s%s", StringUtils.appendIfMissing(repository.getUrl(), "/"), file.getName()); | ||
} | ||
} |
Oops, something went wrong.