2023-09-19 11:28:27 +00:00
|
|
|
/*
|
|
|
|
Copyright The Velero Contributors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2023-07-05 08:21:32 +00:00
|
|
|
package resourcemodifiers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-08-31 05:06:59 +00:00
|
|
|
"strings"
|
2023-07-05 08:21:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r *ResourceModifierRule) Validate() error {
|
|
|
|
if err := r.Conditions.Validate(); err != nil {
|
2023-07-13 08:22:49 +00:00
|
|
|
return err
|
2023-07-05 08:21:32 +00:00
|
|
|
}
|
2023-10-10 08:00:46 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 08:21:32 +00:00
|
|
|
for _, patch := range r.Patches {
|
|
|
|
if err := patch.Validate(); err != nil {
|
2023-07-13 08:22:49 +00:00
|
|
|
return err
|
2023-07-05 08:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2023-07-13 08:22:49 +00:00
|
|
|
return err
|
2023-07-05 08:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-06 05:59:33 +00:00
|
|
|
func (p *JSONPatch) Validate() error {
|
2023-07-05 08:21:32 +00:00
|
|
|
if p.Operation == "" {
|
|
|
|
return fmt.Errorf("operation cannot be empty")
|
|
|
|
}
|
2023-07-11 04:30:08 +00:00
|
|
|
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)
|
|
|
|
}
|
2023-07-05 08:21:32 +00:00
|
|
|
if p.Path == "" {
|
|
|
|
return fmt.Errorf("path cannot be empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conditions) Validate() error {
|
2023-08-31 05:06:59 +00:00
|
|
|
if c.GroupResource == "" {
|
|
|
|
return fmt.Errorf("groupkResource cannot be empty")
|
2023-07-05 08:21:32 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|