From c0dc9e916ae9c2aeb1aa97b7e0dde9c47c3d232e Mon Sep 17 00:00:00 2001 From: Alexandr Stefurishin Date: Tue, 10 Dec 2024 17:08:17 +0300 Subject: [PATCH] simplify Signed-off-by: Alexandr Stefurishin --- images/tlshd/src/cmd/main.go | 77 ++++++++++-------------------------- 1 file changed, 20 insertions(+), 57 deletions(-) diff --git a/images/tlshd/src/cmd/main.go b/images/tlshd/src/cmd/main.go index 8733e36..c7daf5b 100644 --- a/images/tlshd/src/cmd/main.go +++ b/images/tlshd/src/cmd/main.go @@ -46,17 +46,28 @@ func (o *Opt) Parse() { func main() { opt.Parse() + // Create a base context + ctx, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + args := []string{"-s"} if opt.TimeoutWait > 0 { args = append(args, "-c", "/opt/deckhouse/csi/etc/tlshd.conf") + + tctx, cancel := context.WithTimeout(ctx, time.Duration(opt.TimeoutWait)*time.Second) + defer cancel() + ctx = tctx + + log.Printf("Timeout specified: %d seconds", opt.TimeoutWait) + } else { args = append(args, "-c", "/etc/tlshd.conf") + log.Println("No timeout specified. Process will run indefinitely until it finishes or receives a termination signal.") } - cmd := exec.Command("/opt/deckhouse/csi/bin/tlshd", args...) + + cmd := exec.CommandContext(ctx, "/opt/deckhouse/csi/bin/tlshd", args...) var stderrBuf bytes.Buffer // MultiWriter to write to both os.Stderr and the buffer - // // --- because there is no non-zero return code --- // # ./src/tlshd/tlshd -s -c ./src/tlshd/tlshd.conf; echo $? // tlshd[147529]: Built from ktls-utils 0.11 on Nov 20 2024 12:52:22 @@ -68,62 +79,14 @@ func main() { cmd.Stderr = multiStderr cmd.Stdout = os.Stdout - err := cmd.Start() - if err != nil { - log.Fatalf("Failed to start the command: %v", err) - } - log.Printf("Process started with PID: %d", cmd.Process.Pid) - - // Create a base context - ctx, cancel := context.WithCancel(context.Background()) - // defer cancel() - - // If a timeout is specified, wrap the context with a timeout - if opt.TimeoutWait > 0 { - ctx, cancel = context.WithTimeout(ctx, time.Duration(opt.TimeoutWait)*time.Second) - // defer cancel() - log.Printf("Timeout specified: %d seconds", opt.TimeoutWait) - } else { - log.Println("No timeout specified. Process will run indefinitely until it finishes or receives a termination signal.") + // run + if err := cmd.Run(); err != nil { + log.Fatalf("Failed to run process: %v", err) } - // Channel to handle process completion - done := make(chan error, 1) - - // Start a goroutine to monitor when the process finishes - go func() { - done <- cmd.Wait() - }() - - // Channel to handle OS signals - signalChan := make(chan os.Signal, 1) - signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM) - - select { - case <-ctx.Done(): // Timeout or cancellation occurred - log.Println("Context done (timeout or cancellation), terminating the process...") - if err := cmd.Process.Kill(); err != nil { - cancel() - log.Fatalf("Failed to terminate the process: %v", err) - } - log.Println("Process has been terminated.") - case err := <-done: // Process finished on its own - if err != nil { - cancel() - log.Fatalf("Process exited with an error: %v", err) - } - - r := regexp.MustCompile(`\s+is\s+not\s+available`) - if r.Match(stderrBuf.Bytes()) { - os.Exit(1) - } - - log.Println("Process exited successfully.") - case sig := <-signalChan: // Received termination signal - log.Printf("Received signal: %v, terminating the process...", sig) - if err := cmd.Process.Kill(); err != nil { - log.Fatalf("Failed to terminate the process: %v", err) - } - log.Println("Process has been terminated.") + r := regexp.MustCompile(`\s+is\s+not\s+available`) + if r.Match(stderrBuf.Bytes()) { + os.Exit(1) } + log.Printf("success") }