From 86f68a49014a4cffb7dcb51f14a02f6f1816b2ee Mon Sep 17 00:00:00 2001 From: Evan Lezar <elezar@nvidia.com> Date: Mon, 4 Dec 2023 22:22:18 +0100 Subject: [PATCH 1/3] Add errors.Join wrapper Signed-off-by: Evan Lezar <elezar@nvidia.com> --- internal/errors/errors.go | 27 +++++++++++++++++++++++ internal/errors/errors_legacy.go | 36 +++++++++++++++++++++++++++++++ internal/errors/errors_modern.go | 28 ++++++++++++++++++++++++ internal/lookup/merge.go | 8 +++---- internal/modifier/cdi/registry.go | 2 +- internal/runtime/logger.go | 2 +- internal/runtime/runtime.go | 2 +- 7 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 internal/errors/errors.go create mode 100644 internal/errors/errors_legacy.go create mode 100644 internal/errors/errors_modern.go diff --git a/internal/errors/errors.go b/internal/errors/errors.go new file mode 100644 index 000000000..937f52e6a --- /dev/null +++ b/internal/errors/errors.go @@ -0,0 +1,27 @@ +/** +# Copyright 2023 NVIDIA CORPORATION +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +**/ + +package errors + +import ( + "errors" +) + +// New provides a wrapper for errors.New. +// This allows this internal package to be used as a drop-in replacement for the stdlib package. +func New(text string) error { + return errors.New(text) +} diff --git a/internal/errors/errors_legacy.go b/internal/errors/errors_legacy.go new file mode 100644 index 000000000..54c46ecfc --- /dev/null +++ b/internal/errors/errors_legacy.go @@ -0,0 +1,36 @@ +//go:build !go1.20 + +/** +# Copyright 2023 NVIDIA CORPORATION +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +**/ + +package errors + +import "fmt" + +// Join provides a legacy implementation for errors.Join. +func Join(errs ...error) error { + var all error + for _, err := range errs { + if err == nil { + continue + } + if all == nil { + all = err + } + all = fmt.Errorf("%v %w", all, err) + } + return all +} diff --git a/internal/errors/errors_modern.go b/internal/errors/errors_modern.go new file mode 100644 index 000000000..b2c2d414b --- /dev/null +++ b/internal/errors/errors_modern.go @@ -0,0 +1,28 @@ +//go:build go1.20 + +/** +# Copyright 2023 NVIDIA CORPORATION +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +**/ + +package errors + +import ( + "errors" +) + +// Join wraps errors.Join for newer golang versions. +func Join(errs ...error) error { + return errors.Join(errs...) +} diff --git a/internal/lookup/merge.go b/internal/lookup/merge.go index fa20b5125..df202033d 100644 --- a/internal/lookup/merge.go +++ b/internal/lookup/merge.go @@ -16,7 +16,7 @@ package lookup -import "errors" +import "github.com/NVIDIA/nvidia-container-toolkit/internal/errors" type first []Locator @@ -34,14 +34,14 @@ func First(locators ...Locator) Locator { // Locate returns the results for the first locator that returns a non-empty non-error result. func (f first) Locate(pattern string) ([]string, error) { - var allErrors []error + var allErrors error for _, l := range f { if l == nil { continue } candidates, err := l.Locate(pattern) if err != nil { - allErrors = append(allErrors, err) + allErrors = errors.Join(allErrors, err) continue } if len(candidates) > 0 { @@ -49,5 +49,5 @@ func (f first) Locate(pattern string) ([]string, error) { } } - return nil, errors.Join(allErrors...) + return nil, allErrors } diff --git a/internal/modifier/cdi/registry.go b/internal/modifier/cdi/registry.go index d1faffad5..024db4d5e 100644 --- a/internal/modifier/cdi/registry.go +++ b/internal/modifier/cdi/registry.go @@ -17,12 +17,12 @@ package cdi import ( - "errors" "fmt" "github.com/opencontainers/runtime-spec/specs-go" "tags.cncf.io/container-device-interface/pkg/cdi" + "github.com/NVIDIA/nvidia-container-toolkit/internal/errors" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "github.com/NVIDIA/nvidia-container-toolkit/internal/oci" ) diff --git a/internal/runtime/logger.go b/internal/runtime/logger.go index 1b9500265..bce78e156 100644 --- a/internal/runtime/logger.go +++ b/internal/runtime/logger.go @@ -17,7 +17,6 @@ package runtime import ( - "errors" "fmt" "io" "os" @@ -28,6 +27,7 @@ import ( "github.com/sirupsen/logrus" + "github.com/NVIDIA/nvidia-container-toolkit/internal/errors" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" ) diff --git a/internal/runtime/runtime.go b/internal/runtime/runtime.go index c98f96581..c7fe7594e 100644 --- a/internal/runtime/runtime.go +++ b/internal/runtime/runtime.go @@ -18,13 +18,13 @@ package runtime import ( "encoding/json" - "errors" "fmt" "strings" "github.com/opencontainers/runtime-spec/specs-go" "github.com/NVIDIA/nvidia-container-toolkit/internal/config" + "github.com/NVIDIA/nvidia-container-toolkit/internal/errors" "github.com/NVIDIA/nvidia-container-toolkit/internal/info" ) -- GitLab From 5956b04096d1a92b241b13cc1f3e208f8b99eea0 Mon Sep 17 00:00:00 2001 From: Evan Lezar <elezar@nvidia.com> Date: Mon, 4 Dec 2023 22:22:30 +0100 Subject: [PATCH 2/3] Use golang 1.17 Signed-off-by: Evan Lezar <elezar@nvidia.com> --- docker/Dockerfile.devel | 2 +- versions.mk | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/Dockerfile.devel b/docker/Dockerfile.devel index c664467f2..f81f1b3bd 100644 --- a/docker/Dockerfile.devel +++ b/docker/Dockerfile.devel @@ -16,7 +16,7 @@ ARG GOLANGCI_LINT_VERSION=v1.54.1 FROM golang:${GOLANG_VERSION} RUN go install golang.org/x/lint/golint@6edffad5e6160f5949cdefc81710b2706fbcd4f6 -RUN go install github.com/matryer/moq@latest +# RUN go install github.com/matryer/moq@latest RUN go install github.com/gordonklaus/ineffassign@d2c82e48359b033cde9cf1307f6d5550b8d61321 RUN go install github.com/client9/misspell/cmd/misspell@latest RUN go install github.com/google/go-licenses@latest diff --git a/versions.mk b/versions.mk index f5e708e4d..aeb311fc1 100644 --- a/versions.mk +++ b/versions.mk @@ -31,7 +31,7 @@ NVIDIA_CONTAINER_RUNTIME_VERSION := 3.14.0 LIBNVIDIA_CONTAINER0_VERSION := 0.10.0+jetpack CUDA_VERSION := 12.2.2 -GOLANG_VERSION := 1.20.5 +GOLANG_VERSION := 1.17 GIT_COMMIT ?= $(shell git describe --match="" --dirty --long --always --abbrev=40 2> /dev/null || echo "") GIT_COMMIT_SHORT ?= $(shell git rev-parse --short HEAD 2> /dev/null || echo "") -- GitLab From 80756d00a6b75761103c50f605cece5fa7e39392 Mon Sep 17 00:00:00 2001 From: Evan Lezar <elezar@nvidia.com> Date: Mon, 4 Dec 2023 23:13:42 +0100 Subject: [PATCH 3/3] Fix double error wrap fmt Signed-off-by: Evan Lezar <elezar@nvidia.com> --- cmd/nvidia-ctk/config/config.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/nvidia-ctk/config/config.go b/cmd/nvidia-ctk/config/config.go index 3f10f7276..5a21b6886 100644 --- a/cmd/nvidia-ctk/config/config.go +++ b/cmd/nvidia-ctk/config/config.go @@ -17,7 +17,6 @@ package config import ( - "errors" "fmt" "reflect" "strconv" @@ -28,6 +27,7 @@ import ( createdefault "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk/config/create-default" "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk/config/flags" "github.com/NVIDIA/nvidia-container-toolkit/internal/config" + "github.com/NVIDIA/nvidia-container-toolkit/internal/errors" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" ) @@ -141,7 +141,7 @@ func setFlagToKeyValue(setFlag string) (string, interface{}, error) { field, err := getField(key) if err != nil { - return key, nil, fmt.Errorf("%w: %w", errInvalidConfigOption, err) + return key, nil, errors.Join(err, errInvalidConfigOption) } kind := field.Kind() @@ -157,7 +157,7 @@ func setFlagToKeyValue(setFlag string) (string, interface{}, error) { case reflect.Bool: b, err := strconv.ParseBool(value) if err != nil { - return key, value, fmt.Errorf("%w: %w", errInvalidFormat, err) + return key, value, errors.Join(err, errInvalidFormat) } return key, b, err case reflect.String: @@ -172,7 +172,7 @@ func setFlagToKeyValue(setFlag string) (string, interface{}, error) { for _, v := range valueParts { vi, err := strconv.ParseInt(v, 10, 0) if err != nil { - return key, nil, fmt.Errorf("%w: %w", errInvalidFormat, err) + return key, nil, errors.Join(err, errInvalidFormat) } output = append(output, vi) } -- GitLab