Skip to content

Commit

Permalink
NDEV-19864: deferred loading of images
Browse files Browse the repository at this point in the history
Signed-off-by: Anushka Mittal <[email protected]>
  • Loading branch information
anushkamittal2001 committed Aug 27, 2024
1 parent 5b3208b commit 455dffc
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 14 deletions.
63 changes: 50 additions & 13 deletions pkg/engine/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,21 +296,23 @@ func (ctx *context) AddImageInfo(info apiutils.ImageInfo, cfg config.Configurati
}

func (ctx *context) AddImageInfos(resource *unstructured.Unstructured, cfg config.Configuration) error {
images, err := apiutils.ExtractImagesFromResource(*resource, nil, cfg)
if err != nil {
return err
}
if len(images) == 0 {
return nil
}
ctx.images = images
utm, err := convertImagesToUntyped(images)
if err != nil {
return err
imageInfoLoader := &ImageInfoLoader{
resource: resource,
eCtx: ctx,
cfg: cfg,
}
dl, err := NewDeferredLoader("images", imageInfoLoader, logger)

Check failure on line 304 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / build-images

err declared and not used

Check failure on line 304 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

err declared and not used

Check failure on line 304 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / codecov

err declared and not used

Check failure on line 304 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / prepare-images

err declared and not used

Check failure on line 304 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

err declared and not used

Check failure on line 304 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

err declared and not used

Check failure on line 304 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / cli-test

err declared and not used

Check failure on line 304 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

err declared and not used

Check failure on line 304 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / codecov

err declared and not used

Check failure on line 304 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

err declared and not used

Check failure on line 304 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

err declared and not used

logging.V(4).Info("updated image info", "images", utm)
return addToContext(ctx, utm, "images")
if toggle.FromContext(cont.Background()).EnableDeferredLoading() {

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / build-images

undefined: toggle

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / build-images

undefined: cont

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: toggle

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: cont

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / codecov

undefined: toggle

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / codecov

undefined: cont

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / prepare-images

undefined: toggle

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / prepare-images

undefined: cont

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: toggle

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: cont

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / cli-test

undefined: toggle

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / cli-test

undefined: cont

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: toggle

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: cont

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / codecov

undefined: toggle

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / codecov

undefined: cont

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: toggle

Check failure on line 306 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: cont
if err := ctx.AddDeferredLoader(dl); err != nil {
return err
}
} else {
if err := imageInfoLoader.LoadData(); err != nil {
return err
}
}
return nil
}

func convertImagesToUntyped(images map[string]map[string]apiutils.ImageInfo) (map[string]interface{}, error) {
Expand All @@ -335,6 +337,35 @@ func convertImagesToUntyped(images map[string]map[string]apiutils.ImageInfo) (ma
return results, nil
}

type ImageInfoLoader struct {
resource *unstructured.Unstructured
hasLoaded bool
eCtx *context
cfg config.Configuration
}

func (l *ImageInfoLoader) HasLoaded() bool {
return l.hasLoaded
}

func (l *ImageInfoLoader) LoadData() error {
images, err := apiutils.ExtractImagesFromResource(*resource, nil, cfg)

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / build-images

undefined: resource

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / build-images

undefined: cfg

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: resource

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: cfg

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / codecov

undefined: resource

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / codecov

undefined: cfg

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / prepare-images

undefined: resource

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / prepare-images

undefined: cfg

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: resource

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: cfg

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / cli-test

undefined: resource

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / cli-test

undefined: cfg

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: resource

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: cfg

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / codecov

undefined: resource

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / codecov

undefined: cfg

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: resource

Check failure on line 352 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: cfg
if err != nil {
return err
}
if len(images) == 0 {
return nil
}
ctx.images = images

Check failure on line 359 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / build-images

undefined: ctx

Check failure on line 359 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ctx

Check failure on line 359 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / codecov

undefined: ctx

Check failure on line 359 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / prepare-images

undefined: ctx

Check failure on line 359 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ctx

Check failure on line 359 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / cli-test

undefined: ctx

Check failure on line 359 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ctx

Check failure on line 359 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / codecov

undefined: ctx

Check failure on line 359 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ctx
utm, err := convertImagesToUntyped(images)
if err != nil {
return err
}

logging.V(4).Info("updated image info", "images", utm)
return addToContext(ctx, utm, "images")

Check failure on line 366 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / build-images

undefined: ctx

Check failure on line 366 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ctx

Check failure on line 366 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / codecov

undefined: ctx

Check failure on line 366 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / prepare-images

undefined: ctx

Check failure on line 366 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ctx) (typecheck)

Check failure on line 366 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / cli-test

undefined: ctx

Check failure on line 366 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ctx

Check failure on line 366 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / codecov

undefined: ctx

Check failure on line 366 in pkg/engine/context/context.go

View workflow job for this annotation

GitHub Actions / tests

undefined: ctx) (typecheck)
}

func (ctx *context) GenerateCustomImageInfo(resource *unstructured.Unstructured, imageExtractorConfigs kyvernov1.ImageExtractorConfigs, cfg config.Configuration) (map[string]map[string]apiutils.ImageInfo, error) {
images, err := apiutils.ExtractImagesFromResource(*resource, imageExtractorConfigs, cfg)
if err != nil {
Expand All @@ -350,6 +381,12 @@ func (ctx *context) GenerateCustomImageInfo(resource *unstructured.Unstructured,
}

func (ctx *context) ImageInfo() map[string]map[string]apiutils.ImageInfo {
// force load of image info from deferred loader
if len(ctx.images) == 0 {
if err := ctx.loadDeferred("images"); err != nil {
return map[string]map[string]apiutils.ImageInfo{}
}
}
return ctx.images
}

Expand Down
59 changes: 58 additions & 1 deletion pkg/engine/context/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import (
"github.com/kyverno/kyverno/pkg/config"
"github.com/kyverno/kyverno/pkg/engine/jmespath"
authenticationv1 "k8s.io/api/authentication/v1"
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
)

var jp = jmespath.New(config.NewDefaultConfiguration(false))
var (
jp = jmespath.New(config.NewDefaultConfiguration(false))
cfg = config.NewDefaultConfiguration(false)
)

func Test_addResourceAndUserContext(t *testing.T) {
var err error
Expand Down Expand Up @@ -123,3 +127,56 @@ func Test_addResourceAndUserContext(t *testing.T) {
t.Error("expected result does not match")
}
}

func Test_ImageInfoLoader(t *testing.T) {
resource1, err := kubeutils.BytesToUnstructured([]byte(`{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "test-pod",
"namespace": "default"
},
"spec": {
"containers": [{
"name": "test_container",
"image": "nginx:latest"
}]
}
}`))
assert.Nil(t, err)
newctx := newContext()
err = newctx.AddImageInfos(resource1, cfg)
assert.Nil(t, err)
// images not loaded
assert.Nil(t, newctx.images)
// images loaded on Query
name, err := newctx.Query("images.containers.test_container.name")
assert.Nil(t, err)
assert.Equal(t, name, "nginx")
}

func Test_ImageInfoLoader_OnDirectCall(t *testing.T) {
resource1, err := kubeutils.BytesToUnstructured([]byte(`{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "test-pod",
"namespace": "default"
},
"spec": {
"containers": [{
"name": "test_container",
"image": "nginx:latest"
}]
}
}`))
assert.Nil(t, err)
newctx := newContext()
err = newctx.AddImageInfos(resource1, cfg)
assert.Nil(t, err)
// images not loaded
assert.Nil(t, newctx.images)
// images loaded on explicit call to ImageInfo
imageinfos := newctx.ImageInfo()
assert.Equal(t, imageinfos["containers"]["test_container"].Name, "nginx")
}

0 comments on commit 455dffc

Please sign in to comment.