mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 10:30:59 +00:00
Add exception for "validated-by" label
This commit is contained in:
@@ -5,7 +5,6 @@ package target
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha1"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -290,33 +289,36 @@ func (kt *KustTarget) runValidators(ra *accumulator.ResAccumulator) error {
|
|||||||
}
|
}
|
||||||
for _, v := range validators {
|
for _, v := range validators {
|
||||||
// Validators shouldn't modify the resource map
|
// Validators shouldn't modify the resource map
|
||||||
orignalHash, err := getSha1Hash(ra.ResMap())
|
orignal := ra.ResMap().DeepCopy()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = v.Validate(ra.ResMap())
|
err = v.Validate(ra.ResMap())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
newHash, err := getSha1Hash(ra.ResMap())
|
new := ra.ResMap().DeepCopy()
|
||||||
if err != nil {
|
kt.removeValidatedByLabel(new)
|
||||||
return err
|
if err = orignal.ErrorIfNotEqualSets(new); err != nil {
|
||||||
}
|
return fmt.Errorf("validator shouldn't modify the resource map: %v", err)
|
||||||
if !bytes.Equal(orignalHash, newHash) {
|
|
||||||
return fmt.Errorf("validator %#v shouldn't modify the resource map", v)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSha1Hash(rm resmap.ResMap) ([]byte, error) {
|
func (kt *KustTarget) removeValidatedByLabel(rm resmap.ResMap) {
|
||||||
sha1Hash := sha1.New()
|
var validatedByLabelName string = "validated-by"
|
||||||
yamlBytes, err := rm.AsYaml()
|
|
||||||
if err != nil {
|
resources := rm.Resources()
|
||||||
return nil, err
|
for _, r := range resources {
|
||||||
|
labels := r.GetLabels()
|
||||||
|
if _, found := labels[validatedByLabelName]; !found {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
delete(labels, validatedByLabelName)
|
||||||
|
if len(labels) == 0 {
|
||||||
|
r.SetLabels(nil)
|
||||||
|
} else {
|
||||||
|
r.SetLabels(labels)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
sha1Hash.Write(yamlBytes)
|
|
||||||
return sha1Hash.Sum(nil), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kt *KustTarget) configureExternalValidators() ([]resmap.Validator, error) {
|
func (kt *KustTarget) configureExternalValidators() ([]resmap.Validator, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user