88 "reflect"
99 "time"
1010
11+ "github.com/jetkvm/kvm/internal/logging"
1112 "github.com/prometheus/procfs"
13+ "github.com/rs/zerolog"
1214 "github.com/sourcegraph/tf-dag/dag"
1315)
1416
@@ -194,15 +196,15 @@ func (fc *FileChange) checkIfDirIsMountPoint() error {
194196}
195197
196198// GetActualState returns the actual state of the file at the given path.
197- func (fc * FileChange ) getActualState () error {
198- l := defaultLogger . With (). Str ("path" , fc .Path ). Logger ( )
199+ func (fc * FileChange ) getActualState (loggingContext * zerolog. Context ) error {
200+ context := loggingContext . Str ("path" , fc .Path )
199201
200202 fi , err := os .Lstat (fc .Path )
201203 if err != nil {
202204 if os .IsNotExist (err ) {
203205 fc .ActualState = FileStateAbsent
204206 } else {
205- l . Warn (). Err ( err ). Msg ( "failed to stat file" )
207+ _ = logging . LogWarnE ( context , err , "failed to stat file" )
206208 fc .ActualState = FileStateUnknown
207209 }
208210 return nil
@@ -214,15 +216,15 @@ func (fc *FileChange) getActualState() error {
214216 // get the target of the symlink
215217 target , err := os .Readlink (fc .Path )
216218 if err != nil {
217- l . Warn (). Err ( err ). Msg ( "failed to read symlink" )
219+ _ = logging . LogWarnE ( context , err , "failed to read symlink" )
218220 return fmt .Errorf ("failed to read symlink" )
219221 }
220222 // check if the target is a relative path
221223 if ! filepath .IsAbs (target ) {
222224 // make it absolute
223225 target , err = filepath .Abs (filepath .Join (filepath .Dir (fc .Path ), target ))
224226 if err != nil {
225- l . Warn (). Err ( err ). Msg ( "failed to make symlink target absolute" )
227+ _ = logging . LogWarnE ( context , err , "failed to make symlink target absolute" )
226228 return fmt .Errorf ("failed to make symlink target absolute" )
227229 }
228230 }
@@ -237,22 +239,20 @@ func (fc *FileChange) getActualState() error {
237239 case FileStateMountedConfigFS :
238240 err := fc .checkIfDirIsMountPoint ()
239241 if err != nil {
240- l .Warn ().Err (err ).Msg ("failed to check if dir is mount point" )
241- return err
242+ return logging .LogWarnE (context , err , "failed to check if dir is mount point" )
242243 }
243244 case FileStateSymlinkInOrderConfigFS :
244- state , err := checkIfSymlinksInOrder (fc , & l )
245+ state , err := checkIfSymlinksInOrder (fc , & context )
245246 if err != nil {
246- l .Warn ().Err (err ).Msg ("failed to check if symlinks are in order" )
247- return err
247+ return logging .LogWarnE (context , err , "failed to check if symlinks are in order" )
248248 }
249249 fc .ActualState = state
250250 }
251251 return nil
252252 }
253253
254254 if fi .Mode ()& os .ModeDevice == os .ModeDevice {
255- l . Info (). Msg ( "file is a device" )
255+ logging . LogInfo ( context , "file is a device" )
256256 return nil
257257 }
258258
@@ -262,15 +262,14 @@ func (fc *FileChange) getActualState() error {
262262 // get the content of the file
263263 content , err := os .ReadFile (fc .Path )
264264 if err != nil {
265- l . Warn (). Err ( err ). Msg ( "failed to read file" )
265+ logging . LogWarnE ( context , err , "failed to read file" )
266266 return fmt .Errorf ("failed to read file" )
267267 }
268268 fc .ActualContent = content
269269 return nil
270270 }
271271
272- l .Warn ().Interface ("file_info" , fi .Mode ()).Bool ("is_dir" , fi .IsDir ()).Msg ("unknown file type" )
273-
272+ logging .LogWarn (context .Interface ("file_info" , fi .Mode ()).Bool ("is_dir" , fi .IsDir ()), "unknown file type" )
274273 return fmt .Errorf ("unknown file type" )
275274}
276275
@@ -280,18 +279,16 @@ func (fc *FileChange) ResetActionResolution() {
280279 fc .changed = ChangeStateUnknown
281280}
282281
283- func (fc * FileChange ) Action () FileChangeResolvedAction {
282+ func (fc * FileChange ) Action (loggingContext * zerolog. Context ) FileChangeResolvedAction {
284283 if ! fc .checked {
285- fc .action = fc .getFileChangeResolvedAction ()
284+ fc .action = fc .getFileChangeResolvedAction (loggingContext )
286285 fc .checked = true
287286 }
288287
289288 return fc .action
290289}
291290
292- func (fc * FileChange ) getFileChangeResolvedAction () FileChangeResolvedAction {
293- l := defaultLogger .With ().Str ("path" , fc .Path ).Logger ()
294-
291+ func (fc * FileChange ) getFileChangeResolvedAction (loggingContext * zerolog.Context ) FileChangeResolvedAction {
295292 // some actions are not needed to be checked
296293 switch fc .ExpectedState {
297294 case FileStateFileWrite :
@@ -300,8 +297,10 @@ func (fc *FileChange) getFileChangeResolvedAction() FileChangeResolvedAction {
300297 return FileChangeResolvedActionTouch
301298 }
302299
300+ context := loggingContext .Interface ("expected_state" , FileStateString [fc .ExpectedState ])
301+
303302 // get the actual state of the file
304- err := fc .getActualState ()
303+ err := fc .getActualState (& context )
305304 if err != nil {
306305 return FileChangeResolvedActionDoNothing
307306 }
@@ -348,7 +347,7 @@ func (fc *FileChange) getFileChangeResolvedAction() FileChangeResolvedAction {
348347 }
349348 return FileChangeResolvedActionCreateSymlink
350349 case FileStateSymlinkInOrderConfigFS :
351- // if the file is already a symlink, check if the target is the same
350+ // if the file is already a symlink to configfs , check if the target is the same
352351 if fc .ActualState == FileStateSymlinkInOrderConfigFS {
353352 return FileChangeResolvedActionDoNothing
354353 }
@@ -364,7 +363,7 @@ func (fc *FileChange) getFileChangeResolvedAction() FileChangeResolvedAction {
364363 }
365364 return FileChangeResolvedActionMountConfigFS
366365 default :
367- l . Warn (). Interface ( "file_change" , FileStateString [ fc . ExpectedState ]). Msg ( "unknown expected state" )
366+ logging . LogWarn ( context , "unknown expected state" )
368367 return FileChangeResolvedActionDoNothing
369368 }
370369}
@@ -387,18 +386,19 @@ func (c *ChangeSet) AddFileChange(component string, path string, expectedState F
387386 })
388387}
389388
390- func (c * ChangeSet ) ApplyChanges () error {
389+ func (c * ChangeSet ) ApplyChanges (loggingContext * zerolog. Context ) error {
391390 r := ChangeSetResolver {
392391 changeset : c ,
393392 g : & dag.AcyclicGraph {},
394- l : defaultLogger ,
395393 }
396394
397- return r .Apply ()
395+ return r .Apply (loggingContext )
398396}
399397
400- func (c * ChangeSet ) applyChange (change * FileChange ) error {
401- switch change .Action () {
398+ func (c * ChangeSet ) applyChange (change * FileChange , loggingContext * zerolog.Context ) error {
399+ action := change .Action (loggingContext )
400+
401+ switch action {
402402 case FileChangeResolvedActionWriteFile :
403403 return os .WriteFile (change .Path , change .ExpectedContent , 0644 )
404404 case FileChangeResolvedActionUpdateFile :
@@ -413,7 +413,7 @@ func (c *ChangeSet) applyChange(change *FileChange) error {
413413 }
414414 return os .Symlink (string (change .ExpectedContent ), change .Path )
415415 case FileChangeResolvedActionReorderSymlinks :
416- return recreateSymlinks (change , nil )
416+ return recreateSymlinks (change , loggingContext )
417417 case FileChangeResolvedActionCreateDirectory :
418418 return os .MkdirAll (change .Path , 0755 )
419419 case FileChangeResolvedActionRemove :
@@ -427,10 +427,10 @@ func (c *ChangeSet) applyChange(change *FileChange) error {
427427 case FileChangeResolvedActionDoNothing :
428428 return nil
429429 default :
430- return fmt .Errorf ("unknown action: %d" , change . Action () )
430+ return fmt .Errorf ("unknown action: %d" , action )
431431 }
432432}
433433
434- func (c * ChangeSet ) Apply () error {
435- return c .ApplyChanges ()
434+ func (c * ChangeSet ) Apply (loggingContext * zerolog. Context ) error {
435+ return c .ApplyChanges (loggingContext )
436436}
0 commit comments