use of io.stackgres.operator.customresource.prometheus.ServiceMonitorSpec in project stackgres by ongres.
the class PrometheusIntegration method getServiceMonitors.
@NotNull
private Stream<HasMetadata> getServiceMonitors(StackGresClusterContext context, Map<String, String> labels, Prometheus prometheusConfig) {
return prometheusConfig.getPrometheusInstallations().stream().map(pi -> {
ServiceMonitor serviceMonitor = new ServiceMonitor();
serviceMonitor.setMetadata(new ObjectMetaBuilder().withNamespace(pi.getNamespace()).withName(AbstractEnvoy.serviceMonitorName(context)).withLabels(ImmutableMap.<String, String>builder().putAll(pi.getMatchLabels()).putAll(labels).build()).build());
ServiceMonitorSpec spec = new ServiceMonitorSpec();
serviceMonitor.setSpec(spec);
LabelSelector selector = new LabelSelector();
spec.setSelector(selector);
NamespaceSelector namespaceSelector = new NamespaceSelector();
namespaceSelector.setAny(true);
spec.setNamespaceSelector(namespaceSelector);
selector.setMatchLabels(labels);
Endpoint endpoint = new Endpoint();
endpoint.setPort(AbstractEnvoy.NAME);
endpoint.setPath("/stats/prometheus");
spec.setEndpoints(Collections.singletonList(endpoint));
return serviceMonitor;
});
}
use of io.stackgres.operator.customresource.prometheus.ServiceMonitorSpec in project stackgres by ongres.
the class PrometheusIntegration method generateResource.
@Override
public Stream<HasMetadata> generateResource(StackGresClusterContext context) {
final StackGresCluster cluster = context.getSource();
final Map<String, String> defaultLabels = labelFactory.clusterLabels(cluster);
Map<String, String> labels = new ImmutableMap.Builder<String, String>().putAll(labelFactory.clusterCrossNamespaceLabels(cluster)).build();
final String clusterNamespace = cluster.getMetadata().getNamespace();
final Stream<HasMetadata> resources = Stream.of(new ServiceBuilder().withNewMetadata().withNamespace(clusterNamespace).withName(serviceName(context)).withLabels(ImmutableMap.<String, String>builder().putAll(labels).put("container", POSTGRES_EXPORTER_CONTAINER_NAME).build()).endMetadata().withSpec(new ServiceSpecBuilder().withSelector(defaultLabels).withPorts(new ServicePortBuilder().withName(POSTGRES_EXPORTER_CONTAINER_NAME).withProtocol("TCP").withPort(9187).build()).build()).build());
Optional<Stream<HasMetadata>> serviceMonitors = context.getPrometheus().filter(c -> Optional.ofNullable(c.getCreateServiceMonitor()).orElse(false)).map(c -> c.getPrometheusInstallations().stream().map(pi -> {
ServiceMonitor serviceMonitor = new ServiceMonitor();
serviceMonitor.setMetadata(new ObjectMetaBuilder().withNamespace(pi.getNamespace()).withName(serviceMonitorName(context)).withLabels(ImmutableMap.<String, String>builder().putAll(pi.getMatchLabels()).putAll(labels).build()).build());
ServiceMonitorSpec spec = new ServiceMonitorSpec();
serviceMonitor.setSpec(spec);
LabelSelector selector = new LabelSelector();
spec.setSelector(selector);
NamespaceSelector namespaceSelector = new NamespaceSelector();
namespaceSelector.setMatchNames(ImmutableList.of(clusterNamespace));
spec.setNamespaceSelector(namespaceSelector);
selector.setMatchLabels(labels);
Endpoint endpoint = new Endpoint();
endpoint.setPort(POSTGRES_EXPORTER_CONTAINER_NAME);
spec.setEndpoints(Collections.singletonList(endpoint));
return serviceMonitor;
}));
return serviceMonitors.map(hasMetadataStream -> Stream.concat(resources, hasMetadataStream)).orElse(resources);
}
Aggregations