2022-03-30 10:14:52 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 2022, 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 modifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-04-08 05:24:39 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
|
2023-03-22 12:27:43 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2022-03-30 10:14:52 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
|
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
// nvidiaContainerRuntimeHookRemover is a spec modifer that detects and removes inserted nvidia-container-runtime hooks
|
|
|
|
type nvidiaContainerRuntimeHookRemover struct {
|
2023-03-22 12:27:43 +00:00
|
|
|
logger logger.Interface
|
2022-03-30 10:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ oci.SpecModifier = (*nvidiaContainerRuntimeHookRemover)(nil)
|
|
|
|
|
|
|
|
// Modify removes any NVIDIA Container Runtime hooks from the provided spec
|
|
|
|
func (m nvidiaContainerRuntimeHookRemover) Modify(spec *specs.Spec) error {
|
|
|
|
if spec == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if spec.Hooks == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-06 13:13:32 +00:00
|
|
|
if len(spec.Hooks.Prestart) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-06 14:18:56 +00:00
|
|
|
var newPrestart []specs.Hook
|
|
|
|
|
2022-03-30 10:14:52 +00:00
|
|
|
for _, hook := range spec.Hooks.Prestart {
|
|
|
|
if isNVIDIAContainerRuntimeHook(&hook) {
|
2022-04-06 14:18:56 +00:00
|
|
|
m.logger.Debugf("Removing hook %v", hook)
|
|
|
|
continue
|
2022-03-30 10:14:52 +00:00
|
|
|
}
|
2022-04-06 14:18:56 +00:00
|
|
|
newPrestart = append(newPrestart, hook)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(newPrestart) != len(spec.Hooks.Prestart) {
|
|
|
|
m.logger.Debugf("Updating 'prestart' hooks to %v", newPrestart)
|
|
|
|
spec.Hooks.Prestart = newPrestart
|
2022-03-30 10:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isNVIDIAContainerRuntimeHook checks if the provided hook is an nvidia-container-runtime-hook
|
|
|
|
// or nvidia-container-toolkit hook. These are included, for example, by the non-experimental
|
|
|
|
// nvidia-container-runtime or docker when specifying the --gpus flag.
|
|
|
|
func isNVIDIAContainerRuntimeHook(hook *specs.Hook) bool {
|
2022-04-06 15:18:06 +00:00
|
|
|
bins := map[string]struct{}{
|
2022-04-08 05:24:39 +00:00
|
|
|
config.NVIDIAContainerRuntimeHookExecutable: {},
|
|
|
|
config.NVIDIAContainerToolkitExecutable: {},
|
2022-03-30 10:14:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 15:18:06 +00:00
|
|
|
_, exists := bins[filepath.Base(hook.Path)]
|
|
|
|
|
|
|
|
return exists
|
2022-03-30 10:14:52 +00:00
|
|
|
}
|