When accept-empty is false, make health indicator DOWN (#2756)

Fixes #2749
This commit is contained in:
Ryan Baxter
2025-02-22 08:50:26 -05:00
committed by GitHub
parent 4742859c12
commit ef8a62a506
6 changed files with 36 additions and 7 deletions

View File

@@ -25,3 +25,6 @@ You can disable the Health Indicator by setting `management.health.config.enable
Also, you can provide a custom `down` status of your own by setting property `spring.cloud.config.server.health.down-health-status` (valued to `"DOWN'` by default).
NOTE: If `spring.cloud.config.server.accept-empty` is `false` and the health indicator check returns
does not return any repository data the health indicator will return `DOWN` status.

View File

@@ -51,10 +51,19 @@ public class ConfigServerHealthIndicator extends AbstractHealthIndicator {
private String downHealthStatus = Status.DOWN.getCode();
// autowired required or boot constructor binding produces an error
@Autowired
private final boolean acceptEmpty;
@Deprecated
public ConfigServerHealthIndicator(EnvironmentRepository environmentRepository) {
this.environmentRepository = environmentRepository;
this.acceptEmpty = true;
}
// autowired required or boot constructor binding produces an error
@Autowired
public ConfigServerHealthIndicator(EnvironmentRepository environmentRepository, ConfigServerProperties properties) {
this.environmentRepository = environmentRepository;
this.acceptEmpty = properties.isAcceptEmpty();
}
@PostConstruct
@@ -103,6 +112,11 @@ public class ConfigServerHealthIndicator extends AbstractHealthIndicator {
return;
}
}
if (!this.acceptEmpty && details.isEmpty()) {
// If accept-empty is false and no repositories are found, meaning details is
// empty, then set status to DOWN
builder.down().withDetail("acceptEmpty", this.acceptEmpty);
}
builder.withDetail("repositories", details);
}

View File

@@ -156,8 +156,9 @@ public class EnvironmentRepositoryConfiguration {
protected static class ConfigServerActuatorConfiguration {
@Bean
public ConfigServerHealthIndicator configServerHealthIndicator(EnvironmentRepository repository) {
return new ConfigServerHealthIndicator(repository);
public ConfigServerHealthIndicator configServerHealthIndicator(EnvironmentRepository repository,
ConfigServerProperties configServerProperties) {
return new ConfigServerHealthIndicator(repository, configServerProperties);
}
}

View File

@@ -52,7 +52,7 @@ public class ConfigServerHealthIndicatorTests {
@BeforeEach
public void init() {
initMocks(this);
this.indicator = new ConfigServerHealthIndicator(this.repository);
this.indicator = new ConfigServerHealthIndicator(this.repository, new ConfigServerProperties());
this.indicator.init();
}
@@ -89,4 +89,13 @@ public class ConfigServerHealthIndicatorTests {
assertThat(this.indicator.health().getStatus()).as("wrong default status").isEqualTo(Status.UP);
}
@Test
public void acceptEmptyFalse() {
ConfigServerProperties configServerProperties = new ConfigServerProperties();
configServerProperties.setAcceptEmpty(false);
this.indicator = new ConfigServerHealthIndicator(this.repository, configServerProperties);
when(this.repository.findOne("myname", "myprofile", "mylabel", false)).thenReturn(this.environment);
assertThat(this.indicator.health().getStatus()).as("wrong default status").isEqualTo(Status.DOWN);
}
}

View File

@@ -87,7 +87,8 @@ public class EnvironmentRepositoryConfigurationTests {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
EnvironmentRepositoryConfigurationTests.EnableConfigurationPropertiesBeans.class,
EnvironmentRepositoryConfiguration.ConfigServerActuatorConfiguration.class))
EnvironmentRepositoryConfiguration.ConfigServerActuatorConfiguration.class,
ConfigServerProperties.class))
.withPropertyValues("spring.cloud.config.server.health.down-health-status=CUSTOMIZED")
.run((context) -> {
ConfigServerHealthIndicator healthIndicator = context.getBean(ConfigServerHealthIndicator.class);

View File

@@ -27,6 +27,7 @@ import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.config.CompositeConfiguration;
import org.springframework.cloud.config.server.config.ConfigServerHealthIndicator;
import org.springframework.cloud.config.server.config.ConfigServerProperties;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -140,7 +141,7 @@ public class CompositeEnvironmentRepositoryTests {
public void overridingCompositeEnvRepo_contextLoads() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
context.register(OverrideCompositeConfig.class, CompositeConfiguration.class,
ConfigServerHealthIndicator.class);
ConfigServerHealthIndicator.class, ConfigServerProperties.class);
context.refresh();
}
}