Skip to content

Commit

Permalink
Merge pull request #8 from joon6093/1.2.x
Browse files Browse the repository at this point in the history
Release v1.2.2
  • Loading branch information
joon6093 authored Dec 1, 2024
2 parents 77ad8a4 + 4d1a340 commit f55f1c1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion handler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'io.jeyong'
version = '1.2.1'
version = '1.2.2'

ext {
artifactName = 'k8s-sigterm-handler'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package io.jeyong.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.SignalHandler;

public abstract class ApplicationTerminator {

private static final Logger logger = LoggerFactory.getLogger(ApplicationTerminator.class);

private final String terminationMessagePath;
private final String terminationMessage;

Expand All @@ -14,6 +18,7 @@ protected ApplicationTerminator(final String terminationMessagePath, final Strin

public SignalHandler handleTermination() {
return signal -> {
logger.info("Received SIGTERM signal. Initiating termination handler.");
FileUtils.writeToFile(terminationMessagePath, terminationMessage);
System.exit(getExitCode());
};
Expand Down
3 changes: 2 additions & 1 deletion handler/src/main/java/io/jeyong/handler/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -14,7 +15,7 @@ private FileUtils() {
}

public static void writeToFile(final String filePath, final String message) {
if (filePath == null || filePath.isBlank()) {
if (Strings.isBlank(filePath)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.jeyong.handler;

import jakarta.annotation.PostConstruct;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
Expand All @@ -16,6 +20,7 @@
@EnableConfigurationProperties(SigtermHandlerProperties.class)
public class SigtermHandlerConfiguration {

private static final Logger logger = LoggerFactory.getLogger(SigtermHandlerConfiguration.class);
private static final String SIGNAL_TYPE = "TERM";

private final SigtermHandlerProperties sigtermHandlerProperties;
Expand All @@ -24,6 +29,19 @@ public SigtermHandlerConfiguration(final SigtermHandlerProperties sigtermHandler
this.sigtermHandlerProperties = sigtermHandlerProperties;
}

@PostConstruct
public void logInitialization() {
if (Strings.isBlank(sigtermHandlerProperties.getTerminationMessagePath())) {
logger.info("Sigterm handler initialized with exitCode: {}", sigtermHandlerProperties.getExitCode());
} else {
logger.info(
"Sigterm handler initialized with exitCode: {}, terminationMessagePath: {}, terminationMessage: '{}'",
sigtermHandlerProperties.getExitCode(),
sigtermHandlerProperties.getTerminationMessagePath(),
sigtermHandlerProperties.getTerminationMessage());
}
}

@Bean
public ApplicationTerminator applicationTerminator(final ApplicationContext applicationContext) {
return new SpringContextTerminator(applicationContext, sigtermHandlerProperties.getExitCode(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@DisplayName("SigtermHandler Integration Test")
public class SigtermHandlerTest {

private static final int EXPECTED_EXIT_CODE = 10;
private static final int EXIT_CODE = 10;
private static final String TERMINATION_MESSAGE_PATH = "/app/termination-message.message";
private static final String TERMINATION_MESSAGE = "Test termination message";

Expand Down Expand Up @@ -54,7 +54,7 @@ void testExitCode() throws Exception {

// then
Long exitCode = container.getCurrentContainerInfo().getState().getExitCodeLong();
assertThat(exitCode).isEqualTo(EXPECTED_EXIT_CODE);
assertThat(exitCode).isEqualTo(EXIT_CODE);
}

@Test
Expand Down Expand Up @@ -116,7 +116,7 @@ private static void createApplicationYaml(Path resourcesDir) throws Exception {
exit-code: %d
termination-message-path: %s
termination-message: %s
""", EXPECTED_EXIT_CODE, TERMINATION_MESSAGE_PATH, TERMINATION_MESSAGE);
""", EXIT_CODE, TERMINATION_MESSAGE_PATH, TERMINATION_MESSAGE);
Files.writeString(applicationYaml, yamlContent);
}

Expand Down Expand Up @@ -153,6 +153,8 @@ private static void sendSigtermToContainer(GenericContainer<?> container) throws
.withSignal("SIGTERM")
.exec();

Thread.sleep(1000);
while (container.isRunning()) {
Thread.sleep(1000);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@DisplayName("SigtermHandlerProperties Unit Test")
class SigtermHandlerPropertiesTest {

private static final int EXPECTED_EXIT_CODE = 10;
private static final int EXIT_CODE = 10;
private static final String TERMINATION_MESSAGE_PATH = "/termination-message.message";
private static final String TERMINATION_MESSAGE = "Test termination message";

Expand All @@ -24,7 +24,7 @@ class SigtermHandlerPropertiesTest {
classes = TestApplication.class,
properties = {
"kubernetes.sigterm-handler.enabled=true",
"kubernetes.sigterm-handler.exit-code=" + EXPECTED_EXIT_CODE,
"kubernetes.sigterm-handler.exit-code=" + EXIT_CODE,
"kubernetes.sigterm-handler.termination-message-path=" + TERMINATION_MESSAGE_PATH,
"kubernetes.sigterm-handler.termination-message=" + TERMINATION_MESSAGE,
}
Expand All @@ -48,7 +48,7 @@ void testRegisterConfiguration() {
// then
assertSoftly(softly -> {
softly.assertThat(beanExists).isTrue();
softly.assertThat(properties.getExitCode()).isEqualTo(EXPECTED_EXIT_CODE);
softly.assertThat(properties.getExitCode()).isEqualTo(EXIT_CODE);
softly.assertThat(properties.getTerminationMessagePath()).isEqualTo(TERMINATION_MESSAGE_PATH);
softly.assertThat(properties.getTerminationMessage()).isEqualTo(TERMINATION_MESSAGE);
});
Expand Down

0 comments on commit f55f1c1

Please sign in to comment.