This repository has been archived by the owner on May 6, 2022. It is now read-only.
/
serviceclass.go
170 lines (132 loc) · 5.81 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
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 (
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-sigs/service-catalog/pkg/apis/servicecatalog/v1beta1"
)
const commonServiceClassNameMaxLength int = 63
const guidMaxLength int = 63
// validateCommonServiceClassName is the common validation function for
// service class types.
func validateCommonServiceClassName(value string, prefix bool) []string {
var errs []string
if len(value) > commonServiceClassNameMaxLength {
errs = append(errs, utilvalidation.MaxLenError(commonServiceClassNameMaxLength))
}
if len(value) == 0 {
errs = append(errs, utilvalidation.EmptyError())
}
return errs
}
// 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
// ServiceClasses and ServicePlan, they are part of the payload returned from
// the ServiceBroker.
func validateExternalID(value string) []string {
var errs []string
if len(value) > guidMaxLength {
errs = append(errs, utilvalidation.MaxLenError(guidMaxLength))
}
if len(value) == 0 {
errs = append(errs, utilvalidation.EmptyError())
}
return errs
}
// ValidateClusterServiceClass validates a ClusterServiceClass and returns a list of errors.
func ValidateClusterServiceClass(serviceclass *sc.ClusterServiceClass) field.ErrorList {
return internalValidateClusterServiceClass(serviceclass)
}
// 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
}
func internalValidateClusterServiceClass(clusterserviceclass *sc.ClusterServiceClass) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs,
apivalidation.ValidateObjectMeta(
&clusterserviceclass.ObjectMeta,
false, /* namespace required */
validateCommonServiceClassName,
field.NewPath("metadata"))...)
allErrs = append(allErrs, validateClusterServiceClassSpec(&clusterserviceclass.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"))
}
commonErrs := validateCommonServiceClassSpec(&spec.CommonServiceClassSpec, fldPath, create)
if len(commonErrs) != 0 {
allErrs = append(allErrs, commonErrs...)
}
return allErrs
}
// ValidateServiceClass validates a ServiceClass and returns a list of errors.
func ValidateServiceClass(serviceclass *sc.ServiceClass) field.ErrorList {
return internalValidateServiceClass(serviceclass)
}
// ValidateServiceClassUpdate checks that when changing from an older
// ServiceClass to a newer ServiceClass is okay.
func ValidateServiceClassUpdate(new *sc.ServiceClass, old *sc.ServiceClass) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, internalValidateServiceClass(new)...)
return allErrs
}
func internalValidateServiceClass(clusterserviceclass *sc.ServiceClass) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs,
apivalidation.ValidateObjectMeta(
&clusterserviceclass.ObjectMeta,
true, /* namespace required */
validateCommonServiceClassName,
field.NewPath("metadata"))...)
allErrs = append(allErrs, validateServiceClassSpec(&clusterserviceclass.Spec, field.NewPath("spec"), true)...)
return allErrs
}
func validateServiceClassSpec(spec *sc.ServiceClassSpec, fldPath *field.Path, create bool) field.ErrorList {
allErrs := field.ErrorList{}
if "" == spec.ServiceBrokerName {
allErrs = append(allErrs, field.Required(fldPath.Child("serviceBrokerName"), "serviceBrokerName is required"))
}
commonErrs := validateCommonServiceClassSpec(&spec.CommonServiceClassSpec, fldPath, create)
if len(commonErrs) != 0 {
allErrs = append(allErrs, commonErrs...)
}
return allErrs
}
func validateCommonServiceClassSpec(spec *sc.CommonServiceClassSpec, fldPath *field.Path, create bool) field.ErrorList {
commonErrs := field.ErrorList{}
if "" == spec.ExternalID {
commonErrs = append(commonErrs, field.Required(fldPath.Child("externalID"), "externalID is required"))
}
if "" == spec.Description {
commonErrs = append(commonErrs, field.Required(fldPath.Child("description"), "description is required"))
}
for _, msg := range validateCommonServiceClassName(spec.ExternalName, false /* prefix */) {
commonErrs = append(commonErrs, field.Invalid(fldPath.Child("externalName"), spec.ExternalName, msg))
}
for _, msg := range validateExternalID(spec.ExternalID) {
commonErrs = append(commonErrs, field.Invalid(fldPath.Child("externalID"), spec.ExternalID, msg))
}
return commonErrs
}