Skip to content

Commit 0d2bf3b

Browse files
committed
feat: support DTK with Go
Signed-off-by: Fred Rolland <frolland@nvidia.com>
1 parent a9badf3 commit 0d2bf3b

File tree

10 files changed

+487
-25
lines changed

10 files changed

+487
-25
lines changed

dtk_nic_driver_build.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
#!/bin/bash
22
# Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
33

4+
# Load environment variables if file exists
5+
if [ -f "$(dirname "$0")/dtk.env" ]; then
6+
source "$(dirname "$0")/dtk.env"
7+
fi
8+
9+
: ${USE_NEW_ENTRYPOINT:=false}
10+
11+
if [ "$USE_NEW_ENTRYPOINT" = "true" ]; then
12+
exec "$(dirname "$0")/entrypoint" dtk-build
13+
fi
14+
415
: ${ENTRYPOINT_DEBUG:=false}
516
: ${DTK_OCP_NIC_SHARED_DIR:=/mnt/shared-nvidia-nic-driver-toolkit}
617

entrypoint/cmd/main.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package main
1818

1919
import (
20+
"context"
2021
"encoding/json"
2122
"flag"
2223
"fmt"
@@ -30,10 +31,38 @@ import (
3031

3132
"github.com/Mellanox/doca-driver-build/entrypoint/internal/config"
3233
"github.com/Mellanox/doca-driver-build/entrypoint/internal/constants"
34+
"github.com/Mellanox/doca-driver-build/entrypoint/internal/dtk"
3335
"github.com/Mellanox/doca-driver-build/entrypoint/internal/entrypoint"
3436
"github.com/Mellanox/doca-driver-build/entrypoint/internal/version"
3537
)
3638

39+
type ctxData struct {
40+
//nolint:containedctx
41+
Ctx context.Context
42+
Cancel context.CancelFunc
43+
}
44+
45+
// setupSignalHandler takes a signal channel and contexts with cancel functions.
46+
// It starts a goroutine that cancels the first uncanceled context on receiving a signal,
47+
// if no uncanceled context exists, it exits the application with code 1.
48+
func setupSignalHandler(ch chan os.Signal, ctxs []ctxData) {
49+
go func() {
50+
OUT:
51+
for {
52+
<-ch
53+
for _, ctx := range ctxs {
54+
if ctx.Ctx.Err() != nil {
55+
// context is already canceled, try next one
56+
continue
57+
}
58+
ctx.Cancel()
59+
continue OUT
60+
}
61+
os.Exit(1)
62+
}
63+
}()
64+
}
65+
3766
func main() {
3867
cfg, err := config.GetConfig()
3968
if err != nil {
@@ -57,6 +86,18 @@ func main() {
5786
os.Exit(1)
5887
}
5988
log.Info("start manager", "mode", containerMode)
89+
if containerMode == constants.DriverContainerModeDtkBuild {
90+
// Use a context that is canceled on signal
91+
ctx, cancel := context.WithCancel(context.Background())
92+
setupSignalHandler(getSignalChannel(), []ctxData{{Ctx: ctx, Cancel: cancel}})
93+
94+
if err := dtk.RunBuild(ctx, log, cfg); err != nil {
95+
log.Error(err, "DTK Build failed")
96+
os.Exit(1)
97+
}
98+
return
99+
}
100+
60101
if err := entrypoint.Run(getSignalChannel(), log, containerMode, cfg); err != nil {
61102
log.Error(err, "Entrypoint Run failed")
62103
os.Exit(1)
@@ -67,9 +108,11 @@ func getContainerMode() (string, error) {
67108
flag.Parse()
68109
containerMode := flag.Arg(0)
69110
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)
111+
(containerMode != constants.DriverContainerModePrecompiled &&
112+
containerMode != constants.DriverContainerModeSources &&
113+
containerMode != constants.DriverContainerModeDtkBuild) {
114+
return "", fmt.Errorf("container mode argument has invalid value %s, supported values: %s, %s, %s",
115+
containerMode, constants.DriverContainerModePrecompiled, constants.DriverContainerModeSources, constants.DriverContainerModeDtkBuild)
73116
}
74117
return containerMode, nil
75118
}

entrypoint/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
github.com/google/go-cmp v0.7.0 // indirect
1919
github.com/google/pprof v0.0.0-20260115054156-294ebfa9ad83 // indirect
2020
github.com/google/uuid v1.1.2 // indirect
21+
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
2122
github.com/pmezard/go-difflib v1.0.0 // indirect
2223
github.com/spf13/afero v1.9.4 // indirect
2324
github.com/stretchr/objx v0.5.2 // indirect

entrypoint/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
147147
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
148148
github.com/k8snetworkplumbingwg/sriovnet v1.2.0 h1:6ELfAxCB1dvosGUy3DVRmfH+HWTzmPD3W67HKQvMR1M=
149149
github.com/k8snetworkplumbingwg/sriovnet v1.2.0/go.mod h1:jyWzGe6ZtYiPq6ih6aXCOy6mZ49Y9mNyBOLBBXnli+k=
150+
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
151+
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
150152
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
151153
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
152154
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=

entrypoint/internal/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ type Config struct {
4343

4444
DtkOcpDriverBuild bool `env:"DTK_OCP_DRIVER_BUILD"`
4545
DtkOcpNicSharedDir string `env:"DTK_OCP_NIC_SHARED_DIR" envDefault:"/mnt/shared-nvidia-nic-driver-toolkit"`
46+
DtkOcpCompiledDriverVer string `env:"DTK_OCP_COMPILED_DRIVER_VER"`
47+
DtkOcpStartCompileFlag string `env:"DTK_OCP_START_COMPILE_FLAG"`
48+
DtkOcpDoneCompileFlag string `env:"DTK_OCP_DONE_COMPILE_FLAG"`
49+
AppendDriverBuildFlags string `env:"APPEND_DRIVER_BUILD_FLAGS"`
4650
NvidiaNicDriversInventoryPath string `env:"NVIDIA_NIC_DRIVERS_INVENTORY_PATH"`
4751

4852
OfedBlacklistModulesFile string `env:"OFED_BLACKLIST_MODULES_FILE" envDefault:"/host/etc/modprobe.d/blacklist-ofed-modules.conf"`

entrypoint/internal/constants/constants.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121

2222
DriverContainerModeSources = "sources"
2323
DriverContainerModePrecompiled = "precompiled"
24+
DriverContainerModeDtkBuild = "dtk-build"
2425

2526
// OS Types
2627
OSTypeUbuntu = "ubuntu"
@@ -33,4 +34,9 @@ const (
3334
DefaultOpenShiftVersion = "4.9"
3435

3536
InvalidGUID = "00:00:00:00:00:00:00:00"
37+
38+
// DTK constants
39+
DtkOcpBuildScriptPath = "/root/dtk_nic_driver_build.sh"
40+
DtkStartCompileFlag = "dtk_start_compile"
41+
DtkDoneCompileFlagPrefix = "dtk_done_compile_"
3642
)

entrypoint/internal/driver/driver.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -171,25 +171,38 @@ func (d *driverMgr) Build(ctx context.Context) error {
171171
// Mark build as incomplete at the start
172172
d.driverBuildIncomplete = true
173173

174-
// Create inventory directory
175-
if err := d.createInventoryDirectory(ctx, inventoryPath); err != nil {
176-
return fmt.Errorf("failed to create inventory directory: %w", err)
177-
}
174+
// Check if DTK OCP driver build is enabled
175+
if d.cfg.DtkOcpDriverBuild {
176+
if err := d.buildDriverDTK(ctx, kernelVersion, inventoryPath); err != nil {
177+
return err
178+
}
179+
} else {
180+
// Create inventory directory
181+
if err := d.createInventoryDirectory(ctx, inventoryPath); err != nil {
182+
return fmt.Errorf("failed to create inventory directory: %w", err)
183+
}
178184

179-
// Install OS-specific prerequisites
180-
log.V(1).Info("About to install prerequisites", "os", osType, "kernel", kernelVersion)
181-
if err := d.installPrerequisitesForOS(ctx, osType, kernelVersion); err != nil {
182-
return fmt.Errorf("failed to install prerequisites: %w", err)
183-
}
185+
// Install OS-specific prerequisites
186+
log.V(1).Info("About to install prerequisites", "os", osType, "kernel", kernelVersion)
187+
if err := d.installPrerequisitesForOS(ctx, osType, kernelVersion); err != nil {
188+
return fmt.Errorf("failed to install prerequisites: %w", err)
189+
}
184190

185-
// Build driver from source
186-
if err := d.buildDriverFromSource(ctx, d.cfg.NvidiaNicDriverPath, kernelVersion, osType); err != nil {
187-
return fmt.Errorf("failed to build driver from source: %w", err)
188-
}
191+
// Build driver from source
192+
if err := d.buildDriverFromSource(ctx, d.cfg.NvidiaNicDriverPath, kernelVersion, osType); err != nil {
193+
return fmt.Errorf("failed to build driver from source: %w", err)
194+
}
195+
196+
// Copy build artifacts to inventory
197+
if err := d.copyBuildArtifacts(ctx, d.cfg.NvidiaNicDriverPath, inventoryPath, osType); err != nil {
198+
return fmt.Errorf("failed to copy build artifacts: %w", err)
199+
}
189200

190-
// Copy build artifacts to inventory
191-
if err := d.copyBuildArtifacts(ctx, d.cfg.NvidiaNicDriverPath, inventoryPath, osType); err != nil {
192-
return fmt.Errorf("failed to copy build artifacts: %w", err)
201+
// Fix source link if needed
202+
if err := d.fixSourceLink(ctx, kernelVersion); err != nil {
203+
log.V(1).Info("Failed to fix source link", "error", err)
204+
// Non-fatal error, continue
205+
}
193206
}
194207

195208
// Calculate and store checksum
@@ -199,12 +212,6 @@ func (d *driverMgr) Build(ctx context.Context) error {
199212
}
200213
}
201214

202-
// Fix source link if needed
203-
if err := d.fixSourceLink(ctx, kernelVersion); err != nil {
204-
log.V(1).Info("Failed to fix source link", "error", err)
205-
// Non-fatal error, continue
206-
}
207-
208215
// Mark build as complete after successful build
209216
d.driverBuildIncomplete = false
210217

0 commit comments

Comments
 (0)