Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add platform detection logic from nvidia-container-toolkit #28

Merged
merged 4 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ DOCKER ?= docker

PCI_IDS_URL ?= https://pci-ids.ucw.cz/v2.2/pci.ids

CHECK_TARGETS := lint
TARGETS := binary build all check fmt assert-fmt generate lint vet test coverage
DOCKER_TARGETS := $(patsubst %,docker-%, $(TARGETS))
.PHONY: $(TARGETS) $(DOCKER_TARGETS) vendor check-vendor
Expand All @@ -28,7 +29,7 @@ build:
GOOS=$(GOOS) go build ./...

all: check build binary
check: assert-fmt lint vet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't necessarily have an opinion here, just curious what made you decide to remove assert-fmt and vet from the checks?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are all handled by golangci-lint (could have been a different PR though).

check: $(CHECK_TARGETS)

vendor:
go mod tidy
Expand Down Expand Up @@ -59,8 +60,7 @@ generate:
go generate $(MODULE)/...

lint:
# We use `go list -f '{{.Dir}}' $(MODULE)/...` to skip the `vendor` folder.
go list -f '{{.Dir}}' $(MODULE)/... | grep -v pkg/nvml | xargs golint -set_exit_status
golangci-lint run ./...

## goimports: Apply goimports -local to the codebase
goimports:
Expand Down
41 changes: 41 additions & 0 deletions pkg/nvlib/info/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
# Copyright 2024 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 info

// Interface provides the API to the info package.
type Interface interface {
PlatformResolver
PropertyExtractor
}

// PlatformResolver defines a function to resolve the current platform.
type PlatformResolver interface {
ResolvePlatform() Platform
}

// PropertyExtractor provides a set of functions to query capabilities of the
// system.
//
//go:generate moq -rm -out property-extractor_mock.go . PropertyExtractor
type PropertyExtractor interface {
HasDXCore() (bool, string)
HasNvml() (bool, string)
HasTegraFiles() (bool, string)
// Deprecated: Use HasTegraFiles instead.
IsTegraSystem() (bool, string)
UsesOnlyNVGPUModule() (bool, string)
}
78 changes: 78 additions & 0 deletions pkg/nvlib/info/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
# Copyright 2024 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 info

import (
"github.com/NVIDIA/go-nvml/pkg/nvml"

"github.com/NVIDIA/go-nvlib/pkg/nvlib/device"
)

type infolib struct {
PropertyExtractor
PlatformResolver
}

type options struct {
logger basicLogger
root root
nvmllib nvml.Interface
devicelib device.Interface

platform Platform
propertyExtractor PropertyExtractor
}

// New creates a new instance of the 'info' interface.
func New(opts ...Option) Interface {
o := &options{}
for _, opt := range opts {
opt(o)
}
if o.logger == nil {
o.logger = &nullLogger{}
}
if o.root == "" {
o.root = "/"
}
if o.nvmllib == nil {
o.nvmllib = nvml.New(
nvml.WithLibraryPath(o.root.tryResolveLibrary("libnvidia-ml.so.1")),
)
}
if o.devicelib == nil {
o.devicelib = device.New(device.WithNvml(o.nvmllib))
}
if o.platform == "" {
o.platform = PlatformAuto
}
if o.propertyExtractor == nil {
o.propertyExtractor = &propertyExtractor{
root: o.root,
nvmllib: o.nvmllib,
devicelib: o.devicelib,
}
}
return &infolib{
PlatformResolver: &platformResolver{
logger: o.logger,
platform: o.platform,
propertyExtractor: o.propertyExtractor,
},
PropertyExtractor: o.propertyExtractor,
}
}
102 changes: 0 additions & 102 deletions pkg/nvlib/info/info.go

This file was deleted.

28 changes: 28 additions & 0 deletions pkg/nvlib/info/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
# Copyright 2024 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 info

type basicLogger interface {
Debugf(string, ...interface{})
Infof(string, ...interface{})
}

type nullLogger struct{}

func (n *nullLogger) Debugf(string, ...interface{}) {}

func (n *nullLogger) Infof(string, ...interface{}) {}
55 changes: 43 additions & 12 deletions pkg/nvlib/info/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,55 @@

package info

import (
"github.com/NVIDIA/go-nvml/pkg/nvml"

"github.com/NVIDIA/go-nvlib/pkg/nvlib/device"
)

// Option defines a function for passing options to the New() call.
type Option func(*infolib)
type Option func(*options)

// New creates a new instance of the 'info' interface.
func New(opts ...Option) Interface {
i := &infolib{}
for _, opt := range opts {
opt(i)
// WithDeviceLib sets the device library for the library.
func WithDeviceLib(devicelib device.Interface) Option {
return func(i *options) {
i.devicelib = devicelib
}
if i.root == "" {
i.root = "/"
}

// WithLogger sets the logger for the library.
func WithLogger(logger basicLogger) Option {
return func(i *options) {
i.logger = logger
}
}

// WithNvmlLib sets the nvml library for the library.
func WithNvmlLib(nvmllib nvml.Interface) Option {
return func(i *options) {
i.nvmllib = nvmllib
}
return i
}

// WithRoot provides a Option to set the root of the 'info' interface.
func WithRoot(root string) Option {
return func(i *infolib) {
i.root = root
func WithRoot(r string) Option {
return func(i *options) {
i.root = root(r)
}
}

// WithPropertyExtractor provides an Option to set the PropertyExtractor
// interface implementation.
// This is predominantly used for testing.
func WithPropertyExtractor(propertyExtractor PropertyExtractor) Option {
return func(i *options) {
i.propertyExtractor = propertyExtractor
}
}

// WithPlatform provides an option to set the platform explicitly.
func WithPlatform(platform Platform) Option {
return func(i *options) {
i.platform = platform
}
}
Loading