Skip to content

Commit

Permalink
Adding support for jinja env lookup and k8s query
Browse files Browse the repository at this point in the history
  • Loading branch information
computate committed Jun 1, 2024
1 parent 4bba482 commit 05075c6
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 54 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<jfreechart.version>1.5.3</jfreechart.version>
<handlebars.version>4.3.0</handlebars.version>
<jinjava.version>2.7.2</jinjava.version>
<kubernetes-client.version>15.0.1</kubernetes-client.version>
</properties>

<modelVersion>4.0.0</modelVersion>
Expand Down Expand Up @@ -142,6 +143,13 @@
<version>${jinjava.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.fabric8/kubernetes-client -->
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>${kubernetes-client.version}</version>
</dependency>

<!-- Jackson Data -->

<dependency>
Expand Down
100 changes: 84 additions & 16 deletions src/main/java/org/computate/frFR/java/ConfigSite.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -29,6 +30,7 @@
import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
Expand All @@ -45,9 +47,17 @@

import com.hubspot.jinjava.Jinjava;
import com.hubspot.jinjava.JinjavaConfig;
import com.hubspot.jinjava.lib.fn.ELFunctionDefinition;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.kubernetes.client.common.KubernetesObject;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Secret;
import io.kubernetes.client.util.Config;


/**
* NomCanonique.enUS: org.computate.enUS.java.SiteConfig enUS: Loads the
Expand All @@ -57,6 +67,79 @@
*/
public class ConfigSite {

public static String lookup(String type, String arg1) {
if("env".equals(type))
return System.getenv(arg1);
return null;
}

public static KubernetesObject query(String type, String kind, String resource_name, String namespace) {
try {
if("kubernetes.core.k8s".equals(type)) {
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
if("Secret".equals(kind)) {
V1Secret secret = api.readNamespacedSecret(resource_name, namespace, "false");
return secret;
}
}
} catch(Exception ex) {
ExceptionUtils.rethrow(ex);
}
return null;
}

public static JsonObject getConfiguration(YAMLConfiguration classeLangueConfig) {
JsonObject configuration = null;

try {
String configChemin = System.getenv(classeLangueConfig.getString("var_CONFIG_VARS_CHEMIN"));
JinjavaConfig jinjavaConfig = new JinjavaConfig();
Jinjava jinjava = new Jinjava(jinjavaConfig);

jinjava.registerFunction(new ELFunctionDefinition("", "lookup", ConfigSite.class, "lookup", String.class, String.class));
jinjava.registerFunction(new ELFunctionDefinition("", "query", ConfigSite.class, "query", String.class, String.class, String.class, String.class));

File configFichier = new File(configChemin);
String template = Files.readString(configFichier.toPath());
HashMap<String, Object> ctx = new HashMap<>();
configuration = new JsonObject();
ctx.put(classeLangueConfig.getString("var_SITE_NOM"), System.getenv(classeLangueConfig.getString("var_SITE_NOM")));
ctx.put(classeLangueConfig.getString("var_SITE_CHEMIN"), System.getenv(classeLangueConfig.getString("var_SITE_CHEMIN")));
ctx.put(classeLangueConfig.getString("var_SITE_PREFIXE"), System.getenv(classeLangueConfig.getString("var_SITE_PREFIXE")));
ctx.put("COMPUTATE_SRC", System.getenv("COMPUTATE_SRC"));
Yaml yaml = new Yaml();
Map<String, Object> map = yaml.load(template);
for(String key : map.keySet()) {
Object val = map.get(key);
if(val instanceof String) {
String rendered = jinjava.render(val.toString(), ctx);
ctx.put(key, rendered);
configuration.put(key, rendered);
} else if(val instanceof ArrayList) {
List<Object> list1 = (List<Object>)val;
JsonArray list2 = new JsonArray();
for(Object item : list1) {
if(item instanceof String) {
String rendered = jinjava.render(item.toString(), ctx);
list2.add(rendered);
} else {
list2.add(item);
}
configuration.put(key, list2);
}
} else {
ctx.put(key, val);
configuration.put(key, val);
}
}
} catch(Exception ex) {
ExceptionUtils.rethrow(ex);
}
return configuration;
}

protected final Logger LOG = LoggerFactory.getLogger(getClass());

Pattern SPECIAL_REGEX_CHARS = Pattern.compile("[{}()\\[\\].+*?^$\\\\|]");
Expand Down Expand Up @@ -221,21 +304,7 @@ protected void _fichierConfig() throws Exception {
* r: fichierConfig r.enUS: configFile
**/
protected void _config() throws Exception {
String siteConfigChemin = System.getenv(classeLangueConfig.getString("var_CONFIG_VARS_CHEMIN"));
JinjavaConfig jinjavaConfig = new JinjavaConfig();
Jinjava jinjava = new Jinjava(jinjavaConfig);
File configFichier = new File(siteConfigChemin);
String template = Files.readString(configFichier.toPath());
template = template.replace("{{ lookup('env', 'HOME') }}", System.getenv("HOME"));
JsonObject ctx = new JsonObject();
ctx.put(classeLangueConfig.getString("var_SITE_NOM"), siteNom);
ctx.put(classeLangueConfig.getString("var_SITE_CHEMIN"), siteChemin);
ctx.put(classeLangueConfig.getString("var_SITE_PREFIXE"), sitePrefixe);
String configStr = jinjava.render(template, ctx.getMap());
System.out.println(configStr);
Yaml yaml = new Yaml();
Map<String, Object> map = yaml.load(configStr);
config = new JsonObject(map);
config = getConfiguration(classeLangueConfig);
}

/**
Expand Down Expand Up @@ -408,7 +477,6 @@ public void setSolrUrlComputate(String solrUrlComputate) {
* solrUrl r.enUS: solrUrl r: portSolr r.enUS: solrPort
**/
protected void _solrUrlComputate() throws Exception {
// solrUrlComputate = config.getString("solr.solrUrl", "http://localhost:" + portSolr + "/solr/computate");
solrUrlComputate = config
.getString(langueConfigGlobale.getString(ConfigCles.var_SOLR_URL_COMPUTATE));
}
Expand Down
22 changes: 1 addition & 21 deletions src/main/java/org/computate/frFR/java/RegarderClasse.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,8 @@ public static void main(String[] args) throws Exception {
String appComputate = System.getenv("COMPUTATE_SRC");
Configurations configurations = new Configurations();
YAMLConfiguration classeLangueConfig = configurations.fileBased(YAMLConfiguration.class, String.format("%s/src/main/resources/org/computate/i18n/i18n_%s.yaml", appComputate, classeLangueNom));
String SITE_NOM = System.getenv(classeLangueConfig.getString("var_SITE_NOM"));
String SITE_CHEMIN = System.getenv(classeLangueConfig.getString("var_SITE_CHEMIN"));
String SITE_PREFIXE = System.getenv(classeLangueConfig.getString("var_SITE_PREFIXE"));
String COMPUTATE_SRC = System.getenv("COMPUTATE_SRC");


String siteConfigChemin = System.getenv(classeLangueConfig.getString("var_CONFIG_VARS_CHEMIN"));
JinjavaConfig jinjavaConfig = new JinjavaConfig();
Jinjava jinjava = new Jinjava(jinjavaConfig);
File configFichier = new File(siteConfigChemin);
String template = Files.readString(configFichier.toPath());
template = template.replace("{{ lookup('env', 'HOME') }}", System.getenv("HOME"));
JsonObject ctx = new JsonObject();
ctx.put(classeLangueConfig.getString("var_SITE_NOM"), SITE_NOM);
ctx.put(classeLangueConfig.getString("var_SITE_CHEMIN"), SITE_CHEMIN);
ctx.put(classeLangueConfig.getString("var_SITE_PREFIXE"), SITE_PREFIXE);
ctx.put("COMPUTATE_SRC", COMPUTATE_SRC);
String configStr = jinjava.render(template, ctx.getMap());
System.out.println(configStr);
Yaml yaml = new Yaml();
Map<String, Object> map = yaml.load(configStr);
JsonObject siteConfig = new JsonObject(map);
JsonObject siteConfig = ConfigSite.getConfiguration(classeLangueConfig);

regarderClasse.args = args;
regarderClasse.initRegarderClasseBase(classeLangueNom, classeLangueConfig);
Expand Down
18 changes: 1 addition & 17 deletions src/main/java/org/computate/frFR/java/RegarderRepertoire.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,23 +256,7 @@ public static void main(String[] args) throws Exception {
regarderRepertoire.cheminSrcGenJava = SITE_CHEMIN + "/src/gen/java";
regarderRepertoire.cheminsBin.add(SITE_CHEMIN + "/src/main/resources");

regarderRepertoire.configChemin = System.getenv(classeLangueConfig.getString("var_CONFIG_VARS_CHEMIN"));
System.out.println(regarderRepertoire.configChemin);
regarderRepertoire.fichierConfig = new File(regarderRepertoire.configChemin);
JinjavaConfig jinjavaConfig = new JinjavaConfig();
Jinjava jinjava = new Jinjava(jinjavaConfig);
File configFichier = new File(regarderRepertoire.configChemin);
String template = Files.readString(configFichier.toPath());
template = template.replace("{{ lookup('env', 'HOME') }}", System.getenv("HOME"));
JsonObject ctx = new JsonObject();
ctx.put(classeLangueConfig.getString("var_SITE_NOM"), regarderRepertoire.SITE_NOM);
ctx.put(classeLangueConfig.getString("var_SITE_CHEMIN"), regarderRepertoire.SITE_CHEMIN);
ctx.put(classeLangueConfig.getString("var_SITE_PREFIXE"), regarderRepertoire.SITE_PREFIXE);
ctx.put("COMPUTATE_SRC", regarderRepertoire.COMPUTATE_SRC);
String configStr = jinjava.render(template, ctx.getMap());
Yaml yaml = new Yaml();
Map<String, Object> map = yaml.load(configStr);
regarderRepertoire.configuration = new JsonObject(map);
regarderRepertoire.configuration = ConfigSite.getConfiguration(classeLangueConfig);

regarderRepertoire.trace = true;
regarderRepertoire.initialiserRegarderRepertoire(classeLangueConfig);
Expand Down

0 comments on commit 05075c6

Please sign in to comment.