Move containerRoot to utils package

Move the containerRoot type to a utils package so that this can be
shared between hooks.

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2025-02-28 14:13:30 +02:00
parent e7a0067aae
commit 84545b2d4e
No known key found for this signature in database
2 changed files with 14 additions and 13 deletions

View File

@ -25,6 +25,7 @@ import (
"github.com/urfave/cli/v2"
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-cdi-hook/utils"
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
@ -119,9 +120,9 @@ func (m command) run(c *cli.Context, cfg *options) error {
args = append(args, "-r", containerRootDir)
}
containerRoot := containerRoot(containerRootDir)
containerRoot := utils.ContainerRoot(containerRootDir)
if containerRoot.hasPath("/etc/ld.so.cache") {
if containerRoot.HasPath("/etc/ld.so.cache") {
args = append(args, "-C", "/etc/ld.so.cache")
} else {
m.logger.Debugf("No ld.so.cache found, skipping update")
@ -129,7 +130,7 @@ func (m command) run(c *cli.Context, cfg *options) error {
}
folders := cfg.folders.Value()
if containerRoot.hasPath("/etc/ld.so.conf.d") {
if containerRoot.HasPath("/etc/ld.so.conf.d") {
err := m.createLdsoconfdFile(containerRoot, ldsoconfdFilenamePattern, folders...)
if err != nil {
return fmt.Errorf("failed to update ld.so.conf.d: %v", err)
@ -155,13 +156,13 @@ func (m command) resolveLDConfigPath(path string) string {
// createLdsoconfdFile creates a file at /etc/ld.so.conf.d/ in the specified root.
// The file is created at /etc/ld.so.conf.d/{{ .pattern }} using `CreateTemp` and
// contains the specified directories on each line.
func (m command) createLdsoconfdFile(in containerRoot, pattern string, dirs ...string) error {
func (m command) createLdsoconfdFile(in utils.ContainerRoot, pattern string, dirs ...string) error {
if len(dirs) == 0 {
m.logger.Debugf("No directories to add to /etc/ld.so.conf")
return nil
}
ldsoconfdDir, err := in.resolve("/etc/ld.so.conf.d")
ldsoconfdDir, err := in.Resolve("/etc/ld.so.conf.d")
if err != nil {
return err
}

View File

@ -14,7 +14,7 @@
# limitations under the License.
**/
package ldcache
package utils
import (
"os"
@ -23,12 +23,12 @@ import (
"github.com/moby/sys/symlink"
)
// A containerRoot represents the root filesystem of a container.
type containerRoot string
// A ContainerRoot represents the root filesystem of a container.
type ContainerRoot string
// hasPath checks whether the specified path exists in the root.
func (r containerRoot) hasPath(path string) bool {
resolved, err := r.resolve(path)
// HasPath checks whether the specified path exists in the root.
func (r ContainerRoot) HasPath(path string) bool {
resolved, err := r.Resolve(path)
if err != nil {
return false
}
@ -38,9 +38,9 @@ func (r containerRoot) hasPath(path string) bool {
return true
}
// resolve returns the absolute path including root path.
// Resolve returns the absolute path including root path.
// Symlinks are resolved, but are guaranteed to resolve in the root.
func (r containerRoot) resolve(path string) (string, error) {
func (r ContainerRoot) Resolve(path string) (string, error) {
absolute := filepath.Clean(filepath.Join(string(r), path))
return symlink.FollowSymlinkInScope(absolute, string(r))
}