7
7
"fmt"
8
8
"io"
9
9
"io/ioutil"
10
- "log"
11
10
"net/http"
12
11
_ "net/http/pprof"
13
12
"net/url"
@@ -18,6 +17,7 @@ import (
18
17
19
18
"github.com/cenkalti/backoff"
20
19
"github.com/hashicorp/go-multierror"
20
+ "github.com/jetstack/preflight/pkg/logs"
21
21
json "github.com/json-iterator/go"
22
22
"github.com/prometheus/client_golang/prometheus"
23
23
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -86,23 +86,23 @@ func Run(cmd *cobra.Command, args []string) {
86
86
config , preflightClient := getConfiguration ()
87
87
88
88
if Profiling {
89
- log .Printf ("pprof profiling was enabled.\n Running profiling on port :6060" )
89
+ logs . Log .Printf ("pprof profiling was enabled.\n Running profiling on port :6060" )
90
90
go func () {
91
91
err := http .ListenAndServe (":6060" , nil )
92
92
if err != nil && ! errors .Is (err , http .ErrServerClosed ) {
93
- log .Fatalf ("failed to run pprof profiler: %s" , err )
93
+ logs . Log .Fatalf ("failed to run pprof profiler: %s" , err )
94
94
}
95
95
}()
96
96
}
97
97
if Prometheus {
98
- log .Printf ("Prometheus was enabled.\n Running prometheus server on port :8081" )
98
+ logs . Log .Printf ("Prometheus was enabled.\n Running prometheus server on port :8081" )
99
99
go func () {
100
100
prometheus .MustRegister (metricPayloadSize )
101
101
metricsServer := http .NewServeMux ()
102
102
metricsServer .Handle ("/metrics" , promhttp .Handler ())
103
103
err := http .ListenAndServe (":8081" , metricsServer )
104
104
if err != nil && ! errors .Is (err , http .ErrServerClosed ) {
105
- log .Fatalf ("failed to run prometheus server: %s" , err )
105
+ logs . Log .Fatalf ("failed to run prometheus server: %s" , err )
106
106
}
107
107
}()
108
108
}
@@ -115,19 +115,19 @@ func Run(cmd *cobra.Command, args []string) {
115
115
kind := dgConfig .Kind
116
116
if dgConfig .DataPath != "" {
117
117
kind = "local"
118
- log .Fatalf ("running data gatherer %s of type %s as Local, data-path override present: %s" , dgConfig .Name , dgConfig .Kind , dgConfig .DataPath )
118
+ logs . Log .Fatalf ("running data gatherer %s of type %s as Local, data-path override present: %s" , dgConfig .Name , dgConfig .Kind , dgConfig .DataPath )
119
119
}
120
120
121
121
newDg , err := dgConfig .Config .NewDataGatherer (ctx )
122
122
if err != nil {
123
- log .Fatalf ("failed to instantiate %q data gatherer %q: %v" , kind , dgConfig .Name , err )
123
+ logs . Log .Fatalf ("failed to instantiate %q data gatherer %q: %v" , kind , dgConfig .Name , err )
124
124
}
125
125
126
- log .Printf ("starting %q datagatherer" , dgConfig .Name )
126
+ logs . Log .Printf ("starting %q datagatherer" , dgConfig .Name )
127
127
128
128
// start the data gatherers and wait for the cache sync
129
129
if err := newDg .Run (ctx .Done ()); err != nil {
130
- log .Printf ("failed to start %q data gatherer %q: %v" , kind , dgConfig .Name , err )
130
+ logs . Log .Printf ("failed to start %q data gatherer %q: %v" , kind , dgConfig .Name , err )
131
131
}
132
132
133
133
// bootCtx is a context with a timeout to allow the informer 5
@@ -142,7 +142,7 @@ func Run(cmd *cobra.Command, args []string) {
142
142
// the run.
143
143
if err := newDg .WaitForCacheSync (bootCtx .Done ()); err != nil {
144
144
// log sync failure, this might recover in future
145
- log .Printf ("failed to complete initial sync of %q data gatherer %q: %v" , kind , dgConfig .Name , err )
145
+ logs . Log .Printf ("failed to complete initial sync of %q data gatherer %q: %v" , kind , dgConfig .Name , err )
146
146
}
147
147
148
148
// regardless of success, this dataGatherers has been given a
@@ -157,14 +157,14 @@ func Run(cmd *cobra.Command, args []string) {
157
157
c := make (chan struct {})
158
158
go func () {
159
159
defer close (c )
160
- log .Printf ("waiting for datagatherers to complete inital syncs" )
160
+ logs . Log .Printf ("waiting for datagatherers to complete inital syncs" )
161
161
wg .Wait ()
162
162
}()
163
163
select {
164
164
case <- c :
165
- log .Printf ("datagatherers inital sync completed" )
165
+ logs . Log .Printf ("datagatherers inital sync completed" )
166
166
case <- time .After (60 * time .Second ):
167
- log .Fatalf ("datagatherers inital sync failed due to timeout of 60 seconds" )
167
+ logs . Log .Fatalf ("datagatherers inital sync failed due to timeout of 60 seconds" )
168
168
}
169
169
170
170
// begin the datagathering loop, periodically sending data to the
@@ -173,7 +173,7 @@ func Run(cmd *cobra.Command, args []string) {
173
173
for {
174
174
// if period is set in the config, then use that if not already set
175
175
if Period == 0 && config .Period > 0 {
176
- log .Printf ("Using period from config %s" , config .Period )
176
+ logs . Log .Printf ("Using period from config %s" , config .Period )
177
177
Period = config .Period
178
178
}
179
179
@@ -188,16 +188,16 @@ func Run(cmd *cobra.Command, args []string) {
188
188
}
189
189
190
190
func getConfiguration () (Config , client.Client ) {
191
- log .Printf ("Preflight agent version: %s (%s)" , version .PreflightVersion , version .Commit )
191
+ logs . Log .Printf ("Preflight agent version: %s (%s)" , version .PreflightVersion , version .Commit )
192
192
file , err := os .Open (ConfigFilePath )
193
193
if err != nil {
194
- log .Fatalf ("Failed to load config file for agent from: %s" , ConfigFilePath )
194
+ logs . Log .Fatalf ("Failed to load config file for agent from: %s" , ConfigFilePath )
195
195
}
196
196
defer file .Close ()
197
197
198
198
b , err := ioutil .ReadAll (file )
199
199
if err != nil {
200
- log .Fatalf ("Failed to read config file: %s" , err )
200
+ logs . Log .Fatalf ("Failed to read config file: %s" , err )
201
201
}
202
202
203
203
// If the ClientID of the service account is specified, then assume we are in Venafi Cloud mode.
@@ -207,29 +207,29 @@ func getConfiguration() (Config, client.Client) {
207
207
208
208
config , err := ParseConfig (b , VenafiCloudMode )
209
209
if err != nil {
210
- log .Fatalf ("Failed to parse config file: %s" , err )
210
+ logs . Log .Fatalf ("Failed to parse config file: %s" , err )
211
211
}
212
212
213
213
baseURL := config .Server
214
214
if baseURL == "" {
215
- log .Printf ("Using deprecated Endpoint configuration. User Server instead." )
215
+ logs . Log .Printf ("Using deprecated Endpoint configuration. User Server instead." )
216
216
baseURL = fmt .Sprintf ("%s://%s" , config .Endpoint .Protocol , config .Endpoint .Host )
217
217
_ , err = url .Parse (baseURL )
218
218
if err != nil {
219
- log .Fatalf ("Failed to build URL: %s" , err )
219
+ logs . Log .Fatalf ("Failed to build URL: %s" , err )
220
220
}
221
221
}
222
222
223
223
if Period == 0 && config .Period == 0 && ! OneShot {
224
- log .Fatalf ("Failed to load period, must be set as flag or in config" )
224
+ logs . Log .Fatalf ("Failed to load period, must be set as flag or in config" )
225
225
}
226
226
227
227
dump , err := config .Dump ()
228
228
if err != nil {
229
- log .Fatalf ("Failed to dump config: %s" , err )
229
+ logs . Log .Fatalf ("Failed to dump config: %s" , err )
230
230
}
231
231
232
- log .Printf ("Loaded config: \n %s" , dump )
232
+ logs . Log .Printf ("Loaded config: \n %s" , dump )
233
233
234
234
var credentials client.Credentials
235
235
if ClientID != "" {
@@ -240,21 +240,21 @@ func getConfiguration() (Config, client.Client) {
240
240
} else if CredentialsPath != "" {
241
241
file , err = os .Open (CredentialsPath )
242
242
if err != nil {
243
- log .Fatalf ("Failed to load credentials from file %s" , CredentialsPath )
243
+ logs . Log .Fatalf ("Failed to load credentials from file %s" , CredentialsPath )
244
244
}
245
245
defer file .Close ()
246
246
247
247
b , err = io .ReadAll (file )
248
248
if err != nil {
249
- log .Fatalf ("Failed to read credentials file: %v" , err )
249
+ logs . Log .Fatalf ("Failed to read credentials file: %v" , err )
250
250
}
251
251
if VenafiCloudMode {
252
252
credentials , err = client .ParseVenafiCredentials (b )
253
253
} else {
254
254
credentials , err = client .ParseOAuthCredentials (b )
255
255
}
256
256
if err != nil {
257
- log .Fatalf ("Failed to parse credentials file: %s" , err )
257
+ logs . Log .Fatalf ("Failed to parse credentials file: %s" , err )
258
258
}
259
259
}
260
260
@@ -268,15 +268,15 @@ func getConfiguration() (Config, client.Client) {
268
268
case credentials != nil :
269
269
preflightClient , err = createCredentialClient (credentials , config , agentMetadata , baseURL )
270
270
case APIToken != "" :
271
- log .Println ("An API token was specified, using API token authentication." )
271
+ logs . Log .Println ("An API token was specified, using API token authentication." )
272
272
preflightClient , err = client .NewAPITokenClient (agentMetadata , APIToken , baseURL )
273
273
default :
274
- log .Println ("No credentials were specified, using with no authentication." )
274
+ logs . Log .Println ("No credentials were specified, using with no authentication." )
275
275
preflightClient , err = client .NewUnauthenticatedClient (agentMetadata , baseURL )
276
276
}
277
277
278
278
if err != nil {
279
- log .Fatalf ("failed to create client: %v" , err )
279
+ logs . Log .Fatalf ("failed to create client: %v" , err )
280
280
}
281
281
282
282
return config , preflightClient
@@ -285,19 +285,19 @@ func getConfiguration() (Config, client.Client) {
285
285
func createCredentialClient (credentials client.Credentials , config Config , agentMetadata * api.AgentMetadata , baseURL string ) (client.Client , error ) {
286
286
switch creds := credentials .(type ) {
287
287
case * client.VenafiSvcAccountCredentials :
288
- log .Println ("Venafi Cloud mode was specified, using Venafi Service Account authentication." )
288
+ logs . Log .Println ("Venafi Cloud mode was specified, using Venafi Service Account authentication." )
289
289
// check if config has Venafi Cloud data, use config data if it's present
290
290
uploaderID := creds .ClientID
291
291
uploadPath := ""
292
292
if config .VenafiCloud != nil {
293
- log .Println ("Loading uploader_id and upload_path from \" venafi-cloud\" configuration." )
293
+ logs . Log .Println ("Loading uploader_id and upload_path from \" venafi-cloud\" configuration." )
294
294
uploaderID = config .VenafiCloud .UploaderID
295
295
uploadPath = config .VenafiCloud .UploadPath
296
296
}
297
297
return client .NewVenafiCloudClient (agentMetadata , creds , baseURL , uploaderID , uploadPath )
298
298
299
299
case * client.OAuthCredentials :
300
- log .Println ("A credentials file was specified, using oauth authentication." )
300
+ logs . Log .Println ("A credentials file was specified, using oauth authentication." )
301
301
return client .NewOAuthClient (agentMetadata , creds , baseURL )
302
302
default :
303
303
return nil , errors .New ("credentials file is in unknown format" )
@@ -316,14 +316,14 @@ func gatherAndOutputData(config Config, preflightClient client.Client, dataGathe
316
316
}
317
317
318
318
if InputPath != "" {
319
- log .Printf ("Reading data from local file: %s" , InputPath )
319
+ logs . Log .Printf ("Reading data from local file: %s" , InputPath )
320
320
data , err := ioutil .ReadFile (InputPath )
321
321
if err != nil {
322
- log .Fatalf ("failed to read local data file: %s" , err )
322
+ logs . Log .Fatalf ("failed to read local data file: %s" , err )
323
323
}
324
324
err = json .Unmarshal (data , & readings )
325
325
if err != nil {
326
- log .Fatalf ("failed to unmarshal local data file: %s" , err )
326
+ logs . Log .Fatalf ("failed to unmarshal local data file: %s" , err )
327
327
}
328
328
} else {
329
329
readings = gatherData (config , dataGatherers )
@@ -332,13 +332,13 @@ func gatherAndOutputData(config Config, preflightClient client.Client, dataGathe
332
332
if OutputPath != "" {
333
333
data , err := json .MarshalIndent (readings , "" , " " )
334
334
if err != nil {
335
- log .Fatal ("failed to marshal JSON" )
335
+ logs . Log .Fatal ("failed to marshal JSON" )
336
336
}
337
337
err = ioutil .WriteFile (OutputPath , data , 0644 )
338
338
if err != nil {
339
- log .Fatalf ("failed to output to local file: %s" , err )
339
+ logs . Log .Fatalf ("failed to output to local file: %s" , err )
340
340
}
341
- log .Printf ("Data saved to local file: %s" , OutputPath )
341
+ logs . Log .Printf ("Data saved to local file: %s" , OutputPath )
342
342
} else {
343
343
backOff := backoff .NewExponentialBackOff ()
344
344
backOff .InitialInterval = 30 * time .Second
@@ -348,10 +348,10 @@ func gatherAndOutputData(config Config, preflightClient client.Client, dataGathe
348
348
return postData (config , preflightClient , readings )
349
349
}
350
350
err := backoff .RetryNotify (post , backOff , func (err error , t time.Duration ) {
351
- log .Printf ("retrying in %v after error: %s" , t , err )
351
+ logs . Log .Printf ("retrying in %v after error: %s" , t , err )
352
352
})
353
353
if err != nil {
354
- log .Fatalf ("Exiting due to fatal error uploading: %v" , err )
354
+ logs . Log .Fatalf ("Exiting due to fatal error uploading: %v" , err )
355
355
}
356
356
357
357
}
@@ -370,9 +370,9 @@ func gatherData(config Config, dataGatherers map[string]datagatherer.DataGathere
370
370
}
371
371
372
372
if count >= 0 {
373
- log .Printf ("successfully gathered %d items from %q datagatherer" , count , k )
373
+ logs . Log .Printf ("successfully gathered %d items from %q datagatherer" , count , k )
374
374
} else {
375
- log .Printf ("successfully gathered data from %q datagatherer" , k )
375
+ logs . Log .Printf ("successfully gathered data from %q datagatherer" , k )
376
376
}
377
377
readings = append (readings , & api.DataReading {
378
378
ClusterID : config .ClusterID ,
@@ -396,7 +396,7 @@ func gatherData(config Config, dataGatherers map[string]datagatherer.DataGathere
396
396
}
397
397
398
398
if StrictMode && dgError .ErrorOrNil () != nil {
399
- log .Fatalf ("halting datagathering in strict mode due to error: %s" , dgError .ErrorOrNil ())
399
+ logs . Log .Fatalf ("halting datagathering in strict mode due to error: %s" , dgError .ErrorOrNil ())
400
400
}
401
401
402
402
return readings
@@ -405,7 +405,7 @@ func gatherData(config Config, dataGatherers map[string]datagatherer.DataGathere
405
405
func postData (config Config , preflightClient client.Client , readings []* api.DataReading ) error {
406
406
baseURL := config .Server
407
407
408
- log .Println ("Posting data to:" , baseURL )
408
+ logs . Log .Println ("Posting data to:" , baseURL )
409
409
410
410
if VenafiCloudMode {
411
411
// orgID and clusterID are not required for Venafi Cloud auth
@@ -416,23 +416,23 @@ func postData(config Config, preflightClient client.Client, readings []*api.Data
416
416
if err != nil {
417
417
return fmt .Errorf ("post to server failed: %+v" , err )
418
418
}
419
- log .Println ("Data sent successfully." )
419
+ logs . Log .Println ("Data sent successfully." )
420
420
421
421
return nil
422
422
}
423
423
424
424
if config .OrganizationID == "" {
425
425
data , err := json .Marshal (readings )
426
426
if err != nil {
427
- log .Fatalf ("Cannot marshal readings: %+v" , err )
427
+ logs . Log .Fatalf ("Cannot marshal readings: %+v" , err )
428
428
}
429
429
430
430
// log and collect metrics about the upload size
431
431
metric := metricPayloadSize .With (
432
432
prometheus.Labels {"organization" : config .OrganizationID , "cluster" : config .ClusterID },
433
433
)
434
434
metric .Set (float64 (len (data )))
435
- log .Printf ("Data readings upload size: %d" , len (data ))
435
+ logs . Log .Printf ("Data readings upload size: %d" , len (data ))
436
436
path := config .Endpoint .Path
437
437
if path == "" {
438
438
path = "/api/v1/datareadings"
@@ -452,7 +452,7 @@ func postData(config Config, preflightClient client.Client, readings []*api.Data
452
452
453
453
return fmt .Errorf ("received response with status code %d. Body: [%s]" , code , errorContent )
454
454
}
455
- log .Println ("Data sent successfully." )
455
+ logs . Log .Println ("Data sent successfully." )
456
456
return err
457
457
}
458
458
@@ -464,7 +464,7 @@ func postData(config Config, preflightClient client.Client, readings []*api.Data
464
464
if err != nil {
465
465
return fmt .Errorf ("post to server failed: %+v" , err )
466
466
}
467
- log .Println ("Data sent successfully." )
467
+ logs . Log .Println ("Data sent successfully." )
468
468
469
469
return nil
470
470
}
0 commit comments