1717package main
1818
1919import (
20+ "context"
2021 "encoding/json"
2122 "flag"
2223 "fmt"
2324 "os"
2425 "os/signal"
26+ "path/filepath"
2527 "syscall"
2628
2729 "github.com/go-logr/logr"
@@ -30,10 +32,39 @@ import (
3032
3133 "github.com/Mellanox/doca-driver-build/entrypoint/internal/config"
3234 "github.com/Mellanox/doca-driver-build/entrypoint/internal/constants"
35+ "github.com/Mellanox/doca-driver-build/entrypoint/internal/dtk"
3336 "github.com/Mellanox/doca-driver-build/entrypoint/internal/entrypoint"
37+ "github.com/Mellanox/doca-driver-build/entrypoint/internal/utils/cmd"
3438 "github.com/Mellanox/doca-driver-build/entrypoint/internal/version"
3539)
3640
41+ type ctxData struct {
42+ //nolint:containedctx
43+ Ctx context.Context
44+ Cancel context.CancelFunc
45+ }
46+
47+ // setupSignalHandler takes a signal channel and contexts with cancel functions.
48+ // It starts a goroutine that cancels the first uncanceled context on receiving a signal,
49+ // if no uncanceled context exists, it exits the application with code 1.
50+ func setupSignalHandler (ch chan os.Signal , ctxs []ctxData ) {
51+ go func () {
52+ OUT:
53+ for {
54+ <- ch
55+ for _ , ctx := range ctxs {
56+ if ctx .Ctx .Err () != nil {
57+ // context is already canceled, try next one
58+ continue
59+ }
60+ ctx .Cancel ()
61+ continue OUT
62+ }
63+ os .Exit (1 )
64+ }
65+ }()
66+ }
67+
3768func main () {
3869 cfg , err := config .GetConfig ()
3970 if err != nil {
@@ -57,6 +88,20 @@ func main() {
5788 os .Exit (1 )
5889 }
5990 log .Info ("start manager" , "mode" , containerMode )
91+ if containerMode == constants .DriverContainerModeDtkBuild {
92+ // Use a context that is canceled on signal
93+ ctx , cancel := context .WithCancel (context .Background ())
94+ // Attach logger to context
95+ ctx = logr .NewContext (ctx , log )
96+ setupSignalHandler (getSignalChannel (), []ctxData {{Ctx : ctx , Cancel : cancel }})
97+
98+ if err := dtk .RunBuild (ctx , log , cfg , cmd .New ()); err != nil {
99+ log .Error (err , "DTK Build failed" )
100+ os .Exit (1 )
101+ }
102+ return
103+ }
104+
60105 if err := entrypoint .Run (getSignalChannel (), log , containerMode , cfg ); err != nil {
61106 log .Error (err , "Entrypoint Run failed" )
62107 os .Exit (1 )
@@ -67,9 +112,11 @@ func getContainerMode() (string, error) {
67112 flag .Parse ()
68113 containerMode := flag .Arg (0 )
69114 if flag .NArg () != 1 ||
70- (containerMode != constants .DriverContainerModePrecompiled && containerMode != string (constants .DriverContainerModeSources )) {
71- return "" , fmt .Errorf ("container mode argument has invalid value %s, supported values: %s, %s" ,
72- containerMode , constants .DriverContainerModePrecompiled , constants .DriverContainerModeSources )
115+ (containerMode != constants .DriverContainerModePrecompiled &&
116+ containerMode != constants .DriverContainerModeSources &&
117+ containerMode != constants .DriverContainerModeDtkBuild ) {
118+ return "" , fmt .Errorf ("container mode argument has invalid value %s, supported values: %s, %s, %s" ,
119+ containerMode , constants .DriverContainerModePrecompiled , constants .DriverContainerModeSources , constants .DriverContainerModeDtkBuild )
73120 }
74121 return containerMode , nil
75122}
@@ -87,6 +134,11 @@ func getLogger(cfg config.Config) logr.Logger {
87134 if cfg .EntrypointDebug {
88135 logConfig .Level = zap .NewAtomicLevelAt (zap .DebugLevel )
89136 if cfg .DebugLogFile != "" {
137+ // Create directory if it doesn't exist
138+ logDir := filepath .Dir (cfg .DebugLogFile )
139+ if err := os .MkdirAll (logDir , 0o755 ); err != nil {
140+ fmt .Fprintf (os .Stderr , "WARNING: failed to create log directory %s: %v\n " , logDir , err )
141+ }
90142 logConfig .OutputPaths = append (logConfig .OutputPaths , cfg .DebugLogFile )
91143 logConfig .ErrorOutputPaths = append (logConfig .ErrorOutputPaths , cfg .DebugLogFile )
92144 }
0 commit comments