forked from openshift/origin
/
serviceclass.go
107 lines (84 loc) · 3.95 KB
/
serviceclass.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package validation
import (
"regexp"
apivalidation "k8s.io/apimachinery/pkg/api/validation"
utilvalidation "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
sc "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog"
)
// validateServiceClassName is the validation function for ServiceClass names.
var validateServiceClassName = apivalidation.NameIsDNSSubdomain
const guidFmt string = "[a-zA-Z0-9]([-a-zA-Z0-9.]*[a-zA-Z0-9])?"
const guidMaxLength int = 63
// guidRegexp is a loosened validation for
// DNS1123 labels that allows uppercase characters.
var guidRegexp = regexp.MustCompile("^" + guidFmt + "$")
// validateExternalID is the validation function for External IDs that
// have been passed in. External IDs used to be OpenServiceBrokerAPI
// GUIDs, so we will retain that form until there is another provider
// that desires a different form. In the case of the OSBAPI we
// generate GUIDs for ServiceInstances and ServiceBindings, but for ClusterServiceClass and
// ServicePlan, they are part of the payload returned from the ClusterServiceBroker.
func validateExternalID(value string) []string {
var errs []string
if len(value) > guidMaxLength {
errs = append(errs, utilvalidation.MaxLenError(guidMaxLength))
}
if !guidRegexp.MatchString(value) {
errs = append(errs, utilvalidation.RegexError(guidFmt, "my-name", "123-abc", "456-DEF"))
}
return errs
}
// ValidateClusterServiceClass validates a ClusterServiceClass and returns a list of errors.
func ValidateClusterServiceClass(serviceclass *sc.ClusterServiceClass) field.ErrorList {
return internalValidateClusterServiceClass(serviceclass)
}
func internalValidateClusterServiceClass(serviceclass *sc.ClusterServiceClass) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs,
apivalidation.ValidateObjectMeta(
&serviceclass.ObjectMeta,
false, /* namespace required */
validateServiceClassName,
field.NewPath("metadata"))...)
allErrs = append(allErrs, validateClusterServiceClassSpec(&serviceclass.Spec, field.NewPath("spec"), true)...)
return allErrs
}
func validateClusterServiceClassSpec(spec *sc.ClusterServiceClassSpec, fldPath *field.Path, create bool) field.ErrorList {
allErrs := field.ErrorList{}
if "" == spec.ClusterServiceBrokerName {
allErrs = append(allErrs, field.Required(fldPath.Child("clusterServiceBrokerName"), "clusterServiceBrokerName is required"))
}
if "" == spec.ExternalID {
allErrs = append(allErrs, field.Required(fldPath.Child("externalID"), "externalID is required"))
}
if "" == spec.Description {
allErrs = append(allErrs, field.Required(fldPath.Child("description"), "description is required"))
}
for _, msg := range validateServiceClassName(spec.ExternalName, false /* prefix */) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("externalName"), spec.ExternalName, msg))
}
for _, msg := range validateExternalID(spec.ExternalID) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("externalID"), spec.ExternalID, msg))
}
return allErrs
}
// ValidateClusterServiceClassUpdate checks that when changing from an older
// ClusterServiceClass to a newer ClusterServiceClass is okay.
func ValidateClusterServiceClassUpdate(new *sc.ClusterServiceClass, old *sc.ClusterServiceClass) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, internalValidateClusterServiceClass(new)...)
return allErrs
}