-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add common script utilities and constants for operator (#121)
- Introduced constants for Vector log directory and shutdown file: - VectorLogDir: "_vector" - ShutdownFile: "shutdown" - Added script invocation constants for signal handling and termination: - InvokePrepareSignalHandlers: "prepare_signal_handlers" - InvokeWaitForTermination: "wait_for_termination $!" - Provided a set of common Bash trap functions to handle TERM signals and manage child processes. - Implemented utility functions to manage Vector shutdown files: - RemoveVectorShutdownFileCommand: Command to remove the Vector shutdown file if it exists. - CreateVectorShutdownFileCommand: Command to create a shutdown file for the Vector container. - Added a function to export pod address and ports from the listener directory provided by the listener operator. This change enhances the operator's capabilities by providing reusable scripts and constants for common tasks, improving operational efficiency and code consistency.
- Loading branch information
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/zncdatadev/operator-go/pkg/constants" | ||
) | ||
|
||
// Subdirectory of the log directory containing files to control the Vector instance | ||
const ( | ||
VectorLogDir = "_vector" | ||
|
||
// File to signal that Vector should be gracefully shut down | ||
ShutdownFile = "shutdown" | ||
) | ||
|
||
const ( | ||
InvokePrepareSignalHandlers = "prepare_signal_handlers" | ||
InvokeWaitForTermination = "wait_for_termination $!" | ||
) | ||
|
||
const CommonBashTrapFunctions = `prepare_signal_handlers() | ||
{ | ||
unset term_child_pid | ||
unset term_kill_needed | ||
trap 'handle_term_signal' TERM | ||
} | ||
handle_term_signal() | ||
{ | ||
if [ "${term_child_pid}" ]; then | ||
kill -TERM "${term_child_pid}" 2>/dev/null | ||
else | ||
term_kill_needed="yes" | ||
fi | ||
} | ||
wait_for_termination() | ||
{ | ||
set +e | ||
term_child_pid=$1 | ||
if [[ -v term_kill_needed ]]; then | ||
kill -TERM "${term_child_pid}" 2>/dev/null | ||
fi | ||
wait ${term_child_pid} 2>/dev/null | ||
trap - TERM | ||
wait ${term_child_pid} 2>/dev/null | ||
set -e | ||
} | ||
` | ||
|
||
// Use this command to remove the shutdown file (if it exists) created by [`create_vector_shutdown_file_command`]. | ||
// You should execute this command before starting your application. | ||
func RemoveVectorShutdownFileCommand() string { | ||
return fmt.Sprintf("rm -f %s/%s/%s", constants.KubedoopLogDir, VectorLogDir, ShutdownFile) | ||
} | ||
|
||
// Command to create a shutdown file for the vector container. | ||
// Please delete it before starting your application using `RemoveVectorShutdownFileCommand` . | ||
func CreateVectorShutdownFileCommand() string { | ||
return fmt.Sprintf("mkdir -p %s/%s && touch %s/%s/%s", constants.KubedoopLogDir, VectorLogDir, constants.KubedoopLogDir, VectorLogDir, ShutdownFile) | ||
} | ||
|
||
// ExportPodAddress fetch the pod address from the default-address directory and export it as POD_ADDRESS | ||
// the listener was provided by listener operator | ||
func ExportPodAddress() string { | ||
return fmt.Sprintf(`if [[ -d %s ]]; then | ||
export POD_ADDRESS=$(cat %s/default-address/address) | ||
for i in %s/default-address/ports/*; do | ||
export $(basename $i | tr a-z A-Z)_PORT="$(cat $i)" | ||
done | ||
fi`, constants.KubedoopListenerDir, constants.KubedoopListenerDir, constants.KubedoopListenerDir) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package util_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/zncdatadev/operator-go/pkg/util" | ||
) | ||
|
||
var _ = Describe("RemoveVectorShutdownFileCommand", func() { | ||
Context("Testing RemoveVectorShutdownFileCommand function", func() { | ||
It("should generate correct command string ", func() { | ||
command := util.RemoveVectorShutdownFileCommand() | ||
Expect(command).Should(Equal("rm -f /kubedoop/log/_vector/shutdown")) | ||
}) | ||
}) | ||
}) | ||
|
||
var _ = Describe("CreateVectorShutdownFileCommand", func() { | ||
It("should generate correct command string ", func() { | ||
command := util.CreateVectorShutdownFileCommand() | ||
Expect(command).Should(Equal("mkdir -p /kubedoop/log/_vector && touch /kubedoop/log/_vector/shutdown")) | ||
}) | ||
}) | ||
|
||
var _ = Describe("ExportPodAddress", func() { | ||
Context("correctly generates the command", func() { | ||
It("supports valid logDir input", func() { | ||
command := util.ExportPodAddress() | ||
Expect(command).Should(Equal(`if [[ -d /kubedoop/listener ]]; then | ||
export POD_ADDRESS=$(cat /kubedoop/listener/default-address/address) | ||
for i in /kubedoop/listener/default-address/ports/*; do | ||
export $(basename $i | tr a-z A-Z)_PORT="$(cat $i)" | ||
done | ||
fi`)) | ||
}) | ||
}) | ||
}) |