forked from kata-containers/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
176 lines (152 loc) · 3.96 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// Copyright (c) 2017-2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"io/ioutil"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
grpcStatus "google.golang.org/grpc/status"
)
const (
optionPrefix = "agent."
logLevelFlag = optionPrefix + "log"
logsVSockPortFlag = optionPrefix + "log_vport"
devModeFlag = optionPrefix + "devmode"
traceModeFlag = optionPrefix + "trace"
useVsockFlag = optionPrefix + "use_vsock"
debugConsoleFlag = optionPrefix + "debug_console"
debugConsoleVPortFlag = optionPrefix + "debug_console_vport"
hotplugTimeoutFlag = optionPrefix + "hotplug_timeout"
kernelCmdlineFile = "/proc/cmdline"
traceModeStatic = "static"
traceModeDynamic = "dynamic"
traceTypeIsolated = "isolated"
traceTypeCollated = "collated"
defaultTraceType = traceTypeIsolated
)
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 == debugConsoleFlag {
debugConsole = true
return nil
}
if option == traceModeFlag {
enableTracing(traceModeStatic, defaultTraceType)
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 logsVSockPortFlag:
port, err := strconv.ParseUint(split[valuePosition], 10, 32)
if err != nil {
return err
}
logsVSockPort = uint32(port)
case debugConsoleVPortFlag:
port, err := strconv.ParseUint(split[valuePosition], 10, 32)
if err != nil {
return err
}
debugConsole = true
debugConsoleVSockPort = uint32(port)
case hotplugTimeoutFlag:
timeout, err := time.ParseDuration(split[valuePosition])
if err != nil {
return err
}
// Only use the provided timeout if a positive value is provided
if timeout > 0 {
hotplugTimeout = timeout
}
case traceModeFlag:
switch split[valuePosition] {
case traceTypeIsolated:
enableTracing(traceModeStatic, traceTypeIsolated)
case traceTypeCollated:
enableTracing(traceModeStatic, traceTypeCollated)
}
case useVsockFlag:
flag, err := strconv.ParseBool(split[valuePosition])
if err != nil {
return err
}
if flag {
agentLog.Debug("Param passed to use vsock channel")
commCh = vsockCh
} else {
agentLog.Debug("Param passed to NOT use vsock channel")
commCh = serialCh
}
default:
if strings.HasPrefix(split[optionPosition], optionPrefix) {
return grpcStatus.Errorf(codes.NotFound, "Unknown option %s", split[optionPosition])
}
}
return nil
}
func enableTracing(traceMode, traceType string) {
tracing = true
// Enable in case this generates more trace spans
debug = true
collatedTrace = traceType == traceTypeCollated
agentLog.WithFields(logrus.Fields{
"trace-mode": traceMode,
"trace-type": traceType,
}).Info("enabled tracing")
}