Add validation of Ports
Also do caseless compares for "enum" strings.
This commit is contained in:
@@ -18,15 +18,12 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
var (
|
||||
supportedManifestVersions = util.NewStringSet("v1beta1", "v1beta2")
|
||||
)
|
||||
|
||||
// ValidationErrorEnum is a type of validation error.
|
||||
type ValidationErrorEnum string
|
||||
|
||||
@@ -81,6 +78,38 @@ func validateVolumes(volumes []Volume) (util.StringSet, error) {
|
||||
return allNames, nil
|
||||
}
|
||||
|
||||
var supportedPortProtocols util.StringSet = util.NewStringSet("TCP", "UDP")
|
||||
|
||||
func validatePorts(ports []Port) error {
|
||||
allNames := util.StringSet{}
|
||||
for i := range ports {
|
||||
port := &ports[i] // so we can set default values
|
||||
if len(port.Name) > 0 {
|
||||
if len(port.Name) > 63 || !util.IsDNSLabel(port.Name) {
|
||||
return makeInvalidError("Port.Name", port.Name)
|
||||
}
|
||||
if allNames.Has(port.Name) {
|
||||
return makeDuplicateError("Port.name", port.Name)
|
||||
}
|
||||
allNames.Insert(port.Name)
|
||||
}
|
||||
if !util.IsValidPortNum(port.ContainerPort) {
|
||||
return makeInvalidError("Port.ContainerPort", port.ContainerPort)
|
||||
}
|
||||
if port.HostPort == 0 {
|
||||
port.HostPort = port.ContainerPort
|
||||
} else if !util.IsValidPortNum(port.HostPort) {
|
||||
return makeInvalidError("Port.HostPort", port.HostPort)
|
||||
}
|
||||
if len(port.Protocol) == 0 {
|
||||
port.Protocol = "TCP"
|
||||
} else if !supportedPortProtocols.Has(strings.ToUpper(port.Protocol)) {
|
||||
return makeNotSupportedError("Port.Protocol", port.Protocol)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateEnv(vars []EnvVar) error {
|
||||
for i := range vars {
|
||||
ev := &vars[i] // so we can set default values
|
||||
@@ -122,6 +151,28 @@ func validateVolumeMounts(mounts []VolumeMount, volumes util.StringSet) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AccumulateUniquePorts runs an extraction function on each Port of each Container,
|
||||
// accumulating the results and returning an error if any ports conflict.
|
||||
func AccumulateUniquePorts(containers []Container, accumulator map[int]bool, extract func(*Port) int) error {
|
||||
for ci := range containers {
|
||||
ctr := &containers[ci]
|
||||
for pi := range ctr.Ports {
|
||||
port := extract(&ctr.Ports[pi])
|
||||
if accumulator[port] {
|
||||
return makeDuplicateError("Port", port)
|
||||
}
|
||||
accumulator[port] = true
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Checks for colliding Port.HostPort values across a slice of containers.
|
||||
func checkHostPortConflicts(containers []Container) error {
|
||||
allPorts := map[int]bool{}
|
||||
return AccumulateUniquePorts(containers, allPorts, func(p *Port) int { return p.HostPort })
|
||||
}
|
||||
|
||||
func validateContainers(containers []Container, volumes util.StringSet) error {
|
||||
allNames := util.StringSet{}
|
||||
for i := range containers {
|
||||
@@ -136,17 +187,26 @@ func validateContainers(containers []Container, volumes util.StringSet) error {
|
||||
if len(ctr.Image) == 0 {
|
||||
return makeInvalidError("Container.Image", ctr.Name)
|
||||
}
|
||||
if err := validatePorts(ctr.Ports); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateEnv(ctr.Env); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateVolumeMounts(ctr.VolumeMounts, volumes); err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO(thockin): finish validation.
|
||||
}
|
||||
return nil
|
||||
// Check for colliding ports across all containers.
|
||||
// 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
|
||||
// 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.
|
||||
return checkHostPortConflicts(containers)
|
||||
}
|
||||
|
||||
var supportedManifestVersions util.StringSet = util.NewStringSet("v1beta1", "v1beta2")
|
||||
|
||||
// ValidateManifest tests that the specified ContainerManifest has valid data.
|
||||
// This includes checking formatting and uniqueness. It also canonicalizes the
|
||||
// structure by setting default values and implementing any backwards-compatibility
|
||||
@@ -156,7 +216,7 @@ func ValidateManifest(manifest *ContainerManifest) error {
|
||||
if len(manifest.Version) == 0 {
|
||||
return makeInvalidError("ContainerManifest.Version", manifest.Version)
|
||||
}
|
||||
if !supportedManifestVersions.Has(manifest.Version) {
|
||||
if !supportedManifestVersions.Has(strings.ToLower(manifest.Version)) {
|
||||
return makeNotSupportedError("ContainerManifest.Version", manifest.Version)
|
||||
}
|
||||
if len(manifest.ID) > 255 || !util.IsDNSSubdomain(manifest.ID) {
|
||||
|
Reference in New Issue
Block a user