-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathconmonmgr.go
90 lines (76 loc) · 2.42 KB
/
conmonmgr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package conmonmgr
import (
"bytes"
"fmt"
"path"
"strings"
"github.com/blang/semver/v4"
"github.com/cri-o/cri-o/utils/cmdrunner"
"github.com/sirupsen/logrus"
)
var (
versionSupportsSync = semver.MustParse("2.0.19")
versionSupportsLogGlobalSizeMax = semver.MustParse("2.1.2")
)
type ConmonManager struct {
conmonVersion *semver.Version
supportsSync bool
supportsLogGlobalSizeMax bool
}
// this function is heavily based on github.com/containers/common#probeConmon
func New(conmonPath string) (*ConmonManager, error) {
if !path.IsAbs(conmonPath) {
return nil, fmt.Errorf("conmon path is not absolute: %s", conmonPath)
}
out, err := cmdrunner.CombinedOutput(conmonPath, "--version")
if err != nil {
return nil, fmt.Errorf("get conmon version: %w", err)
}
fields := strings.Fields(string(out))
if len(fields) < 3 {
return nil, fmt.Errorf("conmon version output too short: expected three fields, got %d in %s", len(fields), out)
}
c := new(ConmonManager)
if err := c.parseConmonVersion(fields[2]); err != nil {
return nil, fmt.Errorf("get conmon version: %w", err)
}
c.initializeSupportsSync()
c.initializeSupportsLogGlobalSizeMax(conmonPath)
return c, nil
}
func (c *ConmonManager) parseConmonVersion(versionString string) error {
parsedVersion, err := semver.New(versionString)
if err != nil {
return err
}
c.conmonVersion = parsedVersion
return nil
}
func (c *ConmonManager) initializeSupportsLogGlobalSizeMax(conmonPath string) {
c.supportsLogGlobalSizeMax = c.conmonVersion.GTE(versionSupportsLogGlobalSizeMax)
if !c.supportsLogGlobalSizeMax {
// Read help output as a fallback in case the feature was backported to conmon,
// but the version wasn't bumped.
helpOutput, err := cmdrunner.CombinedOutput(conmonPath, "--help")
c.supportsLogGlobalSizeMax = err == nil && bytes.Contains(helpOutput, []byte("--log-global-size-max"))
}
verb := "does not"
if c.supportsLogGlobalSizeMax {
verb = "does"
}
logrus.Infof("Conmon %s support the --log-global-size-max option", verb)
}
func (c *ConmonManager) SupportsLogGlobalSizeMax() bool {
return c.supportsLogGlobalSizeMax
}
func (c *ConmonManager) initializeSupportsSync() {
c.supportsSync = c.conmonVersion.GTE(versionSupportsSync)
verb := "does not"
if c.supportsSync {
verb = "does"
}
logrus.Infof("Conmon %s support the --sync option", verb)
}
func (c *ConmonManager) SupportsSync() bool {
return c.supportsSync
}