Skip to content

Commit

Permalink
fix: Maven properties override/complement XML configuration for HelmP…
Browse files Browse the repository at this point in the history
…ushMojo

Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Apr 14, 2021
1 parent 27e6baf commit a2205dc
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 277 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Optional;

@Builder(toBuilder = true)
@AllArgsConstructor
Expand All @@ -38,6 +39,16 @@ public class HelmRepository {
private String password;
private HelmRepoType type;


public String getTypeAsString() {
return Optional.ofNullable(type).map(HelmRepoType::toString).orElse(null);
}

// Plexus deserialization specific setters
public void setType(String type) {
this.type = HelmRepoType.parseString(type);
}

public enum HelmRepoType {
CHARTMUSEUM(HelmRepositoryConnectionUtils::getConnectionForUploadToChartMuseum),
ARTIFACTORY(HelmRepositoryConnectionUtils::getConnectionForUploadToArtifactory),
Expand All @@ -53,6 +64,10 @@ public HttpURLConnection createConnection(File file, HelmRepository repository)
return connectionCreator.createConnectionForUploadToArtifactory(file, repository);
}

public static HelmRepoType parseString(String repoType) {
return Optional.ofNullable(repoType).map(String::toUpperCase).map(HelmRepoType::valueOf).orElse(null);
}

@FunctionalInterface
protected interface ConnectionCreator {
HttpURLConnection createConnectionForUploadToArtifactory(File file, HelmRepository repository) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jkube.kit.resource.helm.HelmRepository;
import org.eclipse.jkube.kit.resource.helm.HelmRepository.HelmRepoType;
import org.eclipse.jkube.kit.resource.helm.HelmService;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand Down Expand Up @@ -47,7 +46,7 @@ public class HelmPushMojo extends HelmMojo {
protected static final String DEFAULT_SECURITY = "~/.m2/settings-security.xml";

@Component(role = org.sonatype.plexus.components.sec.dispatcher.SecDispatcher.class, hint = "default")
private SecDispatcher securityDispatcher;
protected SecDispatcher securityDispatcher;

@Override
protected boolean canExecute() {
Expand All @@ -61,62 +60,66 @@ public void executeInternal() throws MojoExecutionException {
}
try {
super.executeInternal();
HelmRepository helmRepository = getHelmRepository();
if (helmRepository != null) {
final HelmRepository helmRepository = getHelmRepository();
if (isRepositoryValid(helmRepository)) {
this.setAuthentication(helmRepository);
HelmService.uploadHelmChart(getKitLogger(), helm, helmRepository);
} else {
String error = "No repository configured for upload";
String error = "No repository or invalid repository configured for upload";
getKitLogger().error(error);
throw new MojoExecutionException(error);
}
} catch (Exception exp) {
getKitLogger().error("Error performing helm push", exp);
throw new MojoExecutionException(exp.getMessage());
throw new MojoExecutionException(exp.getMessage(), exp);
}
}

private static boolean isRepositoryValid(HelmRepository repository) {
return repository != null
&& repository.getType() != null
&& StringUtils.isNotBlank(repository.getName())
&& StringUtils.isNotBlank(repository.getUrl());
}

@Override
protected void initDefaults() throws IOException, MojoExecutionException {
super.initDefaults();

if (!StringUtils.isBlank(getProperty(PROPERTY_UPLOAD_REPO_STABLE_NAME))) {
try {
HelmRepository stableRepository = new HelmRepository();
stableRepository.setName(getProperty(PROPERTY_UPLOAD_REPO_STABLE_NAME));
stableRepository.setUrl(getProperty(PROPERTY_UPLOAD_REPO_STABLE_URL));
stableRepository.setUsername(getProperty(PROPERTY_UPLOAD_REPO_STABLE_USERNAME));
stableRepository.setPassword(getProperty(PROPERTY_UPLOAD_REPO_STABLE_PASSWORD));
stableRepository
.setType(HelmRepoType.valueOf(getProperty(PROPERTY_UPLOAD_REPO_STABLE_TYPE)));
getHelm().setStableRepository(stableRepository);
} catch (Exception ex) {
getKitLogger().debug("Error configuring Helm stable repository", ex);
throw new MojoExecutionException("Error configuring Helm stable repository", ex);
}
if (getHelm().getStableRepository() == null) {
getHelm().setStableRepository(new HelmRepository());
}

if (!StringUtils.isBlank(getProperty(PROPERTY_UPLOAD_REPO_SNAPSHOT_NAME))) {
try {
HelmRepository snapshotRepository = new HelmRepository();
snapshotRepository.setName(getProperty(PROPERTY_UPLOAD_REPO_SNAPSHOT_NAME));
snapshotRepository.setUrl(getProperty(PROPERTY_UPLOAD_REPO_SNAPSHOT_URL));
snapshotRepository.setUsername(getProperty(PROPERTY_UPLOAD_REPO_SNAPSHOT_USERNAME));
snapshotRepository.setPassword(getProperty(PROPERTY_UPLOAD_REPO_SNAPSHOT_PASSWORD));
snapshotRepository
.setType(HelmRepoType.valueOf(getProperty(PROPERTY_UPLOAD_REPO_SNAPSHOT_TYPE)));
getHelm().setSnapshotRepository(snapshotRepository);
} catch (Exception ex) {
getKitLogger().debug("Error configuring Helm snapshot repository", ex);
throw new MojoExecutionException("Error configuring Helm snapshot repository", ex);
}
final HelmRepository stableRepository = getHelm().getStableRepository();
initFromPropertyOrDefault(PROPERTY_UPLOAD_REPO_STABLE_TYPE,
stableRepository::getTypeAsString, stableRepository::setType, null);
initFromPropertyOrDefault(PROPERTY_UPLOAD_REPO_STABLE_NAME,
stableRepository::getName, stableRepository::setName, null);
initFromPropertyOrDefault(PROPERTY_UPLOAD_REPO_STABLE_URL,
stableRepository::getUrl, stableRepository::setUrl, null);
initFromPropertyOrDefault(PROPERTY_UPLOAD_REPO_STABLE_USERNAME,
stableRepository::getUsername, stableRepository::setUsername, null);
initFromPropertyOrDefault(PROPERTY_UPLOAD_REPO_STABLE_PASSWORD,
stableRepository::getPassword, stableRepository::setPassword, null);

if (getHelm().getSnapshotRepository() == null) {
getHelm().setSnapshotRepository(new HelmRepository());
}
final HelmRepository snapshotRepository = getHelm().getSnapshotRepository();
initFromPropertyOrDefault(PROPERTY_UPLOAD_REPO_SNAPSHOT_TYPE,
snapshotRepository::getTypeAsString, snapshotRepository::setType, null);
initFromPropertyOrDefault(PROPERTY_UPLOAD_REPO_SNAPSHOT_NAME,
snapshotRepository::getName, snapshotRepository::setName, null);
initFromPropertyOrDefault(PROPERTY_UPLOAD_REPO_SNAPSHOT_URL,
snapshotRepository::getUrl, snapshotRepository::setUrl, null);
initFromPropertyOrDefault(PROPERTY_UPLOAD_REPO_SNAPSHOT_USERNAME,
snapshotRepository::getUsername, snapshotRepository::setUsername, null);
initFromPropertyOrDefault(PROPERTY_UPLOAD_REPO_SNAPSHOT_PASSWORD,
snapshotRepository::getPassword, snapshotRepository::setPassword, null);

initFromPropertyOrDefault(PROPERTY_SECURITY, helm::getSecurity, helm::setSecurity, DEFAULT_SECURITY);

}

HelmRepository getHelmRepository() {
private HelmRepository getHelmRepository() {
if (getHelm().getVersion() != null && getHelm().getVersion().endsWith("-SNAPSHOT")) {
return getHelm().getSnapshotRepository();
}
Expand All @@ -130,11 +133,9 @@ HelmRepository getHelmRepository() {
* @param repository Helm repo with id and optional credentials.
* @throws IllegalArgumentException Unable to get authentication because of misconfiguration.
*/
void setAuthentication(HelmRepository repository)
throws SecDispatcherException {
String id = repository.getName();
private void setAuthentication(HelmRepository repository) throws SecDispatcherException {
final String id = repository.getName();
final String REPO = "Repo ";

if (repository.getUsername() != null) {
if (repository.getPassword() == null) {
throw new IllegalArgumentException(REPO + id + " has a username but no password defined.");
Expand All @@ -144,7 +145,7 @@ void setAuthentication(HelmRepository repository)

Server server = getSettings().getServer(id);
if (server == null) {
getKitLogger().info(
throw new IllegalArgumentException(
"No credentials found for " + id + " in configuration or settings.xml server list.");
} else {

Expand Down
Loading

0 comments on commit a2205dc

Please sign in to comment.