Skip to content

Commit

Permalink
ENG-5487/ENG-5489 Support EntandoApp module-specific environment prop
Browse files Browse the repository at this point in the history
  • Loading branch information
w-caffiero-entando committed Mar 12, 2024
1 parent 1d4efdd commit 45fd495
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 63 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<properties>
<github.organization>entando-k8s</github.organization>
<sonar.projectKey>${github.organization}_${project.artifactId}</sonar.projectKey>
<entando-k8s-operator-common.version>7.3.1-ENG-5347-PR-152</entando-k8s-operator-common.version>
<entando-k8s-operator-common.version>7.3.1-ENG-5487-PR-158</entando-k8s-operator-common.version>
<skipLicenseDownload>true</skipLicenseDownload>
<preDeploymentTestGroups>in-process</preDeploymentTestGroups>
<postDeploymentTestGroups>smoke</postDeploymentTestGroups>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,32 @@ public Optional<String> getHealthCheckPath() {
return Optional.of("/app-builder/favicon-entando.png");
}

/**
* Provides environment variables generated from specific CR specs properties and/or contextual information.
*/
@Override
public List<EnvVar> getEnvironmentVariables() {
List<EnvVar> vars = new ArrayList<>();
vars.add(new EnvVar("DOMAIN", EntandoAppHelper.getNormalizedDeAppWebContextPath(entandoApp),
null));
return vars;
return EntandoAppHelper.subtractEnvironmentVariables(
getBaseEnvironmentVariables(),
entandoApp.getSpec().getEnvironmentVariablesAppBuilder()
);
}

/**
* Provides environment variables from the common and module-specific "environmentVariables" properties of the CR.
*/
@Override
public List<EnvVar> getEnvironmentVariableOverrides() {
return entandoApp.getSpec().getEnvironmentVariables();
return EntandoAppHelper.combineEnvironmentVariables(
entandoApp.getSpec().getEnvironmentVariables(),
entandoApp.getSpec().getEnvironmentVariablesAppBuilder()
);
}

private List<EnvVar> getBaseEnvironmentVariables() {
List<EnvVar> vars = new ArrayList<>();
vars.add(new EnvVar("DOMAIN", EntandoAppHelper.getNormalizedDeAppWebContextPath(entandoApp),
null));
return vars;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ public int getPrimaryPort() {
return 8083;
}

@Override
public List<EnvVar> getEnvironmentVariables() {
private List<EnvVar> getBaseEnvironmentVariables() {
List<EnvVar> vars = new ArrayList<>();
String entandoUrl = EntandoAppDeployableContainer.determineEntandoServiceBaseUrl(this.entandoApp);
vars.add(new EnvVar("ENTANDO_APP_NAME", entandoApp.getMetadata().getName(), null));
Expand Down Expand Up @@ -197,9 +196,26 @@ public String getVolumeMountPath() {
return "/entando-data";
}

/**
* Provides environment variables generated from specific CR specs properties and/or contextual information.
*/
@Override
public List<EnvVar> getEnvironmentVariables() {
return EntandoAppHelper.subtractEnvironmentVariables(
getBaseEnvironmentVariables(),
entandoApp.getSpec().getEnvironmentVariablesComponentManager()
);
}

/**
* Provides environment variables from the common and module-specific "environmentVariables" properties of the CR.
*/
@Override
public List<EnvVar> getEnvironmentVariableOverrides() {
return entandoApp.getSpec().getEnvironmentVariables();
return EntandoAppHelper.combineEnvironmentVariables(
entandoApp.getSpec().getEnvironmentVariables(),
entandoApp.getSpec().getEnvironmentVariablesComponentManager()
);
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package org.entando.kubernetes.controller.app;

import io.fabric8.kubernetes.api.model.EnvVar;
import java.util.List;

public class CustomConfigFromOperator {

private String ecrPostInitConfiguration;
private boolean tlsEnabled;

private List<EnvVar> environmentVariablesAppEngine;
private List<EnvVar> environmentVariablesComponentManager;
private List<EnvVar> environmentVariablesSso;

public boolean isTlsEnabled() {
return tlsEnabled;
}
Expand All @@ -28,27 +21,4 @@ public void setEcrPostInitConfiguration(String ecrPostInitConfiguration) {
this.ecrPostInitConfiguration = ecrPostInitConfiguration;
}

public List<EnvVar> getEnvironmentVariablesAppEngine() {
return environmentVariablesAppEngine;
}

public void setEnvironmentVariablesAppEngine(List<EnvVar> environmentVariablesAppEngine) {
this.environmentVariablesAppEngine = environmentVariablesAppEngine;
}

public List<EnvVar> getEnvironmentVariablesComponentManager() {
return environmentVariablesComponentManager;
}

public void setEnvironmentVariablesComponentManager(List<EnvVar> environmentVariablesComponentManager) {
this.environmentVariablesComponentManager = environmentVariablesComponentManager;
}

public List<EnvVar> getEnvironmentVariablesSso() {
return environmentVariablesSso;
}

public void setEnvironmentVariablesSso(List<EnvVar> environmentVariablesSso) {
this.environmentVariablesSso = environmentVariablesSso;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.entando.kubernetes.controller.spi.common.EntandoOperatorSpiConfig;
import org.entando.kubernetes.controller.spi.common.NameUtils;
import org.entando.kubernetes.controller.spi.common.ResourceUtils;
import org.entando.kubernetes.controller.spi.container.DeployableContainer;
import org.entando.kubernetes.controller.spi.container.ProvidedDatabaseCapability;
import org.entando.kubernetes.controller.spi.container.ProvidedSsoCapability;
import org.entando.kubernetes.controller.spi.deployable.IngressingDeployable;
Expand Down Expand Up @@ -116,15 +115,17 @@ public void run() {
final SsoConnectionInfo ssoConnectionInfo = provideSso();

// SETTINGS
final CustomConfigFromOperator customConfig = readEntandoAppCustomConfig();
final CustomConfigFromOperator customConfig = readOperatorCustomConfig();
final int timeoutForDbAware = calculateDbAwareTimeout();
final int timeoutForNonDbAware = EntandoOperatorSpiConfig.getPodReadinessTimeoutSeconds();

// MODULES
deployAppEngine(ssoConnectionInfo, dbConnectionInfo, customConfig, timeoutForDbAware);
deployAppBuilder(timeoutForNonDbAware);
deployComponentManager(ssoConnectionInfo, dbConnectionInfo, customConfig, timeoutForDbAware);
waitCompletionAndFinalized(timeoutForDbAware, timeoutForNonDbAware);

// FINALIZE
waitCompletionAndFinalize(timeoutForDbAware, timeoutForNonDbAware);
} catch (Exception e) {
attachControllerFailure(e, EntandoAppController.class, NameUtils.MAIN_QUALIFIER);
}
Expand All @@ -134,7 +135,7 @@ public void run() {
});
}

private void waitCompletionAndFinalized(int timeoutForDbAware, int timeoutForNonDbAware) throws InterruptedException, TimeoutException {
private void waitCompletionAndFinalize(int timeoutForDbAware, int timeoutForNonDbAware) throws InterruptedException, TimeoutException {
executor.shutdown();
final int totalTimeout = timeoutForDbAware * 2 + timeoutForNonDbAware;
if (!executor.awaitTermination(totalTimeout, TimeUnit.SECONDS)) {
Expand All @@ -148,14 +149,19 @@ private void deployAppBuilder(int timeoutForNonDbAware) {
queueDeployable(new AppBuilderDeployable(entandoApp.get()), timeoutForNonDbAware);
}

private void deployComponentManager(SsoConnectionInfo ssoConnectionInfo, DatabaseConnectionInfo dbConnectionInfo,
CustomConfigFromOperator customConfig, int timeoutForDbAware) {
EntandoK8SService k8sService = new EntandoK8SService(
k8sClientForControllers.loadControllerService(EntandoAppController.ENTANDO_K8S_SERVICE));
queueDeployable(
new ComponentManagerDeployable(entandoApp.get(), ssoConnectionInfo, k8sService, dbConnectionInfo,
simpleK8SClient.secrets(), customConfig),
timeoutForDbAware);
private void deployComponentManager(
SsoConnectionInfo ssoConnectionInfo, DatabaseConnectionInfo dbConnectionInfo,
CustomConfigFromOperator customConfig, int timeoutForDbAware
) {
var k8sService = new EntandoK8SService(
k8sClientForControllers.loadControllerService(EntandoAppController.ENTANDO_K8S_SERVICE)
);
var deployable = new ComponentManagerDeployable(
entandoApp.get(), ssoConnectionInfo, k8sService, dbConnectionInfo,
simpleK8SClient.secrets(), customConfig
);

queueDeployable(deployable, timeoutForDbAware);
}

private void deployAppEngine(
Expand All @@ -170,7 +176,7 @@ private void deployAppEngine(
queueDeployable(deployable, timeoutForDbAware);
}

private CustomConfigFromOperator readEntandoAppCustomConfig() {
private CustomConfigFromOperator readOperatorCustomConfig() {
CustomConfigFromOperator customConfig = new CustomConfigFromOperator();
customConfig.setEcrPostInitConfiguration(lookupProperty(KEY_ENTANDO_ECR_POSTINIT_CONFIGURATION).orElse(""));
customConfig.setTlsEnabled(StringUtils.isNotBlank(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,6 @@ public String getNameQualifier() {
return NameUtils.DEFAULT_SERVER_QUALIFIER;
}

@Override
public List<EnvVar> getEnvironmentVariables() {
var vars = getBaseEnvironmentVariables();
vars.addAll(customConfig.getEnvironmentVariablesAppEngine());
return vars;
}

@Override
public int getPrimaryPort() {
return PORT;
Expand Down Expand Up @@ -229,9 +222,26 @@ public Optional<EntandoResourceRequirements> getResourceRequirementsOverride() {
return entandoApp.getSpec().getResourceRequirements();
}

/**
* Provides environment variables generated from specific CR specs properties and/or contextual information.
*/
@Override
public List<EnvVar> getEnvironmentVariables() {
return EntandoAppHelper.subtractEnvironmentVariables(
getBaseEnvironmentVariables(),
entandoApp.getSpec().getEnvironmentVariablesAppEngine()
);
}

/**
* Provides environment variables from the common and module-specific "environmentVariables" properties of the CR.
*/
@Override
public List<EnvVar> getEnvironmentVariableOverrides() {
return entandoApp.getSpec().getEnvironmentVariables();
return EntandoAppHelper.combineEnvironmentVariables(
entandoApp.getSpec().getEnvironmentVariables(),
entandoApp.getSpec().getEnvironmentVariablesAppEngine()
);
}

@Override
Expand Down Expand Up @@ -273,7 +283,7 @@ public List<String> getCommand() {

@Override
public List<EnvVar> getEnvironmentVariables() {
return entandoAppDeployableContainer.getBaseEnvironmentVariables();
return entandoAppDeployableContainer.getDatabaseConnectionVariables();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

package org.entando.kubernetes.controller.app;

import io.fabric8.kubernetes.api.model.EnvVar;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.entando.kubernetes.controller.spi.common.EntandoOperatorComplianceMode;
import org.entando.kubernetes.controller.spi.common.EntandoOperatorSpiConfig;
Expand Down Expand Up @@ -66,4 +71,28 @@ private static String getRootIfBlankOrValue(String path) {
return StringUtils.isBlank(path) ? "/" : path;
}

public static List<EnvVar> combineEnvironmentVariables(List<EnvVar> a, List<EnvVar> b) {
if (a == null) {
a = new ArrayList<>();
}
if (b == null) {
b = new ArrayList<>();
}

return Stream.concat(a.stream(), b.stream()).distinct().collect(Collectors.toList());
}

public static List<EnvVar> subtractEnvironmentVariables(List<EnvVar> a, List<EnvVar> b) {
if (a == null) {
return new ArrayList<>();
}

if (b == null || b.isEmpty()) {
return new ArrayList<>(a);
}

return a.stream()
.filter(ea -> b.stream().noneMatch(eb -> eb.getName().equals(ea.getName())))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.mockito.Mockito.verify;

import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.LimitRange;
import io.fabric8.kubernetes.api.model.LimitRangeBuilder;
import io.fabric8.kubernetes.api.model.OwnerReference;
Expand All @@ -42,6 +43,7 @@
import io.qameta.allure.Description;
import io.qameta.allure.Feature;
import io.qameta.allure.Issue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.entando.kubernetes.controller.spi.common.DbmsVendorConfig;
Expand Down Expand Up @@ -93,8 +95,13 @@ class DeployedEntandoAppServerTest extends EntandoAppTestBase implements Variabl
void shouldDeployEntandoEapImageWithDefaultValues() {

initSecretsMock();

EntandoAppSpec spec = new EntandoAppSpecBuilder().withStandardServerImage(JeeServer.WILDFLY).build();

EntandoAppSpec spec = new EntandoAppSpecBuilder()
.withStandardServerImage(JeeServer.WILDFLY)
.withEnvironmentVariables(mkTestEnvVars(false))
.withEnvironmentVariablesAppEngine(mkTestEnvVars(true))
.withEnvironmentVariablesComponentManager(mkTestEnvVars(true))
.build();
this.app = new EntandoAppBuilder()
.withNewMetadata()
.withName(MY_APP)
Expand Down Expand Up @@ -161,6 +168,8 @@ void shouldDeployEntandoEapImageWithDefaultValues() {
mainDbPreprationJob);
verifyEntandoDbVariables(entandoApp, portDbSecret, "PORTDB", populator);
verifyEntandoDbVariables(entandoApp, servDbSecret, "SERVDB", populator);

verifyEntandoModulesVariables(entandoApp);
});

step("And a Kubernetes Deployment was created reflecting the requirements of the Entando Eap container:",
Expand Down Expand Up @@ -391,6 +400,17 @@ void shouldDeployEntandoEapImageWithDefaultValues() {
});
}

private static ArrayList<EnvVar> mkTestEnvVars(boolean overwrite) {
var res = new ArrayList<EnvVar>();
if (!overwrite) {
res.add(new EnvVar("TEST_VAR", "TEST_VALUE", null));
res.add(new EnvVar("TEST_VAR2", "TEST_VALUE2", null));
} else {
res.add(new EnvVar("TEST_VAR2", "TEST_VALUE2_OV", null));
}
return res;
}

private LimitRange getLimitRange(EntandoApp entandoApp) {
final OwnerReference ownerRef = new OwnerReferenceBuilder()
.withApiVersion("entando.org/v1")
Expand Down Expand Up @@ -657,4 +677,27 @@ private void verifyDbJobAdminVariables(EntandoApp entandoApp, Container initCont
verifyDbJobAdminCredentials("default-postgresql-dbms-in-namespace-admin-secret", initContainer);
}

private void verifyEntandoModulesVariables(EntandoApp entandoApp) {
String baseName = entandoApp.getMetadata().getName();
var ae = client.deployments().loadDeployment(entandoApp, baseName + "-" + NameUtils.DEFAULT_DEPLOYMENT_SUFFIX);
var ab = client.deployments().loadDeployment(entandoApp, baseName + "-ab-" + NameUtils.DEFAULT_DEPLOYMENT_SUFFIX);
var cm = client.deployments().loadDeployment(entandoApp, baseName + "-cm-" + NameUtils.DEFAULT_DEPLOYMENT_SUFFIX);

step("and module specific variables were applied to AppEngine", () -> {
var c = ae.getSpec().getTemplate().getSpec().getContainers().get(0);
assertThat(theVariableNamed("TEST_VAR").on(c)).isEqualTo("TEST_VALUE");
assertThat(theVariableNamed("TEST_VAR2").on(c)).isEqualTo("TEST_VALUE2_OV");
});
step("and module specific variables were applied to ComponentManager", () -> {
var c = cm.getSpec().getTemplate().getSpec().getContainers().get(0);
assertThat(theVariableNamed("TEST_VAR").on(c)).isEqualTo("TEST_VALUE");
assertThat(theVariableNamed("TEST_VAR2").on(c)).isEqualTo("TEST_VALUE2_OV");
});
step("and module specific variables were applied to AppBuilder", () -> {
var c = ab.getSpec().getTemplate().getSpec().getContainers().get(0);
assertThat(theVariableNamed("TEST_VAR").on(c)).isEqualTo("TEST_VALUE");
assertThat(theVariableNamed("TEST_VAR2").on(c)).isEqualTo("TEST_VALUE2");
});
}

}
Loading

0 comments on commit 45fd495

Please sign in to comment.