2021-09-06 11:26:48 +00:00
|
|
|
/*
|
|
|
|
# Copyright (c) 2021, 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-22 14:09:45 +00:00
|
|
|
package modifier
|
2021-09-06 11:26:48 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-24 08:21:50 +00:00
|
|
|
"path/filepath"
|
2021-09-06 11:26:48 +00:00
|
|
|
|
2023-12-01 01:10:10 +00:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
|
2023-03-22 12:27:43 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2021-09-06 11:30:15 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
|
2021-09-06 11:26:48 +00:00
|
|
|
)
|
|
|
|
|
2022-02-22 14:09:45 +00:00
|
|
|
// NewStableRuntimeModifier creates an OCI spec modifier that inserts the NVIDIA Container Runtime Hook into an OCI
|
|
|
|
// spec. The specified logger is used to capture log output.
|
2023-03-22 12:27:43 +00:00
|
|
|
func NewStableRuntimeModifier(logger logger.Interface, nvidiaContainerRuntimeHookPath string) oci.SpecModifier {
|
2023-05-24 08:21:50 +00:00
|
|
|
m := stableRuntimeModifier{
|
|
|
|
logger: logger,
|
|
|
|
nvidiaContainerRuntimeHookPath: nvidiaContainerRuntimeHookPath,
|
|
|
|
}
|
2021-09-06 11:26:48 +00:00
|
|
|
|
2022-02-22 14:09:45 +00:00
|
|
|
return &m
|
2021-09-06 11:26:48 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 14:09:45 +00:00
|
|
|
// stableRuntimeModifier modifies an OCI spec inplace, inserting the nvidia-container-runtime-hook as a
|
2022-01-31 14:11:36 +00:00
|
|
|
// prestart hook. If the hook is already present, no modification is made.
|
2022-02-22 14:09:45 +00:00
|
|
|
type stableRuntimeModifier struct {
|
2023-03-22 12:27:43 +00:00
|
|
|
logger logger.Interface
|
2023-05-24 08:21:50 +00:00
|
|
|
nvidiaContainerRuntimeHookPath string
|
2021-09-06 11:26:48 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:11:36 +00:00
|
|
|
// Modify applies the required modification to the incoming OCI spec, inserting the nvidia-container-runtime-hook
|
|
|
|
// as a prestart hook.
|
2022-02-22 14:09:45 +00:00
|
|
|
func (m stableRuntimeModifier) Modify(spec *specs.Spec) error {
|
2022-09-19 12:54:44 +00:00
|
|
|
// If an NVIDIA Container Runtime Hook already exists, we don't make any modifications to the spec.
|
|
|
|
if spec.Hooks != nil {
|
2021-09-06 11:26:48 +00:00
|
|
|
for _, hook := range spec.Hooks.Prestart {
|
2023-08-25 15:50:43 +00:00
|
|
|
hook := hook
|
2022-07-08 10:20:17 +00:00
|
|
|
if isNVIDIAContainerRuntimeHook(&hook) {
|
|
|
|
m.logger.Infof("Existing nvidia prestart hook (%v) found in OCI spec", hook.Path)
|
2022-02-22 14:09:45 +00:00
|
|
|
return nil
|
2021-09-06 11:26:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 08:21:50 +00:00
|
|
|
path := m.nvidiaContainerRuntimeHookPath
|
2022-09-19 12:54:44 +00:00
|
|
|
m.logger.Infof("Using prestart hook path: %v", path)
|
2023-05-24 08:21:50 +00:00
|
|
|
args := []string{filepath.Base(path)}
|
2022-09-19 12:54:44 +00:00
|
|
|
if spec.Hooks == nil {
|
|
|
|
spec.Hooks = &specs.Hooks{}
|
|
|
|
}
|
2021-09-06 11:26:48 +00:00
|
|
|
spec.Hooks.Prestart = append(spec.Hooks.Prestart, specs.Hook{
|
|
|
|
Path: path,
|
|
|
|
Args: append(args, "prestart"),
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|