Skip to content

Commit

Permalink
Added log message for API call failures (kyverno#7834)
Browse files Browse the repository at this point in the history
* Added error message to deferred loader on API call failure

Signed-off-by: Pradyot Ranjan <[email protected]>

* Small change in error message

Signed-off-by: Pradyot Ranjan <[email protected]>

---------

Signed-off-by: Pradyot Ranjan <[email protected]>
Co-authored-by: Pradyot Ranjan <[email protected]>
Co-authored-by: Chip Zoller <[email protected]>
Co-authored-by: shuting <[email protected]>
Co-authored-by: Jim Bugwadia <[email protected]>
  • Loading branch information
5 people authored Jul 19, 2023
1 parent 1fff5ba commit d18a27d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
11 changes: 9 additions & 2 deletions pkg/engine/context/deferred.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package context

import (
"regexp"

"github.com/go-logr/logr"
)

type deferredLoader struct {
name string
matcher regexp.Regexp
loader Loader
logger logr.Logger
}

func NewDeferredLoader(name string, loader Loader) (DeferredLoader, error) {
func NewDeferredLoader(name string, loader Loader, logger logr.Logger) (DeferredLoader, error) {
// match on ASCII word boundaries except do not allow starting with a `.`
// this allows `x` to match `x.y` but not `y.x` or `y.x.z`
matcher, err := regexp.Compile(`(?:\A|\z|\s|[^.0-9A-Za-z])` + name + `\b`)
Expand All @@ -22,6 +25,7 @@ func NewDeferredLoader(name string, loader Loader) (DeferredLoader, error) {
name: name,
matcher: *matcher,
loader: loader,
logger: logger,
}, nil
}

Expand All @@ -34,7 +38,10 @@ func (dl *deferredLoader) HasLoaded() bool {
}

func (dl *deferredLoader) LoadData() error {
return dl.loader.LoadData()
if err := dl.loader.LoadData(); err != nil {
dl.logger.Error(err, "failed to load data", "name", dl.name)
}
return nil
}

func (d *deferredLoader) Matches(query string) bool {
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/context/deferred_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func addDeferredWithQuery(ctx *context, name string, value interface{}, query st
query: query,
}

d, err := NewDeferredLoader(name, loader)
d, err := NewDeferredLoader(name, loader, logger)
if err != nil {
return loader, err
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/engine/factories/contextloaderfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,31 @@ func (l *contextLoader) newLoader(
) (enginecontext.DeferredLoader, error) {
if entry.ConfigMap != nil {
if l.cmResolver != nil {
l := loaders.NewConfigMapLoader(ctx, l.logger, entry, l.cmResolver, jsonContext)
return enginecontext.NewDeferredLoader(entry.Name, l)
ldr := loaders.NewConfigMapLoader(ctx, l.logger, entry, l.cmResolver, jsonContext)
return enginecontext.NewDeferredLoader(entry.Name, ldr, l.logger)
} else {
l.logger.Info("disabled loading of ConfigMap context entry %s", entry.Name)
return nil, nil
}
} else if entry.APICall != nil {
if client != nil {
l := loaders.NewAPILoader(ctx, l.logger, entry, jsonContext, jp, client)
return enginecontext.NewDeferredLoader(entry.Name, l)
ldr := loaders.NewAPILoader(ctx, l.logger, entry, jsonContext, jp, client)
return enginecontext.NewDeferredLoader(entry.Name, ldr, l.logger)
} else {
l.logger.Info("disabled loading of APICall context entry %s", entry.Name)
return nil, nil
}
} else if entry.ImageRegistry != nil {
if rclientFactory != nil {
l := loaders.NewImageDataLoader(ctx, l.logger, entry, jsonContext, jp, rclientFactory)
return enginecontext.NewDeferredLoader(entry.Name, l)
ldr := loaders.NewImageDataLoader(ctx, l.logger, entry, jsonContext, jp, rclientFactory)
return enginecontext.NewDeferredLoader(entry.Name, ldr, l.logger)
} else {
l.logger.Info("disabled loading of ImageRegistry context entry %s", entry.Name)
return nil, nil
}
} else if entry.Variable != nil {
l := loaders.NewVariableLoader(l.logger, entry, jsonContext, jp)
return enginecontext.NewDeferredLoader(entry.Name, l)
ldr := loaders.NewVariableLoader(l.logger, entry, jsonContext, jp)
return enginecontext.NewDeferredLoader(entry.Name, ldr, l.logger)
}
return nil, fmt.Errorf("missing ConfigMap|APICall|ImageRegistry|Variable in context entry %s", entry.Name)
}

0 comments on commit d18a27d

Please sign in to comment.