validation.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package validation
  14. import (
  15. "k8s.io/kubernetes/pkg/api/unversioned"
  16. "k8s.io/kubernetes/pkg/util/validation"
  17. "k8s.io/kubernetes/pkg/util/validation/field"
  18. )
  19. func ValidateLabelSelector(ps *unversioned.LabelSelector, fldPath *field.Path) field.ErrorList {
  20. allErrs := field.ErrorList{}
  21. if ps == nil {
  22. return allErrs
  23. }
  24. allErrs = append(allErrs, ValidateLabels(ps.MatchLabels, fldPath.Child("matchLabels"))...)
  25. for i, expr := range ps.MatchExpressions {
  26. allErrs = append(allErrs, ValidateLabelSelectorRequirement(expr, fldPath.Child("matchExpressions").Index(i))...)
  27. }
  28. return allErrs
  29. }
  30. func ValidateLabelSelectorRequirement(sr unversioned.LabelSelectorRequirement, fldPath *field.Path) field.ErrorList {
  31. allErrs := field.ErrorList{}
  32. switch sr.Operator {
  33. case unversioned.LabelSelectorOpIn, unversioned.LabelSelectorOpNotIn:
  34. if len(sr.Values) == 0 {
  35. allErrs = append(allErrs, field.Required(fldPath.Child("values"), "must be specified when `operator` is 'In' or 'NotIn'"))
  36. }
  37. case unversioned.LabelSelectorOpExists, unversioned.LabelSelectorOpDoesNotExist:
  38. if len(sr.Values) > 0 {
  39. allErrs = append(allErrs, field.Forbidden(fldPath.Child("values"), "may not be specified when `operator` is 'Exists' or 'DoesNotExist'"))
  40. }
  41. default:
  42. allErrs = append(allErrs, field.Invalid(fldPath.Child("operator"), sr.Operator, "not a valid selector operator"))
  43. }
  44. allErrs = append(allErrs, ValidateLabelName(sr.Key, fldPath.Child("key"))...)
  45. return allErrs
  46. }
  47. // ValidateLabelName validates that the label name is correctly defined.
  48. func ValidateLabelName(labelName string, fldPath *field.Path) field.ErrorList {
  49. allErrs := field.ErrorList{}
  50. for _, msg := range validation.IsQualifiedName(labelName) {
  51. allErrs = append(allErrs, field.Invalid(fldPath, labelName, msg))
  52. }
  53. return allErrs
  54. }
  55. // ValidateLabels validates that a set of labels are correctly defined.
  56. func ValidateLabels(labels map[string]string, fldPath *field.Path) field.ErrorList {
  57. allErrs := field.ErrorList{}
  58. for k, v := range labels {
  59. allErrs = append(allErrs, ValidateLabelName(k, fldPath)...)
  60. for _, msg := range validation.IsValidLabelValue(v) {
  61. allErrs = append(allErrs, field.Invalid(fldPath, v, msg))
  62. }
  63. }
  64. return allErrs
  65. }