mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
Check modification
This commit is contained in:
@@ -5,6 +5,7 @@ package target
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha1"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -288,14 +289,36 @@ func (kt *KustTarget) runValidators(ra *accumulator.ResAccumulator) error {
|
||||
return err
|
||||
}
|
||||
for _, v := range validators {
|
||||
err := v.Validate(ra.ResMap())
|
||||
// Validators shouldn't modify the resource map
|
||||
orignalHash, err := getSha1Hash(ra.ResMap())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = v.Validate(ra.ResMap())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newHash, err := getSha1Hash(ra.ResMap())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !bytes.Equal(orignalHash, newHash) {
|
||||
return fmt.Errorf("validator %#v shouldn't modify the resource map", v)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getSha1Hash(rm resmap.ResMap) ([]byte, error) {
|
||||
sha1Hash := sha1.New()
|
||||
yamlBytes, err := rm.AsYaml()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sha1Hash.Write(yamlBytes)
|
||||
return sha1Hash.Sum(nil), nil
|
||||
}
|
||||
|
||||
func (kt *KustTarget) configureExternalValidators() ([]resmap.Validator, error) {
|
||||
ra := accumulator.MakeEmptyAccumulator()
|
||||
ra, err := kt.accumulateResources(ra, kt.kustomization.Validators)
|
||||
|
||||
Reference in New Issue
Block a user