Skip to content
This repository was archived by the owner on Nov 13, 2019. It is now read-only.

Commit 4db5677

Browse files
authored
Merge pull request #7 from project-streamzi/proprerties
Gets default properties from server
2 parents 5076e80 + 88bd221 commit 4db5677

File tree

4 files changed

+121
-10
lines changed

4 files changed

+121
-10
lines changed

manager/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@
4545
<groupId>com.squareup.okhttp3</groupId>
4646
<artifactId>okhttp</artifactId>
4747
</dependency>
48+
4849
</dependencies>
4950
</project>

manager/src/main/java/io/streamzi/openshift/API.java

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package io.streamzi.openshift;
22

33

4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
46
import io.fabric8.kubernetes.api.model.*;
57
import io.fabric8.openshift.api.model.DeploymentConfig;
6-
import io.fabric8.openshift.api.model.DeploymentConfigBuilder;
78
import io.streamzi.openshift.dataflow.model.ProcessorFlow;
89
import io.streamzi.openshift.dataflow.model.ProcessorNodeTemplate;
910
import io.streamzi.openshift.dataflow.model.serialization.ProcessorFlowReader;
@@ -15,10 +16,7 @@
1516
import javax.enterprise.context.ApplicationScoped;
1617
import javax.ws.rs.*;
1718
import java.io.File;
18-
import java.util.ArrayList;
19-
import java.util.HashMap;
20-
import java.util.List;
21-
import java.util.Map;
19+
import java.util.*;
2220
import java.util.logging.Level;
2321
import java.util.logging.Logger;
2422

@@ -33,6 +31,8 @@ public class API {
3331
@EJB(beanInterface = ClientContainer.class)
3432
private ClientContainer container;
3533

34+
private final String bootstrapServersDefault = "my-cluster-kafka";
35+
3636
@GET
3737
@Path("/pods")
3838
@Produces("application/json")
@@ -98,6 +98,25 @@ public List<String> listProcessors() {
9898
//todo: look at applying labels to imagestreams and getting the necessary data from there.
9999
//todo: could apply special labels to the deployment configs to hold the graph structure.
100100

101+
// List<ImageStream> images = container.getOSClient().imageStreams().inAnyNamespace().withLabel("streamzi.io/kind", "processor").list().getItems();
102+
// for (ImageStream image : images) {
103+
// Map<String, String> labels = image.getMetadata().getLabels();
104+
// final ProcessorNodeTemplate template = new ProcessorNodeTemplate();
105+
// template.setId(labels.get("streamzi.io/processor/id"));
106+
// template.setDescription(labels.get("streamzi.io/processor/description"));
107+
// template.setName(labels.get("streamzi.io/processor/label"));
108+
// template.setImageName(labels.get("streamzi.io/processor/imagename"));
109+
//
110+
// String[] inputsLabel = labels.get("inputs").split(",");
111+
// List<String> inputs = new ArrayList<>(Arrays.asList(inputsLabel));
112+
//
113+
// String[] outputsLabel = labels.get("outputs").split(",");
114+
// List<String> outputs = new ArrayList<>(Arrays.asList(outputsLabel));
115+
//
116+
// template.setInputs(inputs);
117+
// template.setOutputs(outputs);
118+
// }
119+
101120
File[] templates = container.getTemplateDir().listFiles();
102121
if (templates != null) {
103122
for (File f : templates) {
@@ -202,16 +221,16 @@ public void postFlow(String flowJson) {
202221

203222
//remove DCs that are no longer required.
204223
List<DeploymentConfig> existingDCs = container.getOSClient().deploymentConfigs().inNamespace(container.getNamespace()).withLabel("app", flow.getName()).list().getItems();
205-
for(DeploymentConfig existingDC : existingDCs){
224+
for (DeploymentConfig existingDC : existingDCs) {
206225

207226
boolean found = false;
208-
for(DeploymentConfig newDC : deploymentConfigs){
209-
if(existingDC.getMetadata().getName().equals(newDC.getMetadata().getName())){
227+
for (DeploymentConfig newDC : deploymentConfigs) {
228+
if (existingDC.getMetadata().getName().equals(newDC.getMetadata().getName())) {
210229
found = true;
211230
}
212231
}
213232

214-
if(!found){
233+
if (!found) {
215234
logger.info("Removing DeploymentConfig: " + container.getNamespace() + "/" + existingDC.getMetadata().getName());
216235
container.getOSClient().deploymentConfigs().inNamespace(container.getNamespace()).withName(existingDC.getMetadata().getName()).delete();
217236
}
@@ -221,4 +240,26 @@ public void postFlow(String flowJson) {
221240
logger.log(Level.SEVERE, "Error parsing JSON flow data: " + e.getMessage(), e);
222241
}
223242
}
243+
244+
@GET
245+
@Path("/globalproperties")
246+
@Produces("application/json")
247+
public String getGlobalProperties() {
248+
final Properties props = new Properties();
249+
250+
String bootstrapServers = EnvironmentResolver.get("bootstrap.servers");
251+
if (bootstrapServers != null && !bootstrapServers.equals("")) {
252+
props.put("bootstrap_servers", bootstrapServers);
253+
} else {
254+
props.put("bootstrap_servers", bootstrapServersDefault);
255+
}
256+
257+
ObjectMapper mapper = new ObjectMapper();
258+
try {
259+
return mapper.writeValueAsString(props);
260+
} catch (JsonProcessingException e) {
261+
logger.severe(e.getMessage());
262+
return "{}";
263+
}
264+
}
224265
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.streamzi.openshift;
2+
3+
/**
4+
* Simple class to look for an environment variable and then System.properties
5+
* if it doesn't exist
6+
*
7+
* @author hhiden
8+
*/
9+
public class EnvironmentResolver {
10+
11+
public static String get(String key) {
12+
String resolved = resolve(key);
13+
if (resolved != null) {
14+
return resolved;
15+
}
16+
17+
resolved = resolve(key.toUpperCase());
18+
if (resolved != null) {
19+
return resolved;
20+
}
21+
22+
resolved = resolve(key.replace(".", "_"));
23+
if (resolved != null) {
24+
return resolved;
25+
}
26+
27+
resolved = resolve(key.replace(".", "_").replace("-", "_"));
28+
if (resolved != null) {
29+
return resolved;
30+
}
31+
32+
resolved = resolve(key
33+
.replace(".", "_")
34+
.replace("-", "_")
35+
.toUpperCase());
36+
37+
return resolved;
38+
}
39+
40+
private static String resolve(String key) {
41+
42+
String value = System.getenv(key);
43+
44+
if (value != null) {
45+
return value;
46+
} else {
47+
return System.getProperty(key);
48+
}
49+
50+
}
51+
}

manager/src/main/webapp/js/ui.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
blocks = new Blocks();
22
blocks.scale = 1.4;
33
var templateMap = {};
4+
var defaults = {};
45

56

67
// Override the addBlock method to add some more stuff
@@ -40,6 +41,11 @@ blocks.addBlock = function (name, x, y){
4041
blocks.run('#blocks');
4142
});
4243

44+
fetchDefaults(function(data){
45+
defaults = data;
46+
console.log(defaults);
47+
});
48+
4349
//setupBlocksJs(data);
4450

4551

@@ -210,7 +216,7 @@ function exportJson(flowName) {
210216
links: linksArray,
211217
settings: {},
212218
globalSettings: {
213-
STREAMZI_KAFKA_BOOTSTRAP_SERVER: "my-cluster-kafka-bootstrap:9092"
219+
STREAMZI_KAFKA_BOOTSTRAP_SERVER: defaults.bootstrap_servers
214220
}
215221
};
216222

@@ -312,6 +318,18 @@ function fetchNodeYaml(callback) {
312318
});
313319
}
314320

321+
function fetchDefaults(callback) {
322+
323+
var promise = $.ajax({
324+
url: "rest/api/globalproperties",
325+
type: 'GET',
326+
dataType: "json",
327+
contentType: "application/json; charset=utf-8"
328+
}).then(function (data) {
329+
callback(data);
330+
});
331+
}
332+
315333
// THIS ISN'T A GUID
316334
function guid() {
317335
function s4() {

0 commit comments

Comments
 (0)