blob: 348cdc0873bde0fd4bb242109000e4e7ad0b1914 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package validation
18
19import (
20 "strings"
21
22 "k8s.io/apimachinery/pkg/util/validation"
23 "k8s.io/apimachinery/pkg/util/validation/field"
24)
25
26const IsNegativeErrorMsg string = `must be greater than or equal to 0`
27
28// ValidateNameFunc validates that the provided name is valid for a given resource type.
29// Not all resources have the same validation rules for names. Prefix is true
30// if the name will have a value appended to it. If the name is not valid,
31// this returns a list of descriptions of individual characteristics of the
32// value that were not valid. Otherwise this returns an empty list or nil.
33type ValidateNameFunc func(name string, prefix bool) []string
34
35// NameIsDNSSubdomain is a ValidateNameFunc for names that must be a DNS subdomain.
36func NameIsDNSSubdomain(name string, prefix bool) []string {
37 if prefix {
38 name = maskTrailingDash(name)
39 }
40 return validation.IsDNS1123Subdomain(name)
41}
42
43// NameIsDNSLabel is a ValidateNameFunc for names that must be a DNS 1123 label.
44func NameIsDNSLabel(name string, prefix bool) []string {
45 if prefix {
46 name = maskTrailingDash(name)
47 }
48 return validation.IsDNS1123Label(name)
49}
50
51// NameIsDNS1035Label is a ValidateNameFunc for names that must be a DNS 952 label.
52func NameIsDNS1035Label(name string, prefix bool) []string {
53 if prefix {
54 name = maskTrailingDash(name)
55 }
56 return validation.IsDNS1035Label(name)
57}
58
59// ValidateNamespaceName can be used to check whether the given namespace name is valid.
60// Prefix indicates this name will be used as part of generation, in which case
61// trailing dashes are allowed.
62var ValidateNamespaceName = NameIsDNSLabel
63
64// ValidateServiceAccountName can be used to check whether the given service account name is valid.
65// Prefix indicates this name will be used as part of generation, in which case
66// trailing dashes are allowed.
67var ValidateServiceAccountName = NameIsDNSSubdomain
68
69// maskTrailingDash replaces the final character of a string with a subdomain safe
70// value if is a dash.
71func maskTrailingDash(name string) string {
72 if strings.HasSuffix(name, "-") {
73 return name[:len(name)-2] + "a"
74 }
75 return name
76}
77
78// Validates that given value is not negative.
79func ValidateNonnegativeField(value int64, fldPath *field.Path) field.ErrorList {
80 allErrs := field.ErrorList{}
81 if value < 0 {
82 allErrs = append(allErrs, field.Invalid(fldPath, value, IsNegativeErrorMsg))
83 }
84 return allErrs
85}