@@ -42,7 +42,7 @@ type ioStore interface {
42
42
43
43
// updateLogLevel updates log level and persists it to disk.
44
44
func updateLogLevel (ctx context.Context , level string ) error {
45
- ai , err := loadAgentInfoWithBackoff (ctx , false , defaultLogLevel , false )
45
+ ai , _ , err := loadAgentInfoWithBackoff (ctx , false , defaultLogLevel , false )
46
46
if err != nil {
47
47
return err
48
48
}
@@ -71,51 +71,65 @@ func generateAgentID() (string, error) {
71
71
return uid .String (), nil
72
72
}
73
73
74
- func getInfoFromStore (s ioStore , logLevel string ) (* persistentAgentInfo , error ) {
74
+ // getInfoFromStore uses the IO store to return the config from agent.* fields in the config,
75
+ // as well as a bool indicating if agent is running in standalone mode.
76
+ func getInfoFromStore (s ioStore , logLevel string ) (* persistentAgentInfo , bool , error ) {
75
77
agentConfigFile := paths .AgentConfigFile ()
76
78
reader , err := s .Load ()
77
79
if err != nil {
78
- return nil , fmt .Errorf ("failed to load from ioStore: %w" , err )
80
+ return nil , false , fmt .Errorf ("failed to load from ioStore: %w" , err )
79
81
}
80
82
81
83
// reader is closed by this function
82
84
cfg , err := config .NewConfigFrom (reader )
83
85
if err != nil {
84
- return nil , errors .New (err ,
86
+ return nil , false , errors .New (err ,
85
87
fmt .Sprintf ("fail to read configuration %s for the agent" , agentConfigFile ),
86
88
errors .TypeFilesystem ,
87
89
errors .M (errors .MetaKeyPath , agentConfigFile ))
88
90
}
89
91
90
92
configMap , err := cfg .ToMapStr ()
91
93
if err != nil {
92
- return nil , errors .New (err ,
94
+ return nil , false , errors .New (err ,
93
95
"failed to unpack stored config to map" ,
94
96
errors .TypeFilesystem )
95
97
}
96
98
99
+ // check fleet config. This behavior emulates configuration.IsStandalone
100
+ fleetmode , fleetExists := configMap ["fleet" ]
101
+ isStandalone := true
102
+ if fleetExists {
103
+ fleetCfg , ok := fleetmode .(map [string ]interface {})
104
+ if ok {
105
+ if fleetCfg ["enabled" ] == true {
106
+ isStandalone = false
107
+ }
108
+ }
109
+ }
110
+
97
111
agentInfoSubMap , found := configMap [agentInfoKey ]
98
112
if ! found {
99
113
return & persistentAgentInfo {
100
114
LogLevel : logLevel ,
101
115
MonitoringHTTP : monitoringConfig .DefaultConfig ().HTTP ,
102
- }, nil
116
+ }, isStandalone , nil
103
117
}
104
118
105
119
cc , err := config .NewConfigFrom (agentInfoSubMap )
106
120
if err != nil {
107
- return nil , errors .New (err , "failed to create config from agent info submap" )
121
+ return nil , false , errors .New (err , "failed to create config from agent info submap" )
108
122
}
109
123
110
124
pid := & persistentAgentInfo {
111
125
LogLevel : logLevel ,
112
126
MonitoringHTTP : monitoringConfig .DefaultConfig ().HTTP ,
113
127
}
114
128
if err := cc .Unpack (& pid ); err != nil {
115
- return nil , errors .New (err , "failed to unpack stored config to map" )
129
+ return nil , false , errors .New (err , "failed to unpack stored config to map" )
116
130
}
117
131
118
- return pid , nil
132
+ return pid , isStandalone , nil
119
133
}
120
134
121
135
func updateAgentInfo (s ioStore , agentInfo * persistentAgentInfo ) error {
@@ -177,53 +191,54 @@ func yamlToReader(in interface{}) (io.Reader, error) {
177
191
return bytes .NewReader (data ), nil
178
192
}
179
193
180
- func loadAgentInfoWithBackoff (ctx context.Context , forceUpdate bool , logLevel string , createAgentID bool ) (* persistentAgentInfo , error ) {
194
+ func loadAgentInfoWithBackoff (ctx context.Context , forceUpdate bool , logLevel string , createAgentID bool ) (* persistentAgentInfo , bool , error ) {
181
195
var err error
182
196
var ai * persistentAgentInfo
197
+ var isStandalone bool
183
198
184
199
signal := make (chan struct {})
185
200
backExp := backoff .NewExpBackoff (signal , 100 * time .Millisecond , 3 * time .Second )
186
201
187
202
for i := 0 ; i <= maxRetriesloadAgentInfo ; i ++ {
188
203
backExp .Wait ()
189
- ai , err = loadAgentInfo (ctx , forceUpdate , logLevel , createAgentID )
204
+ ai , isStandalone , err = loadAgentInfo (ctx , forceUpdate , logLevel , createAgentID )
190
205
if ! errors .Is (err , filelock .ErrAppAlreadyRunning ) {
191
206
break
192
207
}
193
208
}
194
209
195
210
close (signal )
196
- return ai , err
211
+ return ai , isStandalone , err
197
212
}
198
213
199
- func loadAgentInfo (ctx context.Context , forceUpdate bool , logLevel string , createAgentID bool ) (* persistentAgentInfo , error ) {
214
+ func loadAgentInfo (ctx context.Context , forceUpdate bool , logLevel string , createAgentID bool ) (* persistentAgentInfo , bool , error ) {
200
215
idLock := paths .AgentConfigFileLock ()
201
216
if err := idLock .TryLock (); err != nil {
202
- return nil , err
217
+ return nil , false , err
203
218
}
204
219
//nolint:errcheck // keeping the same behavior, and making linter happy
205
220
defer idLock .Unlock ()
206
221
207
222
agentConfigFile := paths .AgentConfigFile ()
208
223
diskStore , err := storage .NewEncryptedDiskStore (ctx , agentConfigFile )
209
224
if err != nil {
210
- return nil , fmt .Errorf ("error instantiating encrypted disk store: %w" , err )
225
+ return nil , false , fmt .Errorf ("error instantiating encrypted disk store: %w" , err )
211
226
}
212
227
213
- agentInfo , err := getInfoFromStore (diskStore , logLevel )
228
+ agentInfo , isStandalone , err := getInfoFromStore (diskStore , logLevel )
214
229
if err != nil {
215
- return nil , fmt .Errorf ("could not get agent info from store: %w" , err )
230
+ return nil , false , fmt .Errorf ("could not get agent info from store: %w" , err )
216
231
}
217
232
218
233
if agentInfo != nil && ! forceUpdate && (agentInfo .ID != "" || ! createAgentID ) {
219
- return agentInfo , nil
234
+ return agentInfo , isStandalone , nil
220
235
}
221
236
222
237
if err := updateID (agentInfo , diskStore ); err != nil {
223
- return nil , fmt .Errorf ("could not update agent ID on disk store: %w" , err )
238
+ return nil , false , fmt .Errorf ("could not update agent ID on disk store: %w" , err )
224
239
}
225
240
226
- return agentInfo , nil
241
+ return agentInfo , isStandalone , nil
227
242
}
228
243
229
244
func updateID (agentInfo * persistentAgentInfo , s ioStore ) error {
0 commit comments