From 49fbbd76d4f4ff52f56cad912dbefefc32b20ba6 Mon Sep 17 00:00:00 2001 From: jeyong <118044367+joon6093@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:31:21 +0900 Subject: [PATCH 1/5] =?UTF-8?q?refactor:=20final=20class=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=EB=A1=9C=20=EC=83=81=EC=86=8D=ED=95=B4=EC=84=9C=20?= =?UTF-8?q?=EC=9D=B4=EC=9A=A9=ED=95=98=EC=A7=80=20=EB=AA=BB=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 | 2 +- .../src/main/java/io/jeyong/handler/SignalHandlerRegistrar.java | 2 +- .../main/java/io/jeyong/handler/SigtermHandlerProperties.java | 2 +- .../main/java/io/jeyong/handler/SpringContextTerminator.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/handler/src/main/java/io/jeyong/handler/FileUtils.java b/handler/src/main/java/io/jeyong/handler/FileUtils.java index b8007f1..b89bec3 100644 --- a/handler/src/main/java/io/jeyong/handler/FileUtils.java +++ b/handler/src/main/java/io/jeyong/handler/FileUtils.java @@ -7,7 +7,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class FileUtils { +public final class FileUtils { private static final Logger logger = LoggerFactory.getLogger(FileUtils.class); diff --git a/handler/src/main/java/io/jeyong/handler/SignalHandlerRegistrar.java b/handler/src/main/java/io/jeyong/handler/SignalHandlerRegistrar.java index 569abff..86990f3 100644 --- a/handler/src/main/java/io/jeyong/handler/SignalHandlerRegistrar.java +++ b/handler/src/main/java/io/jeyong/handler/SignalHandlerRegistrar.java @@ -3,7 +3,7 @@ import jakarta.annotation.PostConstruct; import sun.misc.Signal; -public class SignalHandlerRegistrar { +public final class SignalHandlerRegistrar { private final ApplicationTerminator applicationTerminator; private final String signalType; diff --git a/handler/src/main/java/io/jeyong/handler/SigtermHandlerProperties.java b/handler/src/main/java/io/jeyong/handler/SigtermHandlerProperties.java index 5fa0bbd..3fcedc8 100644 --- a/handler/src/main/java/io/jeyong/handler/SigtermHandlerProperties.java +++ b/handler/src/main/java/io/jeyong/handler/SigtermHandlerProperties.java @@ -51,7 +51,7 @@ */ // @formatter:on @ConfigurationProperties(prefix = "kubernetes.sigterm-handler") -public class SigtermHandlerProperties { +public final class SigtermHandlerProperties { private boolean enabled = true; private int exitCode = 0; diff --git a/handler/src/main/java/io/jeyong/handler/SpringContextTerminator.java b/handler/src/main/java/io/jeyong/handler/SpringContextTerminator.java index 3053305..0562f08 100644 --- a/handler/src/main/java/io/jeyong/handler/SpringContextTerminator.java +++ b/handler/src/main/java/io/jeyong/handler/SpringContextTerminator.java @@ -3,7 +3,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.context.ApplicationContext; -public class SpringContextTerminator extends ApplicationTerminator { +public final class SpringContextTerminator extends ApplicationTerminator { private final ApplicationContext applicationContext; private final int exitCode; From d3a4381a9fa52f22869e2db374b300513b3ebe91 Mon Sep 17 00:00:00 2001 From: jeyong <118044367+joon6093@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:38:04 +0900 Subject: [PATCH 2/5] =?UTF-8?q?refactor:=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=EC=B6=9C=EB=A0=A5=EC=9D=84=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=EB=A1=9C=20=EB=B6=84=EB=A6=AC=ED=95=98=EB=8F=84?= =?UTF-8?q?=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 --- .../main/java/io/jeyong/handler/ApplicationTerminator.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/handler/src/main/java/io/jeyong/handler/ApplicationTerminator.java b/handler/src/main/java/io/jeyong/handler/ApplicationTerminator.java index e33d0bc..97d56dd 100644 --- a/handler/src/main/java/io/jeyong/handler/ApplicationTerminator.java +++ b/handler/src/main/java/io/jeyong/handler/ApplicationTerminator.java @@ -18,11 +18,15 @@ protected ApplicationTerminator(final String terminationMessagePath, final Strin public SignalHandler handleTermination() { return signal -> { - logger.info("Received SIGTERM signal. Initiating termination handler."); + logTerminationSignal(signal.getName()); FileUtils.writeToFile(terminationMessagePath, terminationMessage); System.exit(getExitCode()); }; } + private void logTerminationSignal(final String signalName) { + logger.info("Received {} signal. Initiating termination handler.", signalName); + } + protected abstract int getExitCode(); } From 52ff8bc8ad8b6119c62b6171ec69bbeee1d00bfe Mon Sep 17 00:00:00 2001 From: jeyong <118044367+joon6093@users.noreply.github.com> Date: Mon, 2 Dec 2024 12:02:07 +0900 Subject: [PATCH 3/5] =?UTF-8?q?refactor:=20PostConstruct=EA=B0=80=20?= =?UTF-8?q?=EC=95=84=EB=8B=8C=20ContextRefreshedEvent=EB=A5=BC=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=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 --- .../main/java/io/jeyong/handler/SignalHandlerRegistrar.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/handler/src/main/java/io/jeyong/handler/SignalHandlerRegistrar.java b/handler/src/main/java/io/jeyong/handler/SignalHandlerRegistrar.java index 86990f3..08a6a02 100644 --- a/handler/src/main/java/io/jeyong/handler/SignalHandlerRegistrar.java +++ b/handler/src/main/java/io/jeyong/handler/SignalHandlerRegistrar.java @@ -1,6 +1,7 @@ package io.jeyong.handler; -import jakarta.annotation.PostConstruct; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.context.event.EventListener; import sun.misc.Signal; public final class SignalHandlerRegistrar { @@ -13,7 +14,7 @@ public SignalHandlerRegistrar(final ApplicationTerminator applicationTerminator, this.signalType = signalType; } - @PostConstruct + @EventListener(ContextRefreshedEvent.class) public void registerHandler() { Signal.handle(new Signal(signalType), applicationTerminator.handleTermination()); } From 1c9d9a61af15033f94d7f58f1b1bfdd2961fcf6d Mon Sep 17 00:00:00 2001 From: jeyong Date: Tue, 3 Dec 2024 04:44:09 +0900 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20POSIX=20=ED=91=9C=EC=A4=80=EC=97=90?= =?UTF-8?q?=20=EB=94=B0=EB=9D=BC=20=EC=A2=85=EB=A3=8C=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=EB=A5=BC=200-255=20=EC=82=AC=EC=9D=B4=EC=9D=98=20=EA=B0=92?= =?UTF-8?q?=EC=9D=84=20=EC=82=AC=EC=9A=A9=ED=95=A0=20=EC=88=98=20=EC=9E=88?= =?UTF-8?q?=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 --- .../handler/SigtermHandlerProperties.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/handler/src/main/java/io/jeyong/handler/SigtermHandlerProperties.java b/handler/src/main/java/io/jeyong/handler/SigtermHandlerProperties.java index 3fcedc8..f5812e8 100644 --- a/handler/src/main/java/io/jeyong/handler/SigtermHandlerProperties.java +++ b/handler/src/main/java/io/jeyong/handler/SigtermHandlerProperties.java @@ -53,6 +53,9 @@ @ConfigurationProperties(prefix = "kubernetes.sigterm-handler") public final class SigtermHandlerProperties { + private static final int MIN_EXIT_CODE = 0; + private static final int MAX_EXIT_CODE = 255; + private boolean enabled = true; private int exitCode = 0; private String terminationMessagePath; @@ -71,6 +74,7 @@ public int getExitCode() { } public void setExitCode(final int exitCode) { + validateExitCode(exitCode); this.exitCode = exitCode; } @@ -89,4 +93,18 @@ public String getTerminationMessage() { public void setTerminationMessage(final String terminationMessage) { this.terminationMessage = terminationMessage; } + + /** + * Validates the exit code to ensure it adheres to the POSIX standard. + * + * @param exitCode the exit code to validate + * @throws IllegalArgumentException if the exit code is outside the valid range + */ + private void validateExitCode(final int exitCode) { + if (exitCode < MIN_EXIT_CODE || exitCode > MAX_EXIT_CODE) { + throw new IllegalArgumentException( + String.format("Exit code must be between %d and %d (inclusive) as per POSIX standard.", + MIN_EXIT_CODE, MAX_EXIT_CODE)); + } + } } From b4f997a00851784882961a7894b2d2a2224218f2 Mon Sep 17 00:00:00 2001 From: jeyong Date: Thu, 19 Dec 2024 00:11:29 +0900 Subject: [PATCH 5/5] =?UTF-8?q?fix:=20=EC=8B=9C=EA=B7=B8=EB=84=90=EC=9D=98?= =?UTF-8?q?=20=EC=9D=B4=EB=A6=84=EC=9D=84=20=EB=B6=88=EB=9F=AC=EC=98=A4?= =?UTF-8?q?=EC=A7=80=20=EB=AA=BB=ED=95=B4=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EA=B0=80=20=EA=B9=A8=EC=A7=80=EB=8A=94=20=EB=AC=B8=EC=A0=9C=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/jeyong/test/unit/ApplicationTerminatorTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/src/test/java/io/jeyong/test/unit/ApplicationTerminatorTest.java b/test/src/test/java/io/jeyong/test/unit/ApplicationTerminatorTest.java index 0b15005..02a5598 100644 --- a/test/src/test/java/io/jeyong/test/unit/ApplicationTerminatorTest.java +++ b/test/src/test/java/io/jeyong/test/unit/ApplicationTerminatorTest.java @@ -8,6 +8,7 @@ import java.nio.file.Files; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import sun.misc.Signal; import sun.misc.SignalHandler; @DisplayName("ApplicationTerminator Unit Test") @@ -30,7 +31,7 @@ protected int getExitCode() { }; // when - catchSystemExit(() -> terminator.handleTermination().handle(null)); + catchSystemExit(() -> terminator.handleTermination().handle(new Signal("TERM"))); // then assertThat(Files.readString(tempFile.toPath())).isEqualTo(expectedMessage); @@ -54,7 +55,7 @@ protected int getExitCode() { SignalHandler handler = terminator.handleTermination(); // when - int actualExitCode = catchSystemExit(() -> handler.handle(null)); + int actualExitCode = catchSystemExit(() -> handler.handle(new Signal("TERM"))); // then assertThat(actualExitCode).isEqualTo(expectedExitCode);