Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame^] | 1 | /* |
| 2 | Copyright 2015 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package validation |
| 18 | |
| 19 | import ( |
| 20 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 21 | "k8s.io/apimachinery/pkg/util/validation" |
| 22 | "k8s.io/apimachinery/pkg/util/validation/field" |
| 23 | ) |
| 24 | |
| 25 | func ValidateLabelSelector(ps *metav1.LabelSelector, fldPath *field.Path) field.ErrorList { |
| 26 | allErrs := field.ErrorList{} |
| 27 | if ps == nil { |
| 28 | return allErrs |
| 29 | } |
| 30 | allErrs = append(allErrs, ValidateLabels(ps.MatchLabels, fldPath.Child("matchLabels"))...) |
| 31 | for i, expr := range ps.MatchExpressions { |
| 32 | allErrs = append(allErrs, ValidateLabelSelectorRequirement(expr, fldPath.Child("matchExpressions").Index(i))...) |
| 33 | } |
| 34 | return allErrs |
| 35 | } |
| 36 | |
| 37 | func ValidateLabelSelectorRequirement(sr metav1.LabelSelectorRequirement, fldPath *field.Path) field.ErrorList { |
| 38 | allErrs := field.ErrorList{} |
| 39 | switch sr.Operator { |
| 40 | case metav1.LabelSelectorOpIn, metav1.LabelSelectorOpNotIn: |
| 41 | if len(sr.Values) == 0 { |
| 42 | allErrs = append(allErrs, field.Required(fldPath.Child("values"), "must be specified when `operator` is 'In' or 'NotIn'")) |
| 43 | } |
| 44 | case metav1.LabelSelectorOpExists, metav1.LabelSelectorOpDoesNotExist: |
| 45 | if len(sr.Values) > 0 { |
| 46 | allErrs = append(allErrs, field.Forbidden(fldPath.Child("values"), "may not be specified when `operator` is 'Exists' or 'DoesNotExist'")) |
| 47 | } |
| 48 | default: |
| 49 | allErrs = append(allErrs, field.Invalid(fldPath.Child("operator"), sr.Operator, "not a valid selector operator")) |
| 50 | } |
| 51 | allErrs = append(allErrs, ValidateLabelName(sr.Key, fldPath.Child("key"))...) |
| 52 | return allErrs |
| 53 | } |
| 54 | |
| 55 | // ValidateLabelName validates that the label name is correctly defined. |
| 56 | func ValidateLabelName(labelName string, fldPath *field.Path) field.ErrorList { |
| 57 | allErrs := field.ErrorList{} |
| 58 | for _, msg := range validation.IsQualifiedName(labelName) { |
| 59 | allErrs = append(allErrs, field.Invalid(fldPath, labelName, msg)) |
| 60 | } |
| 61 | return allErrs |
| 62 | } |
| 63 | |
| 64 | // ValidateLabels validates that a set of labels are correctly defined. |
| 65 | func ValidateLabels(labels map[string]string, fldPath *field.Path) field.ErrorList { |
| 66 | allErrs := field.ErrorList{} |
| 67 | for k, v := range labels { |
| 68 | allErrs = append(allErrs, ValidateLabelName(k, fldPath)...) |
| 69 | for _, msg := range validation.IsValidLabelValue(v) { |
| 70 | allErrs = append(allErrs, field.Invalid(fldPath, v, msg)) |
| 71 | } |
| 72 | } |
| 73 | return allErrs |
| 74 | } |
| 75 | |
| 76 | func ValidateDeleteOptions(options *metav1.DeleteOptions) field.ErrorList { |
| 77 | allErrs := field.ErrorList{} |
| 78 | if options.OrphanDependents != nil && options.PropagationPolicy != nil { |
| 79 | allErrs = append(allErrs, field.Invalid(field.NewPath("propagationPolicy"), options.PropagationPolicy, "orphanDependents and deletionPropagation cannot be both set")) |
| 80 | } |
| 81 | if options.PropagationPolicy != nil && |
| 82 | *options.PropagationPolicy != metav1.DeletePropagationForeground && |
| 83 | *options.PropagationPolicy != metav1.DeletePropagationBackground && |
| 84 | *options.PropagationPolicy != metav1.DeletePropagationOrphan { |
| 85 | allErrs = append(allErrs, field.NotSupported(field.NewPath("propagationPolicy"), options.PropagationPolicy, []string{string(metav1.DeletePropagationForeground), string(metav1.DeletePropagationBackground), string(metav1.DeletePropagationOrphan), "nil"})) |
| 86 | } |
| 87 | return allErrs |
| 88 | } |
| 89 | |
| 90 | const UninitializedStatusUpdateErrorMsg string = `must not update status when the object is uninitialized` |