2020-01-07 17:06:17 -08:00
|
|
|
package mutate
|
|
|
|
|
|
|
|
import (
|
2020-08-05 09:11:23 -07:00
|
|
|
"bytes"
|
|
|
|
|
2020-10-07 11:12:31 -07:00
|
|
|
commonAnchors "github.com/kyverno/kyverno/pkg/engine/anchor/common"
|
2020-01-07 17:06:17 -08:00
|
|
|
)
|
|
|
|
|
2020-08-05 09:11:23 -07:00
|
|
|
type buffer struct {
|
|
|
|
*bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (buff buffer) UnmarshalJSON(b []byte) error {
|
|
|
|
buff.Reset()
|
2021-10-11 23:57:43 +02:00
|
|
|
_, err := buff.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-05 09:11:23 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (buff buffer) MarshalJSON() ([]byte, error) {
|
|
|
|
return buff.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2020-01-07 17:06:17 -08:00
|
|
|
// removeAnchor remove special characters around anchored key
|
|
|
|
func removeAnchor(key string) string {
|
2020-08-29 06:52:22 +05:30
|
|
|
if commonAnchors.IsConditionAnchor(key) {
|
2020-01-07 17:06:17 -08:00
|
|
|
return key[1 : len(key)-1]
|
|
|
|
}
|
|
|
|
|
2021-09-13 18:59:28 +03:00
|
|
|
if commonAnchors.IsExistenceAnchor(key) || commonAnchors.IsAddingAnchor(key) || commonAnchors.IsEqualityAnchor(key) || commonAnchors.IsNegationAnchor(key) || commonAnchors.IsGlobalAnchor(key) {
|
2020-01-07 17:06:17 -08:00
|
|
|
return key[2 : len(key)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAnchorAndElementsFromMap gets the condition anchor map and resource map without anchor
|
|
|
|
func getAnchorAndElementsFromMap(anchorsMap map[string]interface{}) (map[string]interface{}, map[string]interface{}) {
|
|
|
|
anchors := make(map[string]interface{})
|
|
|
|
elementsWithoutanchor := make(map[string]interface{})
|
|
|
|
for key, value := range anchorsMap {
|
2020-08-29 06:52:22 +05:30
|
|
|
if commonAnchors.IsConditionAnchor(key) {
|
2020-01-07 17:06:17 -08:00
|
|
|
anchors[key] = value
|
2020-08-29 06:52:22 +05:30
|
|
|
} else if !commonAnchors.IsAddingAnchor(key) {
|
2020-01-07 17:06:17 -08:00
|
|
|
elementsWithoutanchor[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return anchors, elementsWithoutanchor
|
|
|
|
}
|