diff --git a/pkg/docker/Create.go b/pkg/docker/Create.go index c4a3905..677b4ec 100644 --- a/pkg/docker/Create.go +++ b/pkg/docker/Create.go @@ -64,9 +64,6 @@ func (h *SidecarHandler) CreateHandler(w http.ResponseWriter, r *http.Request) { for _, container := range data.Pod.Spec.Containers { - // the name of the POD is the UID of the POD and the name of the container - containerName := podUID + "-" + container.Name - var isGpuRequested bool = false var additionalGpuArgs []string @@ -78,7 +75,7 @@ func (h *SidecarHandler) CreateHandler(w http.ResponseWriter, r *http.Request) { isGpuRequested = true - log.G(h.Ctx).Info("Container " + containerName + " is requesting " + val.String() + " GPU") + log.G(h.Ctx).Info("Container " + container.Name + " is requesting " + val.String() + " GPU") numGpusRequestedInt := int(numGpusRequested) _, err := h.GpuManager.GetAvailableGPUs(numGpusRequestedInt) @@ -88,7 +85,7 @@ func (h *SidecarHandler) CreateHandler(w http.ResponseWriter, r *http.Request) { return } - gpuSpecs, err := h.GpuManager.GetAndAssignAvailableGPUs(numGpusRequestedInt, containerName) + gpuSpecs, err := h.GpuManager.GetAndAssignAvailableGPUs(numGpusRequestedInt, container.Name) if err != nil { HandleErrorAndRemoveData(h, w, statusCode, "Some errors occurred while creating container. Check Docker Sidecar's logs", err, &data) return @@ -106,10 +103,10 @@ func (h *SidecarHandler) CreateHandler(w http.ResponseWriter, r *http.Request) { additionalGpuArgs = append(additionalGpuArgs, "--runtime=nvidia -e NVIDIA_VISIBLE_DEVICES="+gpuUUIDs) } else { - log.G(h.Ctx).Info("Container " + containerName + " is not requesting a GPU") + log.G(h.Ctx).Info("Container " + container.Name + " is not requesting a GPU") } - log.G(h.Ctx).Info("- Creating container " + containerName) + log.G(h.Ctx).Info("- Creating container " + container.Name) var envVars string = "" // add environment variables to the docker command @@ -146,10 +143,10 @@ func (h *SidecarHandler) CreateHandler(w http.ResponseWriter, r *http.Request) { log.G(h.Ctx).Info("- The name of the container is " + container.Name) log.G(h.Ctx).Info("- The pod UID is: " + podUID) - log.G(h.Ctx).Info("- The final name of will be: " + containerName) + log.G(h.Ctx).Info("- The name of the POD will be: " + podUID) - cmd := []string{"run", "-d", "--name", containerName} + cmd := []string{"run", "-d", "--name", podUID} cmd = append(cmd, envVars) @@ -204,34 +201,34 @@ func (h *SidecarHandler) CreateHandler(w http.ResponseWriter, r *http.Request) { } if execReturn.Stdout == "" { - eval := "Conflict. The container name \"/" + containerName + "\" is already in use" + eval := "Conflict. The container name \"/" + container.Name + "\" is already in use" if strings.Contains(execReturn.Stderr, eval) { - log.G(h.Ctx).Warning("Container named " + containerName + " already exists. Skipping its creation.") + log.G(h.Ctx).Warning("Container named " + container.Name + " already exists. Skipping its creation.") } else { - log.G(h.Ctx).Error("Unable to create container " + containerName + " : " + execReturn.Stderr) + log.G(h.Ctx).Error("Unable to create container " + container.Name + " : " + execReturn.Stderr) HandleErrorAndRemoveData(h, w, statusCode, "Some errors occurred while creating container. Check Docker Sidecar's logs", err, &data) return } } else { - log.G(h.Ctx).Info("-- Created container " + containerName) + log.G(h.Ctx).Info("-- Created container " + container.Name) } shell = exec.ExecTask{ Command: "docker", - Args: []string{"ps", "-aqf", "name=^" + containerName + "$"}, + Args: []string{"ps", "-aqf", "name=^" + container.Name + "$"}, Shell: true, } execReturn, err = shell.Execute() execReturn.Stdout = strings.ReplaceAll(execReturn.Stdout, "\n", "") if execReturn.Stderr != "" { - log.G(h.Ctx).Error("Failed to retrieve " + containerName + " ID : " + execReturn.Stderr) + log.G(h.Ctx).Error("Failed to retrieve " + container.Name + " ID : " + execReturn.Stderr) HandleErrorAndRemoveData(h, w, statusCode, "Some errors occurred while creating container. Check Docker Sidecar's logs", err, &data) return } else if execReturn.Stdout == "" { log.G(h.Ctx).Error("Container name not found. Maybe creation failed?") } else { - log.G(h.Ctx).Debug("-- Retrieved " + containerName + " ID: " + execReturn.Stdout) + log.G(h.Ctx).Debug("-- Retrieved " + container.Name + " ID: " + execReturn.Stdout) } } } diff --git a/pkg/docker/Delete.go b/pkg/docker/Delete.go index d2e8321..232e018 100644 --- a/pkg/docker/Delete.go +++ b/pkg/docker/Delete.go @@ -37,16 +37,11 @@ func (h *SidecarHandler) DeleteHandler(w http.ResponseWriter, r *http.Request) { return } - podUID := string(pod.UID) - for _, container := range pod.Spec.Containers { - - containerName := podUID + "-" + container.Name - - log.G(h.Ctx).Debug("- Deleting container " + containerName) + log.G(h.Ctx).Debug("- Deleting container " + container.Name) // added a timeout to the stop container command - cmd := []string{"stop", "-t", "10", containerName} + cmd := []string{"stop", "-t", "10", container.Name} shell := exec.ExecTask{ Command: "docker", Args: cmd, @@ -56,9 +51,9 @@ func (h *SidecarHandler) DeleteHandler(w http.ResponseWriter, r *http.Request) { if execReturn.Stderr != "" { if strings.Contains(execReturn.Stderr, "No such container") { - log.G(h.Ctx).Debug("-- Unable to find container " + containerName + ". Probably already removed? Skipping its removal") + log.G(h.Ctx).Debug("-- Unable to find container " + container.Name + ". Probably already removed? Skipping its removal") } else { - log.G(h.Ctx).Error("-- Error stopping container " + containerName + ". Skipping its removal") + log.G(h.Ctx).Error("-- Error stopping container " + container.Name + ". Skipping its removal") statusCode = http.StatusInternalServerError w.WriteHeader(statusCode) w.Write([]byte("Some errors occurred while deleting container. Check Docker Sidecar's logs")) @@ -78,18 +73,18 @@ func (h *SidecarHandler) DeleteHandler(w http.ResponseWriter, r *http.Request) { execReturn.Stdout = strings.ReplaceAll(execReturn.Stdout, "\n", "") if execReturn.Stderr != "" { - log.G(h.Ctx).Error("-- Error deleting container " + containerName) + log.G(h.Ctx).Error("-- Error deleting container " + container.Name) statusCode = http.StatusInternalServerError w.WriteHeader(statusCode) w.Write([]byte("Some errors occurred while deleting container. Check Docker Sidecar's logs")) return } else { - log.G(h.Ctx).Info("- Deleted container " + containerName) + log.G(h.Ctx).Info("- Deleted container " + container.Name) } } // check if the container has GPU devices attacched using the GpuManager and release them - h.GpuManager.Release(containerName) + h.GpuManager.Release(container.Name) os.RemoveAll(h.Config.DataRootFolder + pod.Namespace + "-" + string(pod.UID)) } diff --git a/pkg/docker/GetLogs.go b/pkg/docker/GetLogs.go index 13260ec..85a6c44 100644 --- a/pkg/docker/GetLogs.go +++ b/pkg/docker/GetLogs.go @@ -44,16 +44,13 @@ func (h *SidecarHandler) GetLogsHandler(w http.ResponseWriter, r *http.Request) return } - podUID := string(req.PodUID) - containerName := podUID + "-" + req.ContainerName - //req = test var cmd *OSexec.Cmd if req.Opts.Timestamps { - cmd = OSexec.Command("docker", "logs", "-t", containerName) + cmd = OSexec.Command("docker", "logs", "-t", req.ContainerName) } else { - cmd = OSexec.Command("docker", "logs", containerName) + cmd = OSexec.Command("docker", "logs", req.ContainerName) } output, err := cmd.CombinedOutput() diff --git a/pkg/docker/Status.go b/pkg/docker/Status.go index ec7f2b1..c6f203d 100644 --- a/pkg/docker/Status.go +++ b/pkg/docker/Status.go @@ -39,15 +39,10 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) { } for i, pod := range req { - - podUID := string(pod.UID) - resp = append(resp, commonIL.PodStatus{PodName: pod.Name, PodUID: string(pod.UID), PodNamespace: pod.Namespace}) for _, container := range pod.Spec.Containers { - containerName := podUID + "-" + container.Name - - log.G(h.Ctx).Debug("- Getting status for container " + containerName) - cmd := []string{"ps -af name=^" + containerName + "$ --format \"{{.Status}}\""} + log.G(h.Ctx).Debug("- Getting status for container " + container.Name) + cmd := []string{"ps -af name=^" + container.Name + "$ --format \"{{.Status}}\""} shell := exec.ExecTask{ Command: "docker", @@ -68,20 +63,20 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) { // TODO: why first container? if execReturn.Stdout != "" { if containerstatus[0] == "Created" { - log.G(h.Ctx).Info("-- Container " + containerName + " is going ready...") - resp[i].Containers = append(resp[i].Containers, v1.ContainerStatus{Name: containerName, State: v1.ContainerState{Waiting: &v1.ContainerStateWaiting{}}, Ready: false}) + log.G(h.Ctx).Info("-- Container " + container.Name + " is going ready...") + resp[i].Containers = append(resp[i].Containers, v1.ContainerStatus{Name: container.Name, State: v1.ContainerState{Waiting: &v1.ContainerStateWaiting{}}, Ready: false}) } else if containerstatus[0] == "Up" { - log.G(h.Ctx).Info("-- Container " + containerName + " is running") - resp[i].Containers = append(resp[i].Containers, v1.ContainerStatus{Name: containerName, State: v1.ContainerState{Running: &v1.ContainerStateRunning{}}, Ready: true}) + log.G(h.Ctx).Info("-- Container " + container.Name + " is running") + resp[i].Containers = append(resp[i].Containers, v1.ContainerStatus{Name: container.Name, State: v1.ContainerState{Running: &v1.ContainerStateRunning{}}, Ready: true}) } else if containerstatus[0] == "Exited" { - log.G(h.Ctx).Info("-- Container " + containerName + " has been stopped") - resp[i].Containers = append(resp[i].Containers, v1.ContainerStatus{Name: containerName, State: v1.ContainerState{Terminated: &v1.ContainerStateTerminated{}}, Ready: false}) + log.G(h.Ctx).Info("-- Container " + container.Name + " has been stopped") + resp[i].Containers = append(resp[i].Containers, v1.ContainerStatus{Name: container.Name, State: v1.ContainerState{Terminated: &v1.ContainerStateTerminated{}}, Ready: false}) // release all the GPUs from the container - h.GpuManager.Release(containerName) + h.GpuManager.Release(container.Name) } } else { - log.G(h.Ctx).Info("-- Container " + containerName + " doesn't exist") - resp[i].Containers = append(resp[i].Containers, v1.ContainerStatus{Name: containerName, State: v1.ContainerState{Terminated: &v1.ContainerStateTerminated{}}, Ready: false}) + log.G(h.Ctx).Info("-- Container " + container.Name + " doesn't exist") + resp[i].Containers = append(resp[i].Containers, v1.ContainerStatus{Name: container.Name, State: v1.ContainerState{Terminated: &v1.ContainerStateTerminated{}}, Ready: false}) } } }