Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Fixes #1206: add support for spring-boot 2 health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro committed Mar 9, 2018
1 parent 8650118 commit 1162d2c
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.fabric8.maven.core.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Standard properties from Spring Boot <code><application.properties/code> files
*/
public class SpringBootConfigurationHelper {

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

public static final String SPRING_BOOT_GROUP_ID = "org.springframework.boot";
public static final String SPRING_BOOT_ARTIFACT_ID = "spring-boot";
public static final String SPRING_BOOT_DEVTOOLS_ARTIFACT_ID = "spring-boot-devtools";
public static final String DEV_TOOLS_REMOTE_SECRET = "spring.devtools.remote.secret";
public static final String DEV_TOOLS_REMOTE_SECRET_ENV = "SPRING_DEVTOOLS_REMOTE_SECRET";

/*
Following are property keys for spring-boot-1 and their spring-boot-2 equivalent
*/
private static final String[] MANAGEMENT_PORT = {"management.port", "management.server.port"};
private static final String[] SERVER_PORT = {"server.port", "server.port"};
private static final String[] SERVER_KEYSTORE = {"server.ssl.key-store", "server.ssl.key-store"};
private static final String[] MANAGEMENT_KEYSTORE = {"management.ssl.key-store", "management.server.ssl.key-store"};
private static final String[] SERVLET_PATH = {"server.servlet-path", "server.servlet.path"};
private static final String[] SERVER_CONTEXT_PATH = {"server.context-path", "server.servlet.context-path"};
private static final String[] MANAGEMENT_CONTEXT_PATH = {"management.context-path", "management.server.servlet.context-path"};
private static final String[] ACTUATOR_BASE_PATH = {null, "management.endpoints.web.base-path"};
private static final String[] ACTUATOR_DEFAULT_BASE_PATH = {"", "/actuator"};

private int propertyOffset;

public SpringBootConfigurationHelper(String springBootVersion) {
this.propertyOffset = propertyOffset(springBootVersion);
}

public String getManagementPortPropertyKey() {
return lookup(MANAGEMENT_PORT);
}

public String getServerPortPropertyKey() {
return lookup(SERVER_PORT);
}

public String getServerKeystorePropertyKey() {
return lookup(SERVER_KEYSTORE);
}

public String getManagementKeystorePropertyKey() {
return lookup(MANAGEMENT_KEYSTORE);
}

public String getServletPathPropertyKey() {
return lookup(SERVLET_PATH);
}

public String getServerContextPathPropertyKey() {
return lookup(SERVER_CONTEXT_PATH);
}

public String getManagementContextPathPropertyKey() {
return lookup(MANAGEMENT_CONTEXT_PATH);
}

public String getActuatorBasePathPropertyKey() {
return lookup(ACTUATOR_BASE_PATH);
}

public String getActuatorDefaultBasePath() {
return lookup(ACTUATOR_DEFAULT_BASE_PATH);
}

private String lookup(String[] keys) {
return keys[propertyOffset];
}

private int propertyOffset(String springBootVersion) {
Integer majorVersion = majorVersion(springBootVersion);
int idx = majorVersion != null ? majorVersion - 1 : 0;
idx = Math.min(idx, 1);
idx = Math.max(idx, 0);
return idx;
}

private Integer majorVersion(String version) {
if (version != null) {
try {
return Integer.parseInt(version.substring(0, version.indexOf(".")));
} catch (Exception e) {
LOG.warn("Cannot spring boot major version from {}", version);
}
}
return null;
}


}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ protected static Properties getPropertiesFromYamlResource(URL resource) {
* Determine the spring-boot devtools version for the current project
*/
public static String getSpringBootDevToolsVersion(MavenProject mavenProject) {
return MavenUtil.getDependencyVersion(mavenProject, SpringBootProperties.SPRING_BOOT_GROUP_ID, SpringBootProperties.SPRING_BOOT_ARTIFACT_ID);
return getSpringBootVersion(mavenProject);
}

/**
* Determine the spring-boot major version for the current project
*/
public static String getSpringBootVersion(MavenProject mavenProject) {
return MavenUtil.getDependencyVersion(mavenProject, SpringBootConfigurationHelper.SPRING_BOOT_GROUP_ID, SpringBootConfigurationHelper.SPRING_BOOT_ARTIFACT_ID);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import io.fabric8.kubernetes.api.model.Probe;
import io.fabric8.kubernetes.api.model.ProbeBuilder;
import io.fabric8.maven.core.util.MavenUtil;
import io.fabric8.maven.core.util.SpringBootProperties;
import io.fabric8.maven.core.util.SpringBootConfigurationHelper;
import io.fabric8.maven.core.util.SpringBootUtil;
import io.fabric8.maven.enricher.api.AbstractHealthCheckEnricher;
import io.fabric8.maven.enricher.api.EnricherContext;
Expand Down Expand Up @@ -69,29 +69,36 @@ protected Probe discoverSpringBootHealthCheck(int initialDelay) {
}

protected Probe buildProbe(Properties springBootProperties, int initialDelay) {
Integer managementPort = PropertiesHelper.getInteger(springBootProperties, SpringBootProperties.MANAGEMENT_PORT);
SpringBootConfigurationHelper propertyHelper = new SpringBootConfigurationHelper(SpringBootUtil.getSpringBootVersion(getContext().getProject()));
Integer managementPort = PropertiesHelper.getInteger(springBootProperties, propertyHelper.getManagementPortPropertyKey());
boolean usingManagementPort = managementPort != null;

Integer port = managementPort;
if (port == null) {
port = PropertiesHelper.getInteger(springBootProperties, SpringBootProperties.SERVER_PORT, DEFAULT_SERVER_PORT);
port = PropertiesHelper.getInteger(springBootProperties, propertyHelper.getServerPortPropertyKey(), DEFAULT_SERVER_PORT);
}

String scheme;
String prefix;
if (usingManagementPort) {
scheme = Strings.isNotBlank(springBootProperties.getProperty(SpringBootProperties.MANAGEMENT_KEYSTORE)) ? SCHEME_HTTPS : SCHEME_HTTP;
prefix = springBootProperties.getProperty(SpringBootProperties.MANAGEMENT_CONTEXT_PATH, "");
scheme = Strings.isNotBlank(springBootProperties.getProperty(propertyHelper.getManagementKeystorePropertyKey())) ? SCHEME_HTTPS : SCHEME_HTTP;
prefix = springBootProperties.getProperty(propertyHelper.getManagementContextPathPropertyKey(), "");
} else {
scheme = Strings.isNotBlank(springBootProperties.getProperty(SpringBootProperties.SERVER_KEYSTORE)) ? SCHEME_HTTPS : SCHEME_HTTP;
prefix = springBootProperties.getProperty(SpringBootProperties.SERVER_CONTEXT_PATH, "");
prefix += springBootProperties.getProperty(SpringBootProperties.SERVLET_PATH, "");
prefix += springBootProperties.getProperty(SpringBootProperties.MANAGEMENT_CONTEXT_PATH, "");
scheme = Strings.isNotBlank(springBootProperties.getProperty(propertyHelper.getServerKeystorePropertyKey())) ? SCHEME_HTTPS : SCHEME_HTTP;
prefix = springBootProperties.getProperty(propertyHelper.getServerContextPathPropertyKey(), "");
prefix += springBootProperties.getProperty(propertyHelper.getServletPathPropertyKey(), "");
prefix += springBootProperties.getProperty(propertyHelper.getManagementContextPathPropertyKey(), "");
}

String actuatorBasePathKey = propertyHelper.getActuatorBasePathPropertyKey();
String actuatorBasePath = propertyHelper.getActuatorDefaultBasePath();
if (actuatorBasePathKey != null) {
actuatorBasePath = springBootProperties.getProperty(actuatorBasePathKey, actuatorBasePath);
}

// lets default to adding a spring boot actuator health check
return new ProbeBuilder().
withNewHttpGet().withNewPort(port).withPath(prefix + "/health").withScheme(scheme).endHttpGet().
withNewHttpGet().withNewPort(port).withPath(prefix + actuatorBasePath + "/health").withScheme(scheme).endHttpGet().
withInitialDelaySeconds(initialDelay).build();
}

Expand Down
Loading

0 comments on commit 1162d2c

Please sign in to comment.