@@ -23,6 +23,65 @@ type SidecarHandler struct {
2323 GpuManager gpustrategies.GPUManagerInterface
2424}
2525
26+ func parseContainerCommandAndReturnArgs (Ctx context.Context , config commonIL.InterLinkConfig , data []commonIL.RetrievedPodData , container v1.Container ) ([]string , []string , []string , error ) {
27+
28+ podUID := ""
29+ podNamespace := ""
30+
31+ for _ , podData := range data {
32+ podUID = string (podData .Pod .UID )
33+ podNamespace = string (podData .Pod .Namespace )
34+ }
35+
36+ if container .Command == nil {
37+ return []string {}, container .Command , container .Args , nil
38+ }
39+
40+ wd , err := os .Getwd ()
41+ if err != nil {
42+ log .G (Ctx ).Error (err )
43+ return nil , nil , nil , err
44+ }
45+
46+ if len (container .Command ) > 0 {
47+ if container .Command [0 ] == "/bin/bash" || container .Command [0 ] == "/bin/sh" {
48+ fileName := "script.sh"
49+ if len (container .Command ) > 1 {
50+ if strings .HasSuffix (container .Command [1 ], ".sh" ) {
51+ return []string {}, container .Command , container .Args , nil
52+ }
53+ if container .Command [1 ] == "-c" {
54+ // get the actual current path of the folder
55+ fileNamePath := filepath .Join (wd , config .DataRootFolder + podNamespace + "-" + podUID , fileName )
56+ log .G (Ctx ).Info ("Creating file " + fileNamePath )
57+ err = os .WriteFile (fileNamePath , []byte (container .Args [0 ]), 0644 )
58+ if err != nil {
59+ log .G (Ctx ).Error (err )
60+ return nil , nil , nil , err
61+ }
62+ return []string {"-v " + fileNamePath + ":/" + fileName }, []string {container .Command [0 ] + " /" + fileName }, []string {}, nil
63+ }
64+ }
65+ } else if strings .HasPrefix (container .Command [0 ], "python" ) {
66+ fileName := "script.py"
67+ if container .Command [1 ] == "-c" {
68+ fileNamePath := filepath .Join (wd , config .DataRootFolder + podNamespace + "-" + podUID , fileName )
69+ log .G (Ctx ).Info ("Creating file " + fileNamePath )
70+ err = os .WriteFile (fileNamePath , []byte (container .Args [0 ]), 0644 )
71+ if err != nil {
72+ log .G (Ctx ).Error (err )
73+ return nil , nil , nil , err
74+ }
75+ return []string {"-v " + fileNamePath + ":/" + fileName }, []string {container .Command [0 ] + " /" + fileName }, []string {}, nil
76+ }
77+ } else {
78+ return []string {}, container .Command , container .Args , nil
79+ }
80+
81+ }
82+ return []string {}, container .Command , container .Args , nil
83+ }
84+
2685// prepareMounts iterates along the struct provided in the data parameter and checks for ConfigMaps, Secrets and EmptyDirs to be mounted.
2786// For each element found, the mountData function is called.
2887// It returns a string composed as the docker -v command to bind mount directories and files and the first encountered error.
@@ -79,6 +138,7 @@ func prepareMounts(Ctx context.Context, config commonIL.InterLinkConfig, data []
79138 }
80139
81140 for _ , emptyDir := range cont .EmptyDirs {
141+ log .G (Ctx ).Info ("-- EmptyDir to handle " + emptyDir )
82142 if containerName == podNamespace + "-" + podUID + "-" + cont .Name {
83143 paths , err := mountData (Ctx , config , podData .Pod , emptyDir , container )
84144 if err != nil {
@@ -257,6 +317,25 @@ func mountData(Ctx context.Context, config commonIL.InterLinkConfig, pod v1.Pod,
257317 if podVolumeSpec != nil && podVolumeSpec .EmptyDir != nil {
258318 var edPath string
259319
320+ parts := strings .Split (data .(string ), "/" )
321+ emptyDirName := parts [len (parts )- 1 ]
322+ if emptyDirName != vol .Name {
323+ log .G (Ctx ).Info ("Skipping " + vol .Name + " as it is not the same as " + emptyDirName )
324+ continue
325+ }
326+
327+ emptyDirMountPath := ""
328+ isReadOnly := false
329+ for _ , mountSpec := range container .VolumeMounts {
330+ if mountSpec .Name == vol .Name {
331+ emptyDirMountPath = mountSpec .MountPath
332+ if mountSpec .ReadOnly {
333+ isReadOnly = true
334+ }
335+ break
336+ }
337+ }
338+
260339 edPath = filepath .Join (wd + "/" + config .DataRootFolder , string (pod .UID )+ "/" + "emptyDirs/" + vol .Name )
261340 log .G (Ctx ).Info ("-- Creating EmptyDir in " + edPath )
262341 cmd := []string {"-p " + edPath }
@@ -274,8 +353,14 @@ func mountData(Ctx context.Context, config commonIL.InterLinkConfig, pod v1.Pod,
274353 log .G (Ctx ).Debug ("-- Created EmptyDir in " + edPath )
275354 }
276355
277- edPath += (":" + mountSpec .MountPath + "/" )
278- return []string {edPath }, nil
356+ // if the emptyDir is read only, append :ro to the path
357+ if isReadOnly {
358+ edPath += (":" + emptyDirMountPath + "/:ro" )
359+ return []string {edPath }, nil
360+ } else {
361+ edPath += (":" + emptyDirMountPath + "/" )
362+ return []string {edPath }, nil
363+ }
279364 }
280365 }
281366 }
0 commit comments