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 bacf569 commit 2dcc79a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
44 changes: 42 additions & 2 deletions pkg/engine/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,45 @@ 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)
imageInfoLoader := &ImageInfoLoader{
resource: resource,
eCtx: ctx,
cfg: cfg,
}
dl, err := NewDeferredLoader("images", imageInfoLoader, logger)
if err != nil {
return err
}
if toggle.FromContext(cont.Background()).EnableDeferredLoading() {

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

View workflow job for this annotation

GitHub Actions / build-images

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / build-images

undefined: cont

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

View workflow job for this annotation

GitHub Actions / tests

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / tests

undefined: cont

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

View workflow job for this annotation

GitHub Actions / prepare-images

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / prepare-images

undefined: cont

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

View workflow job for this annotation

GitHub Actions / tests

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / tests

undefined: cont

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

View workflow job for this annotation

GitHub Actions / kubectl-validate-cli-test

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / kubectl-validate-cli-test

undefined: cont

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

View workflow job for this annotation

GitHub Actions / tests

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / tests

undefined: cont) (typecheck)

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

View workflow job for this annotation

GitHub Actions / tests

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / tests

undefined: cont) (typecheck)

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

View workflow job for this annotation

GitHub Actions / tests

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / tests

undefined: cont) (typecheck)

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

View workflow job for this annotation

GitHub Actions / prepare-cli

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / prepare-cli

undefined: cont

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

View workflow job for this annotation

GitHub Actions / codecov

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / codecov

undefined: cont

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

View workflow job for this annotation

GitHub Actions / tests

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / tests

undefined: cont) (typecheck)

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

View workflow job for this annotation

GitHub Actions / tests

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / tests

undefined: cont) (typecheck)

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

View workflow job for this annotation

GitHub Actions / tests

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / tests

undefined: cont) (typecheck)

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

View workflow job for this annotation

GitHub Actions / cli-test

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / cli-test

undefined: cont

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

View workflow job for this annotation

GitHub Actions / codecov

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / codecov

undefined: cont

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

View workflow job for this annotation

GitHub Actions / verify-codegen

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / verify-codegen

undefined: cont

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

View workflow job for this annotation

GitHub Actions / verify-codegen

undefined: toggle

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

View workflow job for this annotation

GitHub Actions / verify-codegen

undefined: cont
if err := ctx.AddDeferredLoader(dl); err != nil {
return err
}
} else {
if err := imageInfoLoader.LoadData(); err != nil {
return err
}
}
return 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(*l.resource, nil, l.cfg)
if err != nil {
return err
}
return ctx.addImageInfos(images)

return l.eCtx.addImageInfos(images)
}

func (ctx *context) addImageInfos(images map[string]map[string]apiutils.ImageInfo) error {
Expand Down Expand Up @@ -361,6 +395,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 2dcc79a

Please sign in to comment.