Skip to content

Commit 88b1d36

Browse files
Merge pull request #4458 from haircommander/collect-mode-1.17
[1.17] set inactive-or-failed CollectMode if appropriate
2 parents 7f979af + d5d18fa commit 88b1d36

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

internal/config/node/node.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// +build linux
2+
3+
package node
4+
5+
import (
6+
"github.com/pkg/errors"
7+
"github.com/sirupsen/logrus"
8+
)
9+
10+
// ValidateConfig initializes and validates all of the singleton variables
11+
// that store the node's configuration.
12+
// Currently, we check hugetlb, cgroup v1 or v2, pid and memory swap support for cgroups.
13+
// We check the error at server configuration validation, and if we error, shutdown
14+
// cri-o early, instead of when we're already trying to run containers.
15+
func ValidateConfig() error {
16+
toInit := []struct {
17+
name string
18+
init func() bool
19+
err *error
20+
activated *bool
21+
fatal bool
22+
}{
23+
{
24+
name: "systemd CollectMode",
25+
init: SystemdHasCollectMode,
26+
err: &systemdHasCollectModeErr,
27+
activated: &systemdHasCollectMode,
28+
fatal: false,
29+
},
30+
}
31+
for _, i := range toInit {
32+
i.init()
33+
if *i.err != nil {
34+
err := errors.Errorf("node configuration validation for %s failed: %v", i.name, *i.err)
35+
if i.fatal {
36+
return err
37+
}
38+
logrus.Error(err.Error())
39+
}
40+
if i.activated != nil {
41+
logrus.Infof("node configuration value for %s is %v", i.name, *i.activated)
42+
}
43+
}
44+
return nil
45+
}

internal/config/node/systemd.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// +build linux
2+
3+
package node
4+
5+
import (
6+
"os/exec"
7+
"regexp"
8+
"strconv"
9+
"sync"
10+
11+
"github.com/pkg/errors"
12+
)
13+
14+
var (
15+
systemdHasCollectModeOnce sync.Once
16+
systemdHasCollectMode bool
17+
systemdHasCollectModeErr error
18+
)
19+
20+
func SystemdHasCollectMode() bool {
21+
systemdHasCollectModeOnce.Do(func() {
22+
stdout, err := exec.Command("systemctl", "--version").Output()
23+
if err != nil {
24+
systemdHasCollectModeErr = err
25+
return
26+
}
27+
matches := regexp.MustCompile(`^systemd (?P<Version>\d+)`).FindStringSubmatch(string(stdout))
28+
29+
if len(matches) != 2 {
30+
systemdHasCollectModeErr = errors.Errorf("systemd version command returned incompatible formatted information: %v", string(stdout))
31+
return
32+
}
33+
34+
systemdVersion, err := strconv.Atoi(matches[1])
35+
if err != nil {
36+
systemdHasCollectModeErr = err
37+
return
38+
}
39+
if systemdVersion >= 236 {
40+
systemdHasCollectMode = true
41+
}
42+
})
43+
return systemdHasCollectMode
44+
}

pkg/config/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
createconfig "github.com/containers/libpod/pkg/spec"
1919
"github.com/containers/storage"
2020
cstorage "github.com/containers/storage"
21+
"github.com/cri-o/cri-o/internal/config/node"
2122
"github.com/cri-o/cri-o/internal/version"
2223
"github.com/cri-o/cri-o/utils"
2324
units "github.com/docker/go-units"
@@ -583,6 +584,12 @@ func (c *Config) Validate(systemContext *types.SystemContext, onExecution bool)
583584
return fmt.Errorf("unrecognized image volume type specified")
584585
}
585586

587+
if onExecution {
588+
if err := node.ValidateConfig(); err != nil {
589+
return err
590+
}
591+
}
592+
586593
if err := c.RootConfig.Validate(onExecution); err != nil {
587594
return errors.Wrapf(err, "root config")
588595
}

server/container_create_linux.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/containers/libpod/pkg/rootless"
1818
selinux "github.com/containers/libpod/pkg/selinux"
1919
createconfig "github.com/containers/libpod/pkg/spec"
20+
"github.com/cri-o/cri-o/internal/config/node"
2021
"github.com/cri-o/cri-o/internal/lib"
2122
"github.com/cri-o/cri-o/internal/lib/sandbox"
2223
"github.com/cri-o/cri-o/internal/oci"
@@ -520,6 +521,9 @@ func (s *Server) createSandboxContainer(ctx context.Context, containerID, contai
520521
if useSystemd {
521522
specgen.AddAnnotation("org.systemd.property.TimeoutStopUSec",
522523
"uint64 "+t+"000000") // sec to usec
524+
if node.SystemdHasCollectMode() {
525+
specgen.AddAnnotation("org.systemd.property.CollectMode", "'inactive-or-failed'")
526+
}
523527
}
524528
}
525529

server/sandbox_run_linux.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/containers/libpod/pkg/annotations"
1919
"github.com/containers/libpod/pkg/cgroups"
2020
"github.com/containers/storage"
21+
"github.com/cri-o/cri-o/internal/config/node"
2122
"github.com/cri-o/cri-o/internal/lib"
2223
"github.com/cri-o/cri-o/internal/lib/sandbox"
2324
"github.com/cri-o/cri-o/internal/oci"
@@ -342,6 +343,10 @@ func (s *Server) runPodSandbox(ctx context.Context, req *pb.RunPodSandboxRequest
342343
g.AddAnnotation("org.opencontainers.image.stopSignal", podContainer.Config.Config.StopSignal)
343344
}
344345

346+
if s.config.CgroupManager == oci.SystemdCgroupsManager && node.SystemdHasCollectMode() {
347+
g.AddAnnotation("org.systemd.property.CollectMode", "'inactive-or-failed'")
348+
}
349+
345350
created := time.Now()
346351
g.AddAnnotation(annotations.Created, created.Format(time.RFC3339Nano))
347352

0 commit comments

Comments
 (0)