forked from kata-containers/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
124 lines (101 loc) · 2.43 KB
/
config.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// Copyright (c) 2017-2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"io/ioutil"
"strings"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
grpcStatus "google.golang.org/grpc/status"
)
const (
optionPrefix = "agent."
logLevelFlag = optionPrefix + "log"
devModeFlag = optionPrefix + "devmode"
traceModeFlag = optionPrefix + "trace"
kernelCmdlineFile = "/proc/cmdline"
traceValueIsolated = "isolated"
traceValueCollated = "collated"
)
type agentConfig struct {
logLevel logrus.Level
}
func newConfig(level logrus.Level) agentConfig {
return agentConfig{
logLevel: level,
}
}
//Get the agent configuration from kernel cmdline
func (c *agentConfig) getConfig(cmdLineFile string) error {
if cmdLineFile == "" {
return grpcStatus.Error(codes.FailedPrecondition, "Kernel cmdline file cannot be empty")
}
kernelCmdline, err := ioutil.ReadFile(cmdLineFile)
if err != nil {
return err
}
words := strings.Fields(string(kernelCmdline))
for _, word := range words {
if err := c.parseCmdlineOption(word); err != nil {
agentLog.WithFields(logrus.Fields{
"error": err,
"option": word,
}).Warn("Failed to parse kernel option")
}
}
return nil
}
//Parse a string that represents a kernel cmdline option
func (c *agentConfig) parseCmdlineOption(option string) error {
const (
optionPosition = iota
valuePosition
optionSeparator = "="
)
if option == devModeFlag {
crashOnError = true
debug = true
return nil
}
if option == traceModeFlag {
enableTracing(false)
return nil
}
split := strings.Split(option, optionSeparator)
if len(split) < valuePosition+1 {
return nil
}
switch split[optionPosition] {
case logLevelFlag:
level, err := logrus.ParseLevel(split[valuePosition])
if err != nil {
return err
}
c.logLevel = level
if level == logrus.DebugLevel {
debug = true
}
case traceModeFlag:
switch split[valuePosition] {
case traceValueIsolated:
enableTracing(false)
case traceValueCollated:
enableTracing(true)
}
default:
if strings.HasPrefix(split[optionPosition], optionPrefix) {
return grpcStatus.Errorf(codes.NotFound, "Unknown option %s", split[optionPosition])
}
}
return nil
}
func enableTracing(enableCollatedTrace bool) {
agentLog.Info("enabling tracing")
tracing = true
// Enable in case this generates more trace spans
debug = true
collatedTrace = enableCollatedTrace
}