diff --git a/pkg/docker/Create.go b/pkg/docker/Create.go index d1d7540..7fc6ca8 100644 --- a/pkg/docker/Create.go +++ b/pkg/docker/Create.go @@ -246,6 +246,30 @@ func (h *SidecarHandler) CreateHandler(w http.ResponseWriter, r *http.Request) { log.G(h.Ctx).Info("\u23F3 [CREATE CALL] Received create call from InterLink ") + // get a dind container ID from dind manager of the sidecard handler + dindContainerID, err := h.DindManager.GetAvailableDind() + if err != nil { + + log.G(h.Ctx).Info("\u2705 [POD FLOW] No available DIND container found, creating a new one") + + h.DindManager.BuildDindContainers(1) + dindContainerID, err = h.DindManager.GetAvailableDind() + if err != nil { + HandleErrorAndRemoveData(h, w, "During creation of new DIND container, an error occurred during the request of get available DIND container", err, "", "") + return + } + } + + // remove the dind container from the list of available dind containers + err = h.DindManager.SetDindUnavailable(dindContainerID) + if err != nil { + HandleErrorAndRemoveData(h, w, "An error occurred during the removal of the DIND container from the list of available DIND containers", err, "", "") + return + } + + // create a new dind container in background + go h.DindManager.BuildDindContainers(1) + //var execReturn exec.ExecResult statusCode := http.StatusOK @@ -401,20 +425,6 @@ func (h *SidecarHandler) CreateHandler(w http.ResponseWriter, r *http.Request) { // log.G(h.Ctx).Info("\u2705 [POD FLOW] DIND container is up and running, ready to create the containers inside of it") - // get a dind container ID from dind manager of the sidecard handler - dindContainerID, err := h.DindManager.GetAvailableDind() - if err != nil { - HandleErrorAndRemoveData(h, w, "An error occurred during the request of get available DIND container", err, "", "") - return - } - - // remove the dind container from the list of available dind containers - err = h.DindManager.SetDindUnavailable(dindContainerID) - if err != nil { - HandleErrorAndRemoveData(h, w, "An error occurred during the removal of the DIND container from the list of available DIND containers", err, "", "") - return - } - // set the podUID to the dind container err = h.DindManager.SetPodUIDToDind(dindContainerID, podUID) if err != nil { @@ -422,9 +432,6 @@ func (h *SidecarHandler) CreateHandler(w http.ResponseWriter, r *http.Request) { return } - // create a new dind container in background - go h.DindManager.BuildDindContainers(1) - // run the docker command to rename the container to the pod UID shell := exec.ExecTask{ Command: "docker", diff --git a/pkg/docker/Delete.go b/pkg/docker/Delete.go index eb59fc8..e17edbe 100644 --- a/pkg/docker/Delete.go +++ b/pkg/docker/Delete.go @@ -66,30 +66,36 @@ func (h *SidecarHandler) DeleteHandler(w http.ResponseWriter, r *http.Request) { log.G(h.Ctx).Info("\u2705 [DELETE CALL] Deleted container " + podUID + "_dind") } - // delete also the network of the docker dind container that is called string(data.Pod.UID) + "_dind_network" - - // retrieve the network ID from the dind manager - dindSpec := dindmanager.DindSpecs{} dindSpec, err = h.DindManager.GetDindFromPodUID(podUID) - // log the retrieved dindSpec - log.G(h.Ctx).Info("\u2705 [DELETE CALL] Retrieved DindSpecs: " + dindSpec.DindID + " " + dindSpec.PodUID + " " + dindSpec.DindNetworkID + " ") - - cmd = []string{"network", "rm", dindSpec.DindNetworkID} - shell = exec.ExecTask{ - Command: "docker", - Args: cmd, - Shell: true, - } - execReturn, _ = shell.Execute() - execReturn.Stdout = strings.ReplaceAll(execReturn.Stdout, "\n", "") - if execReturn.Stderr != "" { - log.G(h.Ctx).Error("\u274C [DELETE CALL] Error deleting network " + dindSpec.DindNetworkID) + if err != nil { + log.G(h.Ctx).Error("\u274C [DELETE CALL] Error retrieving DindSpecs, maybe the Dind container has already been deleted") } else { - log.G(h.Ctx).Info("\u2705 [DELETE CALL] Deleted network " + dindSpec.DindNetworkID) + log.G(h.Ctx).Info("\u2705 [DELETE CALL] Retrieved DindSpecs: " + dindSpec.DindID + " " + dindSpec.PodUID + " " + dindSpec.DindNetworkID + " ") + + // log the retrieved dindSpec + log.G(h.Ctx).Info("\u2705 [DELETE CALL] Retrieved DindSpecs: " + dindSpec.DindID + " " + dindSpec.PodUID + " " + dindSpec.DindNetworkID + " ") + + cmd = []string{"network", "rm", dindSpec.DindNetworkID} + shell = exec.ExecTask{ + Command: "docker", + Args: cmd, + Shell: true, + } + execReturn, _ = shell.Execute() + execReturn.Stdout = strings.ReplaceAll(execReturn.Stdout, "\n", "") + if execReturn.Stderr != "" { + log.G(h.Ctx).Error("\u274C [DELETE CALL] Error deleting network " + dindSpec.DindNetworkID) + } else { + log.G(h.Ctx).Info("\u2705 [DELETE CALL] Deleted network " + dindSpec.DindNetworkID) + } + // set the dind available again + err = h.DindManager.RemoveDindFromList(dindSpec.PodUID) + if err != nil { + log.G(h.Ctx).Error("\u274C [DELETE CALL] Error setting DIND container available") + } } - wd, err := os.Getwd() if err != nil { HandleErrorAndRemoveData(h, w, "Unable to get current working directory", err, "", "") @@ -100,12 +106,6 @@ func (h *SidecarHandler) DeleteHandler(w http.ResponseWriter, r *http.Request) { err = os.RemoveAll(podDirectoryPathToDelete) - // set the dind available again - err = h.DindManager.RemoveDindFromList(dindSpec.PodUID) - if err != nil { - log.G(h.Ctx).Error("\u274C [DELETE CALL] Error setting DIND container available") - } - w.WriteHeader(statusCode) if statusCode != http.StatusOK { w.Write([]byte("Some errors occurred deleting containers. Check Docker Sidecar's logs"))