From c1e29e8ce22e6136ae27697cbd6a2b9d5af53f1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20C?= <82955277+gc-advens@users.noreply.github.com> Date: Mon, 30 Aug 2021 13:13:53 +0200 Subject: [PATCH 1/4] Fix #496: compatibility with cgroup v2 --- functions/onprem/orborus/orborus.go | 67 +++-------------------------- functions/onprem/orborus/run.sh | 1 - functions/onprem/worker/worker.go | 43 +----------------- 3 files changed, 8 insertions(+), 103 deletions(-) diff --git a/functions/onprem/orborus/orborus.go b/functions/onprem/orborus/orborus.go index 6ca42b24..73faafeb 100644 --- a/functions/onprem/orborus/orborus.go +++ b/functions/onprem/orborus/orborus.go @@ -16,7 +16,6 @@ import ( "log" "net/http" "os" - "os/exec" "strconv" "strings" "time" @@ -53,13 +52,12 @@ var orgId = os.Getenv("ORG_ID") var baseUrl = os.Getenv("BASE_URL") var environment = os.Getenv("ENVIRONMENT_NAME") var dockerApiVersion = os.Getenv("DOCKER_API_VERSION") -var runningMode = strings.ToLower(os.Getenv("RUNNING_MODE")) var cleanupEnv = strings.ToLower(os.Getenv("CLEANUP")) var timezone = os.Getenv("TZ") +var containerName = os.Getenv("CONTAINER_NAME") var executionIds = []string{} var dockercli *dockerclient.Client -var containerId string func init() { var err error @@ -68,60 +66,16 @@ func init() { if err != nil { panic(fmt.Sprintf("Unable to create docker client: %s", err)) } - - getThisContainerId() -} - -// form id of current running container -func getThisContainerId() { - fCol := "" - - // some adjusting based on current running mode - switch runningMode { - case "kubernetes": - // cgroup will be like: - // 11:net_cls,net_prio:/kubepods/besteffort/podf132b44d-cfcf-43f7-9906-79f58e268333/851466f8b5ed5aa0f265b1c95c6d2bafbc51a38dd5c5a1621b6e586572150009 - fCol = "5" - log.Printf("[INFO] Running containerized in Kubernetes!") - - case "docker": - // cgroup will be like: - // 12:perf_event:/docker/0f06810364f52a2cd6e80bfba27419cb8a29758a204cd676388f4913bb366f2b - fCol = "3" - log.Printf("[INFO] Running containerized in Docker!") - - default: - fCol = "3" // for backward-compatibility with production - log.Printf("[WARNING] RUNNING_MODE not set - defaulting to Docker (NOT Kubernetes).") - } - - if fCol != "" { - cmd := fmt.Sprintf("cat /proc/self/cgroup | grep memory | tail -1 | cut -d/ -f%s | grep -o -E '[0-9A-z]{64}'", fCol) - out, err := exec.Command("bash", "-c", cmd).Output() - if err == nil { - containerId = strings.TrimSpace(string(out)) - - // cgroup error. Hardcoding this. - // https://github.com/moby/moby/issues/7015 - //log.Printf("Checking if %s is in %s", ".scope", string(out)) - if strings.Contains(string(out), ".scope") { - containerId = "shuffle-orborus" - //docker-76c537e9a4b7c7233011f5d70e6b7f2d600b6413ac58a96519b8dca7a3f7117a.scope - } - } else { - if fCol == "0" { - containerId = "shuffle-orborus" - log.Printf("[WARNING] Failed getting container ID: %s", err) - } - } - } - - log.Printf(`[INFO] Started with containerId "%s"`, containerId) } // Deploys the internal worker whenever something happens // https://docs.docker.com/engine/api/sdk/examples/ func deployWorker(image string, identifier string, env []string) { + if containerName == "" { + log.Printf("[INFO] CONTAINER_NAME is not set, defaulting to 'shuffle-orborus'") + containerName = "shuffle-orborus" + } + // Binds is the actual "-v" volume. // Max 20% CPU every second @@ -137,14 +91,7 @@ func deployWorker(image string, identifier string, env []string) { Binds: []string{ "/var/run/docker.sock:/var/run/docker.sock:rw", }, - } - - // form container id and use it as network source if it's not empty - if containerId != "" { - //log.Printf("[INFO] Found container ID %s", containerId) - hostConfig.NetworkMode = container.NetworkMode(fmt.Sprintf("container:%s", containerId)) - } else { - //log.Printf("[INFO] Empty self container id, continue without NetworkMode") + NetworkMode: container.NetworkMode(fmt.Sprintf("container:%s", containerName)) } if cleanupEnv == "true" { diff --git a/functions/onprem/orborus/run.sh b/functions/onprem/orborus/run.sh index 48e5b701..9c6d8db5 100644 --- a/functions/onprem/orborus/run.sh +++ b/functions/onprem/orborus/run.sh @@ -3,7 +3,6 @@ docker run \ --env ENVIRONMENT_NAME="Shuffle" \ --env BASE_URL=http://shuffle-backend:5001 \ --env DOCKER_API_VERSION=1.42 \ - --env RUNNING_MODE="Docker" \ --network "shuffle_shuffle" \ -v /var/run/docker.sock:/var/run/docker.sock \ frikky/shuffle:orborus diff --git a/functions/onprem/worker/worker.go b/functions/onprem/worker/worker.go index dfbe8312..b9ae4e59 100644 --- a/functions/onprem/worker/worker.go +++ b/functions/onprem/worker/worker.go @@ -16,7 +16,6 @@ import ( "net/http" "net/url" "os" - "os/exec" "strings" "time" @@ -42,7 +41,6 @@ var cleanupEnv = strings.ToLower(os.Getenv("CLEANUP")) var timezone = os.Getenv("TZ") var baseimagename = "frikky/shuffle" var registryName = "registry.hub.docker.com" -var fallbackName = "shuffle-orborus" var sleepTime = 2 var requestCache *cache.Cache var topClient *http.Client @@ -61,38 +59,6 @@ var startAction string var results []shuffle.ActionResult var allLogs map[string]string -var containerId string - -// form container id of current running container -func getThisContainerId() string { - if len(containerId) > 0 { - return containerId - } - - id := "" - cmd := fmt.Sprintf("cat /proc/self/cgroup | grep memory | tail -1 | cut -d/ -f3 | grep -o -E '[0-9A-z]{64}'") - out, err := exec.Command("bash", "-c", cmd).Output() - if err == nil { - id = strings.TrimSpace(string(out)) - - //log.Printf("Checking if %s is in %s", ".scope", string(out)) - if strings.Contains(string(out), ".scope") { - id = fallbackName - } - } - - return id -} - -func init() { - containerId = getThisContainerId() - if len(containerId) == 0 { - log.Printf("[WARNING] No container ID found. Not running containerized? This should only show during testing") - } else { - log.Printf("[INFO] Found container ID for this worker: %s", containerId) - } -} - // removes every container except itself (worker) func shutdown(workflowExecution shuffle.WorkflowExecution, nodeId string, reason string, handleResultSend bool) { log.Printf("[INFO] Shutdown (%s) started with reason %#v. Result amount: %d. ResultsSent: %d, Send result: %#v", workflowExecution.Status, reason, len(workflowExecution.Results), requestsSent, handleResultSend) @@ -221,14 +187,7 @@ func deployApp(cli *dockerclient.Client, image string, identifier string, env [] Config: map[string]string{}, }, Resources: container.Resources{}, - } - - // form container id and use it as network source if it's not empty - containerId = getThisContainerId() - if containerId != "" { - hostConfig.NetworkMode = container.NetworkMode(fmt.Sprintf("container:%s", containerId)) - } else { - log.Printf("[WARNING] Empty self container id, continue without NetworkMode") + NetworkMode: container.NetworkMode(fmt.Sprintf("container:worker-%s", workflowExecution.ExecutionId)), } // Removing because log extraction should happen first From 9f3889c76d06f8e1c47b01168a032df809a09e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20C?= <82955277+gc-advens@users.noreply.github.com> Date: Tue, 31 Aug 2021 08:20:14 +0200 Subject: [PATCH 2/4] Revert changes --- functions/onprem/orborus/orborus.go | 67 ++++++++++++++++++++++++++--- functions/onprem/orborus/run.sh | 1 + 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/functions/onprem/orborus/orborus.go b/functions/onprem/orborus/orborus.go index 73faafeb..6ca42b24 100644 --- a/functions/onprem/orborus/orborus.go +++ b/functions/onprem/orborus/orborus.go @@ -16,6 +16,7 @@ import ( "log" "net/http" "os" + "os/exec" "strconv" "strings" "time" @@ -52,12 +53,13 @@ var orgId = os.Getenv("ORG_ID") var baseUrl = os.Getenv("BASE_URL") var environment = os.Getenv("ENVIRONMENT_NAME") var dockerApiVersion = os.Getenv("DOCKER_API_VERSION") +var runningMode = strings.ToLower(os.Getenv("RUNNING_MODE")) var cleanupEnv = strings.ToLower(os.Getenv("CLEANUP")) var timezone = os.Getenv("TZ") -var containerName = os.Getenv("CONTAINER_NAME") var executionIds = []string{} var dockercli *dockerclient.Client +var containerId string func init() { var err error @@ -66,16 +68,60 @@ func init() { if err != nil { panic(fmt.Sprintf("Unable to create docker client: %s", err)) } + + getThisContainerId() +} + +// form id of current running container +func getThisContainerId() { + fCol := "" + + // some adjusting based on current running mode + switch runningMode { + case "kubernetes": + // cgroup will be like: + // 11:net_cls,net_prio:/kubepods/besteffort/podf132b44d-cfcf-43f7-9906-79f58e268333/851466f8b5ed5aa0f265b1c95c6d2bafbc51a38dd5c5a1621b6e586572150009 + fCol = "5" + log.Printf("[INFO] Running containerized in Kubernetes!") + + case "docker": + // cgroup will be like: + // 12:perf_event:/docker/0f06810364f52a2cd6e80bfba27419cb8a29758a204cd676388f4913bb366f2b + fCol = "3" + log.Printf("[INFO] Running containerized in Docker!") + + default: + fCol = "3" // for backward-compatibility with production + log.Printf("[WARNING] RUNNING_MODE not set - defaulting to Docker (NOT Kubernetes).") + } + + if fCol != "" { + cmd := fmt.Sprintf("cat /proc/self/cgroup | grep memory | tail -1 | cut -d/ -f%s | grep -o -E '[0-9A-z]{64}'", fCol) + out, err := exec.Command("bash", "-c", cmd).Output() + if err == nil { + containerId = strings.TrimSpace(string(out)) + + // cgroup error. Hardcoding this. + // https://github.com/moby/moby/issues/7015 + //log.Printf("Checking if %s is in %s", ".scope", string(out)) + if strings.Contains(string(out), ".scope") { + containerId = "shuffle-orborus" + //docker-76c537e9a4b7c7233011f5d70e6b7f2d600b6413ac58a96519b8dca7a3f7117a.scope + } + } else { + if fCol == "0" { + containerId = "shuffle-orborus" + log.Printf("[WARNING] Failed getting container ID: %s", err) + } + } + } + + log.Printf(`[INFO] Started with containerId "%s"`, containerId) } // Deploys the internal worker whenever something happens // https://docs.docker.com/engine/api/sdk/examples/ func deployWorker(image string, identifier string, env []string) { - if containerName == "" { - log.Printf("[INFO] CONTAINER_NAME is not set, defaulting to 'shuffle-orborus'") - containerName = "shuffle-orborus" - } - // Binds is the actual "-v" volume. // Max 20% CPU every second @@ -91,7 +137,14 @@ func deployWorker(image string, identifier string, env []string) { Binds: []string{ "/var/run/docker.sock:/var/run/docker.sock:rw", }, - NetworkMode: container.NetworkMode(fmt.Sprintf("container:%s", containerName)) + } + + // form container id and use it as network source if it's not empty + if containerId != "" { + //log.Printf("[INFO] Found container ID %s", containerId) + hostConfig.NetworkMode = container.NetworkMode(fmt.Sprintf("container:%s", containerId)) + } else { + //log.Printf("[INFO] Empty self container id, continue without NetworkMode") } if cleanupEnv == "true" { diff --git a/functions/onprem/orborus/run.sh b/functions/onprem/orborus/run.sh index 9c6d8db5..48e5b701 100644 --- a/functions/onprem/orborus/run.sh +++ b/functions/onprem/orborus/run.sh @@ -3,6 +3,7 @@ docker run \ --env ENVIRONMENT_NAME="Shuffle" \ --env BASE_URL=http://shuffle-backend:5001 \ --env DOCKER_API_VERSION=1.42 \ + --env RUNNING_MODE="Docker" \ --network "shuffle_shuffle" \ -v /var/run/docker.sock:/var/run/docker.sock \ frikky/shuffle:orborus From 233477407c8ec69b148ba6dc7c721ea26f8fbc8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20C?= <82955277+gc-advens@users.noreply.github.com> Date: Tue, 31 Aug 2021 08:45:41 +0200 Subject: [PATCH 3/4] Add fallback strategies when orborus container id was not found --- functions/onprem/orborus/orborus.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/functions/onprem/orborus/orborus.go b/functions/onprem/orborus/orborus.go index 6ca42b24..7bc275d0 100644 --- a/functions/onprem/orborus/orborus.go +++ b/functions/onprem/orborus/orborus.go @@ -56,6 +56,7 @@ var dockerApiVersion = os.Getenv("DOCKER_API_VERSION") var runningMode = strings.ToLower(os.Getenv("RUNNING_MODE")) var cleanupEnv = strings.ToLower(os.Getenv("CLEANUP")) var timezone = os.Getenv("TZ") +var containerName = os.Getenv("CONTAINER_NAME") var executionIds = []string{} var dockercli *dockerclient.Client @@ -101,18 +102,25 @@ func getThisContainerId() { if err == nil { containerId = strings.TrimSpace(string(out)) - // cgroup error. Hardcoding this. + // cgroup error. Use fallback strategy below. // https://github.com/moby/moby/issues/7015 //log.Printf("Checking if %s is in %s", ".scope", string(out)) if strings.Contains(string(out), ".scope") { - containerId = "shuffle-orborus" + containerId = "" //docker-76c537e9a4b7c7233011f5d70e6b7f2d600b6413ac58a96519b8dca7a3f7117a.scope } } else { - if fCol == "0" { - containerId = "shuffle-orborus" - log.Printf("[WARNING] Failed getting container ID: %s", err) - } + log.Printf("[WARNING] Failed getting container ID: %s", err) + } + } + + if containerId == "" { + if containerName != "" { + containerId = containerName + log.Printf("[INFO] Falling back to CONTAINER_NAME as container ID") + } else { + containerId = "shuffle-orborus" + log.Printf(`[WARNING] CONTAINER_NAME is not set. Falling back to default name "%s" as container ID`, containerId) } } @@ -137,14 +145,7 @@ func deployWorker(image string, identifier string, env []string) { Binds: []string{ "/var/run/docker.sock:/var/run/docker.sock:rw", }, - } - - // form container id and use it as network source if it's not empty - if containerId != "" { - //log.Printf("[INFO] Found container ID %s", containerId) - hostConfig.NetworkMode = container.NetworkMode(fmt.Sprintf("container:%s", containerId)) - } else { - //log.Printf("[INFO] Empty self container id, continue without NetworkMode") + NetworkMode: container.NetworkMode(fmt.Sprintf("container:%s", containerId)), } if cleanupEnv == "true" { From 80ee585e819e6b8c0b6c37e33c84051e54ddbe3b Mon Sep 17 00:00:00 2001 From: frikky Date: Thu, 2 Sep 2021 01:46:21 +0200 Subject: [PATCH 4/4] #501: Temporary fix for issues with cgroup v2 --- .env | 3 ++- functions/onprem/orborus/build.sh | 2 +- functions/onprem/orborus/orborus.go | 2 +- functions/onprem/worker/build.sh | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.env b/.env index 8610fe2b..ae66b902 100644 --- a/.env +++ b/.env @@ -41,7 +41,8 @@ HTTP_PROXY= HTTPS_PROXY= SHUFFLE_PASS_WORKER_PROXY=TRUE SHUFFLE_PASS_APP_PROXY=FALSE -TZ=Europe/Amsterdam #Timezone-handler in Orborus, Worker and Apps +TZ=Europe/Amsterdam # Timezone-handler in Orborus, Worker and Apps +ORBORUS_CONTAINER_NAME= # Used to FIND the containername. cgroup v2: issue 501 SHUFFLE_BASE_IMAGE_REGISTRY=ghcr.io SHUFFLE_BASE_IMAGE_NAME=frikky diff --git a/functions/onprem/orborus/build.sh b/functions/onprem/orborus/build.sh index eb1d12db..3beae1f3 100644 --- a/functions/onprem/orborus/build.sh +++ b/functions/onprem/orborus/build.sh @@ -1,5 +1,5 @@ NAME=shuffle-orborus -VERSION=0.9.10 +VERSION=0.9.14 echo "Running docker build with $NAME:$VERSION" #docker rmi frikky/shuffle:$NAME --force diff --git a/functions/onprem/orborus/orborus.go b/functions/onprem/orborus/orborus.go index 7bc275d0..eb46a708 100644 --- a/functions/onprem/orborus/orborus.go +++ b/functions/onprem/orborus/orborus.go @@ -56,7 +56,7 @@ var dockerApiVersion = os.Getenv("DOCKER_API_VERSION") var runningMode = strings.ToLower(os.Getenv("RUNNING_MODE")) var cleanupEnv = strings.ToLower(os.Getenv("CLEANUP")) var timezone = os.Getenv("TZ") -var containerName = os.Getenv("CONTAINER_NAME") +var containerName = os.Getenv("ORBORUS_CONTAINER_NAME") var executionIds = []string{} var dockercli *dockerclient.Client diff --git a/functions/onprem/worker/build.sh b/functions/onprem/worker/build.sh index b2f269c3..2dbd7f04 100644 --- a/functions/onprem/worker/build.sh +++ b/functions/onprem/worker/build.sh @@ -1,5 +1,5 @@ NAME=shuffle-worker -VERSION=0.9.13 +VERSION=0.9.14 echo "Running docker build with $NAME:$VERSION" #CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o worker.bin .