mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-15 17:51:20 +00:00
6f7bd7451b
* 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>
32 lines
935 B
Go
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)
|
|
}
|