Fix #496: compatibility with cgroup v2

This commit is contained in:
Grégoire C
2021-08-30 13:13:53 +02:00
parent f213451f9b
commit c1e29e8ce2
3 changed files with 8 additions and 103 deletions
+7 -60
View File
@@ -16,7 +16,6 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"os/exec"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -53,13 +52,12 @@ var orgId = os.Getenv("ORG_ID")
var baseUrl = os.Getenv("BASE_URL") var baseUrl = os.Getenv("BASE_URL")
var environment = os.Getenv("ENVIRONMENT_NAME") var environment = os.Getenv("ENVIRONMENT_NAME")
var dockerApiVersion = os.Getenv("DOCKER_API_VERSION") var dockerApiVersion = os.Getenv("DOCKER_API_VERSION")
var runningMode = strings.ToLower(os.Getenv("RUNNING_MODE"))
var cleanupEnv = strings.ToLower(os.Getenv("CLEANUP")) var cleanupEnv = strings.ToLower(os.Getenv("CLEANUP"))
var timezone = os.Getenv("TZ") var timezone = os.Getenv("TZ")
var containerName = os.Getenv("CONTAINER_NAME")
var executionIds = []string{} var executionIds = []string{}
var dockercli *dockerclient.Client var dockercli *dockerclient.Client
var containerId string
func init() { func init() {
var err error var err error
@@ -68,60 +66,16 @@ func init() {
if err != nil { if err != nil {
panic(fmt.Sprintf("Unable to create docker client: %s", err)) 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 // Deploys the internal worker whenever something happens
// https://docs.docker.com/engine/api/sdk/examples/ // https://docs.docker.com/engine/api/sdk/examples/
func deployWorker(image string, identifier string, env []string) { 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. // Binds is the actual "-v" volume.
// Max 20% CPU every second // Max 20% CPU every second
@@ -137,14 +91,7 @@ func deployWorker(image string, identifier string, env []string) {
Binds: []string{ Binds: []string{
"/var/run/docker.sock:/var/run/docker.sock:rw", "/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" { if cleanupEnv == "true" {
-1
View File
@@ -3,7 +3,6 @@ docker run \
--env ENVIRONMENT_NAME="Shuffle" \ --env ENVIRONMENT_NAME="Shuffle" \
--env BASE_URL=http://shuffle-backend:5001 \ --env BASE_URL=http://shuffle-backend:5001 \
--env DOCKER_API_VERSION=1.42 \ --env DOCKER_API_VERSION=1.42 \
--env RUNNING_MODE="Docker" \
--network "shuffle_shuffle" \ --network "shuffle_shuffle" \
-v /var/run/docker.sock:/var/run/docker.sock \ -v /var/run/docker.sock:/var/run/docker.sock \
frikky/shuffle:orborus frikky/shuffle:orborus
+1 -42
View File
@@ -16,7 +16,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"os/exec"
"strings" "strings"
"time" "time"
@@ -42,7 +41,6 @@ var cleanupEnv = strings.ToLower(os.Getenv("CLEANUP"))
var timezone = os.Getenv("TZ") var timezone = os.Getenv("TZ")
var baseimagename = "frikky/shuffle" var baseimagename = "frikky/shuffle"
var registryName = "registry.hub.docker.com" var registryName = "registry.hub.docker.com"
var fallbackName = "shuffle-orborus"
var sleepTime = 2 var sleepTime = 2
var requestCache *cache.Cache var requestCache *cache.Cache
var topClient *http.Client var topClient *http.Client
@@ -61,38 +59,6 @@ var startAction string
var results []shuffle.ActionResult var results []shuffle.ActionResult
var allLogs map[string]string 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) // removes every container except itself (worker)
func shutdown(workflowExecution shuffle.WorkflowExecution, nodeId string, reason string, handleResultSend bool) { 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) 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{}, Config: map[string]string{},
}, },
Resources: container.Resources{}, Resources: container.Resources{},
} NetworkMode: container.NetworkMode(fmt.Sprintf("container:worker-%s", workflowExecution.ExecutionId)),
// 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")
} }
// Removing because log extraction should happen first // Removing because log extraction should happen first