Search in sources :

Example 1 with ServiceMonitor

use of io.fabric8.openshift.api.model.monitoring.v1.ServiceMonitor in project kubernetes-client by fabric8io.

the class ServiceMonitorTest method create.

@Test
void create() {
    // Given
    ServiceMonitor serviceMonitor = getServiceMonitor();
    server.expect().post().withPath("/apis/monitoring.coreos.com/v1/namespaces/ns1/servicemonitors").andReturn(HttpURLConnection.HTTP_OK, serviceMonitor).once();
    // When
    serviceMonitor = client.monitoring().serviceMonitors().inNamespace("ns1").create(serviceMonitor);
    // Then
    assertNotNull(serviceMonitor);
    assertEquals("foo", serviceMonitor.getMetadata().getName());
}
Also used : ServiceMonitor(io.fabric8.openshift.api.model.monitoring.v1.ServiceMonitor) Test(org.junit.jupiter.api.Test)

Example 2 with ServiceMonitor

use of io.fabric8.openshift.api.model.monitoring.v1.ServiceMonitor 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 3 with ServiceMonitor

use of io.fabric8.openshift.api.model.monitoring.v1.ServiceMonitor 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)

Example 4 with ServiceMonitor

use of io.fabric8.openshift.api.model.monitoring.v1.ServiceMonitor in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class BridgeExecutorController method reconcile.

@Override
public UpdateControl<BridgeExecutor> reconcile(BridgeExecutor bridgeExecutor, Context context) {
    LOGGER.debug("Create or update BridgeProcessor: '{}' in namespace '{}'", bridgeExecutor.getMetadata().getName(), bridgeExecutor.getMetadata().getNamespace());
    Secret secret = bridgeExecutorService.fetchBridgeExecutorSecret(bridgeExecutor);
    if (secret == null) {
        LOGGER.debug("Secrets for the BridgeProcessor '{}' have been not created yet.", bridgeExecutor.getMetadata().getName());
        return UpdateControl.noUpdate();
    }
    Deployment deployment = bridgeExecutorService.fetchOrCreateBridgeExecutorDeployment(bridgeExecutor, secret);
    if (!Readiness.isDeploymentReady(deployment)) {
        LOGGER.debug("Executor deployment BridgeProcessor: '{}' in namespace '{}' is NOT ready", bridgeExecutor.getMetadata().getName(), bridgeExecutor.getMetadata().getNamespace());
        bridgeExecutor.getStatus().setConditionsFromDeployment(deployment);
        if (DeploymentStatusUtils.isTimeoutFailure(deployment)) {
            notifyDeploymentFailure(bridgeExecutor, DeploymentStatusUtils.getReasonAndMessageForTimeoutFailure(deployment));
        } else if (DeploymentStatusUtils.isStatusReplicaFailure(deployment)) {
            notifyDeploymentFailure(bridgeExecutor, DeploymentStatusUtils.getReasonAndMessageForReplicaFailure(deployment));
        }
        return UpdateControl.updateStatus(bridgeExecutor);
    }
    LOGGER.debug("Executor deployment BridgeProcessor: '{}' in namespace '{}' is ready", bridgeExecutor.getMetadata().getName(), bridgeExecutor.getMetadata().getNamespace());
    // Create Service
    Service service = bridgeExecutorService.fetchOrCreateBridgeExecutorService(bridgeExecutor, deployment);
    if (service.getStatus() == null) {
        LOGGER.debug("Executor service BridgeProcessor: '{}' in namespace '{}' is NOT ready", bridgeExecutor.getMetadata().getName(), bridgeExecutor.getMetadata().getNamespace());
        bridgeExecutor.getStatus().markConditionFalse(ConditionType.Ready);
        bridgeExecutor.getStatus().markConditionTrue(ConditionType.Augmentation, ConditionReason.ServiceNotReady);
        return UpdateControl.updateStatus(bridgeExecutor);
    }
    Optional<ServiceMonitor> serviceMonitor = monitorService.fetchOrCreateServiceMonitor(bridgeExecutor, service, BridgeExecutor.COMPONENT_NAME);
    if (serviceMonitor.isPresent()) {
        // this is an optional resource
        LOGGER.debug("Executor service monitor resource BridgeExecutor: '{}' in namespace '{}' is ready", bridgeExecutor.getMetadata().getName(), bridgeExecutor.getMetadata().getNamespace());
    } else {
        LOGGER.warn("Executor service monitor resource BridgeExecutor: '{}' in namespace '{}' is failed to deploy, Prometheus not installed.", bridgeExecutor.getMetadata().getName(), bridgeExecutor.getMetadata().getNamespace());
        BridgeError prometheusNotAvailableError = bridgeErrorService.getError(PrometheusNotInstalledException.class).orElseThrow(() -> new RuntimeException("PrometheusNotInstalledException not found in error catalog"));
        bridgeExecutor.getStatus().markConditionFalse(ConditionType.Ready, ConditionReason.PrometheusUnavailable, prometheusNotAvailableError.getReason(), prometheusNotAvailableError.getCode());
        notifyManager(bridgeExecutor, ManagedResourceStatus.FAILED);
        return UpdateControl.updateStatus(bridgeExecutor);
    }
    LOGGER.debug("Executor service BridgeProcessor: '{}' in namespace '{}' is ready", bridgeExecutor.getMetadata().getName(), bridgeExecutor.getMetadata().getNamespace());
    if (!bridgeExecutor.getStatus().isReady()) {
        bridgeExecutor.getStatus().markConditionTrue(ConditionType.Ready);
        bridgeExecutor.getStatus().markConditionFalse(ConditionType.Augmentation);
        notifyManager(bridgeExecutor, ManagedResourceStatus.READY);
        return UpdateControl.updateStatus(bridgeExecutor);
    }
    return UpdateControl.noUpdate();
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) BridgeError(com.redhat.service.bridge.infra.exceptions.BridgeError) ServiceMonitor(io.fabric8.openshift.api.model.monitoring.v1.ServiceMonitor) PrometheusNotInstalledException(com.redhat.service.bridge.infra.exceptions.definitions.platform.PrometheusNotInstalledException) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) BridgeErrorService(com.redhat.service.bridge.infra.exceptions.BridgeErrorService) Service(io.fabric8.kubernetes.api.model.Service) BridgeExecutorService(com.redhat.service.bridge.shard.operator.BridgeExecutorService) ManagerSyncService(com.redhat.service.bridge.shard.operator.ManagerSyncService) ServiceMonitorService(com.redhat.service.bridge.shard.operator.monitoring.ServiceMonitorService)

Example 5 with ServiceMonitor

use of io.fabric8.openshift.api.model.monitoring.v1.ServiceMonitor in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class BridgeIngressController method reconcile.

@Override
public UpdateControl<BridgeIngress> reconcile(BridgeIngress bridgeIngress, Context context) {
    LOGGER.debug("Create or update BridgeIngress: '{}' in namespace '{}'", bridgeIngress.getMetadata().getName(), bridgeIngress.getMetadata().getNamespace());
    Secret secret = bridgeIngressService.fetchBridgeIngressSecret(bridgeIngress);
    if (secret == null) {
        LOGGER.debug("Secrets for the BridgeIngress '{}' have been not created yet.", bridgeIngress.getMetadata().getName());
        return UpdateControl.noUpdate();
    }
    Deployment deployment = bridgeIngressService.fetchOrCreateBridgeIngressDeployment(bridgeIngress, secret);
    if (!Readiness.isDeploymentReady(deployment)) {
        LOGGER.debug("Ingress deployment BridgeIngress: '{}' in namespace '{}' is NOT ready", bridgeIngress.getMetadata().getName(), bridgeIngress.getMetadata().getNamespace());
        bridgeIngress.getStatus().setConditionsFromDeployment(deployment);
        if (DeploymentStatusUtils.isTimeoutFailure(deployment)) {
            notifyDeploymentFailure(bridgeIngress, DeploymentStatusUtils.getReasonAndMessageForTimeoutFailure(deployment));
        } else if (DeploymentStatusUtils.isStatusReplicaFailure(deployment)) {
            notifyDeploymentFailure(bridgeIngress, DeploymentStatusUtils.getReasonAndMessageForReplicaFailure(deployment));
        }
        return UpdateControl.updateStatus(bridgeIngress);
    }
    LOGGER.debug("Ingress deployment BridgeIngress: '{}' in namespace '{}' is ready", bridgeIngress.getMetadata().getName(), bridgeIngress.getMetadata().getNamespace());
    // Create Service
    Service service = bridgeIngressService.fetchOrCreateBridgeIngressService(bridgeIngress, deployment);
    if (service.getStatus() == null) {
        LOGGER.debug("Ingress service BridgeIngress: '{}' in namespace '{}' is NOT ready", bridgeIngress.getMetadata().getName(), bridgeIngress.getMetadata().getNamespace());
        bridgeIngress.getStatus().markConditionFalse(ConditionType.Ready);
        bridgeIngress.getStatus().markConditionTrue(ConditionType.Augmentation, ConditionReason.ServiceNotReady);
        return UpdateControl.updateStatus(bridgeIngress);
    }
    LOGGER.debug("Ingress service BridgeIngress: '{}' in namespace '{}' is ready", bridgeIngress.getMetadata().getName(), bridgeIngress.getMetadata().getNamespace());
    // Create Route
    NetworkResource networkResource = networkingService.fetchOrCreateNetworkIngress(bridgeIngress, service);
    if (!networkResource.isReady()) {
        LOGGER.debug("Ingress networking resource BridgeIngress: '{}' in namespace '{}' is NOT ready", bridgeIngress.getMetadata().getName(), bridgeIngress.getMetadata().getNamespace());
        bridgeIngress.getStatus().markConditionFalse(ConditionType.Ready);
        bridgeIngress.getStatus().markConditionTrue(ConditionType.Augmentation, ConditionReason.NetworkResourceNotReady);
        return UpdateControl.updateStatus(bridgeIngress);
    }
    LOGGER.debug("Ingress networking resource BridgeIngress: '{}' in namespace '{}' is ready", bridgeIngress.getMetadata().getName(), bridgeIngress.getMetadata().getNamespace());
    Optional<ServiceMonitor> serviceMonitor = monitorService.fetchOrCreateServiceMonitor(bridgeIngress, service, BridgeIngress.COMPONENT_NAME);
    if (serviceMonitor.isPresent()) {
        // this is an optional resource
        LOGGER.debug("Ingress monitor resource BridgeIngress: '{}' in namespace '{}' is ready", bridgeIngress.getMetadata().getName(), bridgeIngress.getMetadata().getNamespace());
    } else {
        LOGGER.warn("Ingress monitor resource BridgeIngress: '{}' in namespace '{}' is failed to deploy, Prometheus not installed.", bridgeIngress.getMetadata().getName(), bridgeIngress.getMetadata().getNamespace());
        BridgeError prometheusNotAvailableError = bridgeErrorService.getError(PrometheusNotInstalledException.class).orElseThrow(() -> new RuntimeException("PrometheusNotInstalledException not found in error catalog"));
        bridgeIngress.getStatus().markConditionFalse(ConditionType.Ready, ConditionReason.PrometheusUnavailable, prometheusNotAvailableError.getReason(), prometheusNotAvailableError.getCode());
        notifyManager(bridgeIngress, ManagedResourceStatus.FAILED);
        return UpdateControl.updateStatus(bridgeIngress);
    }
    if (!bridgeIngress.getStatus().isReady() || !networkResource.getEndpoint().equals(bridgeIngress.getStatus().getEndpoint())) {
        bridgeIngress.getStatus().setEndpoint(networkResource.getEndpoint());
        bridgeIngress.getStatus().markConditionTrue(ConditionType.Ready);
        bridgeIngress.getStatus().markConditionFalse(ConditionType.Augmentation);
        notifyManager(bridgeIngress, ManagedResourceStatus.READY);
        return UpdateControl.updateStatus(bridgeIngress);
    }
    return UpdateControl.noUpdate();
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) NetworkResource(com.redhat.service.bridge.shard.operator.networking.NetworkResource) BridgeError(com.redhat.service.bridge.infra.exceptions.BridgeError) ServiceMonitor(io.fabric8.openshift.api.model.monitoring.v1.ServiceMonitor) PrometheusNotInstalledException(com.redhat.service.bridge.infra.exceptions.definitions.platform.PrometheusNotInstalledException) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) NetworkingService(com.redhat.service.bridge.shard.operator.networking.NetworkingService) BridgeErrorService(com.redhat.service.bridge.infra.exceptions.BridgeErrorService) Service(io.fabric8.kubernetes.api.model.Service) BridgeIngressService(com.redhat.service.bridge.shard.operator.BridgeIngressService) ManagerSyncService(com.redhat.service.bridge.shard.operator.ManagerSyncService) ServiceMonitorService(com.redhat.service.bridge.shard.operator.monitoring.ServiceMonitorService)

Aggregations

ServiceMonitor (io.fabric8.openshift.api.model.monitoring.v1.ServiceMonitor)13 Service (io.fabric8.kubernetes.api.model.Service)8 Secret (io.fabric8.kubernetes.api.model.Secret)6 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)6 Test (org.junit.jupiter.api.Test)5 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)4 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)4 BridgeError (com.redhat.service.bridge.infra.exceptions.BridgeError)2 BridgeErrorService (com.redhat.service.bridge.infra.exceptions.BridgeErrorService)2 PrometheusNotInstalledException (com.redhat.service.bridge.infra.exceptions.definitions.platform.PrometheusNotInstalledException)2 BridgeIngressService (com.redhat.service.bridge.shard.operator.BridgeIngressService)2 ManagerSyncService (com.redhat.service.bridge.shard.operator.ManagerSyncService)2 ServiceMonitorService (com.redhat.service.bridge.shard.operator.monitoring.ServiceMonitorService)2 BridgeError (com.redhat.service.smartevents.infra.exceptions.BridgeError)2 BridgeErrorService (com.redhat.service.smartevents.infra.exceptions.BridgeErrorService)2 PrometheusNotInstalledException (com.redhat.service.smartevents.infra.exceptions.definitions.platform.PrometheusNotInstalledException)2 BridgeIngressService (com.redhat.service.smartevents.shard.operator.BridgeIngressService)2 ServiceMonitorService (com.redhat.service.smartevents.shard.operator.monitoring.ServiceMonitorService)2 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 SecretBuilder (io.fabric8.kubernetes.api.model.SecretBuilder)2