Skip to content

Commit

Permalink
Refactor common broker validations (openshift#1865)
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Nelson authored and pmorie committed Mar 22, 2018
1 parent eeaf285 commit 5d0f773
Showing 1 changed file with 37 additions and 23 deletions.
60 changes: 37 additions & 23 deletions pkg/apis/servicecatalog/validation/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ import (
sc "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog"
)

// ValidateClusterServiceBrokerName is the validation function for Broker names.
var ValidateClusterServiceBrokerName = apivalidation.NameIsDNSSubdomain
// validateCommonServiceBrokerName is the validation function for common
// broker names.
var validateCommonServiceBrokerName = apivalidation.NameIsDNSSubdomain

// ValidateClusterServiceBroker implements the validation rules for a BrokerResource.
// ValidateClusterServiceBroker implements the validation rules for a
// ClusterServiceBroker.
func ValidateClusterServiceBroker(broker *sc.ClusterServiceBroker) field.ErrorList {
allErrs := field.ErrorList{}

allErrs = append(allErrs,
apivalidation.ValidateObjectMeta(&broker.ObjectMeta,
false, /* namespace required */
ValidateClusterServiceBrokerName,
validateCommonServiceBrokerName,
field.NewPath("metadata"))...)

allErrs = append(allErrs, validateClusterServiceBrokerSpec(&broker.Spec, field.NewPath("spec"))...)
Expand All @@ -44,12 +46,6 @@ func ValidateClusterServiceBroker(broker *sc.ClusterServiceBroker) field.ErrorLi
func validateClusterServiceBrokerSpec(spec *sc.ClusterServiceBrokerSpec, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}

if "" == spec.URL {
allErrs = append(allErrs,
field.Required(fldPath.Child("url"),
"brokers must have a remote url to contact"))
}

// if there is auth information, check it to make sure that it's properly formatted
if spec.AuthInfo != nil {
if spec.AuthInfo.Basic != nil {
Expand Down Expand Up @@ -91,12 +87,30 @@ func validateClusterServiceBrokerSpec(spec *sc.ClusterServiceBrokerSpec, fldPath
}
}

commonErrs := validateCommonServiceBrokerSpec(&spec.CommonServiceBrokerSpec, fldPath)

if len(commonErrs) != 0 {
allErrs = append(commonErrs)
}

return allErrs
}

func validateCommonServiceBrokerSpec(spec *sc.CommonServiceBrokerSpec, fldPath *field.Path) field.ErrorList {
commonErrs := field.ErrorList{}

if "" == spec.URL {
commonErrs = append(commonErrs,
field.Required(fldPath.Child("url"),
"brokers must have a remote url to contact"))
}

if spec.InsecureSkipTLSVerify && len(spec.CABundle) > 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("caBundle"), spec.CABundle, "caBundle cannot be used when insecureSkipTLSVerify is true"))
commonErrs = append(commonErrs, field.Invalid(fldPath.Child("caBundle"), spec.CABundle, "caBundle cannot be used when insecureSkipTLSVerify is true"))
}

if "" == spec.RelistBehavior {
allErrs = append(allErrs,
commonErrs = append(commonErrs,
field.Required(fldPath.Child("relistBehavior"),
"relist behavior is required"))
}
Expand All @@ -105,44 +119,44 @@ func validateClusterServiceBrokerSpec(spec *sc.ClusterServiceBrokerSpec, fldPath
spec.RelistBehavior == sc.ServiceBrokerRelistBehaviorManual
if !isValidRelistBehavior {
errMsg := "relist behavior must be \"Manual\" or \"Duration\""
allErrs = append(
allErrs,
commonErrs = append(
commonErrs,
field.Required(fldPath.Child("relistBehavior"), errMsg),
)
}

if spec.RelistBehavior == sc.ServiceBrokerRelistBehaviorDuration && spec.RelistDuration == nil {
allErrs = append(
allErrs,
commonErrs = append(
commonErrs,
field.Required(fldPath.Child("relistDuration"), "relistDuration must be set if relistBehavior is set to Duration"),
)
}

if spec.RelistBehavior == sc.ServiceBrokerRelistBehaviorManual && spec.RelistDuration != nil {
allErrs = append(
allErrs,
commonErrs = append(
commonErrs,
field.Required(fldPath.Child("relistDuration"), "relistDuration must not be set if relistBehavior is set to Manual"),
)
}

if spec.RelistRequests < 0 {
allErrs = append(
allErrs,
commonErrs = append(
commonErrs,
field.Required(fldPath.Child("relistRequests"), "relistRequests must be greater than zero"),
)
}

if spec.RelistDuration != nil {
zeroDuration := metav1.Duration{Duration: 0}
if spec.RelistDuration.Duration <= zeroDuration.Duration {
allErrs = append(
allErrs,
commonErrs = append(
commonErrs,
field.Required(fldPath.Child("relistDuration"), "relistDuration must be greater than zero"),
)
}
}

return allErrs
return commonErrs
}

// ValidateClusterServiceBrokerUpdate checks that when changing from an older broker to a newer broker is okay ?
Expand Down

0 comments on commit 5d0f773

Please sign in to comment.