Define an errorList type and use it to accumulate

This commit is contained in:
Tim Hockin 2014-07-08 17:33:15 -07:00
parent 54790080b3
commit 4ecefd1ba3

View File

@ -63,16 +63,23 @@ func makeNotFoundError(field string, value interface{}) ValidationError {
return ValidationError{ErrTypeNotFound, field, value} return ValidationError{ErrTypeNotFound, field, value}
} }
func validateVolumes(volumes []Volume) (util.StringSet, []error) { // A helper for accumulating errors. This could be moved to util if anyone else needs it.
allErrs := []error{} type errorList []error;
func (list *errorList) Append(errs ...error) {
*list = append(*list, errs...)
}
func validateVolumes(volumes []Volume) (util.StringSet, errorList) {
allErrs := errorList{}
allNames := util.StringSet{} allNames := util.StringSet{}
for i := range volumes { for i := range volumes {
vol := &volumes[i] // so we can set default values vol := &volumes[i] // so we can set default values
if !util.IsDNSLabel(vol.Name) { if !util.IsDNSLabel(vol.Name) {
allErrs = append(allErrs, makeInvalidError("Volume.Name", vol.Name)) allErrs.Append(makeInvalidError("Volume.Name", vol.Name))
} else if allNames.Has(vol.Name) { } else if allNames.Has(vol.Name) {
allErrs = append(allErrs, makeDuplicateError("Volume.Name", vol.Name)) allErrs.Append(makeDuplicateError("Volume.Name", vol.Name))
} else { } else {
allNames.Insert(vol.Name) allNames.Insert(vol.Name)
} }
@ -82,47 +89,47 @@ func validateVolumes(volumes []Volume) (util.StringSet, []error) {
var supportedPortProtocols util.StringSet = util.NewStringSet("TCP", "UDP") var supportedPortProtocols util.StringSet = util.NewStringSet("TCP", "UDP")
func validatePorts(ports []Port) []error { func validatePorts(ports []Port) errorList {
allErrs := []error{} allErrs := errorList{}
allNames := util.StringSet{} allNames := util.StringSet{}
for i := range ports { for i := range ports {
port := &ports[i] // so we can set default values port := &ports[i] // so we can set default values
if len(port.Name) > 0 { if len(port.Name) > 0 {
if len(port.Name) > 63 || !util.IsDNSLabel(port.Name) { if len(port.Name) > 63 || !util.IsDNSLabel(port.Name) {
allErrs = append(allErrs, makeInvalidError("Port.Name", port.Name)) allErrs.Append(makeInvalidError("Port.Name", port.Name))
} else if allNames.Has(port.Name) { } else if allNames.Has(port.Name) {
allErrs = append(allErrs, makeDuplicateError("Port.name", port.Name)) allErrs.Append(makeDuplicateError("Port.name", port.Name))
} else { } else {
allNames.Insert(port.Name) allNames.Insert(port.Name)
} }
} }
if !util.IsValidPortNum(port.ContainerPort) { if !util.IsValidPortNum(port.ContainerPort) {
allErrs = append(allErrs, makeInvalidError("Port.ContainerPort", port.ContainerPort)) allErrs.Append(makeInvalidError("Port.ContainerPort", port.ContainerPort))
} }
if port.HostPort == 0 { if port.HostPort == 0 {
port.HostPort = port.ContainerPort port.HostPort = port.ContainerPort
} else if !util.IsValidPortNum(port.HostPort) { } else if !util.IsValidPortNum(port.HostPort) {
allErrs = append(allErrs, makeInvalidError("Port.HostPort", port.HostPort)) allErrs.Append(makeInvalidError("Port.HostPort", port.HostPort))
} }
if len(port.Protocol) == 0 { if len(port.Protocol) == 0 {
port.Protocol = "TCP" port.Protocol = "TCP"
} else if !supportedPortProtocols.Has(strings.ToUpper(port.Protocol)) { } else if !supportedPortProtocols.Has(strings.ToUpper(port.Protocol)) {
allErrs = append(allErrs, makeNotSupportedError("Port.Protocol", port.Protocol)) allErrs.Append(makeNotSupportedError("Port.Protocol", port.Protocol))
} }
} }
return allErrs return allErrs
} }
func validateEnv(vars []EnvVar) []error { func validateEnv(vars []EnvVar) errorList {
allErrs := []error{} allErrs := errorList{}
for i := range vars { for i := range vars {
ev := &vars[i] // so we can set default values ev := &vars[i] // so we can set default values
if len(ev.Name) == 0 { if len(ev.Name) == 0 {
// Backwards compat. // Backwards compat.
if len(ev.Key) == 0 { if len(ev.Key) == 0 {
allErrs = append(allErrs, makeInvalidError("EnvVar.Name", ev.Name)) allErrs.Append(makeInvalidError("EnvVar.Name", ev.Name))
} else { } else {
glog.Warning("DEPRECATED: EnvVar.Key has been replaced by EnvVar.Name") glog.Warning("DEPRECATED: EnvVar.Key has been replaced by EnvVar.Name")
ev.Name = ev.Key ev.Name = ev.Key
@ -130,26 +137,26 @@ func validateEnv(vars []EnvVar) []error {
} }
} }
if !util.IsCIdentifier(ev.Name) { if !util.IsCIdentifier(ev.Name) {
allErrs = append(allErrs, makeInvalidError("EnvVar.Name", ev.Name)) allErrs.Append(makeInvalidError("EnvVar.Name", ev.Name))
} }
} }
return allErrs return allErrs
} }
func validateVolumeMounts(mounts []VolumeMount, volumes util.StringSet) []error { func validateVolumeMounts(mounts []VolumeMount, volumes util.StringSet) errorList {
allErrs := []error{} allErrs := errorList{}
for i := range mounts { for i := range mounts {
mnt := &mounts[i] // so we can set default values mnt := &mounts[i] // so we can set default values
if len(mnt.Name) == 0 { if len(mnt.Name) == 0 {
allErrs = append(allErrs, makeInvalidError("VolumeMount.Name", mnt.Name)) allErrs.Append(makeInvalidError("VolumeMount.Name", mnt.Name))
} else if !volumes.Has(mnt.Name) { } else if !volumes.Has(mnt.Name) {
allErrs = append(allErrs, makeNotFoundError("VolumeMount.Name", mnt.Name)) allErrs.Append(makeNotFoundError("VolumeMount.Name", mnt.Name))
} }
if len(mnt.MountPath) == 0 { if len(mnt.MountPath) == 0 {
// Backwards compat. // Backwards compat.
if len(mnt.Path) == 0 { if len(mnt.Path) == 0 {
allErrs = append(allErrs, makeInvalidError("VolumeMount.MountPath", mnt.MountPath)) allErrs.Append(makeInvalidError("VolumeMount.MountPath", mnt.MountPath))
} else { } else {
glog.Warning("DEPRECATED: VolumeMount.Path has been replaced by VolumeMount.MountPath") glog.Warning("DEPRECATED: VolumeMount.Path has been replaced by VolumeMount.MountPath")
mnt.MountPath = mnt.Path mnt.MountPath = mnt.Path
@ -162,15 +169,15 @@ func validateVolumeMounts(mounts []VolumeMount, volumes util.StringSet) []error
// AccumulateUniquePorts runs an extraction function on each Port of each Container, // AccumulateUniquePorts runs an extraction function on each Port of each Container,
// accumulating the results and returning an error if any ports conflict. // accumulating the results and returning an error if any ports conflict.
func AccumulateUniquePorts(containers []Container, accumulator map[int]bool, extract func(*Port) int) []error { func AccumulateUniquePorts(containers []Container, accumulator map[int]bool, extract func(*Port) int) errorList {
allErrs := []error{} allErrs := errorList{}
for ci := range containers { for ci := range containers {
ctr := &containers[ci] ctr := &containers[ci]
for pi := range ctr.Ports { for pi := range ctr.Ports {
port := extract(&ctr.Ports[pi]) port := extract(&ctr.Ports[pi])
if accumulator[port] { if accumulator[port] {
allErrs = append(allErrs, makeDuplicateError("Port", port)) allErrs.Append(makeDuplicateError("Port", port))
} else { } else {
accumulator[port] = true accumulator[port] = true
} }
@ -180,46 +187,39 @@ func AccumulateUniquePorts(containers []Container, accumulator map[int]bool, ext
} }
// Checks for colliding Port.HostPort values across a slice of containers. // Checks for colliding Port.HostPort values across a slice of containers.
func checkHostPortConflicts(containers []Container) []error { func checkHostPortConflicts(containers []Container) errorList {
allPorts := map[int]bool{} allPorts := map[int]bool{}
return AccumulateUniquePorts(containers, allPorts, func(p *Port) int { return p.HostPort }) return AccumulateUniquePorts(containers, allPorts, func(p *Port) int { return p.HostPort })
} }
func validateContainers(containers []Container, volumes util.StringSet) []error { func validateContainers(containers []Container, volumes util.StringSet) errorList {
allErrs := []error{} allErrs := errorList{}
allNames := util.StringSet{} allNames := util.StringSet{}
for i := range containers { for i := range containers {
ctr := &containers[i] // so we can set default values ctr := &containers[i] // so we can set default values
if !util.IsDNSLabel(ctr.Name) { if !util.IsDNSLabel(ctr.Name) {
allErrs = append(allErrs, makeInvalidError("Container.Name", ctr.Name)) allErrs.Append(makeInvalidError("Container.Name", ctr.Name))
} else if allNames.Has(ctr.Name) { } else if allNames.Has(ctr.Name) {
allErrs = append(allErrs, makeDuplicateError("Container.Name", ctr.Name)) allErrs.Append(makeDuplicateError("Container.Name", ctr.Name))
} else { } else {
allNames.Insert(ctr.Name) allNames.Insert(ctr.Name)
} }
if len(ctr.Image) == 0 { if len(ctr.Image) == 0 {
allErrs = append(allErrs, makeInvalidError("Container.Image", ctr.Name)) allErrs.Append(makeInvalidError("Container.Image", ctr.Name))
}
if errs := validatePorts(ctr.Ports); len(errs) != 0 {
allErrs = append(allErrs, errs...)
}
if errs := validateEnv(ctr.Env); len(errs) != 0 {
allErrs = append(allErrs, errs...)
}
if errs := validateVolumeMounts(ctr.VolumeMounts, volumes); len(errs) != 0 {
allErrs = append(allErrs, errs...)
} }
allErrs.Append(validatePorts(ctr.Ports)...)
allErrs.Append(validateEnv(ctr.Env)...)
allErrs.Append(validateVolumeMounts(ctr.VolumeMounts, volumes)...)
} }
// Check for colliding ports across all containers. // Check for colliding ports across all containers.
// TODO(thockin): This really is dependent on the network config of the host (IP per pod?) // TODO(thockin): This really is dependent on the network config of the host (IP per pod?)
// and the config of the new manifest. But we have not specced that out yet, so we'll just // and the config of the new manifest. But we have not specced that out yet, so we'll just
// make some assumptions for now. As of now, pods share a network namespace, which means that // make some assumptions for now. As of now, pods share a network namespace, which means that
// every Port.HostPort across the whole pod must be unique. // every Port.HostPort across the whole pod must be unique.
if errs := checkHostPortConflicts(containers); len(errs) != 0 { allErrs.Append(checkHostPortConflicts(containers)...)
allErrs = append(allErrs, errs...)
} return allErrs;
return allErrs
} }
var supportedManifestVersions util.StringSet = util.NewStringSet("v1beta1", "v1beta2") var supportedManifestVersions util.StringSet = util.NewStringSet("v1beta1", "v1beta2")
@ -229,22 +229,20 @@ var supportedManifestVersions util.StringSet = util.NewStringSet("v1beta1", "v1b
// structure by setting default values and implementing any backwards-compatibility // structure by setting default values and implementing any backwards-compatibility
// tricks. // tricks.
func ValidateManifest(manifest *ContainerManifest) []error { func ValidateManifest(manifest *ContainerManifest) []error {
allErrs := []error{} allErrs := errorList{}
if len(manifest.Version) == 0 { if len(manifest.Version) == 0 {
allErrs = append(allErrs, makeInvalidError("ContainerManifest.Version", manifest.Version)) allErrs.Append(makeInvalidError("ContainerManifest.Version", manifest.Version))
} else if !supportedManifestVersions.Has(strings.ToLower(manifest.Version)) { } else if !supportedManifestVersions.Has(strings.ToLower(manifest.Version)) {
allErrs = append(allErrs, makeNotSupportedError("ContainerManifest.Version", manifest.Version)) allErrs.Append(makeNotSupportedError("ContainerManifest.Version", manifest.Version))
} }
if !util.IsDNSSubdomain(manifest.ID) { if !util.IsDNSSubdomain(manifest.ID) {
allErrs = append(allErrs, makeInvalidError("ContainerManifest.ID", manifest.ID)) allErrs.Append(makeInvalidError("ContainerManifest.ID", manifest.ID))
} }
allVolumes, errs := validateVolumes(manifest.Volumes) allVolumes, errs := validateVolumes(manifest.Volumes)
if len(errs) != 0 { if len(errs) != 0 {
allErrs = append(allErrs, errs...) allErrs.Append(errs...)
} }
if errs := validateContainers(manifest.Containers, allVolumes); len(errs) != 0 { allErrs.Append(validateContainers(manifest.Containers, allVolumes)...)
allErrs = append(allErrs, errs...) return []error(allErrs)
}
return allErrs
} }