...
1
16
17 package validation
18
19 import (
20 "fmt"
21
22 "k8s.io/apimachinery/pkg/apis/meta/internalversion"
23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 "k8s.io/apimachinery/pkg/util/validation/field"
25 )
26
27
28 func ValidateListOptions(options *internalversion.ListOptions, isWatchListFeatureEnabled bool) field.ErrorList {
29 if options.Watch {
30 return validateWatchOptions(options, isWatchListFeatureEnabled)
31 }
32 allErrs := field.ErrorList{}
33 if match := options.ResourceVersionMatch; len(match) > 0 {
34 if len(options.ResourceVersion) == 0 {
35 allErrs = append(allErrs, field.Forbidden(field.NewPath("resourceVersionMatch"), "resourceVersionMatch is forbidden unless resourceVersion is provided"))
36 }
37 if len(options.Continue) > 0 {
38 allErrs = append(allErrs, field.Forbidden(field.NewPath("resourceVersionMatch"), "resourceVersionMatch is forbidden when continue is provided"))
39 }
40 if match != metav1.ResourceVersionMatchExact && match != metav1.ResourceVersionMatchNotOlderThan {
41 allErrs = append(allErrs, field.NotSupported(field.NewPath("resourceVersionMatch"), match, []string{string(metav1.ResourceVersionMatchExact), string(metav1.ResourceVersionMatchNotOlderThan), ""}))
42 }
43 if match == metav1.ResourceVersionMatchExact && options.ResourceVersion == "0" {
44 allErrs = append(allErrs, field.Forbidden(field.NewPath("resourceVersionMatch"), "resourceVersionMatch \"exact\" is forbidden for resourceVersion \"0\""))
45 }
46 }
47 if options.SendInitialEvents != nil {
48 allErrs = append(allErrs, field.Forbidden(field.NewPath("sendInitialEvents"), "sendInitialEvents is forbidden for list"))
49 }
50 return allErrs
51 }
52
53 func validateWatchOptions(options *internalversion.ListOptions, isWatchListFeatureEnabled bool) field.ErrorList {
54 allErrs := field.ErrorList{}
55 match := options.ResourceVersionMatch
56 if options.SendInitialEvents != nil {
57 if match != metav1.ResourceVersionMatchNotOlderThan {
58 allErrs = append(allErrs, field.Forbidden(field.NewPath("resourceVersionMatch"), fmt.Sprintf("sendInitialEvents requires setting resourceVersionMatch to %s", metav1.ResourceVersionMatchNotOlderThan)))
59 }
60 if !isWatchListFeatureEnabled {
61 allErrs = append(allErrs, field.Forbidden(field.NewPath("sendInitialEvents"), "sendInitialEvents is forbidden for watch unless the WatchList feature gate is enabled"))
62 }
63 }
64 if len(match) > 0 {
65 if options.SendInitialEvents == nil {
66 allErrs = append(allErrs, field.Forbidden(field.NewPath("resourceVersionMatch"), "resourceVersionMatch is forbidden for watch unless sendInitialEvents is provided"))
67 }
68 if match != metav1.ResourceVersionMatchNotOlderThan {
69 allErrs = append(allErrs, field.NotSupported(field.NewPath("resourceVersionMatch"), match, []string{string(metav1.ResourceVersionMatchNotOlderThan)}))
70 }
71 if len(options.Continue) > 0 {
72 allErrs = append(allErrs, field.Forbidden(field.NewPath("resourceVersionMatch"), "resourceVersionMatch is forbidden when continue is provided"))
73 }
74 }
75 return allErrs
76 }
77
View as plain text