/** * Returns the named port for the given service name or blank */ private static String serviceToPortOrBlank(String serviceName) { String envVarName = toServicePortEnvironmentVariable(serviceName); return getEnvVarOrSystemProperty(envVarName, ""); }
/** * Returns the service host and port for the given environment variable name. * * @param serviceName the name of the service which is used as a prefix to access the <code>${serviceName}_SERVICE_HOST</code> and <code>${serviceName}_SERVICE_PORT</code> environment variables to find the hos and port * @param defaultHost the default host to use if not injected via an environment variable (e.g. localhost) * @parma defaultPort the default port to use to connect to the service if there is not an environment variable defined */ private static String getServiceHostAndPort(String serviceName, String defaultHost, String defaultPort) { String serviceEnvVarPrefix = getServiceEnvVarPrefix(serviceName); String hostEnvVar = serviceEnvVarPrefix + "_HOST"; String portEnvVar = serviceEnvVarPrefix + "_PORT"; String host = getEnvVarOrSystemProperty(hostEnvVar, hostEnvVar, defaultHost); String port = getEnvVarOrSystemProperty(portEnvVar, portEnvVar, defaultPort); String answer = host + ":" + port; return answer; }
/** * Returns the service host name or a blank string if it could not be resolved */ private static String serviceToHostOrBlank(String serviceName) { return getEnvVarOrSystemProperty(toServiceHostEnvironmentVariable(serviceName), ""); }
private static String serviceToProtocol(String serviceName, String servicePort) { return getEnvVarOrSystemProperty(toEnvVariable(serviceName + PORT_SUFFIX + "_" + servicePort + PROTO_SUFFIX), DEFAULT_PROTO); }
String serviceHost = serviceToHostOrBlank(serviceName); String servicePort = serviceToPortOrBlank(serviceName); String serviceProto = serviceProtocol != null ? serviceProtocol : serviceToProtocol(serviceName, servicePort); String hostAndPort = getServiceHostAndPort(serviceName, "", ""); if (!hostAndPort.startsWith(":")) { return serviceProto + "://" + hostAndPort; ServicePort port = findServicePortByName(srv, null); if (port == null) { throw new RuntimeException("Couldn't find port: " + null + " for service:" + serviceName); if (backend != null) { String backendServiceName = backend.getServiceName(); if (serviceName.equals(backendServiceName) && portsMatch(port, backend.getServicePort())) { String pathPostfix = path.getPath(); if (tls != null) { for (String host : hosts) { if (StringUtils.isNotBlank(host)) { return String.format("https://%s/%s", host, preparePath(pathPostfix)); return String.format("http://%s/%s",answer, preparePath(pathPostfix));
private static String getEnvVarOrSystemProperty(String envVarName, String defaultValue) { return getEnvVarOrSystemProperty(envVarName,envVarName,defaultValue); } }
ServiceUrlUtil.getServiceURL(kubernetes, serviceName, ns, protocol, true) : null; } catch (Throwable e) {