Search in sources :

Example 1 with NamespaceSelector

use of io.stackgres.operator.customresource.prometheus.NamespaceSelector 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;
    });
}
Also used : ServiceMonitorSpec(io.stackgres.operator.customresource.prometheus.ServiceMonitorSpec) ServiceMonitor(io.stackgres.operator.customresource.prometheus.ServiceMonitor) Endpoint(io.stackgres.operator.customresource.prometheus.Endpoint) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) NamespaceSelector(io.stackgres.operator.customresource.prometheus.NamespaceSelector) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with NamespaceSelector

use of io.stackgres.operator.customresource.prometheus.NamespaceSelector 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);
}
Also used : LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) StackGresVersion(io.stackgres.operator.common.StackGresVersion) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) Singleton(javax.inject.Singleton) StackgresClusterContainers(io.stackgres.common.StackgresClusterContainers) ServiceMonitor(io.stackgres.operator.customresource.prometheus.ServiceMonitor) Inject(javax.inject.Inject) NamespaceSelector(io.stackgres.operator.customresource.prometheus.NamespaceSelector) ImmutableList(com.google.common.collect.ImmutableList) ResourceGenerator(io.stackgres.operator.conciliation.ResourceGenerator) Map(java.util.Map) ServiceSpecBuilder(io.fabric8.kubernetes.api.model.ServiceSpecBuilder) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) ServiceMonitorSpec(io.stackgres.operator.customresource.prometheus.ServiceMonitorSpec) ImmutableMap(com.google.common.collect.ImmutableMap) StackGresClusterContext(io.stackgres.operator.conciliation.cluster.StackGresClusterContext) Endpoint(io.stackgres.operator.customresource.prometheus.Endpoint) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) ResourceUtil(io.stackgres.operatorframework.resource.ResourceUtil) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) OperatorVersionBinder(io.stackgres.operator.conciliation.OperatorVersionBinder) Stream(java.util.stream.Stream) Optional(java.util.Optional) Collections(java.util.Collections) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) ServiceSpecBuilder(io.fabric8.kubernetes.api.model.ServiceSpecBuilder) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) NamespaceSelector(io.stackgres.operator.customresource.prometheus.NamespaceSelector) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) ServiceSpecBuilder(io.fabric8.kubernetes.api.model.ServiceSpecBuilder) ServiceMonitorSpec(io.stackgres.operator.customresource.prometheus.ServiceMonitorSpec) ServiceMonitor(io.stackgres.operator.customresource.prometheus.ServiceMonitor) Endpoint(io.stackgres.operator.customresource.prometheus.Endpoint) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) Stream(java.util.stream.Stream)

Aggregations

LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)2 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)2 Endpoint (io.stackgres.operator.customresource.prometheus.Endpoint)2 NamespaceSelector (io.stackgres.operator.customresource.prometheus.NamespaceSelector)2 ServiceMonitor (io.stackgres.operator.customresource.prometheus.ServiceMonitor)2 ServiceMonitorSpec (io.stackgres.operator.customresource.prometheus.ServiceMonitorSpec)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)1 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)1 ServiceSpecBuilder (io.fabric8.kubernetes.api.model.ServiceSpecBuilder)1 LabelFactoryForCluster (io.stackgres.common.LabelFactoryForCluster)1 StackgresClusterContainers (io.stackgres.common.StackgresClusterContainers)1 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)1 StackGresVersion (io.stackgres.operator.common.StackGresVersion)1 OperatorVersionBinder (io.stackgres.operator.conciliation.OperatorVersionBinder)1 ResourceGenerator (io.stackgres.operator.conciliation.ResourceGenerator)1 StackGresClusterContext (io.stackgres.operator.conciliation.cluster.StackGresClusterContext)1 ResourceUtil (io.stackgres.operatorframework.resource.ResourceUtil)1