Skip to content

Commit

Permalink
wip: kubernetes executor database uri built
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolife999 committed Feb 9, 2024
1 parent 8caf4e0 commit e7b4015
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@ private Delimiters() {

public static final String SQL_TOKEN_DELIMITER = "_";
public static final String SQL_SCHEMA_DELIMITER = ".";

public static final String KUBERNETES_TOKEN_DELIMITER="-";

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
import fr.insee.arc.core.service.kubernetes.configuration.BuildJsonConfiguration;
import fr.insee.arc.core.service.kubernetes.security.BuildAccessToken;
import fr.insee.arc.utils.exception.ArcException;
import fr.insee.arc.utils.kubernetes.KubernetesService;
import fr.insee.arc.utils.kubernetes.bo.KubernetesServiceResult;
import fr.insee.arc.utils.kubernetes.KubernetesApiService;
import fr.insee.arc.utils.kubernetes.bo.KubernetesApiResult;

public class ApiManageExecutorDatabase {

private ApiManageExecutorDatabase() {
throw new IllegalStateException("Utility class");
}

public static List<KubernetesServiceResult> create() throws ArcException
public static List<KubernetesApiResult> create() throws ArcException
{
List<KubernetesServiceResult> results = new ArrayList<>();
List<KubernetesApiResult> results = new ArrayList<>();

// create stateful set of executor databases
results.add(KubernetesService.execute(
results.add(KubernetesApiService.execute(
BuildRestQuery.stateful().getUri(), //
BuildRestQuery.stateful().getHttpMethod(), //
BuildAccessToken.retrieve().getToken(), //
Expand All @@ -31,7 +31,7 @@ public static List<KubernetesServiceResult> create() throws ArcException

for (String serviceJsonConfiguration : BuildJsonConfiguration.services())
{
results.add(KubernetesService.execute(
results.add(KubernetesApiService.execute(
BuildRestQuery.service().getUri(), //
BuildRestQuery.service().getHttpMethod(), //
BuildAccessToken.retrieve().getToken(), //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.http.HttpMethod;

import fr.insee.arc.core.service.kubernetes.bo.RestQuery;
import fr.insee.arc.utils.kubernetes.KubernetesService;
import fr.insee.arc.utils.kubernetes.KubernetesApiService;
import fr.insee.arc.utils.ressourceUtils.PropertiesHandler;

public class BuildRestQuery {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import fr.insee.arc.core.service.p0initialisation.ApiInitialisationService;
import fr.insee.arc.utils.exception.ArcException;
import fr.insee.arc.utils.exception.ArcExceptionMessage;
import fr.insee.arc.utils.kubernetes.provider.KubernetesServiceLayer;
import fr.insee.arc.utils.ressourceUtils.PropertiesHandler;

public class BuildJsonConfiguration {
Expand Down Expand Up @@ -66,7 +67,8 @@ private static String service(int executorReplicaIndex) throws ArcException
{
PropertiesHandler properties = PropertiesHandler.getInstance();
return readConfiguration("kubernetes/executorDatabaseServiceTemplate.json"
, JsonFileParameter.EXECUTOR_LABEL, String.join(Delimiters.KUBERNETES_TOKEN_DELIMITER, properties.getKubernetesExecutorLabel(), String.valueOf(executorReplicaIndex)) //
, JsonFileParameter.EXECUTOR_LABEL,
KubernetesServiceLayer.getName(properties.getKubernetesExecutorLabel(), executorReplicaIndex)
)
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.http.HttpMethod;

import fr.insee.arc.utils.kubernetes.bo.KubernetesServiceResult;
import fr.insee.arc.utils.kubernetes.bo.KubernetesApiResult;

public class KubernetesService {
public class KubernetesApiService {

private KubernetesService() {
private KubernetesApiService() {
throw new IllegalStateException("Utility class");
}

public static KubernetesServiceResult execute(String urlProvided, HttpMethod httpMethod, String token,
public static KubernetesApiResult execute(String urlProvided, HttpMethod httpMethod, String token,
String json) {
int responseCode = -1;
StringBuilder response = new StringBuilder();
Expand Down Expand Up @@ -96,7 +96,7 @@ public void checkServerTrusted(java.security.cert.X509Certificate[] certs, Strin
response.append(ExceptionUtils.getStackTrace(e));
}

return new KubernetesServiceResult(responseCode, response.toString());
return new KubernetesApiResult(responseCode, response.toString());
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package fr.insee.arc.utils.kubernetes.bo;

public class KubernetesServiceResult {
public class KubernetesApiResult {

public KubernetesServiceResult(int responseCode, String response) {
public KubernetesApiResult(int responseCode, String response) {
super();
this.responseCode = responseCode;
this.response = response;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package fr.insee.arc.utils.kubernetes.provider;

public class KubernetesServiceLayer {

public static final String KUBERNETES_TOKEN_DELIMITER="-";
public static final String USER_NAME="postgres";


/**
* return service name for a given statefuleSet label and given replica index
* @param statefuleLabel
* @param executorReplicaIndex
* @return
*/
public static String getName(String statefulLabel, int executorReplicaIndex)
{
return String.join(KUBERNETES_TOKEN_DELIMITER, statefulLabel, String.valueOf(executorReplicaIndex));
}


public static String getUri(String statefulLabel, int executorReplicaIndex)
{
return "jdbc:postgresql://"+getName(statefulLabel, executorReplicaIndex)+":5432/defaultdb";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import fr.insee.arc.utils.exception.ArcException;
import fr.insee.arc.utils.exception.ArcExceptionMessage;
import fr.insee.arc.utils.kubernetes.provider.KubernetesServiceLayer;
import fr.insee.arc.utils.utils.ManipString;

@Service("properties")
Expand Down Expand Up @@ -427,21 +428,41 @@ public List<ConnectionAttribute> getConnectionProperties() {
if (this.connectionProperties == null) {
connectionProperties = new ArrayList<>();

String[] databaseUrls = ConnectionAttribute.unserialize(this.databaseUrl);
String[] databaseUsernames = ConnectionAttribute.unserialize(this.databaseUsername);
String[] databasePasswords = ConnectionAttribute.unserialize(this.databasePassword);
String[] databaseDriverClassNames = ConnectionAttribute.unserialize(this.databaseDriverClassName);

for (int tokenIndex = 0; tokenIndex < databaseUrls.length; tokenIndex++) {
if (this.isKubernetesActive())
{
connectionProperties
.add(new ConnectionAttribute(databaseUrls[tokenIndex], databaseUsernames[tokenIndex],
databasePasswords[tokenIndex], databaseDriverClassNames[tokenIndex]));
.add(new ConnectionAttribute(this.databaseUrl, this.databaseUsername, this.databasePassword, this.databaseDriverClassName));

for (int i=0; i<this.getKubernetesExecutorNumber(); i++)
{
connectionProperties
.add(new ConnectionAttribute(
KubernetesServiceLayer.getUri(this.kubernetesExecutorLabel, i) //
, KubernetesServiceLayer.USER_NAME //
, this.databasePassword //
, this.databaseDriverClassName //
));
}
}
else
{
String[] databaseUrls = ConnectionAttribute.unserialize(this.databaseUrl);
String[] databaseUsernames = ConnectionAttribute.unserialize(this.databaseUsername);
String[] databasePasswords = ConnectionAttribute.unserialize(this.databasePassword);
String[] databaseDriverClassNames = ConnectionAttribute.unserialize(this.databaseDriverClassName);

for (int tokenIndex = 0; tokenIndex < databaseUrls.length; tokenIndex++) {
connectionProperties
.add(new ConnectionAttribute(databaseUrls[tokenIndex], databaseUsernames[tokenIndex],
databasePasswords[tokenIndex], databaseDriverClassNames[tokenIndex]));
}
}
}

return this.connectionProperties;
}


public void setConnectionProperties(List<ConnectionAttribute> connectionProperties) {
this.connectionProperties = connectionProperties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import fr.insee.arc.core.service.kubernetes.ApiManageExecutorDatabase;
import fr.insee.arc.utils.exception.ArcException;
import fr.insee.arc.utils.kubernetes.bo.KubernetesServiceResult;
import fr.insee.arc.utils.kubernetes.bo.KubernetesApiResult;

@Service
public class ServiceViewKubernetes extends InteractorMaintenanceOperations {
Expand All @@ -17,9 +17,9 @@ public String createDatabases(Model model) {
StringBuilder result = new StringBuilder();

try {
List<KubernetesServiceResult> results = ApiManageExecutorDatabase.create();
List<KubernetesApiResult> results = ApiManageExecutorDatabase.create();

for (KubernetesServiceResult r : results)
for (KubernetesApiResult r : results)
{
result.append(r.toString());
}
Expand Down

0 comments on commit e7b4015

Please sign in to comment.