From 86a327953ad8761ce9f4dd03cdca25dcdf3b34e9 Mon Sep 17 00:00:00 2001 From: Bianco95 Date: Tue, 25 Jun 2024 10:07:10 +0200 Subject: [PATCH] updated status call --- pkg/common/types.go | 1 + pkg/docker/Create.go | 17 +++++++++-------- pkg/docker/Status.go | 21 ++++++++++++++++++++- pkg/docker/types.go | 12 ++++++++++++ 4 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 pkg/docker/types.go diff --git a/pkg/common/types.go b/pkg/common/types.go index 0b01cc0..0810681 100644 --- a/pkg/common/types.go +++ b/pkg/common/types.go @@ -18,6 +18,7 @@ type PodStatus struct { PodName string `json:"name"` PodUID string `json:"UID"` PodNamespace string `json:"namespace"` + JobID string `json:"JID"` Containers []v1.ContainerStatus `json:"containers"` InitContainers []v1.ContainerStatus `json:"initContainers"` } diff --git a/pkg/docker/Create.go b/pkg/docker/Create.go index 15fc018..107e047 100644 --- a/pkg/docker/Create.go +++ b/pkg/docker/Create.go @@ -21,13 +21,6 @@ import ( "path/filepath" ) -type DockerRunStruct struct { - Name string `json:"name"` - Command string `json:"command"` - IsInitContainer bool `json:"isInitContainer"` - GpuArgs string `json:"gpuArgs"` -} - func (h *SidecarHandler) prepareDockerRuns(podData commonIL.RetrievedPodData, w http.ResponseWriter) ([]DockerRunStruct, error) { var dockerRunStructs []DockerRunStruct @@ -490,12 +483,20 @@ func (h *SidecarHandler) CreateHandler(w http.ResponseWriter, r *http.Request) { log.G(h.Ctx).Info("\u2705 [POD FLOW] Containers created successfully") + createResponse := CreateStruct{PodUID: string(data.Pod.UID), PodJID: dindContainerID} + createResponseBytes, err := json.Marshal(createResponse) + if err != nil { + statusCode = http.StatusInternalServerError + HandleErrorAndRemoveData(h, w, "An error occurred during the json marshal of the returned JID", err, "", "") + return + } + w.WriteHeader(statusCode) if statusCode != http.StatusOK { w.Write([]byte("Some errors occurred while creating containers. Check Docker Sidecar's logs")) } else { - w.Write([]byte("Containers created")) + w.Write(createResponseBytes) } } diff --git a/pkg/docker/Status.go b/pkg/docker/Status.go index 4e002f9..b16ea9c 100644 --- a/pkg/docker/Status.go +++ b/pkg/docker/Status.go @@ -44,8 +44,27 @@ func (h *SidecarHandler) StatusHandler(w http.ResponseWriter, r *http.Request) { podUID := string(pod.UID) podNamespace := string(pod.Namespace) - resp = append(resp, commonIL.PodStatus{PodName: pod.Name, PodUID: podUID, PodNamespace: podNamespace}) + // send a docker command to retrieve the uuid of the dind container + // the command to exec is: docker inspect --format '{{.Id}}' podUID + "_dind" + cmd := []string{"inspect", "--format", "{{.Id}}", podUID + "_dind"} + shell := exec.ExecTask{ + Command: "docker", + Args: cmd, + Shell: true, + } + execReturn, err := shell.Execute() + if err != nil { + log.G(h.Ctx).Error(err) + statusCode = http.StatusInternalServerError + break + } + + dindUUID := strings.ReplaceAll(execReturn.Stdout, "\n", "") + log.G(h.Ctx).Info("\u2705 [STATUS CALL] UUID of the dind container retrieved successfully: ", dindUUID) + + resp = append(resp, commonIL.PodStatus{PodName: pod.Name, PodUID: podUID, PodNamespace: podNamespace, JobID: dindUUID}) for _, container := range pod.Spec.Containers { + containerName := podNamespace + "-" + podUID + "-" + container.Name cmd := []string{"exec " + podUID + "_dind" + " docker ps -af name=^" + containerName + "$ --format \"{{.Status}}\""} diff --git a/pkg/docker/types.go b/pkg/docker/types.go new file mode 100644 index 0000000..bbe7694 --- /dev/null +++ b/pkg/docker/types.go @@ -0,0 +1,12 @@ +package docker + +type DockerRunStruct struct { + Name string `json:"name"` + Command string `json:"command"` + IsInitContainer bool `json:"isInitContainer"` + GpuArgs string `json:"gpuArgs"` +} +type CreateStruct struct { + PodUID string `json:"PodUID"` + PodJID string `json:"PodJID"` +}