2021-03-23 10:34:03 -07:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2021-07-22 14:23:03 -07:00
|
|
|
"fmt"
|
2021-03-23 10:34:03 -07:00
|
|
|
|
2021-07-22 14:23:03 -07:00
|
|
|
engineutils "github.com/kyverno/kyverno/pkg/engine/utils"
|
2021-03-23 10:34:03 -07:00
|
|
|
)
|
|
|
|
|
2021-10-06 22:19:47 -07:00
|
|
|
// 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.
|
2022-04-09 13:52:50 +02:00
|
|
|
func MutateResourceWithImageInfo(raw []byte, ctx Interface) error {
|
2021-07-22 14:23:03 -07:00
|
|
|
images := ctx.ImageInfo()
|
|
|
|
if images == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-11 10:30:38 +01:00
|
|
|
var patches [][]byte
|
2021-07-22 14:23:03 -07:00
|
|
|
buildJSONPatch := func(op, path, value string) []byte {
|
|
|
|
p := fmt.Sprintf(`{ "op": "%s", "path": "%s", "value":"%s" }`, op, path, value)
|
|
|
|
return []byte(p)
|
|
|
|
}
|
2022-04-11 10:30:38 +01:00
|
|
|
for _, infoMaps := range images {
|
|
|
|
for _, info := range infoMaps {
|
|
|
|
patches = append(patches, buildJSONPatch("replace", info.Pointer, info.String()))
|
|
|
|
}
|
2022-01-07 14:25:52 +05:30
|
|
|
}
|
2021-07-22 14:23:03 -07:00
|
|
|
patchedResource, err := engineutils.ApplyPatches(raw, patches)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-09 13:52:50 +02:00
|
|
|
return AddResource(ctx, patchedResource)
|
2021-07-22 14:23:03 -07:00
|
|
|
}
|