From 2a0511508a618b96f0184e138a8d0c35049a0068 Mon Sep 17 00:00:00 2001 From: jeyong Date: Sat, 30 Nov 2024 07:21:18 +0900 Subject: [PATCH 1/5] =?UTF-8?q?refactor:=20=ED=8C=8C=EC=9D=BC=20=EA=B2=BD?= =?UTF-8?q?=EB=A1=9C=EA=B0=80=20=EB=B9=88=20=EA=B0=92=EC=9D=B8=EC=A7=80?= =?UTF-8?q?=EB=A5=BC=20=ED=99=95=EC=9D=B8=ED=95=A0=20=EB=95=8C=20apache?= =?UTF-8?q?=EC=9D=98=20util=EC=9D=84=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/src/main/java/io/jeyong/handler/FileUtils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/handler/src/main/java/io/jeyong/handler/FileUtils.java b/handler/src/main/java/io/jeyong/handler/FileUtils.java index 8d7eda3..b8007f1 100644 --- a/handler/src/main/java/io/jeyong/handler/FileUtils.java +++ b/handler/src/main/java/io/jeyong/handler/FileUtils.java @@ -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; @@ -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; } From 6efe566d70e3b6245d40d573eb390d5fb80f6448 Mon Sep 17 00:00:00 2001 From: jeyong Date: Sat, 30 Nov 2024 07:35:36 +0900 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20=EB=9D=BC=EC=9D=B4=EB=B8=8C?= =?UTF-8?q?=EB=9F=AC=EB=A6=AC=20=EC=82=AC=EC=9A=A9=20=EC=8B=9C=20=EC=B4=88?= =?UTF-8?q?=EA=B8=B0=ED=99=94=20=EB=A9=94=EC=84=B8=EC=A7=80=EB=A5=BC=20?= =?UTF-8?q?=ED=86=B5=ED=95=B4=20=EC=84=A4=EC=A0=95=20=EC=A0=95=EB=B3=B4?= =?UTF-8?q?=EB=A5=BC=20=EC=B6=9C=EB=A0=A5=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/SigtermHandlerConfiguration.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/handler/src/main/java/io/jeyong/handler/SigtermHandlerConfiguration.java b/handler/src/main/java/io/jeyong/handler/SigtermHandlerConfiguration.java index 009ec34..265aaef 100644 --- a/handler/src/main/java/io/jeyong/handler/SigtermHandlerConfiguration.java +++ b/handler/src/main/java/io/jeyong/handler/SigtermHandlerConfiguration.java @@ -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; @@ -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; @@ -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(), From 6a65e7da210412957e043f1cb87cf2b440d8851e Mon Sep 17 00:00:00 2001 From: jeyong Date: Sat, 30 Nov 2024 07:35:45 +0900 Subject: [PATCH 3/5] =?UTF-8?q?feat:=20SIGTERM=20=EC=8B=A0=ED=98=B8?= =?UTF-8?q?=EB=A5=BC=20=EC=88=98=EC=8B=A0=ED=95=98=EC=98=80=EB=8B=A4?= =?UTF-8?q?=EB=8A=94=20=EB=A9=94=EC=84=B8=EC=A7=80=EB=A5=BC=20=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=ED=95=98=EB=8F=84=EB=A1=9D=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/jeyong/handler/ApplicationTerminator.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/handler/src/main/java/io/jeyong/handler/ApplicationTerminator.java b/handler/src/main/java/io/jeyong/handler/ApplicationTerminator.java index 1dc168a..e33d0bc 100644 --- a/handler/src/main/java/io/jeyong/handler/ApplicationTerminator.java +++ b/handler/src/main/java/io/jeyong/handler/ApplicationTerminator.java @@ -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; @@ -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()); }; From 7d09baa9f917717ee7749e5c431e027a0fbe5ae8 Mon Sep 17 00:00:00 2001 From: jeyong Date: Sat, 30 Nov 2024 08:01:19 +0900 Subject: [PATCH 4/5] =?UTF-8?q?fix:=20=EC=BB=A8=ED=85=8C=EC=9D=B4=EB=84=88?= =?UTF-8?q?=EA=B0=80=20=EC=A2=85=EB=A3=8C=EB=90=98=EA=B8=B0=20=EC=A0=84?= =?UTF-8?q?=EC=97=90=20=EA=B0=92=EC=9D=84=20=EA=B2=80=EC=A6=9D=ED=95=B4?= =?UTF-8?q?=EC=84=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EA=B0=80=20=EC=8B=A4?= =?UTF-8?q?=ED=8C=A8=ED=95=98=EB=8A=94=20=EC=98=A4=EB=A5=98=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/jeyong/test/integration/SigtermHandlerTest.java | 10 ++++++---- .../jeyong/test/unit/SigtermHandlerPropertiesTest.java | 6 +++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/test/src/test/java/io/jeyong/test/integration/SigtermHandlerTest.java b/test/src/test/java/io/jeyong/test/integration/SigtermHandlerTest.java index 9ea290d..180be55 100644 --- a/test/src/test/java/io/jeyong/test/integration/SigtermHandlerTest.java +++ b/test/src/test/java/io/jeyong/test/integration/SigtermHandlerTest.java @@ -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"; @@ -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 @@ -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); } @@ -153,6 +153,8 @@ private static void sendSigtermToContainer(GenericContainer container) throws .withSignal("SIGTERM") .exec(); - Thread.sleep(1000); + while (container.isRunning()) { + Thread.sleep(1000); + } } } diff --git a/test/src/test/java/io/jeyong/test/unit/SigtermHandlerPropertiesTest.java b/test/src/test/java/io/jeyong/test/unit/SigtermHandlerPropertiesTest.java index e1b0102..3cc145f 100644 --- a/test/src/test/java/io/jeyong/test/unit/SigtermHandlerPropertiesTest.java +++ b/test/src/test/java/io/jeyong/test/unit/SigtermHandlerPropertiesTest.java @@ -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"; @@ -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, } @@ -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); }); From 4d1a340d8dfffa26dbc80c32fa86c2581cce0f09 Mon Sep 17 00:00:00 2001 From: jeyong Date: Sat, 30 Nov 2024 08:04:39 +0900 Subject: [PATCH 5/5] =?UTF-8?q?chore:=20=EB=A6=B4=EB=A6=AC=EC=A6=88=20?= =?UTF-8?q?=EB=B2=84=EC=A0=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handler/build.gradle b/handler/build.gradle index 1d614eb..05d3b6b 100644 --- a/handler/build.gradle +++ b/handler/build.gradle @@ -5,7 +5,7 @@ plugins { } group = 'io.jeyong' -version = '1.2.1' +version = '1.2.2' ext { artifactName = 'k8s-sigterm-handler'