1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-15 17:51:20 +00:00
kyverno/pkg/engine/context/imageutils.go
Sambhav Kothari 6f7bd7451b
Refactor image extraction to allow extracting custom resources (#3572)
* refactor: image extraction

Signed-off-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>

* Refactor image extraction to allow extracting custom resources

Signed-off-by: Sambhav Kothari <skothari44@bloomberg.net>

Co-authored-by: Charles-Edouard Brétéché <charled.breteche@gmail.com>
2022-04-11 09:30:38 +00:00

32 lines
935 B
Go

package context
import (
"fmt"
engineutils "github.com/kyverno/kyverno/pkg/engine/utils"
)
// MutateResourceWithImageInfo will set images to their canonical form so that they can be compared
// in a predictable manner. This sets the default registry as `docker.io` and the tag as `latest` if
// these are missing.
func MutateResourceWithImageInfo(raw []byte, ctx Interface) error {
images := ctx.ImageInfo()
if images == nil {
return nil
}
var patches [][]byte
buildJSONPatch := func(op, path, value string) []byte {
p := fmt.Sprintf(`{ "op": "%s", "path": "%s", "value":"%s" }`, op, path, value)
return []byte(p)
}
for _, infoMaps := range images {
for _, info := range infoMaps {
patches = append(patches, buildJSONPatch("replace", info.Pointer, info.String()))
}
}
patchedResource, err := engineutils.ApplyPatches(raw, patches)
if err != nil {
return err
}
return AddResource(ctx, patchedResource)
}