2023-07-07 10:34:43 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# 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 image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-10-10 11:48:38 +00:00
|
|
|
|
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2023-07-07 10:34:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type builder struct {
|
2023-10-10 11:48:38 +00:00
|
|
|
env map[string]string
|
|
|
|
mounts []specs.Mount
|
2023-07-07 10:34:43 +00:00
|
|
|
disableRequire bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new CUDA image from the input options.
|
|
|
|
func New(opt ...Option) (CUDA, error) {
|
|
|
|
b := &builder{}
|
|
|
|
for _, o := range opt {
|
2023-10-10 11:48:38 +00:00
|
|
|
if err := o(b); err != nil {
|
|
|
|
return CUDA{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if b.env == nil {
|
|
|
|
b.env = make(map[string]string)
|
2023-07-07 10:34:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
// build creates a CUDA image from the builder.
|
|
|
|
func (b builder) build() (CUDA, error) {
|
|
|
|
if b.disableRequire {
|
2024-10-14 13:06:06 +00:00
|
|
|
b.env[EnvVarNvidiaDisableRequire] = "true"
|
2023-07-07 10:34:43 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 11:48:38 +00:00
|
|
|
c := CUDA{
|
|
|
|
env: b.env,
|
|
|
|
mounts: b.mounts,
|
|
|
|
}
|
2023-07-07 10:34:43 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Option is a functional option for creating a CUDA image.
|
2023-10-10 11:48:38 +00:00
|
|
|
type Option func(*builder) error
|
2023-07-07 10:34:43 +00:00
|
|
|
|
|
|
|
// WithDisableRequire sets the disable require option.
|
|
|
|
func WithDisableRequire(disableRequire bool) Option {
|
2023-10-10 11:48:38 +00:00
|
|
|
return func(b *builder) error {
|
2023-07-07 10:34:43 +00:00
|
|
|
b.disableRequire = disableRequire
|
2023-10-10 11:48:38 +00:00
|
|
|
return nil
|
2023-07-07 10:34:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithEnv sets the environment variables to use when creating the CUDA image.
|
2023-10-10 11:48:38 +00:00
|
|
|
// Note that this also overwrites the values set with WithEnvMap.
|
2023-07-07 10:34:43 +00:00
|
|
|
func WithEnv(env []string) Option {
|
2023-10-10 11:48:38 +00:00
|
|
|
return func(b *builder) error {
|
|
|
|
envmap := make(map[string]string)
|
|
|
|
for _, e := range env {
|
|
|
|
parts := strings.SplitN(e, "=", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return fmt.Errorf("invalid environment variable: %v", e)
|
|
|
|
}
|
|
|
|
envmap[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
return WithEnvMap(envmap)(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithEnvMap sets the environment variable map to use when creating the CUDA image.
|
|
|
|
// Note that this also overwrites the values set with WithEnv.
|
|
|
|
func WithEnvMap(env map[string]string) Option {
|
|
|
|
return func(b *builder) error {
|
2023-07-07 10:34:43 +00:00
|
|
|
b.env = env
|
2023-10-10 11:48:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMounts sets the mounts associated with the CUDA image.
|
|
|
|
func WithMounts(mounts []specs.Mount) Option {
|
|
|
|
return func(b *builder) error {
|
|
|
|
b.mounts = mounts
|
|
|
|
return nil
|
2023-07-07 10:34:43 +00:00
|
|
|
}
|
|
|
|
}
|