Skip to content

Commit

Permalink
added attach of dedicated network for a dind container
Browse files Browse the repository at this point in the history
  • Loading branch information
Bianco95 committed Jun 18, 2024
1 parent 33d44b1 commit 9e181a7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/docker/Create.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,16 +334,30 @@ func (h *SidecarHandler) CreateHandler(w http.ResponseWriter, r *http.Request) {
dindImage = "docker:dind"
}

// create a dedicated docker network for the dind container
shell := exec.ExecTask{
Command: "docker",
Args: []string{"network", "create", "--driver", "bridge", string(data.Pod.UID) + "_dind_network"},
Shell: true,
}
execReturn, err = shell.Execute()
if err != nil {
HandleErrorAndRemoveData(h, w, "An error occurred during the creation of the network for the DIND container", err, "", "")
return
}

dindContainerArgs := []string{"run"}
dindContainerArgs = append(dindContainerArgs, gpuArgsAsArray...)
if _, err := os.Stat("/cvmfs"); err == nil {
dindContainerArgs = append(dindContainerArgs, "-v", "/cvmfs:/cvmfs")
}

// add the network to the dind container
dindContainerArgs = append(dindContainerArgs, "--network", string(data.Pod.UID)+"_dind_network")
dindContainerArgs = append(dindContainerArgs, "--privileged", "-v", wd+":/"+wd, "-v", "/home:/home", "-v", "/var/lib/docker/overlay2:/var/lib/docker/overlay2", "-v", "/var/lib/docker/image:/var/lib/docker/image", "-d", "--name", string(data.Pod.UID)+"_dind", dindImage)

var dindContainerID string
shell := exec.ExecTask{
shell = exec.ExecTask{
Command: "docker",
Args: dindContainerArgs,
Shell: true,
Expand Down
15 changes: 15 additions & 0 deletions pkg/docker/Delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ func (h *SidecarHandler) DeleteHandler(w http.ResponseWriter, r *http.Request) {
// check if the container has GPU devices attacched using the GpuManager and release them
h.GpuManager.Release(containerName)

// delete also the network of the docker dind container that is called string(data.Pod.UID) + "_dind_network"
cmd = []string{"network", "rm", podUID + "_dind_network"}
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 " + podUID + "_dind_network")
} else {
log.G(h.Ctx).Info("\u2705 [DELETE CALL] Deleted network " + podUID + "_dind_network")
}

wd, err := os.Getwd()
if err != nil {
HandleErrorAndRemoveData(h, w, "Unable to get current working directory", err, "", "")
Expand Down

0 comments on commit 9e181a7

Please sign in to comment.