From 2c878e653e934d11164288badbe080a00d86cef0 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Fri, 28 Feb 2025 14:13:30 +0200 Subject: [PATCH] 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 --- .../update-ldcache/update-ldcache.go | 11 ++++++----- .../{update-ldcache => utils}/container-root.go | 16 ++++++++-------- 2 files changed, 14 insertions(+), 13 deletions(-) rename cmd/nvidia-cdi-hook/{update-ldcache => utils}/container-root.go (73%) diff --git a/cmd/nvidia-cdi-hook/update-ldcache/update-ldcache.go b/cmd/nvidia-cdi-hook/update-ldcache/update-ldcache.go index 18cf4e1c..70b89d59 100644 --- a/cmd/nvidia-cdi-hook/update-ldcache/update-ldcache.go +++ b/cmd/nvidia-cdi-hook/update-ldcache/update-ldcache.go @@ -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 } diff --git a/cmd/nvidia-cdi-hook/update-ldcache/container-root.go b/cmd/nvidia-cdi-hook/utils/container-root.go similarity index 73% rename from cmd/nvidia-cdi-hook/update-ldcache/container-root.go rename to cmd/nvidia-cdi-hook/utils/container-root.go index 71a49469..ec5000ac 100644 --- a/cmd/nvidia-cdi-hook/update-ldcache/container-root.go +++ b/cmd/nvidia-cdi-hook/utils/container-root.go @@ -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)) }