PageRenderTime 77ms CodeModel.GetById 20ms RepoModel.GetById 4ms app.codeStats 0ms

/pkg/template/apis/template/validation/validation.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 163 lines | 128 code | 24 blank | 11 comment | 39 complexity | c0b9d21c9979e8e3287445ddd1ce103b MD5 | raw file
  1. package validation
  2. import (
  3. "fmt"
  4. "regexp"
  5. "k8s.io/apimachinery/pkg/util/validation/field"
  6. kapi "k8s.io/kubernetes/pkg/apis/core"
  7. kapihelper "k8s.io/kubernetes/pkg/apis/core/helper"
  8. "k8s.io/kubernetes/pkg/apis/core/validation"
  9. templateapi "github.com/openshift/origin/pkg/template/apis/template"
  10. )
  11. var ParameterNameRegexp = regexp.MustCompile(`^[a-zA-Z0-9_]+$`)
  12. // ValidateParameter tests if required fields in the Parameter are set.
  13. func ValidateParameter(param *templateapi.Parameter, fldPath *field.Path) (allErrs field.ErrorList) {
  14. if len(param.Name) == 0 {
  15. allErrs = append(allErrs, field.Required(fldPath.Child("name"), ""))
  16. return
  17. }
  18. if !ParameterNameRegexp.MatchString(param.Name) {
  19. allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), param.Name, fmt.Sprintf("does not match %v", ParameterNameRegexp)))
  20. }
  21. return
  22. }
  23. // ValidateProcessedTemplate tests if required fields in the Template are set for processing
  24. func ValidateProcessedTemplate(template *templateapi.Template) field.ErrorList {
  25. return validateTemplateBody(template)
  26. }
  27. var ValidateTemplateName = validation.NameIsDNSSubdomain
  28. // ValidateTemplate tests if required fields in the Template are set.
  29. func ValidateTemplate(template *templateapi.Template) (allErrs field.ErrorList) {
  30. allErrs = validation.ValidateObjectMeta(&template.ObjectMeta, true, ValidateTemplateName, field.NewPath("metadata"))
  31. allErrs = append(allErrs, validateTemplateBody(template)...)
  32. return
  33. }
  34. // ValidateTemplateUpdate tests if required fields in the template are set during an update
  35. func ValidateTemplateUpdate(template, oldTemplate *templateapi.Template) field.ErrorList {
  36. return validation.ValidateObjectMetaUpdate(&template.ObjectMeta, &oldTemplate.ObjectMeta, field.NewPath("metadata"))
  37. }
  38. // validateTemplateBody checks the body of a template.
  39. func validateTemplateBody(template *templateapi.Template) (allErrs field.ErrorList) {
  40. for i := range template.Parameters {
  41. allErrs = append(allErrs, ValidateParameter(&template.Parameters[i], field.NewPath("parameters").Index(i))...)
  42. }
  43. return
  44. }
  45. var ValidateTemplateInstanceName = validation.NameIsDNSSubdomain
  46. // ValidateTemplateInstance tests if required fields in the TemplateInstance are set.
  47. func ValidateTemplateInstance(templateInstance *templateapi.TemplateInstance) (allErrs field.ErrorList) {
  48. allErrs = validation.ValidateObjectMeta(&templateInstance.ObjectMeta, true, ValidateTemplateInstanceName, field.NewPath("metadata"))
  49. // Allow the nested template name and namespace to be empty. If not empty,
  50. // the fields should pass validation.
  51. templateCopy := templateInstance.Spec.Template.DeepCopy()
  52. if templateCopy.Name == "" {
  53. templateCopy.Name = "dummy"
  54. }
  55. if templateCopy.Namespace == "" {
  56. templateCopy.Namespace = "dummy"
  57. }
  58. for _, err := range ValidateTemplate(templateCopy) {
  59. err.Field = "spec.template." + err.Field
  60. allErrs = append(allErrs, err)
  61. }
  62. if templateInstance.Spec.Secret != nil {
  63. if templateInstance.Spec.Secret.Name != "" {
  64. for _, msg := range validation.ValidateSecretName(templateInstance.Spec.Secret.Name, false) {
  65. allErrs = append(allErrs, field.Invalid(field.NewPath("spec.secret.name"), templateInstance.Spec.Secret.Name, msg))
  66. }
  67. } else {
  68. allErrs = append(allErrs, field.Required(field.NewPath("spec.secret.name"), ""))
  69. }
  70. }
  71. if templateInstance.Spec.Requester == nil {
  72. allErrs = append(allErrs, field.Required(field.NewPath("spec.requester"), ""))
  73. } else if templateInstance.Spec.Requester.Username == "" {
  74. allErrs = append(allErrs, field.Required(field.NewPath("spec.requester.username"), ""))
  75. }
  76. return
  77. }
  78. // ValidateTemplateInstanceUpdate tests if required fields in the TemplateInstance are set during an update
  79. func ValidateTemplateInstanceUpdate(templateInstance, oldTemplateInstance *templateapi.TemplateInstance) (allErrs field.ErrorList) {
  80. allErrs = validation.ValidateObjectMetaUpdate(&templateInstance.ObjectMeta, &oldTemplateInstance.ObjectMeta, field.NewPath("metadata"))
  81. if !kapihelper.Semantic.DeepEqual(templateInstance.Spec, oldTemplateInstance.Spec) {
  82. allErrs = append(allErrs, field.Forbidden(field.NewPath("spec"), "field is immutable"))
  83. }
  84. return
  85. }
  86. var ValidateBrokerTemplateInstanceName = validation.NameIsDNSSubdomain
  87. // ValidateBrokerTemplateInstance tests if required fields in the BrokerTemplateInstance are set.
  88. func ValidateBrokerTemplateInstance(brokerTemplateInstance *templateapi.BrokerTemplateInstance) (allErrs field.ErrorList) {
  89. allErrs = validation.ValidateObjectMeta(&brokerTemplateInstance.ObjectMeta, false, ValidateBrokerTemplateInstanceName, field.NewPath("metadata"))
  90. allErrs = append(allErrs, validateTemplateInstanceReference(&brokerTemplateInstance.Spec.TemplateInstance, field.NewPath("spec.templateInstance"), "TemplateInstance")...)
  91. allErrs = append(allErrs, validateTemplateInstanceReference(&brokerTemplateInstance.Spec.Secret, field.NewPath("spec.secret"), "Secret")...)
  92. for _, id := range brokerTemplateInstance.Spec.BindingIDs {
  93. for _, msg := range nameIsUUID(id, false) {
  94. allErrs = append(allErrs, field.Invalid(field.NewPath("spec.bindingIDs"), id, msg))
  95. }
  96. }
  97. return
  98. }
  99. // ValidateBrokerTemplateInstanceUpdate tests if required fields in the BrokerTemplateInstance are set during an update
  100. func ValidateBrokerTemplateInstanceUpdate(brokerTemplateInstance, oldBrokerTemplateInstance *templateapi.BrokerTemplateInstance) (allErrs field.ErrorList) {
  101. allErrs = validation.ValidateObjectMetaUpdate(&brokerTemplateInstance.ObjectMeta, &oldBrokerTemplateInstance.ObjectMeta, field.NewPath("metadata"))
  102. allErrs = append(allErrs, validateTemplateInstanceReference(&brokerTemplateInstance.Spec.TemplateInstance, field.NewPath("spec.templateInstance"), "TemplateInstance")...)
  103. allErrs = append(allErrs, validateTemplateInstanceReference(&brokerTemplateInstance.Spec.Secret, field.NewPath("spec.secret"), "Secret")...)
  104. for _, id := range brokerTemplateInstance.Spec.BindingIDs {
  105. for _, msg := range nameIsUUID(id, false) {
  106. allErrs = append(allErrs, field.Invalid(field.NewPath("spec.bindingIDs"), id, msg))
  107. }
  108. }
  109. return
  110. }
  111. var uuidRegex = regexp.MustCompile("^(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
  112. func nameIsUUID(name string, prefix bool) []string {
  113. if uuidRegex.MatchString(name) {
  114. return nil
  115. }
  116. return []string{"is not a valid UUID"}
  117. }
  118. func validateTemplateInstanceReference(ref *kapi.ObjectReference, fldPath *field.Path, kind string) (allErrs field.ErrorList) {
  119. if len(ref.Kind) == 0 {
  120. allErrs = append(allErrs, field.Required(fldPath.Child("kind"), ""))
  121. } else if ref.Kind != kind {
  122. allErrs = append(allErrs, field.Invalid(fldPath.Child("kind"), ref.Kind, "must be "+kind))
  123. }
  124. if len(ref.Name) == 0 {
  125. allErrs = append(allErrs, field.Required(fldPath.Child("name"), ""))
  126. } else {
  127. for _, msg := range ValidateTemplateName(ref.Name, false) {
  128. allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), ref.Name, msg))
  129. }
  130. }
  131. if len(ref.Namespace) == 0 {
  132. allErrs = append(allErrs, field.Required(fldPath.Child("namespace"), ""))
  133. } else {
  134. for _, msg := range validation.ValidateNamespaceName(ref.Namespace, false) {
  135. allErrs = append(allErrs, field.Invalid(fldPath.Child("namespace"), ref.Namespace, msg))
  136. }
  137. }
  138. return allErrs
  139. }