@@ -27,8 +27,7 @@ import (
27
27
"strings"
28
28
"time"
29
29
30
- "github.com/golang/glog"
31
-
30
+ "github.com/go-logr/logr"
32
31
"k8s.io/apimachinery/pkg/util/sets"
33
32
)
34
33
@@ -57,8 +56,8 @@ const (
57
56
// inotify or fanotify to receive events when the content in the volume is
58
57
// updated.
59
58
type AtomicWriter struct {
60
- targetDir string
61
- logContext string
59
+ targetDir string
60
+ log logr. Logger
62
61
}
63
62
64
63
type FileProjection struct {
@@ -68,13 +67,13 @@ type FileProjection struct {
68
67
69
68
// NewAtomicWriter creates a new AtomicWriter configured to write to the given
70
69
// target directory, or returns an error if the target directory does not exist.
71
- func NewAtomicWriter (targetDir string , logContext string ) (* AtomicWriter , error ) {
70
+ func NewAtomicWriter (targetDir string , log logr. Logger ) (* AtomicWriter , error ) {
72
71
_ , err := os .Stat (targetDir )
73
72
if os .IsNotExist (err ) {
74
73
return nil , err
75
74
}
76
75
77
- return & AtomicWriter {targetDir : targetDir , logContext : logContext }, nil
76
+ return & AtomicWriter {targetDir : targetDir , log : log }, nil
78
77
}
79
78
80
79
const (
@@ -120,7 +119,7 @@ func (w *AtomicWriter) Write(payload map[string]FileProjection) error {
120
119
// (1)
121
120
cleanPayload , err := validatePayload (payload )
122
121
if err != nil {
123
- glog . Errorf ( "%s: invalid payload: %v" , w . logContext , err )
122
+ w . log . Error ( err , " invalid payload" )
124
123
return err
125
124
}
126
125
@@ -129,7 +128,7 @@ func (w *AtomicWriter) Write(payload map[string]FileProjection) error {
129
128
oldTsDir , err := os .Readlink (dataDirPath )
130
129
if err != nil {
131
130
if ! os .IsNotExist (err ) {
132
- glog . Errorf ( "%s: error reading link for data directory: %v" , w . logContext , err )
131
+ w . log . Error ( err , "unable to read link for data directory" )
133
132
return err
134
133
}
135
134
// although Readlink() returns "" on err, don't be fragile by relying on it (since it's not specified in docs)
@@ -144,49 +143,49 @@ func (w *AtomicWriter) Write(payload map[string]FileProjection) error {
144
143
// (3)
145
144
pathsToRemove , err = w .pathsToRemove (cleanPayload , oldTsPath )
146
145
if err != nil {
147
- glog . Errorf ( "%s: error determining user-visible files to remove: %v" , w . logContext , err )
146
+ w . log . Error ( err , "unable to determine user-visible files to remove" )
148
147
return err
149
148
}
150
149
151
150
// (4)
152
151
if should , err := shouldWritePayload (cleanPayload , oldTsPath ); err != nil {
153
- glog . Errorf ( "%s: error determining whether payload should be written to disk: %v" , w . logContext , err )
152
+ w . log . Error ( err , "unable to determine whether payload should be written to disk" )
154
153
return err
155
154
} else if ! should && len (pathsToRemove ) == 0 {
156
- glog . V ( 4 ). Infof ( "%s: no update required for target directory %v " , w . logContext , w .targetDir )
155
+ w . log . V ( 1 ). Info ( " no update required for target directory" , "directory" , w .targetDir )
157
156
return nil
158
157
} else {
159
- glog . V ( 4 ). Infof ( "%s: write required for target directory %v " , w . logContext , w .targetDir )
158
+ w . log . V ( 1 ). Info ( " write required for target directory" , "directory" , w .targetDir )
160
159
}
161
160
}
162
161
163
162
// (5)
164
163
tsDir , err := w .newTimestampDir ()
165
164
if err != nil {
166
- glog . V ( 4 ). Infof ( "%s: error creating new ts data directory: %v" , w . logContext , err )
165
+ w . log . Error ( err , " error creating new ts data directory" )
167
166
return err
168
167
}
169
168
tsDirName := filepath .Base (tsDir )
170
169
171
170
// (6)
172
171
if err = w .writePayloadToDir (cleanPayload , tsDir ); err != nil {
173
- glog . Errorf ( "%s: error writing payload to ts data directory %s: %v " , w . logContext , tsDir , err )
172
+ w . log . Error ( err , "unable to write payload to ts data directory" , "ts directory" , tsDir )
174
173
return err
175
174
} else {
176
- glog . V ( 4 ). Infof ( "%s: performed write of new data to ts data directory: %s " , w . logContext , tsDir )
175
+ w . log . V ( 1 ). Info ( " performed write of new data to ts data directory" , "ts directory" , tsDir )
177
176
}
178
177
179
178
// (7)
180
179
if err = w .createUserVisibleFiles (cleanPayload ); err != nil {
181
- glog . Errorf ( "%s: error creating visible symlinks in %s: %v " , w . logContext , w .targetDir , err )
180
+ w . log . Error ( err , "unable to create visible symlinks in target directory " , "target directory" , w .targetDir )
182
181
return err
183
182
}
184
183
185
184
// (8)
186
185
newDataDirPath := path .Join (w .targetDir , newDataDirName )
187
186
if err = os .Symlink (tsDirName , newDataDirPath ); err != nil {
188
187
os .RemoveAll (tsDir )
189
- glog . Errorf ( "%s: error creating symbolic link for atomic update: %v" , w . logContext , err )
188
+ w . log . Error ( err , "unable to create symbolic link for atomic update" )
190
189
return err
191
190
}
192
191
@@ -201,20 +200,20 @@ func (w *AtomicWriter) Write(payload map[string]FileProjection) error {
201
200
if err != nil {
202
201
os .Remove (newDataDirPath )
203
202
os .RemoveAll (tsDir )
204
- glog . Errorf ( "%s: error renaming symbolic link for data directory %s: %v " , w . logContext , newDataDirPath , err )
203
+ w . log . Error ( err , "unable to rename symbolic link for data directory" , "data directory" , newDataDirPath )
205
204
return err
206
205
}
207
206
208
207
// (10)
209
208
if err = w .removeUserVisiblePaths (pathsToRemove ); err != nil {
210
- glog . Errorf ( "%s: error removing old visible symlinks: %v" , w . logContext , err )
209
+ w . log . Error ( err , "unable to remove old visible symlinks" )
211
210
return err
212
211
}
213
212
214
213
// (11)
215
214
if len (oldTsDir ) > 0 {
216
215
if err = os .RemoveAll (oldTsPath ); err != nil {
217
- glog . Errorf ( "%s: error removing old data directory %s: %v " , w . logContext , oldTsDir , err )
216
+ w . log . Error ( err , "unable to remove old data directory" , "data directory" , oldTsDir )
218
217
return err
219
218
}
220
219
}
@@ -329,7 +328,7 @@ func (w *AtomicWriter) pathsToRemove(payload map[string]FileProjection, oldTsDir
329
328
} else if err != nil {
330
329
return nil , err
331
330
}
332
- glog . V ( 5 ). Infof ( "%s: current paths: %+v " , w .targetDir , paths .List ())
331
+ w . log . V ( 1 ). Info ( " current paths" , "target directory " , w .targetDir , "paths" , paths .List ())
333
332
334
333
newPaths := sets .NewString ()
335
334
for file := range payload {
@@ -341,10 +340,10 @@ func (w *AtomicWriter) pathsToRemove(payload map[string]FileProjection, oldTsDir
341
340
subPath = strings .TrimSuffix (subPath , string (os .PathSeparator ))
342
341
}
343
342
}
344
- glog . V ( 5 ). Infof ( "%s: new paths: %+v " , w .targetDir , newPaths .List ())
343
+ w . log . V ( 1 ). Info ( " new paths" , "target directory " , w .targetDir , "paths" , newPaths .List ())
345
344
346
345
result := paths .Difference (newPaths )
347
- glog . V ( 5 ). Infof ( "%s: paths to remove: %+v" , w .targetDir , result )
346
+ w . log . V ( 1 ). Info ( " paths to remove" , "target directory" , w .targetDir , "paths" , result )
348
347
349
348
return result , nil
350
349
}
@@ -353,7 +352,7 @@ func (w *AtomicWriter) pathsToRemove(payload map[string]FileProjection, oldTsDir
353
352
func (w * AtomicWriter ) newTimestampDir () (string , error ) {
354
353
tsDir , err := ioutil .TempDir (w .targetDir , time .Now ().UTC ().Format ("..2006_01_02_15_04_05." ))
355
354
if err != nil {
356
- glog . Errorf ( "%s: unable to create new temp directory: %v" , w . logContext , err )
355
+ w . log . Error ( err , " unable to create new temp directory" )
357
356
return "" , err
358
357
}
359
358
@@ -362,7 +361,7 @@ func (w *AtomicWriter) newTimestampDir() (string, error) {
362
361
// regardless of the process' umask.
363
362
err = os .Chmod (tsDir , 0755 )
364
363
if err != nil {
365
- glog . Errorf ( "%s: unable to set mode on new temp directory: %v" , w . logContext , err )
364
+ w . log . Error ( err , " unable to set mode on new temp directory" )
366
365
return "" , err
367
366
}
368
367
@@ -380,13 +379,13 @@ func (w *AtomicWriter) writePayloadToDir(payload map[string]FileProjection, dir
380
379
381
380
err := os .MkdirAll (baseDir , os .ModePerm )
382
381
if err != nil {
383
- glog . Errorf ( "%s: unable to create directory %s: %v " , w . logContext , baseDir , err )
382
+ w . log . Error ( err , " unable to create directory" , "directory" , baseDir )
384
383
return err
385
384
}
386
385
387
386
err = ioutil .WriteFile (fullPath , content , mode )
388
387
if err != nil {
389
- glog . Errorf ( "%s: unable to write file %s with mode %v: %v " , w . logContext , fullPath , mode , err )
388
+ w . log . Error ( err , " unable to write file" , "file" , fullPath , " mode" , mode )
390
389
return err
391
390
}
392
391
// Chmod is needed because ioutil.WriteFile() ends up calling
@@ -395,7 +394,7 @@ func (w *AtomicWriter) writePayloadToDir(payload map[string]FileProjection, dir
395
394
// in the file no matter what the umask is.
396
395
err = os .Chmod (fullPath , mode )
397
396
if err != nil {
398
- glog . Errorf ( "%s: unable to write file %s with mode %v: %v " , w . logContext , fullPath , mode , err )
397
+ w . log . Error ( err , " unable to write file" , "file" , fullPath , " mode" , mode )
399
398
}
400
399
}
401
400
@@ -445,7 +444,7 @@ func (w *AtomicWriter) removeUserVisiblePaths(paths sets.String) error {
445
444
continue
446
445
}
447
446
if err := os .Remove (path .Join (w .targetDir , p )); err != nil {
448
- glog . Errorf ( "%s: error pruning old user-visible path %s: %v " , w . logContext , p , err )
447
+ w . log . Error ( err , "unable to prune old user-visible path" , "path" , p )
449
448
lasterr = err
450
449
}
451
450
}
0 commit comments