events.go 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright 2014 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. "fmt"
  16. "time"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/util/validation"
  19. "k8s.io/apimachinery/pkg/util/validation/field"
  20. "k8s.io/kubernetes/pkg/apis/core"
  21. )
  22. const (
  23. ReportingInstanceLengthLimit = 128
  24. ActionLengthLimit = 128
  25. ReasonLengthLimit = 128
  26. NoteLengthLimit = 1024
  27. )
  28. // ValidateEvent makes sure that the event makes sense.
  29. func ValidateEvent(event *core.Event) field.ErrorList {
  30. allErrs := field.ErrorList{}
  31. // Because go
  32. zeroTime := time.Time{}
  33. // "New" Events need to have EventTime set, so it's validating old object.
  34. if event.EventTime.Time == zeroTime {
  35. // Make sure event.Namespace and the involvedInvolvedObject.Namespace agree
  36. if len(event.InvolvedObject.Namespace) == 0 {
  37. // event.Namespace must also be empty (or "default", for compatibility with old clients)
  38. if event.Namespace != metav1.NamespaceNone && event.Namespace != metav1.NamespaceDefault {
  39. allErrs = append(allErrs, field.Invalid(field.NewPath("involvedObject", "namespace"), event.InvolvedObject.Namespace, "does not match event.namespace"))
  40. }
  41. } else {
  42. // event namespace must match
  43. if event.Namespace != event.InvolvedObject.Namespace {
  44. allErrs = append(allErrs, field.Invalid(field.NewPath("involvedObject", "namespace"), event.InvolvedObject.Namespace, "does not match event.namespace"))
  45. }
  46. }
  47. } else {
  48. if len(event.InvolvedObject.Namespace) == 0 && event.Namespace != metav1.NamespaceSystem {
  49. allErrs = append(allErrs, field.Invalid(field.NewPath("involvedObject", "namespace"), event.InvolvedObject.Namespace, "does not match event.namespace"))
  50. }
  51. if len(event.ReportingController) == 0 {
  52. allErrs = append(allErrs, field.Required(field.NewPath("reportingController"), ""))
  53. }
  54. for _, msg := range validation.IsQualifiedName(event.ReportingController) {
  55. allErrs = append(allErrs, field.Invalid(field.NewPath("reportingController"), event.ReportingController, msg))
  56. }
  57. if len(event.ReportingInstance) == 0 {
  58. allErrs = append(allErrs, field.Required(field.NewPath("reportingInstance"), ""))
  59. }
  60. if len(event.ReportingInstance) > ReportingInstanceLengthLimit {
  61. allErrs = append(allErrs, field.Invalid(field.NewPath("reportingInstance"), "", fmt.Sprintf("can have at most %v characters", ReportingInstanceLengthLimit)))
  62. }
  63. if len(event.Action) == 0 {
  64. allErrs = append(allErrs, field.Required(field.NewPath("action"), ""))
  65. }
  66. if len(event.Action) > ActionLengthLimit {
  67. allErrs = append(allErrs, field.Invalid(field.NewPath("action"), "", fmt.Sprintf("can have at most %v characters", ActionLengthLimit)))
  68. }
  69. if len(event.Reason) == 0 {
  70. allErrs = append(allErrs, field.Required(field.NewPath("reason"), ""))
  71. }
  72. if len(event.Reason) > ReasonLengthLimit {
  73. allErrs = append(allErrs, field.Invalid(field.NewPath("reason"), "", fmt.Sprintf("can have at most %v characters", ReasonLengthLimit)))
  74. }
  75. if len(event.Message) > NoteLengthLimit {
  76. allErrs = append(allErrs, field.Invalid(field.NewPath("message"), "", fmt.Sprintf("can have at most %v characters", NoteLengthLimit)))
  77. }
  78. }
  79. for _, msg := range validation.IsDNS1123Subdomain(event.Namespace) {
  80. allErrs = append(allErrs, field.Invalid(field.NewPath("namespace"), event.Namespace, msg))
  81. }
  82. return allErrs
  83. }