@@ -18,6 +18,7 @@ package validation
18
18
19
19
import (
20
20
"fmt"
21
+ "time"
21
22
22
23
"k8s.io/apimachinery/pkg/api/meta"
23
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -28,24 +29,68 @@ import (
28
29
"k8s.io/kubernetes/pkg/apis/core"
29
30
)
30
31
32
+ const (
33
+ ReportingInstanceLengthLimit = 128
34
+ ActionLengthLimit = 128
35
+ ReasonLengthLimit = 128
36
+ NoteLengthLimit = 1024
37
+ )
38
+
31
39
// ValidateEvent makes sure that the event makes sense.
32
40
func ValidateEvent (event * core.Event ) field.ErrorList {
33
41
allErrs := field.ErrorList {}
42
+ // Because go
43
+ zeroTime := time.Time {}
34
44
35
- // Make sure event.Namespace and the involvedObject.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" ))
45
+ // "New" Events need to have EventTime set, so it's validating old object.
46
+ if event .EventTime .Time == zeroTime {
47
+ // Make sure event.Namespace and the involvedInvolvedObject.Namespace agree
48
+ if len (event .InvolvedObject .Namespace ) == 0 {
49
+ // event.Namespace must also be empty (or "default", for compatibility with old clients)
50
+ if event .Namespace != metav1 .NamespaceNone && event .Namespace != metav1 .NamespaceDefault {
51
+ allErrs = append (allErrs , field .Invalid (field .NewPath ("involvedObject" , "namespace" ), event .InvolvedObject .Namespace , "does not match event.namespace" ))
52
+ }
53
+ } else {
54
+ // event namespace must match
55
+ if event .Namespace != event .InvolvedObject .Namespace {
56
+ allErrs = append (allErrs , field .Invalid (field .NewPath ("involvedObject" , "namespace" ), event .InvolvedObject .Namespace , "does not match event.namespace" ))
57
+ }
40
58
}
59
+
41
60
} else {
42
- // event namespace must match
43
- if event .Namespace != event .InvolvedObject .Namespace {
61
+ if len (event .InvolvedObject .Namespace ) == 0 && event .Namespace != metav1 .NamespaceSystem {
44
62
allErrs = append (allErrs , field .Invalid (field .NewPath ("involvedObject" , "namespace" ), event .InvolvedObject .Namespace , "does not match event.namespace" ))
45
63
}
64
+ if len (event .ReportingController ) == 0 {
65
+ allErrs = append (allErrs , field .Required (field .NewPath ("reportingController" ), "" ))
66
+ }
67
+ for _ , msg := range validation .IsQualifiedName (event .ReportingController ) {
68
+ allErrs = append (allErrs , field .Invalid (field .NewPath ("reportingController" ), event .ReportingController , msg ))
69
+ }
70
+ if len (event .ReportingInstance ) == 0 {
71
+ allErrs = append (allErrs , field .Required (field .NewPath ("reportingInstance" ), "" ))
72
+ }
73
+ if len (event .ReportingInstance ) > ReportingInstanceLengthLimit {
74
+ allErrs = append (allErrs , field .Invalid (field .NewPath ("repotingIntance" ), "" , fmt .Sprintf ("can have at most %v characters" , ReportingInstanceLengthLimit )))
75
+ }
76
+ if len (event .Action ) == 0 {
77
+ allErrs = append (allErrs , field .Required (field .NewPath ("action" ), "" ))
78
+ }
79
+ if len (event .Action ) > ActionLengthLimit {
80
+ allErrs = append (allErrs , field .Invalid (field .NewPath ("action" ), "" , fmt .Sprintf ("can have at most %v characters" , ActionLengthLimit )))
81
+ }
82
+ if len (event .Reason ) == 0 {
83
+ allErrs = append (allErrs , field .Required (field .NewPath ("reason" ), "" ))
84
+ }
85
+ if len (event .Reason ) > ReasonLengthLimit {
86
+ allErrs = append (allErrs , field .Invalid (field .NewPath ("reason" ), "" , fmt .Sprintf ("can have at most %v characters" , ReasonLengthLimit )))
87
+ }
88
+ if len (event .Message ) > NoteLengthLimit {
89
+ allErrs = append (allErrs , field .Invalid (field .NewPath ("message" ), "" , fmt .Sprintf ("can have at most %v characters" , NoteLengthLimit )))
90
+ }
46
91
}
47
92
48
- // For kinds we recognize, make sure involvedObject .Namespace is set for namespaced kinds
93
+ // For kinds we recognize, make sure InvolvedObject .Namespace is set for namespaced kinds
49
94
if namespaced , err := isNamespacedKind (event .InvolvedObject .Kind , event .InvolvedObject .APIVersion ); err == nil {
50
95
if namespaced && len (event .InvolvedObject .Namespace ) == 0 {
51
96
allErrs = append (allErrs , field .Required (field .NewPath ("involvedObject" , "namespace" ), fmt .Sprintf ("required for kind %s" , event .InvolvedObject .Kind )))
0 commit comments