Skip to content

Commit acd57bb

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

File tree

15 files changed

+691
-198
lines changed

15 files changed

+691
-198
lines changed

Golang_RHEL_Dockerfile

Lines changed: 0 additions & 167 deletions
This file was deleted.

RHEL_Dockerfile

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,35 @@ ARG STIG_COMPLIANT=false
1212
# Final clean image of precompiled driver container
1313
ARG D_FINAL_BASE_IMAGE=registry.access.redhat.com/ubi9/ubi:latest
1414

15-
##################################################################
16-
# Stage: Minimal base image update and install common requirements
17-
1815
# DTK base image (below example for specific kernel headers version)
1916
ARG D_BASE_IMAGE="registry.redhat.io/openshift4/driver-toolkit-rhel9:v4.13.0-202309112001.p0.gd719bdc.assembly.stream"
2017
# Standart: registry.access.redhat.com/ubi9:latest
2118

2219
ARG D_PYTHON_VERSION="36"
2320
ARG D_PYTHON="python${D_PYTHON_VERSION}"
2421

22+
##################################################################
23+
# Stage: build go binary for entrypoint
24+
FROM golang:1.24 AS go_builder
25+
26+
# Set GOPROXY if provided
27+
ARG GOPROXY
28+
ENV GOPROXY=$GOPROXY
29+
30+
WORKDIR /workspace
31+
32+
COPY entrypoint/go.mod go.mod
33+
COPY entrypoint/go.sum go.sum
34+
35+
RUN go mod download
36+
37+
COPY entrypoint/ .
38+
39+
RUN TARGETARCH=${D_ARCH} TARGETOS=linux make build
40+
41+
##################################################################
42+
# Stage: Minimal base image update and install common requirements
43+
2544
FROM $D_BASE_IMAGE AS base
2645

2746
# Inherited global args
@@ -38,6 +57,13 @@ RUN set -x && \
3857
# Container functional requirements
3958
jq iproute kmod procps-ng udev
4059

60+
COPY --from=go_builder /workspace/build/entrypoint /root/entrypoint
61+
WORKDIR /root
62+
ADD ./entrypoint.sh /root/entrypoint.sh
63+
ADD ./loader.sh /root/loader.sh
64+
65+
ENTRYPOINT ["/root/loader.sh"]
66+
4167
##############################################################################################
4268
# Stage: Download NVIDIA driver sources and install src driver container packages requirements
4369

@@ -78,7 +104,7 @@ RUN if file ${D_OFED_SRC_ARCHIVE} | grep compressed; then \
78104
mv ${D_OFED_SRC_ARCHIVE}/MLNX_OFED_SRC-${D_OFED_VERSION} . ; \
79105
fi
80106

81-
WORKDIR /
107+
WORKDIR /root
82108
ADD ./entrypoint.sh /root/entrypoint.sh
83109
ADD ./dtk_nic_driver_build.sh /root/dtk_nic_driver_build.sh
84110

@@ -92,7 +118,7 @@ RUN set -x && \
92118
fi && \
93119
rm -f /tmp/stig-fixer.sh
94120

95-
ENTRYPOINT ["/root/entrypoint.sh"]
121+
ENTRYPOINT ["/root/loader.sh"]
96122
CMD ["sources"]
97123

98124
LABEL doca-version=${D_DOCA_VERSION}
@@ -153,5 +179,5 @@ WORKDIR /
153179
ADD ./entrypoint.sh /root/entrypoint.sh
154180
ADD ./dtk_nic_driver_build.sh /root/dtk_nic_driver_build.sh
155181

156-
ENTRYPOINT ["/root/entrypoint.sh"]
182+
ENTRYPOINT ["/root/loader.sh"]
157183
CMD ["precompiled"]

THIRD_PARTY_NOTICES

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,29 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
720720
See the License for the specific language governing permissions and
721721
limitations under the License.
722722

723+
---
724+
## go-shellquote
725+
726+
Copyright (C) 2014 Kevin Ballard
727+
728+
Permission is hereby granted, free of charge, to any person obtaining
729+
a copy of this software and associated documentation files (the "Software"),
730+
to deal in the Software without restriction, including without limitation
731+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
732+
and/or sell copies of the Software, and to permit persons to whom the
733+
Software is furnished to do so, subject to the following conditions:
734+
735+
The above copyright notice and this permission notice shall be included
736+
in all copies or substantial portions of the Software.
737+
738+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
739+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
740+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
741+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
742+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
743+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
744+
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
745+
723746
---
724747
## entrypoint
725748

dtk_nic_driver_build.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
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+
echo "Using Go entrypoint for DTK build"
13+
exec "$(dirname "$0")/entrypoint" dtk-build
14+
fi
15+
416
: ${ENTRYPOINT_DEBUG:=false}
517
: ${DTK_OCP_NIC_SHARED_DIR:=/mnt/shared-nvidia-nic-driver-toolkit}
618

entrypoint/cmd/main.go

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
package main
1818

1919
import (
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+
3768
func 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

Comments
 (0)