Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
Merge pull request #39 from bonndan/remove_state_provider
Browse files Browse the repository at this point in the history
Remove state provider
  • Loading branch information
bonndan committed Oct 28, 2019
2 parents 9a6af44 + 1ebd7c9 commit 8999121
Show file tree
Hide file tree
Showing 37 changed files with 232 additions and 579 deletions.
17 changes: 0 additions & 17 deletions docs/source/model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,3 @@ will not be overwritten by templates applied later.
statuses
For CQ queries, read https://github.com/npgall/cqengine#string-based-queries-sql-and-cqn-dialects.

Service state (alpha)
---------------------

You can also add state providers which are used to gather live data and thereby provide state for the items. Currently only prometheus is supported.

.. code-block:: yaml
:linenos:
identifier: nivio:example
name: Landscape example
...
stateProviders:
- type: prometheus-exporter
target: http://prometheus_exporter.url
2 changes: 1 addition & 1 deletion src/main/java/de/bonndan/nivio/api/ApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public ProcessLog deleteService(
if (from == null)
return new ProcessLog(new ProcessingException(landscape, "Could use fully qualified identifier " + fqi));

Optional<LandscapeItem> item = ServiceItems.find(FullyQualifiedIdentifier.build(from.getLandscape(), from.getGroup(), from.getIdentifier()), landscape.getItems());
Optional<LandscapeItem> item = Items.find(FullyQualifiedIdentifier.build(from.getLandscape(), from.getGroup(), from.getIdentifier()), landscape.getItems());
if (!item.isPresent()) {
return new ProcessLog(new ProcessingException(landscape, "Could find item " + fqi));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public static LandscapeDTO from(Landscape item) {
l.identifier = item.getIdentifier();
l.name = item.getName();
l.contact = item.getContact();
l.stateProviders = item.getStateProviders();
l.source = item.getSource();

return l;
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/de/bonndan/nivio/api/dto/LandscapeDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import de.bonndan.nivio.model.GroupItem;
import de.bonndan.nivio.model.LandscapeConfig;
import de.bonndan.nivio.model.Landscape;
import de.bonndan.nivio.model.StateProviderConfig;
import org.springframework.hateoas.ResourceSupport;

import java.util.List;
Expand All @@ -17,7 +16,6 @@ public class LandscapeDTO extends ResourceSupport implements Landscape {
public String identifier;
public String name;
public String contact;
public List<StateProviderConfig> stateProviders;
public String source;

@Override
Expand All @@ -40,11 +38,6 @@ public String getSource() {
return source;
}

@Override
public List<StateProviderConfig> getStateProviders() {
return stateProviders;
}

@Override
public LandscapeConfig getConfig() {
return null;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/de/bonndan/nivio/input/Indexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void diff(final LandscapeDescription input, final LandscapeImpl landscap
kept.forEach(
item -> {

ItemDescription description = (ItemDescription) ServiceItems.find(item.getFullyQualifiedIdentifier(), input.getItemDescriptions()).orElse(null);
ItemDescription description = (ItemDescription) Items.find(item.getFullyQualifiedIdentifier(), input.getItemDescriptions()).orElse(null);
if (description == null) {
if (input.isPartial()) {
inLandscape.add((Item) item);
Expand Down Expand Up @@ -132,7 +132,7 @@ private void fillGroups(LandscapeDescription input, LandscapeImpl landscape) {
GroupDescription description = (GroupDescription) groupItem;
Group group = (Group) landscape.getGroups().get(description.getIdentifier());
description.getContains().forEach(condition -> {
group.getItems().addAll(ServiceItems.filter(condition, List.copyOf(landscape.getItems())));
group.getItems().addAll(Items.filter(condition, List.copyOf(landscape.getItems())));
});
});
}
Expand Down Expand Up @@ -163,7 +163,7 @@ private void linkAllProviders(Set<Item> items, LandscapeDescription landscapeDes
items.forEach(
service -> {
ItemDescription description =
(ItemDescription) ServiceItems.find(service.getFullyQualifiedIdentifier(), landscapeDescription.getItemDescriptions()).orElse(null);
(ItemDescription) Items.find(service.getFullyQualifiedIdentifier(), landscapeDescription.getItemDescriptions()).orElse(null);
if (description == null) {
if (isPartial)
return;
Expand All @@ -179,7 +179,7 @@ private void linkAllProviders(Set<Item> items, LandscapeDescription landscapeDes
Item provider;
try {
var fqi = FullyQualifiedIdentifier.from(providerName);
provider = (Item) ServiceItems.find(fqi, items).orElse(null);
provider = (Item) Items.find(fqi, items).orElse(null);
if (provider == null) {
logger.warn("Could not find service " + fqi + " in landscape " + landscapeDescription + " while linking providers for service " + description.getFullyQualifiedIdentifier());
return;
Expand All @@ -202,7 +202,7 @@ private void linkAllProviders(Set<Item> items, LandscapeDescription landscapeDes

private void linkDataflow(final LandscapeDescription input, final LandscapeImpl landscape, ProcessLog logger) {
input.getItemDescriptions().forEach(serviceDescription -> {
Item origin = (Item) ServiceItems.pick(serviceDescription, landscape.getItems());
Item origin = (Item) Items.pick(serviceDescription, landscape.getItems());
if (!input.isPartial() && origin.getDataFlow().size() > 0) {
logger.info("Clearing dataflow of " + origin);
origin.getDataFlow().clear(); //delete all dataflow on full update
Expand All @@ -211,7 +211,7 @@ private void linkDataflow(final LandscapeDescription input, final LandscapeImpl
serviceDescription.getDataFlow().forEach(description -> {

var fqi = FullyQualifiedIdentifier.from(description.getTarget());
Item target = (Item) ServiceItems.find(fqi, landscape.getItems()).orElse(null);
Item target = (Item) Items.find(fqi, landscape.getItems()).orElse(null);
if (target == null) {
logger.warn("Dataflow target service " + description.getTarget() + " not found");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import de.bonndan.nivio.input.http.HttpService;
import de.bonndan.nivio.input.kubernetes.ItemDescriptionFactoryKubernetes;
import de.bonndan.nivio.input.nivio.ItemDescriptionFactoryNivio;
import de.bonndan.nivio.input.rancher1.ItemDescriptionFactoryRancher1;
import de.bonndan.nivio.util.URLHelper;

import java.net.URL;
Expand All @@ -28,11 +29,14 @@ public static ItemDescriptionFactory getFactory(SourceReference reference, Lands
if (reference == null || reference.getFormat() == null)
return new ItemDescriptionFactoryNivio(fetcher, baseUrl);

//TODO use SourceFormat.from or similar
switch (reference.getFormat()) {
case DOCKER_COMPOSE2:
return new ItemDescriptionFactoryCompose2(new FileFetcher(new HttpService()), baseUrl);
case KUBERNETES:
return new ItemDescriptionFactoryKubernetes(reference);
case RANCHER1_PROMETHEUS:
return new ItemDescriptionFactoryRancher1(baseUrl);
}

return new ItemDescriptionFactoryNivio(fetcher, baseUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import de.bonndan.nivio.input.dto.LandscapeDescription;
import de.bonndan.nivio.input.dto.ItemDescription;
import de.bonndan.nivio.model.LandscapeItem;
import de.bonndan.nivio.model.ServiceItems;
import de.bonndan.nivio.model.Items;

import java.util.ArrayList;
import java.util.List;

import static de.bonndan.nivio.model.ServiceItems.find;
import static de.bonndan.nivio.model.Items.find;

public class SourceReferencesResolver {

Expand All @@ -31,7 +31,7 @@ public void resolve(final LandscapeDescription env, final ProcessLog log) {
}

value.forEach(identifier -> {
ServiceItems.filter(identifier, descriptions)
Items.filter(identifier, descriptions)
.forEach(item -> ItemDescriptionFactory.assignTemplateValues((ItemDescription) item, (ItemDescription) template));
});

Expand All @@ -57,12 +57,12 @@ private void resolveTemplateQueries(final List<ItemDescription> itemDescriptions
List<String> providedBy = description.getProvidedBy();
description.setProvidedBy(new ArrayList<>());
providedBy.forEach(condition -> {
ServiceItems.filter(condition, itemDescriptions)
Items.filter(condition, itemDescriptions)
.forEach(result -> description.getProvidedBy().add(result.getIdentifier()));
});

description.getDataFlow().forEach(dataFlowItem -> {
ServiceItems.filter(dataFlowItem.getTarget(), itemDescriptions).stream()
Items.filter(dataFlowItem.getTarget(), itemDescriptions).stream()
.findFirst()
.ifPresent(service -> ((DataFlowDescription)dataFlowItem).setTarget(service.getFullyQualifiedIdentifier().toString()));
});
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/bonndan/nivio/input/dto/ItemDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public ItemDescription(String identifier) {
this.identifier = identifier;
}

public ItemDescription(FullyQualifiedIdentifier fqi) {
this.identifier = fqi.getIdentifier();
this.group = fqi.getGroup();
}

public String getIdentifier() {
return identifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ public class LandscapeDescription implements Landscape {
*/
private List<ItemDescription> itemDescriptions = new ArrayList<>();

private List<StateProviderConfig> stateProviders;

private LandscapeConfig config;

private boolean isPartial = false;
Expand Down Expand Up @@ -94,15 +92,6 @@ public String getContact() {
return contact;
}

@Override
public List<StateProviderConfig> getStateProviders() {
return stateProviders;
}

public void setStateProviders(List<StateProviderConfig> stateProviders) {
this.stateProviders = stateProviders;
}

public List<SourceReference> getSourceReferences() {
return sources;
}
Expand Down Expand Up @@ -141,7 +130,6 @@ public LandscapeImpl toLandscape() {
landscape.setName(name);
landscape.setContact(contact);
landscape.setSource(source);
landscape.setStateProviders(stateProviders);
landscape.setConfig(config);
return landscape;
}
Expand All @@ -154,7 +142,7 @@ public void addItems(List<ItemDescription> incoming) {
desc.setEnvironment(this.identifier);

ItemDescription existing = (ItemDescription)
ServiceItems.find(desc.getIdentifier(), desc.getGroup(), itemDescriptions).orElse(null);
Items.find(desc.getIdentifier(), desc.getGroup(), itemDescriptions).orElse(null);
if (existing != null) {
ItemDescriptionFactory.assignNotNull(existing, desc);
} else {
Expand Down
27 changes: 13 additions & 14 deletions src/main/java/de/bonndan/nivio/input/dto/SourceFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@

public enum SourceFormat {

//TODO move to package, use annotations or service loader
DOCKER_COMPOSE2("docker-compose-v2"),
KUBERNETES("kubernetes", "k8s"),
NIVIO("nivio", "");
NIVIO("nivio", ""),
RANCHER1_PROMETHEUS("rancher1-prometheus");

private final List<String> formats;

private static final List<SourceFormat> KNOWN_FORMATS;

static {
KNOWN_FORMATS = Arrays.asList(NIVIO, DOCKER_COMPOSE2, KUBERNETES, RANCHER1_PROMETHEUS);
}

SourceFormat(String format, String... formats) {

this.formats = new ArrayList<>();
Expand All @@ -30,18 +38,9 @@ public static SourceFormat from(String format) {
return NIVIO;
format = format.toLowerCase();

if (DOCKER_COMPOSE2.formats.contains(format)) {
return DOCKER_COMPOSE2;
}

if (NIVIO.formats.contains(format)) {
return NIVIO;
}

if (KUBERNETES.formats.contains(format)) {
return KUBERNETES;
}

return NIVIO;
String finalFormat = format;
return KNOWN_FORMATS.stream()
.filter(sourceFormat -> sourceFormat.formats.contains(finalFormat))
.findFirst().orElse(NIVIO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import de.bonndan.nivio.input.dto.ItemDescription;
import de.bonndan.nivio.input.dto.SourceReference;
import de.bonndan.nivio.model.LandscapeItem;
import de.bonndan.nivio.model.ServiceItems;
import de.bonndan.nivio.model.Items;
import de.bonndan.nivio.util.URLHelper;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Pod;
Expand Down Expand Up @@ -87,7 +87,7 @@ private Collection<? extends ItemDescription> createDescriptionFromService(Servi
if (selector != null)
targetId = selector.getOrDefault("app", null);
if (!StringUtils.isEmpty(targetId)) {
ServiceItems.find(targetId, group, items).ifPresent(provider -> {
Items.find(targetId, group, items).ifPresent(provider -> {
((ItemDescription) provider).getProvidedBy().add(description.getIdentifier());
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package de.bonndan.nivio.input.rancher1;

import de.bonndan.nivio.input.ItemDescriptionFactory;
import de.bonndan.nivio.input.dto.ItemDescription;
import de.bonndan.nivio.input.dto.SourceReference;
import de.bonndan.nivio.util.URLHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class ItemDescriptionFactoryRancher1 implements ItemDescriptionFactory {

private static final Logger logger = LoggerFactory.getLogger(ItemDescriptionFactoryRancher1.class);
private final URL baseUrl;

public ItemDescriptionFactoryRancher1(URL baseUrl) {
this.baseUrl = baseUrl;
}

@Override
public List<ItemDescription> getDescriptions(SourceReference reference) {

String landscape = reference.getLandscapeDescription().getIdentifier();

String combine = URLHelper.combine(baseUrl, reference.getUrl());
try {
URL url = new URL(combine);
PrometheusExporter prometheusExporter = new PrometheusExporter(landscape, url);
return prometheusExporter.getDescriptions();
} catch (MalformedURLException e) {
logger.error("Could not work on prometheus url {}", combine);
return new ArrayList<>();
}
}
}

0 comments on commit 8999121

Please sign in to comment.