ValidatePatch validates a ComposedPatch.
(p PatchInterface)
| 161 | |
| 162 | // ValidatePatch validates a ComposedPatch. |
| 163 | func ValidatePatch(p PatchInterface) *field.Error { //nolint: gocyclo // This is a long but simple/same-y switch. |
| 164 | switch p.GetType() { |
| 165 | case v1beta1.PatchTypeFromCompositeFieldPath, |
| 166 | v1beta1.PatchTypeToCompositeFieldPath, |
| 167 | v1beta1.PatchTypeFromEnvironmentFieldPath, |
| 168 | v1beta1.PatchTypeToEnvironmentFieldPath: |
| 169 | if p.GetFromFieldPath() == "" { |
| 170 | return field.Required(field.NewPath("fromFieldPath"), fmt.Sprintf("fromFieldPath must be set for patch type %s", p.GetType())) |
| 171 | } |
| 172 | case v1beta1.PatchTypePatchSet: |
| 173 | ps, ok := p.(PatchWithPatchSetName) |
| 174 | if !ok { |
| 175 | return field.Invalid(field.NewPath("type"), p.GetType(), fmt.Sprintf("patch type %T does not support patch of type %s", p, p.GetType())) |
| 176 | } |
| 177 | if ps.GetPatchSetName() == "" { |
| 178 | return field.Required(field.NewPath("patchSetName"), fmt.Sprintf("patchSetName must be set for patch type %s", p.GetType())) |
| 179 | } |
| 180 | case v1beta1.PatchTypeCombineFromComposite, |
| 181 | v1beta1.PatchTypeCombineToComposite, |
| 182 | v1beta1.PatchTypeCombineFromEnvironment, |
| 183 | v1beta1.PatchTypeCombineToEnvironment: |
| 184 | if p.GetCombine() == nil { |
| 185 | return field.Required(field.NewPath("combine"), fmt.Sprintf("combine must be set for patch type %s", p.GetType())) |
| 186 | } |
| 187 | if p.GetToFieldPath() == "" { |
| 188 | return field.Required(field.NewPath("toFieldPath"), fmt.Sprintf("toFieldPath must be set for patch type %s", p.GetType())) |
| 189 | } |
| 190 | return WrapFieldError(ValidateCombine(p.GetCombine()), field.NewPath("combine")) |
| 191 | default: |
| 192 | // Should never happen |
| 193 | return field.Invalid(field.NewPath("type"), p.GetType(), "unknown patch type") |
| 194 | } |
| 195 | for i, t := range p.GetTransforms() { |
| 196 | if err := ValidateTransform(t); err != nil { |
| 197 | return WrapFieldError(err, field.NewPath("transforms").Index(i)) |
| 198 | } |
| 199 | } |
| 200 | if pp := p.GetPolicy(); pp != nil { |
| 201 | switch pp.GetToFieldPathPolicy() { |
| 202 | case v1beta1.ToFieldPathPolicyReplace, |
| 203 | v1beta1.ToFieldPathPolicyMergeObjects, |
| 204 | v1beta1.ToFieldPathPolicyMergeObjectsAppendArrays, |
| 205 | v1beta1.ToFieldPathPolicyForceMergeObjects, |
| 206 | v1beta1.ToFieldPathPolicyForceMergeObjectsAppendArrays, |
| 207 | v1beta1.ToFieldPathPolicyMergeObject, //nolint:staticcheck // MergeObject is deprecated but we must still support it. |
| 208 | v1beta1.ToFieldPathPolicyAppendArray: //nolint:staticcheck // AppendArray is deprecated but we must still support it. |
| 209 | // ok |
| 210 | default: |
| 211 | return field.Invalid(field.NewPath("policy", "toFieldPathPolicy"), pp.GetToFieldPathPolicy(), "unknown toFieldPathPolicy") |
| 212 | } |
| 213 | switch pp.GetFromFieldPathPolicy() { |
| 214 | case v1beta1.FromFieldPathPolicyRequired, |
| 215 | v1beta1.FromFieldPathPolicyOptional: |
| 216 | // ok |
| 217 | default: |
| 218 | return field.Invalid(field.NewPath("policy", "fromFieldPathPolicy"), pp.GetFromFieldPathPolicy(), "unknown fromFieldPathPolicy") |
| 219 | } |
| 220 | } |