Skip to content

Commit be5fdba

Browse files
authored
Merge pull request #537 from jetstack/log-improvements
fix(log): Some improvements to the logs
2 parents 1dffb7f + e84e2e2 commit be5fdba

File tree

12 files changed

+42
-40
lines changed

12 files changed

+42
-40
lines changed

pkg/agent/dummy_data_gatherer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (g *dummyDataGatherer) Delete() error {
4444
return nil
4545
}
4646

47-
func (c *dummyDataGatherer) Fetch() (interface{}, error) {
47+
func (c *dummyDataGatherer) Fetch() (interface{}, int, error) {
4848
var err error
4949
if c.attemptNumber < c.FailedAttempts {
5050
err = fmt.Errorf("First %d attempts will fail", c.FailedAttempts)
@@ -53,5 +53,5 @@ func (c *dummyDataGatherer) Fetch() (interface{}, error) {
5353
err = fmt.Errorf("This data gatherer will always fail")
5454
}
5555
c.attemptNumber++
56-
return nil, err
56+
return nil, -1, err
5757
}

pkg/agent/run.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func gatherAndOutputData(config Config, preflightClient client.Client, dataGathe
351351
log.Printf("retrying in %v after error: %s", t, err)
352352
})
353353
if err != nil {
354-
log.Fatalf("%v", err)
354+
log.Fatalf("Exiting due to fatal error uploading: %v", err)
355355
}
356356

357357
}
@@ -362,14 +362,18 @@ func gatherData(config Config, dataGatherers map[string]datagatherer.DataGathere
362362

363363
var dgError *multierror.Error
364364
for k, dg := range dataGatherers {
365-
dgData, err := dg.Fetch()
365+
dgData, count, err := dg.Fetch()
366366
if err != nil {
367367
dgError = multierror.Append(dgError, fmt.Errorf("error in datagatherer %s: %w", k, err))
368368

369369
continue
370370
}
371371

372-
log.Printf("successfully gathered data from %q datagatherer", k)
372+
if count >= 0 {
373+
log.Printf("successfully gathered %d items from %q datagatherer", count, k)
374+
} else {
375+
log.Printf("successfully gathered data from %q datagatherer", k)
376+
}
373377
readings = append(readings, &api.DataReading{
374378
ClusterID: config.ClusterID,
375379
DataGatherer: k,
@@ -401,7 +405,6 @@ func gatherData(config Config, dataGatherers map[string]datagatherer.DataGathere
401405
func postData(config Config, preflightClient client.Client, readings []*api.DataReading) error {
402406
baseURL := config.Server
403407

404-
log.Println("Running Agent...")
405408
log.Println("Posting data to:", baseURL)
406409

407410
if VenafiCloudMode {
@@ -447,7 +450,7 @@ func postData(config Config, preflightClient client.Client, readings []*api.Data
447450
}
448451
defer res.Body.Close()
449452

450-
return fmt.Errorf("received response with status code %d. Body: %s", code, errorContent)
453+
return fmt.Errorf("received response with status code %d. Body: [%s]", code, errorContent)
451454
}
452455
log.Println("Data sent successfully.")
453456
return err

pkg/client/client_api_token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (c *APITokenClient) PostDataReadings(orgID, clusterID string, readings []*a
7171
errorContent = string(body)
7272
}
7373

74-
return fmt.Errorf("received response with status code %d. Body: %s", code, errorContent)
74+
return fmt.Errorf("received response with status code %d. Body: [%s]", code, errorContent)
7575
}
7676

7777
return nil

pkg/client/client_oauth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (c *OAuthClient) PostDataReadings(orgID, clusterID string, readings []*api.
125125
errorContent = string(body)
126126
}
127127

128-
return fmt.Errorf("received response with status code %d. Body: %s", code, errorContent)
128+
return fmt.Errorf("received response with status code %d. Body: [%s]", code, errorContent)
129129
}
130130

131131
return nil

pkg/client/client_unauthenticated.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (c *UnauthenticatedClient) PostDataReadings(orgID, clusterID string, readin
6767
errorContent = string(body)
6868
}
6969

70-
return fmt.Errorf("received response with status code %d. Body: %s", code, errorContent)
70+
return fmt.Errorf("received response with status code %d. Body: [%s]", code, errorContent)
7171
}
7272

7373
return nil

pkg/client/client_venafi_cloud.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (c *VenafiCloudClient) PostDataReadingsWithOptions(readings []*api.DataRead
199199
if err == nil {
200200
errorContent = string(body)
201201
}
202-
return fmt.Errorf("received response with status code %d. Body: %s", code, errorContent)
202+
return fmt.Errorf("received response with status code %d. Body: [%s]", code, errorContent)
203203
}
204204

205205
return nil
@@ -235,7 +235,7 @@ func (c *VenafiCloudClient) PostDataReadings(_ string, _ string, readings []*api
235235
if err == nil {
236236
errorContent = string(body)
237237
}
238-
return fmt.Errorf("received response with status code %d. Body: %s", code, errorContent)
238+
return fmt.Errorf("received response with status code %d. Body: [%s]", code, errorContent)
239239
}
240240

241241
return nil
@@ -327,7 +327,7 @@ func (c *VenafiCloudClient) sendHTTPRequest(request *http.Request, responseObjec
327327

328328
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
329329
body, _ := io.ReadAll(response.Body)
330-
return fmt.Errorf("failed to execute http request to VaaS. Request %s, status code: %d, body: %s", request.URL, response.StatusCode, body)
330+
return fmt.Errorf("failed to execute http request to Venafi Control Plane. Request %s, status code: %d, body: [%s]", request.URL, response.StatusCode, body)
331331
}
332332

333333
body, err := io.ReadAll(response.Body)

pkg/datagatherer/datagatherer.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ type Config interface {
1212
// DataGatherer is the interface for Data Gatherers. Data Gatherers are in charge of fetching data from a certain cloud provider API or Kubernetes component.
1313
type DataGatherer interface {
1414
// Fetch retrieves data.
15-
Fetch() (interface{}, error)
15+
// count is the number of items that were discovered. A negative count means the number
16+
// of items was indeterminate.
17+
Fetch() (data interface{}, count int, err error)
1618
// Run starts the data gatherer's informers for resource collection.
1719
// Returns error if the data gatherer informer wasn't initialized
1820
Run(stopCh <-chan struct{}) error

pkg/datagatherer/k8s/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func NewDiscoveryClient(kubeconfigPath string) (discovery.DiscoveryClient, error
3333

3434
cfg, err := loadRESTConfig(kubeconfigPath)
3535
if err != nil {
36-
return *discoveryClient, errors.WithStack(err)
36+
return discovery.DiscoveryClient{}, errors.WithStack(err)
3737
}
3838

3939
discoveryClient, err = discovery.NewDiscoveryClientForConfig(cfg)

pkg/datagatherer/k8s/discovery.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ func (g *DataGathererDiscovery) Delete() error {
6262
}
6363

6464
// Fetch will fetch discovery data from the apiserver, or return an error
65-
func (g *DataGathererDiscovery) Fetch() (interface{}, error) {
65+
func (g *DataGathererDiscovery) Fetch() (interface{}, int, error) {
6666
data, err := g.cl.ServerVersion()
6767
if err != nil {
68-
return nil, fmt.Errorf("failed to get server version: %v", err)
68+
return nil, -1, fmt.Errorf("failed to get server version: %v", err)
6969
}
7070

7171
response := map[string]interface{}{
7272
// data has type Info: https://godoc.org/k8s.io/apimachinery/pkg/version#Info
7373
"server_version": data,
7474
}
7575

76-
return response, nil
76+
return response, len(response), nil
7777
}

pkg/datagatherer/k8s/dynamic.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,6 @@ type DataGathererDynamic struct {
251251
informer k8scache.SharedIndexInformer
252252
dynamicSharedInformer dynamicinformer.DynamicSharedInformerFactory
253253
nativeSharedInformer informers.SharedInformerFactory
254-
informerCtx context.Context
255-
informerCancel context.CancelFunc
256254

257255
// isInitialized is set to true when data is first collected, prior to
258256
// this the fetch method will return an error
@@ -266,21 +264,13 @@ func (g *DataGathererDynamic) Run(stopCh <-chan struct{}) error {
266264
return fmt.Errorf("informer was not initialized, impossible to start")
267265
}
268266

269-
// starting a new ctx for the informer
270-
// WithCancel copies the parent ctx and creates a new done() channel
271-
informerCtx, cancel := context.WithCancel(g.ctx)
272-
g.informerCtx = informerCtx
273-
g.informerCancel = cancel
274-
275267
// attach WatchErrorHandler, it needs to be set before starting an informer
276268
err := g.informer.SetWatchErrorHandler(func(r *k8scache.Reflector, err error) {
277269
if strings.Contains(fmt.Sprintf("%s", err), "the server could not find the requested resource") {
278270
log.Printf("server missing resource for datagatherer of %q ", g.groupVersionResource)
279271
} else {
280272
log.Printf("datagatherer informer for %q has failed and is backing off due to error: %s", g.groupVersionResource, err)
281273
}
282-
// cancel the informer ctx to stop the informer in case of error
283-
cancel()
284274
})
285275
if err != nil {
286276
return fmt.Errorf("failed to SetWatchErrorHandler on informer: %s", err)
@@ -302,7 +292,7 @@ func (g *DataGathererDynamic) Run(stopCh <-chan struct{}) error {
302292
// before collecting the resources.
303293
func (g *DataGathererDynamic) WaitForCacheSync(stopCh <-chan struct{}) error {
304294
if !k8scache.WaitForCacheSync(stopCh, g.informer.HasSynced) {
305-
return fmt.Errorf("timed out waiting for caches to sync, using parent stop channel")
295+
return fmt.Errorf("timed out waiting for Kubernetes caches to sync")
306296
}
307297

308298
return nil
@@ -312,15 +302,14 @@ func (g *DataGathererDynamic) WaitForCacheSync(stopCh <-chan struct{}) error {
312302
// informer
313303
func (g *DataGathererDynamic) Delete() error {
314304
g.cache.Flush()
315-
g.informerCancel()
316305
return nil
317306
}
318307

319308
// Fetch will fetch the requested data from the apiserver, or return an error
320309
// if fetching the data fails.
321-
func (g *DataGathererDynamic) Fetch() (interface{}, error) {
310+
func (g *DataGathererDynamic) Fetch() (interface{}, int, error) {
322311
if g.groupVersionResource.String() == "" {
323-
return nil, fmt.Errorf("resource type must be specified")
312+
return nil, -1, fmt.Errorf("resource type must be specified")
324313
}
325314

326315
var list = map[string]interface{}{}
@@ -344,19 +333,19 @@ func (g *DataGathererDynamic) Fetch() (interface{}, error) {
344333
}
345334
continue
346335
}
347-
return nil, fmt.Errorf("failed to parse cached resource")
336+
return nil, -1, fmt.Errorf("failed to parse cached resource")
348337
}
349338

350339
// Redact Secret data
351340
err := redactList(items)
352341
if err != nil {
353-
return nil, errors.WithStack(err)
342+
return nil, -1, errors.WithStack(err)
354343
}
355344

356345
// add gathered resources to items
357346
list["items"] = items
358347

359-
return list, nil
348+
return list, len(items), nil
360349
}
361350

362351
func redactList(list []*api.GatheredResource) error {

0 commit comments

Comments
 (0)