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 changes for usage of nvsandboxutils #629

Merged
merged 5 commits into from
Sep 25, 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
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ issues:
linters:
- errcheck
text: config.Delete
# RENDERD refers to the Render Device and not the past tense of render.
- path: .*.go
linters:
- misspell
text: "`RENDERD` is a misspelling of `RENDERED`"
80 changes: 80 additions & 0 deletions internal/discover/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
# 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 discover

import "sync"

type cache struct {
d Discover

sync.Mutex
devices []Device
hooks []Hook
mounts []Mount
}

var _ Discover = (*cache)(nil)

// WithCache decorates the specified disoverer with a cache.
func WithCache(d Discover) Discover {
if d == nil {
return None{}
}
return &cache{d: d}
}

func (c *cache) Devices() ([]Device, error) {
c.Lock()
defer c.Unlock()

if c.devices == nil {
devices, err := c.d.Devices()
if err != nil {
return nil, err
}
c.devices = devices
}
return c.devices, nil
}

func (c *cache) Hooks() ([]Hook, error) {
c.Lock()
defer c.Unlock()

if c.hooks == nil {
hooks, err := c.d.Hooks()
if err != nil {
return nil, err
}
c.hooks = hooks
}
return c.hooks, nil
}

func (c *cache) Mounts() ([]Mount, error) {
c.Lock()
defer c.Unlock()

if c.mounts == nil {
mounts, err := c.d.Mounts()
if err != nil {
return nil, err
}
c.mounts = mounts
}
return c.mounts, nil
}
72 changes: 72 additions & 0 deletions internal/discover/first-valid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
# 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 discover

import "errors"

type firstOf []Discover

// FirstValid returns a discoverer that returns the first non-error result from a list of discoverers.
func FirstValid(discoverers ...Discover) Discover {
var f firstOf
for _, d := range discoverers {
if d == nil {
continue
}
f = append(f, d)
}
return f
}

func (f firstOf) Devices() ([]Device, error) {
var errs error
for _, d := range f {
devices, err := d.Devices()
if err != nil {
errs = errors.Join(errs, err)
continue
}
return devices, nil
}
return nil, errs
}

func (f firstOf) Hooks() ([]Hook, error) {
var errs error
for _, d := range f {
hooks, err := d.Hooks()
if err != nil {
errs = errors.Join(errs, err)
continue
}
return hooks, nil
}
return nil, errs
}

func (f firstOf) Mounts() ([]Mount, error) {
var errs error
for _, d := range f {
mounts, err := d.Mounts()
if err != nil {
errs = errors.Join(errs, err)
continue
}
return mounts, nil
}
return nil, nil
}
45 changes: 45 additions & 0 deletions internal/nvsandboxutils/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
# 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 nvsandboxutils

// libraryOptions hold the parameters than can be set by a LibraryOption
type libraryOptions struct {
path string
flags int
}

// LibraryOption represents a functional option to configure the underlying nvsandboxutils library
type LibraryOption func(*libraryOptions)

// WithLibraryPath provides an option to set the library name to be used by the nvsandboxutils library.
func WithLibraryPath(path string) LibraryOption {
return func(o *libraryOptions) {
o.path = path
}
}

// SetLibraryOptions applies the specified options to the nvsandboxutils library.
// If this is called when a library is already loaded, an error is raised.
func SetLibraryOptions(opts ...LibraryOption) error {
libnvsandboxutils.Lock()
defer libnvsandboxutils.Unlock()
if libnvsandboxutils.refcount != 0 {
return errLibraryAlreadyLoaded
}
libnvsandboxutils.init(opts...)
return nil
}
25 changes: 25 additions & 0 deletions internal/nvsandboxutils/cgo_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
# 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.
**/

// WARNING: THIS FILE WAS AUTOMATICALLY GENERATED.
// Code generated by https://git.io/c-for-go. DO NOT EDIT.

#include "nvsandboxutils.h"
#include <stdlib.h>
#pragma once

#define __CGOGEN 1

38 changes: 38 additions & 0 deletions internal/nvsandboxutils/cgo_helpers_static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
# 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 nvsandboxutils

var cgoAllocsUnknown = new(struct{})

func clen(n []byte) int {
for i := 0; i < len(n); i++ {
if n[i] == 0 {
return i
}
}
return len(n)
}

// Creates an int8 array of fixed input length to store the Go string.
// TODO: Add error check if input string has a length greater than INPUT_LENGTH
func convertStringToFixedArray(str string) [INPUT_LENGTH]int8 {
var output [INPUT_LENGTH]int8
for i, s := range str {
output[i] = int8(s)
}
return output
}
Loading