mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-02-12 23:01:39 +00:00
support JSON Merge Patch and Strategic Merge Patch in Resource Modifiers Signed-off-by: lou <alex1988@outlook.com>
70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package resourcemodifiers
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func (r *ResourceModifierRule) Validate() error {
|
|
if err := r.Conditions.Validate(); err != nil {
|
|
return err
|
|
}
|
|
|
|
count := 0
|
|
for _, size := range []int{
|
|
len(r.Patches),
|
|
len(r.MergePatches),
|
|
len(r.StrategicPatches),
|
|
} {
|
|
if size != 0 {
|
|
count++
|
|
}
|
|
if count >= 2 {
|
|
return fmt.Errorf("only one of patches, mergePatches, strategicPatches can be specified")
|
|
}
|
|
}
|
|
|
|
for _, patch := range r.Patches {
|
|
if err := patch.Validate(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *ResourceModifiers) Validate() error {
|
|
if !strings.EqualFold(p.Version, ResourceModifierSupportedVersionV1) {
|
|
return fmt.Errorf("unsupported resource modifier version %s", p.Version)
|
|
}
|
|
if len(p.ResourceModifierRules) == 0 {
|
|
return fmt.Errorf("resource modifier rules cannot be empty")
|
|
}
|
|
for _, rule := range p.ResourceModifierRules {
|
|
if err := rule.Validate(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *JSONPatch) Validate() error {
|
|
if p.Operation == "" {
|
|
return fmt.Errorf("operation cannot be empty")
|
|
}
|
|
if operation := strings.ToLower(p.Operation); operation != "add" && operation != "remove" && operation != "replace" && operation != "test" && operation != "move" && operation != "copy" {
|
|
return fmt.Errorf("unsupported operation %s", p.Operation)
|
|
}
|
|
if p.Path == "" {
|
|
return fmt.Errorf("path cannot be empty")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *Conditions) Validate() error {
|
|
if c.GroupResource == "" {
|
|
return fmt.Errorf("groupkResource cannot be empty")
|
|
}
|
|
return nil
|
|
}
|