Skip to content

Commit

Permalink
Remove dependency helm annotations from dekorate helm
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgitario committed Nov 10, 2023
1 parent 7fe18d8 commit 721c8a6
Show file tree
Hide file tree
Showing 20 changed files with 871 additions and 391 deletions.
15 changes: 2 additions & 13 deletions deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,8 @@
<artifactId>quarkus-kubernetes-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.dekorate</groupId>
<artifactId>helm-annotations</artifactId>
<classifier>noapt</classifier>
<exclusions>
<exclusion>
<groupId>io.sundr</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
</exclusion>
</exclusions>
<groupId>io.github.yaml-path</groupId>
<artifactId>yaml-path</artifactId>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.quarkiverse.helm.deployment;

import static org.apache.commons.lang3.StringUtils.*;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -14,19 +18,16 @@
import java.net.URL;
import java.nio.charset.Charset;

import org.apache.commons.lang3.StringUtils;
import org.jboss.logging.Logger;

import io.dekorate.utils.Strings;

public final class HelmChartUploader {

private static Logger LOGGER = Logger.getLogger(HelmProcessor.class);
private static final Logger LOGGER = Logger.getLogger(HelmProcessor.class);

private static String APPLICATION_GZIP = "application/gzip";
private static final String APPLICATION_GZIP = "application/gzip";
private static final String CONTENT_TYPE = "Content-Type";
private static String POST = "POST";
private static String PUT = "PUT";
private static final String POST = "POST";
private static final String PUT = "PUT";

private HelmChartUploader() {

Expand Down Expand Up @@ -61,7 +62,7 @@ static void pushToHelmRepository(File tarball, HelmRepository helmRepository) {
}

private static void validate(HelmRepository repository) {
if (repository.url().isEmpty() || Strings.isNullOrEmpty(repository.url().get())) {
if (repository.url().isEmpty() || isEmpty(repository.url().get())) {
throw new RuntimeException("The push to a Helm repository is enabled (the property `quarkus.helm.repository.push` "
+ "is true), but the repository URL was not provided (the property `quarkus.helm.repository.url`).");
}
Expand All @@ -71,8 +72,8 @@ private static void validate(HelmRepository repository) {
+ "is true), but the repository type was not provided (the property `quarkus.helm.repository.type`).");
}

if ((Strings.isNotNullOrEmpty(repository.getUsername()) && Strings.isNullOrEmpty(repository.getPassword()))
|| (Strings.isNotNullOrEmpty(repository.getPassword()) && Strings.isNullOrEmpty(repository.getUsername()))) {
if ((isNotEmpty(repository.getUsername()) && isEmpty(repository.getPassword()))
|| (isNotEmpty(repository.getPassword()) && isEmpty(repository.getUsername()))) {
throw new RuntimeException("The push to a Helm repository is enabled (the property `quarkus.helm.repository.push` "
+ "is true), but either the username (the property `quarkus.helm.repository.username`) "
+ "or the password (the property `quarkus.helm.repository.password`) was not set.");
Expand Down Expand Up @@ -106,7 +107,7 @@ private static HttpURLConnection deductConnectionByRepositoryType(File tarball,
}

private static String formatRepositoryURL(File file, HelmRepository repository) {
return String.format("%s%s", StringUtils.appendIfMissing(repository.url().get(), "/"), file.getName());
return String.format("%s%s", appendIfMissing(repository.url().get(), "/"), file.getName());
}

private static HttpURLConnection createConnection(HelmRepository repository, String url) throws IOException {
Expand All @@ -119,7 +120,7 @@ private static HttpURLConnection createConnection(HelmRepository repository, Str
}

private static void verifyAndSetAuthentication(HelmRepository helmRepository) {
if (Strings.isNotNullOrEmpty(helmRepository.getUsername()) && Strings.isNotNullOrEmpty(helmRepository.getPassword())) {
if (isNotEmpty(helmRepository.getUsername()) && isNotEmpty(helmRepository.getPassword())) {
PasswordAuthentication authentication = new PasswordAuthentication(helmRepository.getUsername(),
helmRepository.getPassword().toCharArray());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package io.quarkiverse.helm.deployment;

import static io.github.yamlpath.utils.StringUtils.EMPTY;
import static io.quarkiverse.helm.deployment.HelmChartUploader.pushToHelmRepository;
import static io.quarkiverse.helm.deployment.utils.SystemPropertiesUtils.getPropertyFromSystem;
import static io.quarkiverse.helm.deployment.utils.SystemPropertiesUtils.getSystemProperties;
import static io.quarkiverse.helm.deployment.utils.SystemPropertiesUtils.hasSystemProperties;
import static io.quarkus.deployment.Capability.OPENSHIFT;
import static org.apache.commons.lang3.StringUtils.EMPTY;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -33,16 +32,9 @@

import io.dekorate.ConfigReference;
import io.dekorate.Session;
import io.dekorate.helm.config.HelmChartConfigBuilder;
import io.dekorate.helm.config.HelmDependencyBuilder;
import io.dekorate.helm.config.ValuesSchema;
import io.dekorate.helm.config.ValuesSchemaBuilder;
import io.dekorate.helm.config.ValuesSchemaProperty;
import io.dekorate.helm.config.ValuesSchemaPropertyBuilder;
import io.dekorate.kubernetes.config.ContainerBuilder;
import io.dekorate.kubernetes.decorator.AddInitContainerDecorator;
import io.dekorate.project.Project;
import io.dekorate.utils.Strings;
import io.quarkiverse.helm.deployment.decorators.LowPriorityAddEnvVarDecorator;
import io.quarkiverse.helm.deployment.utils.HelmConfigUtils;
import io.quarkus.deployment.Capabilities;
Expand Down Expand Up @@ -184,10 +176,6 @@ private void doGenerateResources(ApplicationInfoBuildItem app, OutputTargetBuild
final Map<String, Set<File>> deploymentTargets = toDeploymentTargets(dekorateOutput.getGeneratedFiles(),
generatedResources);

// Config
io.dekorate.helm.config.HelmChartConfig dekorateHelmChartConfig = toDekorateHelmChartConfig(app, config);
List<ConfigReference> valueReferencesFromUser = toValueReferences(config);

// Deduct deployment target to push
String deploymentTargetToPush = deductDeploymentTarget(config, deploymentTargets);

Expand All @@ -197,14 +185,14 @@ private void doGenerateResources(ApplicationInfoBuildItem app, OutputTargetBuild
Path chartOutputFolder = outputFolder.resolve(deploymentTarget);
deleteOutputHelmFolderIfExists(chartOutputFolder);

Map<String, String> generated = helmWriter.writeHelmFiles(project,
dekorateHelmChartConfig,
valueReferencesFromUser,
Map<String, String> generated = helmWriter.writeHelmFiles(
config.name().orElse(app.getName()),
project,
config,
getConfigReferencesFromSession(deploymentTarget, dekorateOutput),
inputFolder,
chartOutputFolder,
filesInDeploymentTarget.getValue(),
config.valuesProfileSeparator());
filesInDeploymentTarget.getValue());

// Push to Helm repository if enabled
if (config.repository().push() && deploymentTargetToPush.equals(deploymentTarget)) {
Expand Down Expand Up @@ -359,125 +347,6 @@ private Map<String, Set<File>> toDeploymentTargets(List<String> generatedFiles,
return filesByDeploymentTarget;
}

private io.dekorate.helm.config.HelmChartConfig toDekorateHelmChartConfig(ApplicationInfoBuildItem app,
HelmChartConfig config) {
HelmChartConfigBuilder builder = new HelmChartConfigBuilder()
.withEnabled(config.enabled())
.withApiVersion(config.apiVersion())
.withName(config.name().orElse(app.getName()))
.withCreateTarFile(config.createTarFile() || config.repository().push())
.withCreateValuesSchemaFile(config.createValuesSchemaFile())
.withCreateReadmeFile(config.createReadmeFile())
.withVersion(config.version().orElse(app.getVersion()))
.withExtension(config.extension())
.withValuesRootAlias(config.valuesRootAlias())
.withNotes(config.notes());
config.description().ifPresent(builder::withDescription);
config.keywords().ifPresent(builder::addAllToKeywords);
config.icon().ifPresent(builder::withIcon);
config.condition().ifPresent(builder::withCondition);
config.tags().ifPresent(builder::withTags);
config.appVersion().ifPresent(builder::withAppVersion);
config.deprecated().ifPresent(builder::withDeprecated);
config.annotations().entrySet().forEach(e -> builder.addNewAnnotation(e.getKey(), e.getValue()));
config.kubeVersion().ifPresent(builder::withKubeVersion);
config.type().ifPresent(builder::withType);
config.home().ifPresent(builder::withHome);
config.sources().ifPresent(builder::addAllToSources);
config.maintainers().entrySet()
.forEach(e -> builder.addNewMaintainer(
defaultString(e.getValue().name(), e.getKey()),
defaultString(e.getValue().email()),
defaultString(e.getValue().url())));
config.dependencies().entrySet()
.forEach(e -> builder.addToDependencies(toDekorateHelmDependencyConfig(e.getKey(), e.getValue())));
config.tarFileClassifier().ifPresent(builder::withTarFileClassifier);
config.expressions().values().forEach(e -> builder.addNewExpression(e.path(), e.expression()));
config.addIfStatement().entrySet()
.forEach(e -> {
builder.addNewAddIfStatement(
defaultString(e.getValue().property(), e.getKey()),
defaultString(e.getValue().onResourceKind()),
defaultString(e.getValue().onResourceName()),
e.getValue().withDefaultValue(),
e.getValue().description());
});

builder.withValuesSchema(toValuesSchema(config.valuesSchema()));

return builder.build();
}

private ValuesSchema toValuesSchema(ValuesSchemaConfig valuesSchema) {
List<ValuesSchemaProperty> properties = new ArrayList<>();
for (Map.Entry<String, ValuesSchemaPropertyConfig> property : valuesSchema.properties().entrySet()) {
String name = property.getValue().name().orElse(property.getKey());

properties.add(new ValuesSchemaPropertyBuilder()
.withName(name)
.withType(property.getValue().type())
.withDescription(defaultString(property.getValue().description()))
.withMaximum(property.getValue().maximum().orElse(Integer.MAX_VALUE))
.withMinimum(property.getValue().minimum().orElse(Integer.MIN_VALUE))
.withRequired(property.getValue().required())
.withPattern(defaultString(property.getValue().pattern()))
.build());
}

return new ValuesSchemaBuilder()
.withTitle(valuesSchema.title())
.withProperties(properties.toArray(new ValuesSchemaProperty[0]))
.build();
}

private io.dekorate.helm.config.HelmDependency toDekorateHelmDependencyConfig(String dependencyName,
HelmDependencyConfig dependency) {
HelmDependencyBuilder builder = new HelmDependencyBuilder()
.withName(defaultString(dependency.name(), dependencyName))
.withAlias(defaultString(dependency.alias(), defaultString(dependency.name(), dependencyName)))
.withVersion(dependency.version())
.withRepository(dependency.repository())
.withCondition(defaultString(dependency.condition()))
.withTags(defaultArray(dependency.tags()))
.withEnabled(dependency.enabled().orElse(true));

return builder.build();
}

private List<ConfigReference> toValueReferences(HelmChartConfig config) {
return config.values().entrySet().stream()
.map(e -> new ConfigReference.Builder(defaultString(e.getValue().property(), e.getKey()),
defaultArray(e.getValue().paths()))
.withValue(toValue(e.getValue()))
.withDescription(defaultString(e.getValue().description(), EMPTY))
.withExpression(defaultString(e.getValue().expression()))
.withProfile(defaultString(e.getValue().profile()))
.withRequired(e.getValue().required())
.withPattern(defaultString(e.getValue().pattern()))
.withMaximum(e.getValue().maximum().orElse(Integer.MAX_VALUE))
.withMinimum(e.getValue().minimum().orElse(Integer.MIN_VALUE))
.build())
.collect(Collectors.toList());
}

private Object toValue(ValueReferenceConfig v) {
if (v.valueAsInt().isPresent()) {
return v.valueAsInt().get();
} else if (v.valueAsBool().isPresent()) {
return v.valueAsBool().get();
} else if (!v.valueAsMap().isEmpty()) {
return v.valueAsMap();
} else if (v.valueAsList().isPresent()) {
return v.valueAsList().get();
}

return v.value().orElse(null);
}

private String defaultString(Optional<String> value) {
return defaultString(value, null);
}

private String defaultString(Optional<String> value, String defaultStr) {
if (value.isEmpty() || StringUtils.isEmpty(value.get())) {
return defaultStr;
Expand All @@ -486,10 +355,6 @@ private String defaultString(Optional<String> value, String defaultStr) {
return value.get();
}

private static String[] defaultArray(Optional<List<String>> optional) {
return optional.map(l -> l.toArray(new String[0])).orElse(new String[0]);
}

private String mapProperty(String deploymentName, BuildProducer<DecoratorBuildItem> decorators, String property,
Map<String, String> propertiesFromConfigSource) {
if (!hasSystemProperties(property)) {
Expand Down Expand Up @@ -542,7 +407,7 @@ public static String getDeploymentName(Capabilities capabilities, ApplicationInf
}

private boolean isPropertiesConfigSource(String sourceName) {
return Strings.isNotNullOrEmpty(sourceName) && sourceName.startsWith(PROPERTIES_CONFIG_SOURCE);
return StringUtils.isNotEmpty(sourceName) && sourceName.startsWith(PROPERTIES_CONFIG_SOURCE);
}

private boolean isBuildTimeProperty(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import java.util.Optional;

import io.dekorate.utils.Strings;
import org.apache.commons.lang3.StringUtils;

import io.smallrye.config.WithDefault;

public interface HelmRepository {
Expand Down Expand Up @@ -39,10 +40,10 @@ public interface HelmRepository {
Optional<String> password();

default String getUsername() {
return username().filter(Strings::isNotNullOrEmpty).orElse(null);
return username().filter(StringUtils::isNotEmpty).orElse(null);
}

default String getPassword() {
return password().filter(Strings::isNotNullOrEmpty).orElse(null);
return password().filter(StringUtils::isNotEmpty).orElse(null);
}
}
Loading

0 comments on commit 721c8a6

Please sign in to comment.