Skip to content

Commit 82e90f1

Browse files
Improve container name detection in cgroups v2 unified hierarchy mode
refactoring: docker-related code moved into new package
1 parent 7bde528 commit 82e90f1

File tree

6 files changed

+115
-40
lines changed

6 files changed

+115
-40
lines changed

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/golang-jwt/jwt v3.2.2+incompatible
1515
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
1616
github.com/mitchellh/go-homedir v1.1.0
17+
github.com/opencontainers/runc v1.1.2
1718
github.com/pkg/errors v0.9.1
1819
github.com/rs/zerolog v1.19.0
1920
github.com/ryanuber/columnize v2.1.0+incompatible
@@ -32,11 +33,14 @@ require (
3233
github.com/containerd/cgroups v1.0.3 // indirect
3334
github.com/containerd/containerd v1.6.6 // indirect
3435
github.com/containerd/continuity v0.2.2 // indirect
36+
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
37+
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
3538
github.com/davecgh/go-spew v1.1.1 // indirect
3639
github.com/docker/distribution v2.7.1+incompatible // indirect
3740
github.com/docker/docker v20.10.17+incompatible // indirect
3841
github.com/docker/go-connections v0.4.0 // indirect
3942
github.com/docker/go-units v0.4.0 // indirect
43+
github.com/godbus/dbus/v5 v5.0.6 // indirect
4044
github.com/gogo/protobuf v1.3.2 // indirect
4145
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
4246
github.com/golang/protobuf v1.5.2 // indirect
@@ -50,7 +54,7 @@ require (
5054
github.com/morikuni/aec v1.0.0 // indirect
5155
github.com/opencontainers/go-digest v1.0.0 // indirect
5256
github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect
53-
github.com/opencontainers/runc v1.1.2 // indirect
57+
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect
5458
github.com/pavel-v-chernykh/keystore-go v2.1.0+incompatible // indirect
5559
github.com/pmezard/go-difflib v1.0.0 // indirect
5660
github.com/sirupsen/logrus v1.8.1 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,11 @@ github.com/coreos/go-semver v0.3.0 h1:wkHLiw0WNATZnSG7epLsujiMCgPAc9xhjJ4tgnAxmf
242242
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
243243
github.com/coreos/go-systemd v0.0.0-20161114122254-48702e0da86b/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
244244
github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
245+
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e h1:Wf6HqHfScWJN9/ZjdUKyjop4mf3Qdd+1TvvltAvM3m8=
245246
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
246247
github.com/coreos/go-systemd/v22 v22.0.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk=
247248
github.com/coreos/go-systemd/v22 v22.1.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk=
249+
github.com/coreos/go-systemd/v22 v22.3.2 h1:D9/bQk5vlXQFZ6Kwuu6zaiXJ9oTPe68++AzAJc1DzSI=
248250
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
249251
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
250252
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
@@ -254,6 +256,7 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
254256
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
255257
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
256258
github.com/cyphar/filepath-securejoin v0.2.2/go.mod h1:FpkQEhXnPnOthhzymB7CGsFk2G9VLXONKD9G7QGMM+4=
259+
github.com/cyphar/filepath-securejoin v0.2.3 h1:YX6ebbZCZP7VkM3scTTokDgBL2TY741X51MTk3ycuNI=
257260
github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
258261
github.com/d2g/dhcp4 v0.0.0-20170904100407-a1d1b6c41b1c/go.mod h1:Ct2BUK8SB0YC1SMSibvLzxjeJLnrYEVLULFNiHY9YfQ=
259262
github.com/d2g/dhcp4client v1.0.0/go.mod h1:j0hNfjhrt2SxUOw55nL0ATM/z4Yt3t2Kd1mW34z5W5s=
@@ -348,9 +351,11 @@ github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh
348351
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
349352
github.com/godbus/dbus v0.0.0-20151105175453-c7fdd8b5cd55/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
350353
github.com/godbus/dbus v0.0.0-20180201030542-885f9cc04c9c/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
354+
github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e h1:BWhy2j3IXJhjCbC68FptL43tDKIq8FladmaTs3Xs7Z8=
351355
github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
352356
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
353357
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
358+
github.com/godbus/dbus/v5 v5.0.6 h1:mkgN1ofwASrYnJ5W6U/BxG15eXXXjirgZc7CLqkcaro=
354359
github.com/godbus/dbus/v5 v5.0.6/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
355360
github.com/gogo/googleapis v1.2.0/go.mod h1:Njal3psf3qN6dwBtQfUmBZh2ybovJ0tlu3o/AC7HYjU=
356361
github.com/gogo/googleapis v1.4.0/go.mod h1:5YRNX2z1oM5gXdAkurHa942MDgEJyk02w4OecKY87+c=
@@ -600,6 +605,7 @@ github.com/opencontainers/runtime-spec v1.0.1/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/
600605
github.com/opencontainers/runtime-spec v1.0.2-0.20190207185410-29686dbc5559/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
601606
github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
602607
github.com/opencontainers/runtime-spec v1.0.3-0.20200929063507-e6143ca7d51d/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
608+
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 h1:3snG66yBm59tKhhSPQrQ/0bCrv1LQbKt40LnUPiUxdc=
603609
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
604610
github.com/opencontainers/runtime-tools v0.0.0-20181011054405-1d69bd0f9c39/go.mod h1:r3f7wjNzSs2extwzU3Y+6pKfobzPh+kKFJ3ofN+3nfs=
605611
github.com/opencontainers/selinux v1.6.0/go.mod h1:VVGKuOLlE7v4PJyT6h7mNWvq1rzqiriPsEqVhc+svHE=

main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
driver "github.com/arangodb/go-driver"
4747

4848
_ "github.com/arangodb-helper/arangodb/client"
49+
"github.com/arangodb-helper/arangodb/pkg/docker"
4950
"github.com/arangodb-helper/arangodb/pkg/features"
5051
"github.com/arangodb-helper/arangodb/pkg/logging"
5152
"github.com/arangodb-helper/arangodb/pkg/net"
@@ -506,10 +507,11 @@ func mustPrepareService(generateAutoKeyFile bool) (*service.Service, service.Boo
506507
// Auto detect docker container ID (if needed)
507508
dockerStarterImage := defaultDockerStarterImage
508509
runningInDocker := false
509-
if isRunningInDocker() {
510+
if docker.IsRunningInDocker() {
510511
runningInDocker = true
511-
info, err := findDockerContainerInfo(opts.docker.endpoint)
512+
info, err := docker.FindDockerContainerInfo(opts.docker.endpoint)
512513
if err != nil {
514+
log.Warn().Err(err).Msgf("could not find docker container info")
513515
if opts.docker.containerName == "" {
514516
showDockerContainerNameMissingHelp()
515517
}
@@ -701,7 +703,7 @@ func mustPrepareService(generateAutoKeyFile bool) (*service.Service, service.Boo
701703
LogRotateFilesToKeep: opts.log.rotateFilesToKeep,
702704
LogRotateInterval: opts.log.rotateInterval,
703705
InstanceUpTimeout: opts.starter.instanceUpTimeout,
704-
RunningInDocker: isRunningInDocker(),
706+
RunningInDocker: docker.IsRunningInDocker(),
705707
DockerContainerName: opts.docker.containerName,
706708
DockerEndpoint: opts.docker.endpoint,
707709
DockerArangodImage: opts.docker.arangodImage,

pkg/docker/cgroups_nonunix.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2017-2022 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
//go:build !linux && !freebsd && !openbsd && !netbsd && !solaris
22+
// +build !linux,!freebsd,!openbsd,!netbsd,!solaris
23+
24+
package docker
25+
26+
func IsCGroup2UnifiedMode() bool {
27+
return false
28+
}

pkg/docker/cgroups_unix.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2017-2022 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
//go:build linux || freebsd || openbsd || netbsd || solaris
22+
// +build linux freebsd openbsd netbsd solaris
23+
24+
package docker
25+
26+
import "github.com/opencontainers/runc/libcontainer/cgroups"
27+
28+
func IsCGroup2UnifiedMode() bool {
29+
return cgroups.IsCgroup2UnifiedMode()
30+
}

docker.go renamed to pkg/docker/docker.go

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// Author Ewout Prangsma
2121
//
2222

23-
package main
23+
package docker
2424

2525
import (
2626
"fmt"
@@ -37,13 +37,13 @@ const (
3737
cgroupDockerCEMarker = ":/docker-ce/docker/"
3838
)
3939

40-
type containerInfo struct {
40+
type ContainerInfo struct {
4141
Name string
4242
ImageName string
4343
}
4444

45-
// isRunningInDocker checks if the process is running in a docker container.
46-
func isRunningInDocker() bool {
45+
// IsRunningInDocker checks if the process is running in a docker container.
46+
func IsRunningInDocker() bool {
4747
if os.Getenv("RUNNING_IN_DOCKER") != "true" {
4848
return false
4949
}
@@ -53,49 +53,32 @@ func isRunningInDocker() bool {
5353
return true
5454
}
5555

56-
// findDockerContainerInfo find information (name or if not possible the ID, image-name) of the container that is used to run this process.
57-
func findDockerContainerInfo(dockerEndpoint string) (containerInfo, error) {
58-
findID := func() (string, error) {
59-
raw, err := ioutil.ReadFile("/proc/self/cgroup")
60-
if err != nil {
61-
return "", errors.WithStack(err)
62-
}
63-
lines := strings.Split(string(raw), "\n")
64-
for _, line := range lines {
65-
66-
if i := strings.Index(line, cgroupDockerCEMarker); i > 0 {
67-
id := strings.TrimSpace(line[i+len(cgroupDockerCEMarker):])
68-
if id != "" {
69-
return id, nil
70-
}
71-
} else if i := strings.Index(line, cgroupDockerMarker); i > 0 {
72-
id := strings.TrimSpace(line[i+len(cgroupDockerMarker):])
73-
if id != "" {
74-
return id, nil
75-
}
76-
}
77-
}
78-
return "", errors.WithStack(fmt.Errorf("Cannot find docker marker"))
79-
}
80-
81-
id, err := findID()
56+
// FindDockerContainerInfo find information (name or if not possible the ID, image-name) of the container that is used to run this process.
57+
func FindDockerContainerInfo(dockerEndpoint string) (ContainerInfo, error) {
58+
id, err := findRunningContainerID()
8259
if err != nil {
83-
return containerInfo{}, errors.WithStack(err)
60+
// Determining the container ID in unified-hierarchy mode is impossible.
61+
// Falling back to hostname if available
62+
id = os.Getenv("HOSTNAME")
63+
if !IsCGroup2UnifiedMode() || id == "" {
64+
return ContainerInfo{}, errors.WithMessagef(err, "hostname env val %s", id)
65+
}
8466
}
85-
info := containerInfo{Name: id}
67+
info := ContainerInfo{Name: id}
8668

8769
// Find name for container with ID.
8870
client, err := docker.NewClient(dockerEndpoint)
8971
if err != nil {
90-
return info, nil // fallback to ID
72+
return info, nil
9173
}
9274
container, err := client.InspectContainer(id)
9375
if err != nil {
94-
return info, nil // fallback to ID
76+
return info, nil
9577
}
9678

97-
if name := container.Name; name != "" {
98-
info.Name = name
79+
info.Name = container.ID
80+
if container.Name != "" {
81+
info.Name = container.Name
9982
}
10083
info.ImageName = container.Image
10184

@@ -107,3 +90,25 @@ func findDockerContainerInfo(dockerEndpoint string) (containerInfo, error) {
10790

10891
return info, nil
10992
}
93+
94+
func findRunningContainerID() (string, error) {
95+
raw, err := ioutil.ReadFile("/proc/self/cgroup")
96+
if err != nil {
97+
return "", errors.WithStack(err)
98+
}
99+
lines := strings.Split(string(raw), "\n")
100+
for _, line := range lines {
101+
if i := strings.Index(line, cgroupDockerCEMarker); i > 0 {
102+
id := strings.TrimSpace(line[i+len(cgroupDockerCEMarker):])
103+
if id != "" {
104+
return id, nil
105+
}
106+
} else if i := strings.Index(line, cgroupDockerMarker); i > 0 {
107+
id := strings.TrimSpace(line[i+len(cgroupDockerMarker):])
108+
if id != "" {
109+
return id, nil
110+
}
111+
}
112+
}
113+
return "", errors.WithStack(fmt.Errorf("cannot find docker marker"))
114+
}

0 commit comments

Comments
 (0)