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