| 1 |
/* |
| 2 |
Copyright 2020 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 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 |
// ValidateListOptions returns all validation errors found while validating the ListOptions. |
| 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 |
} |