From 1c1abefaebcc866c3cb02ea470fc1597a035ab9c Mon Sep 17 00:00:00 2001
From: kaiyou <dev@kaiyou.fr>
Date: Fri, 27 Oct 2023 22:51:42 +0200
Subject: [PATCH] Extend mount() behavior to support subpath

Subpath mounts in kubelet require more mount options to be
taken into account, which is still manageable without importing
a full mount command
---
 cmd/hepto/hooks.go | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/cmd/hepto/hooks.go b/cmd/hepto/hooks.go
index 0d2f8f4..36b2dee 100644
--- a/cmd/hepto/hooks.go
+++ b/cmd/hepto/hooks.go
@@ -2,6 +2,8 @@ package hepto
 
 import (
 	"context"
+	"flag"
+	"fmt"
 	"os"
 	"strings"
 
@@ -21,11 +23,24 @@ import (
 )
 
 func Mount() error {
-	// Hook the mount command for mounting configmaps
+	// Hook the mount command for mounting configmaps and subpaths
 	// This is fairly naive mount implementation, kubelet only evers calls
-	// mount with very simple very formatted arguments in that order:
+	// mount with very simple very formatted arguments in the following forms:
 	//   mount -t tmpfs -o size=1234 /src /dst
-	return unix.Mount(os.Args[5], os.Args[6], os.Args[2], 0, os.Args[4])
+	//   mount --no-canonicalize -o bind /src /dst
+	fs := flag.NewFlagSet("mount", flag.ExitOnError)
+	fstype := fs.String("t", "bind", "type")
+	data := fs.String("o", "", "options")
+	fs.Bool("no-canonicalize", false, "")
+	fs.Parse(os.Args[1:])
+	var flags uintptr
+	if fs.NArg() < 2 {
+		return fmt.Errorf("must specify src and dst")
+	}
+	if *fstype == "bind" || *data == "bind" {
+		flags |= unix.MS_BIND
+	}
+	return unix.Mount(fs.Arg(0), fs.Arg(1), *fstype, flags, *data)
 }
 
 func Umount() error {
-- 
GitLab