Skip to content

Commit 6753f5d

Browse files
committed
Merge remote-tracking branch 'origin/main' into 2-light-version-no-gpu
2 parents d02e37f + 0fe3547 commit 6753f5d

File tree

3 files changed

+137
-8
lines changed

3 files changed

+137
-8
lines changed

pkg/docker/Create.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,44 @@ func (h *SidecarHandler) CreateHandler(w http.ResponseWriter, r *http.Request) {
128128
cmd = append(cmd, mounts)
129129
}
130130

131+
memoryLimitsArray := []string{}
132+
cpuLimitsArray := []string{}
133+
134+
if container.Resources.Limits.Memory().Value() != 0 {
135+
memoryLimitsArray = append(memoryLimitsArray, "--memory", strconv.Itoa(int(container.Resources.Limits.Memory().Value()))+"b")
136+
}
137+
if container.Resources.Limits.Cpu().Value() != 0 {
138+
cpuLimitsArray = append(cpuLimitsArray, "--cpus", strconv.FormatFloat(float64(container.Resources.Limits.Cpu().Value()), 'f', -1, 64))
139+
}
140+
141+
cmd = append(cmd, memoryLimitsArray...)
142+
cmd = append(cmd, cpuLimitsArray...)
143+
144+
containerCommands := []string{}
145+
containerArgs := []string{}
146+
mountFileCommand := []string{}
147+
148+
// if container has a command and args, call parseContainerCommandAndReturnArgs
149+
if len(container.Command) > 0 || len(container.Args) > 0 {
150+
log.G(h.Ctx).Info("Container has command and args defined. Parsing...")
151+
log.G(h.Ctx).Info("Container command: " + strings.Join(container.Command, " "))
152+
log.G(h.Ctx).Info("Container args: " + strings.Join(container.Args, " "))
153+
154+
mountFileCommand, containerCommands, containerArgs, err = parseContainerCommandAndReturnArgs(h.Ctx, h.Config, req, container)
155+
if err != nil {
156+
HandleErrorAndRemoveData(h, w, statusCode, "Some errors occurred while creating container. Check Docker Sidecar's logs", err, &data)
157+
return
158+
}
159+
cmd = append(cmd, mountFileCommand...)
160+
}
161+
162+
// log container commands and args
163+
log.G(h.Ctx).Info("Container commands: " + strings.Join(containerCommands, " "))
164+
log.G(h.Ctx).Info("Container args: " + strings.Join(containerArgs, " "))
165+
131166
cmd = append(cmd, container.Image)
132-
cmd = append(cmd, container.Command...)
133-
cmd = append(cmd, container.Args...)
167+
cmd = append(cmd, containerCommands...)
168+
cmd = append(cmd, containerArgs...)
134169

135170
dockerOptions := ""
136171

@@ -141,15 +176,14 @@ func (h *SidecarHandler) CreateHandler(w http.ResponseWriter, r *http.Request) {
141176
}
142177
}
143178

144-
// print the docker command
145-
log.G(h.Ctx).Info("Docker command: " + "docker" + dockerOptions + " " + strings.Join(cmd, " "))
146-
147179
shell := exec.ExecTask{
148180
Command: "docker" + dockerOptions,
149181
Args: cmd,
150182
Shell: true,
151183
}
152184

185+
log.G(h.Ctx).Info("Docker command: " + strings.Join(shell.Args, " "))
186+
153187
execReturn, err = shell.Execute()
154188
if err != nil {
155189
HandleErrorAndRemoveData(h, w, statusCode, "Some errors occurred while creating container. Check Docker Sidecar's logs", err, &data)

pkg/docker/Status.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"io"
66
"net/http"
7+
"strconv"
78
"strings"
89

910
exec "github.com/alexellis/go-execute/pkg/v1"
@@ -76,7 +77,16 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) {
7677
resp[i].Containers = append(resp[i].Containers, v1.ContainerStatus{Name: container.Name, State: v1.ContainerState{Running: &v1.ContainerStateRunning{}}, Ready: true})
7778
} else if containerstatus[0] == "Exited" {
7879
log.G(h.Ctx).Info("-- Container " + containerName + " has been stopped")
79-
resp[i].Containers = append(resp[i].Containers, v1.ContainerStatus{Name: container.Name, State: v1.ContainerState{Terminated: &v1.ContainerStateTerminated{}}, Ready: false})
80+
containerExitCode := strings.Split(containerstatus[1], "(")
81+
exitCode, err := strconv.Atoi(strings.Trim(containerExitCode[1], ")"))
82+
if err != nil {
83+
log.G(h.Ctx).Error(err)
84+
exitCode = 0
85+
}
86+
log.G(h.Ctx).Info("-- Container exit code is: " + strconv.Itoa(exitCode))
87+
resp[i].Containers = append(resp[i].Containers, v1.ContainerStatus{Name: container.Name, State: v1.ContainerState{Terminated: &v1.ContainerStateTerminated{ExitCode: int32(exitCode)}}, Ready: false})
88+
// release all the GPUs from the container
89+
h.GpuManager.Release(containerName)
8090
}
8191
} else {
8292
log.G(h.Ctx).Info("-- Container " + containerName + " doesn't exist")

pkg/docker/aux.go

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)