All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodic.kubernetes.istio.util.MeshUtils Maven / Gradle / Ivy

package org.zodic.kubernetes.istio.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.zodic.kubernetes.istio.IstioClientInfo;

public class MeshUtils {

    private static final Logger LOG = LoggerFactory.getLogger(MeshUtils.class);

    private final IstioClientInfo istioClientInfo;

    private RestTemplate restTemplate = new RestTemplateBuilder().build();

    public MeshUtils(IstioClientInfo istioClientInfo) {
        this.istioClientInfo = istioClientInfo;
    }

    public Boolean isIstioEnabled() {
        return checkIstioServices();
    }

    private synchronized boolean checkIstioServices() {
        try {
            // Check if Istio Envoy proxy is installed. Notice that the check is done to
            // localhost.
            // TODO: We can improve this initial detection if better methods are found.
            String resource = "http://localhost:" + this.istioClientInfo.getEnvoyPort();
            ResponseEntity response =
                this.restTemplate.getForEntity(resource + "/" + this.istioClientInfo.getTestPath(), String.class);
            if (response.getStatusCode().is2xxSuccessful()) {
                LOG.info("Istio Resources Found.");
                return true;
            }
            LOG.warn("Although Envoy proxy did respond at port{}, it did not respond with HTTP 200 to path: {}. You may need to tweak the test path in order to get proper Istio support.",
                this.istioClientInfo.getEnvoyPort(), this.istioClientInfo.getTestPath());
            return false;
        } catch (Throwable t) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Envoy proxy could not be located at port: {}. Assuming that the application is not running inside the Istio Service Mesh.",
                    this.istioClientInfo.getEnvoyPort());
            }
            return false;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy