Skip to content

Commit

Permalink
if no available DIND is available, a new one is created
Browse files Browse the repository at this point in the history
  • Loading branch information
Bianco95 committed Aug 22, 2024
1 parent 5b20a45 commit d5f0f09
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 42 deletions.
41 changes: 24 additions & 17 deletions pkg/docker/Create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -401,30 +425,13 @@ 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 {
HandleErrorAndRemoveData(h, w, "An error occurred during the setting of the pod UID to the DIND container", err, "", "")
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",
Expand Down
50 changes: 25 additions & 25 deletions pkg/docker/Delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "", "")
Expand All @@ -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"))
Expand Down

0 comments on commit d5f0f09

Please sign in to comment.