2014-07-01 20:01:39 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2014-08-29 22:48:41 +00:00
|
|
|
package validation
|
2014-07-01 20:01:39 +00:00
|
|
|
|
|
|
|
import (
|
2014-07-08 04:32:56 +00:00
|
|
|
"strings"
|
2014-07-01 20:01:39 +00:00
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-08-14 20:46:22 +00:00
|
|
|
errs "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
|
2014-09-16 14:04:12 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/capabilities"
|
2014-07-25 16:15:17 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
2014-07-01 20:01:39 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
|
|
|
)
|
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
func validateVolumes(volumes []api.Volume) (util.StringSet, errs.ErrorList) {
|
2014-08-14 20:46:22 +00:00
|
|
|
allErrs := errs.ErrorList{}
|
2014-07-08 06:20:30 +00:00
|
|
|
|
2014-07-01 21:40:36 +00:00
|
|
|
allNames := util.StringSet{}
|
|
|
|
for i := range volumes {
|
|
|
|
vol := &volumes[i] // so we can set default values
|
2014-08-14 20:46:22 +00:00
|
|
|
el := errs.ErrorList{}
|
2014-07-16 19:32:59 +00:00
|
|
|
// TODO(thockin) enforce that a source is set once we deprecate the implied form.
|
|
|
|
if vol.Source != nil {
|
2014-08-20 03:54:20 +00:00
|
|
|
el = validateSource(vol.Source).Prefix("source")
|
2014-07-15 01:39:30 +00:00
|
|
|
}
|
2014-08-20 02:57:48 +00:00
|
|
|
if len(vol.Name) == 0 {
|
2014-09-03 21:16:00 +00:00
|
|
|
el = append(el, errs.NewFieldRequired("name", vol.Name))
|
2014-08-20 02:57:48 +00:00
|
|
|
} else if !util.IsDNSLabel(vol.Name) {
|
2014-09-03 21:16:00 +00:00
|
|
|
el = append(el, errs.NewFieldInvalid("name", vol.Name))
|
2014-07-08 06:20:30 +00:00
|
|
|
} else if allNames.Has(vol.Name) {
|
2014-09-03 21:16:00 +00:00
|
|
|
el = append(el, errs.NewFieldDuplicate("name", vol.Name))
|
2014-07-16 19:32:59 +00:00
|
|
|
}
|
2014-08-14 20:46:22 +00:00
|
|
|
if len(el) == 0 {
|
2014-07-08 06:20:30 +00:00
|
|
|
allNames.Insert(vol.Name)
|
2014-07-16 19:32:59 +00:00
|
|
|
} else {
|
2014-08-20 03:54:20 +00:00
|
|
|
allErrs = append(allErrs, el.PrefixIndex(i)...)
|
2014-07-01 21:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-08 06:20:30 +00:00
|
|
|
return allNames, allErrs
|
2014-07-01 21:40:36 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
func validateSource(source *api.VolumeSource) errs.ErrorList {
|
2014-07-16 19:32:59 +00:00
|
|
|
numVolumes := 0
|
2014-08-14 20:46:22 +00:00
|
|
|
allErrs := errs.ErrorList{}
|
2014-07-16 19:32:59 +00:00
|
|
|
if source.HostDirectory != nil {
|
|
|
|
numVolumes++
|
2014-08-20 03:54:20 +00:00
|
|
|
allErrs = append(allErrs, validateHostDir(source.HostDirectory).Prefix("hostDirectory")...)
|
2014-07-16 19:32:59 +00:00
|
|
|
}
|
|
|
|
if source.EmptyDirectory != nil {
|
|
|
|
numVolumes++
|
|
|
|
//EmptyDirs have nothing to validate
|
|
|
|
}
|
|
|
|
if numVolumes != 1 {
|
2014-09-03 21:16:00 +00:00
|
|
|
allErrs = append(allErrs, errs.NewFieldInvalid("", source))
|
2014-07-16 19:32:59 +00:00
|
|
|
}
|
|
|
|
return allErrs
|
|
|
|
}
|
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
func validateHostDir(hostDir *api.HostDirectory) errs.ErrorList {
|
2014-08-14 20:46:22 +00:00
|
|
|
allErrs := errs.ErrorList{}
|
2014-07-15 01:39:30 +00:00
|
|
|
if hostDir.Path == "" {
|
2014-08-20 03:54:20 +00:00
|
|
|
allErrs = append(allErrs, errs.NewNotFound("path", hostDir.Path))
|
2014-07-15 01:39:30 +00:00
|
|
|
}
|
|
|
|
return allErrs
|
|
|
|
}
|
|
|
|
|
2014-07-10 11:46:35 +00:00
|
|
|
var supportedPortProtocols = util.NewStringSet("TCP", "UDP")
|
2014-07-08 04:32:56 +00:00
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
func validatePorts(ports []api.Port) errs.ErrorList {
|
2014-08-14 20:46:22 +00:00
|
|
|
allErrs := errs.ErrorList{}
|
2014-07-08 06:20:30 +00:00
|
|
|
|
2014-07-08 04:32:56 +00:00
|
|
|
allNames := util.StringSet{}
|
|
|
|
for i := range ports {
|
2014-08-20 03:54:20 +00:00
|
|
|
pErrs := errs.ErrorList{}
|
2014-07-08 04:32:56 +00:00
|
|
|
port := &ports[i] // so we can set default values
|
|
|
|
if len(port.Name) > 0 {
|
|
|
|
if len(port.Name) > 63 || !util.IsDNSLabel(port.Name) {
|
2014-09-03 21:16:00 +00:00
|
|
|
pErrs = append(pErrs, errs.NewFieldInvalid("name", port.Name))
|
2014-07-08 06:20:30 +00:00
|
|
|
} else if allNames.Has(port.Name) {
|
2014-09-03 21:16:00 +00:00
|
|
|
pErrs = append(pErrs, errs.NewFieldDuplicate("name", port.Name))
|
2014-07-08 06:20:30 +00:00
|
|
|
} else {
|
|
|
|
allNames.Insert(port.Name)
|
2014-07-08 04:32:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-20 02:57:48 +00:00
|
|
|
if port.ContainerPort == 0 {
|
2014-09-03 21:16:00 +00:00
|
|
|
pErrs = append(pErrs, errs.NewFieldRequired("containerPort", port.ContainerPort))
|
2014-08-20 02:57:48 +00:00
|
|
|
} else if !util.IsValidPortNum(port.ContainerPort) {
|
2014-09-03 21:16:00 +00:00
|
|
|
pErrs = append(pErrs, errs.NewFieldInvalid("containerPort", port.ContainerPort))
|
2014-07-08 04:32:56 +00:00
|
|
|
}
|
2014-08-19 22:18:49 +00:00
|
|
|
if port.HostPort != 0 && !util.IsValidPortNum(port.HostPort) {
|
2014-09-03 21:16:00 +00:00
|
|
|
pErrs = append(pErrs, errs.NewFieldInvalid("hostPort", port.HostPort))
|
2014-07-08 04:32:56 +00:00
|
|
|
}
|
|
|
|
if len(port.Protocol) == 0 {
|
|
|
|
port.Protocol = "TCP"
|
|
|
|
} else if !supportedPortProtocols.Has(strings.ToUpper(port.Protocol)) {
|
2014-09-03 21:16:00 +00:00
|
|
|
pErrs = append(pErrs, errs.NewFieldNotSupported("protocol", port.Protocol))
|
2014-07-08 04:32:56 +00:00
|
|
|
}
|
2014-08-20 03:54:20 +00:00
|
|
|
allErrs = append(allErrs, pErrs.PrefixIndex(i)...)
|
2014-07-08 04:32:56 +00:00
|
|
|
}
|
2014-07-08 06:20:30 +00:00
|
|
|
return allErrs
|
2014-07-08 04:32:56 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
func validateEnv(vars []api.EnvVar) errs.ErrorList {
|
2014-08-14 20:46:22 +00:00
|
|
|
allErrs := errs.ErrorList{}
|
2014-07-08 06:20:30 +00:00
|
|
|
|
2014-07-01 22:56:30 +00:00
|
|
|
for i := range vars {
|
2014-08-20 03:54:20 +00:00
|
|
|
vErrs := errs.ErrorList{}
|
2014-07-01 22:56:30 +00:00
|
|
|
ev := &vars[i] // so we can set default values
|
|
|
|
if len(ev.Name) == 0 {
|
2014-09-03 21:16:00 +00:00
|
|
|
vErrs = append(vErrs, errs.NewFieldRequired("name", ev.Name))
|
2014-07-01 22:56:30 +00:00
|
|
|
}
|
|
|
|
if !util.IsCIdentifier(ev.Name) {
|
2014-09-03 21:16:00 +00:00
|
|
|
vErrs = append(vErrs, errs.NewFieldInvalid("name", ev.Name))
|
2014-07-01 22:56:30 +00:00
|
|
|
}
|
2014-08-20 03:54:20 +00:00
|
|
|
allErrs = append(allErrs, vErrs.PrefixIndex(i)...)
|
2014-07-01 22:56:30 +00:00
|
|
|
}
|
2014-07-08 06:20:30 +00:00
|
|
|
return allErrs
|
2014-07-01 22:56:30 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
func validateVolumeMounts(mounts []api.VolumeMount, volumes util.StringSet) errs.ErrorList {
|
2014-08-14 20:46:22 +00:00
|
|
|
allErrs := errs.ErrorList{}
|
2014-07-08 06:20:30 +00:00
|
|
|
|
2014-07-05 02:46:56 +00:00
|
|
|
for i := range mounts {
|
2014-08-20 03:54:20 +00:00
|
|
|
mErrs := errs.ErrorList{}
|
2014-07-05 02:46:56 +00:00
|
|
|
mnt := &mounts[i] // so we can set default values
|
|
|
|
if len(mnt.Name) == 0 {
|
2014-09-03 21:16:00 +00:00
|
|
|
mErrs = append(mErrs, errs.NewFieldRequired("name", mnt.Name))
|
2014-07-08 06:20:30 +00:00
|
|
|
} else if !volumes.Has(mnt.Name) {
|
2014-08-20 03:54:20 +00:00
|
|
|
mErrs = append(mErrs, errs.NewNotFound("name", mnt.Name))
|
2014-07-05 02:46:56 +00:00
|
|
|
}
|
|
|
|
if len(mnt.MountPath) == 0 {
|
2014-09-03 21:16:00 +00:00
|
|
|
mErrs = append(mErrs, errs.NewFieldRequired("mountPath", mnt.MountPath))
|
2014-07-15 01:39:30 +00:00
|
|
|
}
|
2014-08-20 03:54:20 +00:00
|
|
|
allErrs = append(allErrs, mErrs.PrefixIndex(i)...)
|
2014-07-05 02:46:56 +00:00
|
|
|
}
|
2014-07-08 06:20:30 +00:00
|
|
|
return allErrs
|
2014-07-05 02:46:56 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 04:32:56 +00:00
|
|
|
// AccumulateUniquePorts runs an extraction function on each Port of each Container,
|
|
|
|
// accumulating the results and returning an error if any ports conflict.
|
2014-08-30 01:20:27 +00:00
|
|
|
func AccumulateUniquePorts(containers []api.Container, accumulator map[int]bool, extract func(*api.Port) int) errs.ErrorList {
|
2014-08-14 20:46:22 +00:00
|
|
|
allErrs := errs.ErrorList{}
|
2014-07-08 06:20:30 +00:00
|
|
|
|
2014-07-08 04:32:56 +00:00
|
|
|
for ci := range containers {
|
2014-08-20 03:54:20 +00:00
|
|
|
cErrs := errs.ErrorList{}
|
2014-07-08 04:32:56 +00:00
|
|
|
ctr := &containers[ci]
|
|
|
|
for pi := range ctr.Ports {
|
|
|
|
port := extract(&ctr.Ports[pi])
|
2014-08-19 22:18:49 +00:00
|
|
|
if port == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2014-07-08 04:32:56 +00:00
|
|
|
if accumulator[port] {
|
2014-09-03 21:16:00 +00:00
|
|
|
cErrs = append(cErrs, errs.NewFieldDuplicate("Port", port))
|
2014-07-08 06:20:30 +00:00
|
|
|
} else {
|
|
|
|
accumulator[port] = true
|
2014-07-08 04:32:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-20 03:54:20 +00:00
|
|
|
allErrs = append(allErrs, cErrs.PrefixIndex(ci)...)
|
2014-07-08 04:32:56 +00:00
|
|
|
}
|
2014-07-08 06:20:30 +00:00
|
|
|
return allErrs
|
2014-07-08 04:32:56 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// checkHostPortConflicts checks for colliding Port.HostPort values across
|
|
|
|
// a slice of containers.
|
2014-08-30 01:20:27 +00:00
|
|
|
func checkHostPortConflicts(containers []api.Container) errs.ErrorList {
|
2014-07-08 04:32:56 +00:00
|
|
|
allPorts := map[int]bool{}
|
2014-08-30 01:20:27 +00:00
|
|
|
return AccumulateUniquePorts(containers, allPorts, func(p *api.Port) int { return p.HostPort })
|
2014-07-08 04:32:56 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 23:04:10 +00:00
|
|
|
func validateExecAction(exec *api.ExecAction) errs.ErrorList {
|
|
|
|
allErrors := errs.ErrorList{}
|
|
|
|
if len(exec.Command) == 0 {
|
|
|
|
allErrors = append(allErrors, errs.NewFieldRequired("command", exec.Command))
|
|
|
|
}
|
|
|
|
return allErrors
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateHTTPGetAction(http *api.HTTPGetAction) errs.ErrorList {
|
|
|
|
allErrors := errs.ErrorList{}
|
|
|
|
if len(http.Path) == 0 {
|
|
|
|
allErrors = append(allErrors, errs.NewFieldRequired("path", http.Path))
|
|
|
|
}
|
|
|
|
return allErrors
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateHandler(handler *api.Handler) errs.ErrorList {
|
|
|
|
allErrors := errs.ErrorList{}
|
|
|
|
if handler.Exec != nil {
|
|
|
|
allErrors = append(allErrors, validateExecAction(handler.Exec).Prefix("exec")...)
|
|
|
|
} else if handler.HTTPGet != nil {
|
|
|
|
allErrors = append(allErrors, validateHTTPGetAction(handler.HTTPGet).Prefix("httpGet")...)
|
|
|
|
} else {
|
|
|
|
allErrors = append(allErrors, errs.NewFieldInvalid("", handler))
|
|
|
|
}
|
|
|
|
return allErrors
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateLifecycle(lifecycle *api.Lifecycle) errs.ErrorList {
|
|
|
|
allErrs := errs.ErrorList{}
|
|
|
|
if lifecycle.PostStart != nil {
|
|
|
|
allErrs = append(allErrs, validateHandler(lifecycle.PostStart).Prefix("postStart")...)
|
|
|
|
}
|
|
|
|
if lifecycle.PreStop != nil {
|
|
|
|
allErrs = append(allErrs, validateHandler(lifecycle.PreStop).Prefix("preStop")...)
|
|
|
|
}
|
|
|
|
return allErrs
|
|
|
|
}
|
|
|
|
|
2014-08-30 01:20:27 +00:00
|
|
|
func validateContainers(containers []api.Container, volumes util.StringSet) errs.ErrorList {
|
2014-08-14 20:46:22 +00:00
|
|
|
allErrs := errs.ErrorList{}
|
2014-07-08 06:20:30 +00:00
|
|
|
|
2014-07-01 22:14:25 +00:00
|
|
|
allNames := util.StringSet{}
|
|
|
|
for i := range containers {
|
2014-08-20 03:54:20 +00:00
|
|
|
cErrs := errs.ErrorList{}
|
2014-07-01 22:14:25 +00:00
|
|
|
ctr := &containers[i] // so we can set default values
|
2014-09-16 14:04:12 +00:00
|
|
|
capabilities := capabilities.GetCapabilities()
|
2014-08-20 02:57:48 +00:00
|
|
|
if len(ctr.Name) == 0 {
|
2014-09-03 21:16:00 +00:00
|
|
|
cErrs = append(cErrs, errs.NewFieldRequired("name", ctr.Name))
|
2014-08-20 02:57:48 +00:00
|
|
|
} else if !util.IsDNSLabel(ctr.Name) {
|
2014-09-03 21:16:00 +00:00
|
|
|
cErrs = append(cErrs, errs.NewFieldInvalid("name", ctr.Name))
|
2014-07-08 06:20:30 +00:00
|
|
|
} else if allNames.Has(ctr.Name) {
|
2014-09-03 21:16:00 +00:00
|
|
|
cErrs = append(cErrs, errs.NewFieldDuplicate("name", ctr.Name))
|
2014-09-16 14:04:12 +00:00
|
|
|
} else if ctr.Privileged && !capabilities.AllowPrivileged {
|
|
|
|
cErrs = append(cErrs, errs.NewFieldInvalid("privileged", ctr.Privileged))
|
2014-07-08 06:20:30 +00:00
|
|
|
} else {
|
|
|
|
allNames.Insert(ctr.Name)
|
2014-07-01 22:14:25 +00:00
|
|
|
}
|
|
|
|
if len(ctr.Image) == 0 {
|
2014-09-03 21:16:00 +00:00
|
|
|
cErrs = append(cErrs, errs.NewFieldRequired("image", ctr.Image))
|
2014-07-05 02:46:56 +00:00
|
|
|
}
|
2014-09-12 23:04:10 +00:00
|
|
|
if ctr.Lifecycle != nil {
|
|
|
|
cErrs = append(cErrs, validateLifecycle(ctr.Lifecycle).Prefix("lifecycle")...)
|
|
|
|
}
|
2014-08-20 03:54:20 +00:00
|
|
|
cErrs = append(cErrs, validatePorts(ctr.Ports).Prefix("ports")...)
|
|
|
|
cErrs = append(cErrs, validateEnv(ctr.Env).Prefix("env")...)
|
|
|
|
cErrs = append(cErrs, validateVolumeMounts(ctr.VolumeMounts, volumes).Prefix("volumeMounts")...)
|
|
|
|
allErrs = append(allErrs, cErrs.PrefixIndex(i)...)
|
2014-07-01 22:14:25 +00:00
|
|
|
}
|
2014-07-08 04:32:56 +00:00
|
|
|
// 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.
|
2014-08-16 20:34:06 +00:00
|
|
|
allErrs = append(allErrs, checkHostPortConflicts(containers)...)
|
2014-07-09 00:33:15 +00:00
|
|
|
|
2014-07-10 11:46:35 +00:00
|
|
|
return allErrs
|
2014-07-01 22:14:25 +00:00
|
|
|
}
|
|
|
|
|
2014-07-10 11:46:35 +00:00
|
|
|
var supportedManifestVersions = util.NewStringSet("v1beta1", "v1beta2")
|
2014-07-08 04:32:56 +00:00
|
|
|
|
2014-07-01 20:01:39 +00:00
|
|
|
// 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
|
|
|
|
// tricks.
|
2014-08-30 01:20:27 +00:00
|
|
|
func ValidateManifest(manifest *api.ContainerManifest) errs.ErrorList {
|
2014-08-14 20:46:22 +00:00
|
|
|
allErrs := errs.ErrorList{}
|
2014-07-08 06:20:30 +00:00
|
|
|
|
2014-07-01 20:01:39 +00:00
|
|
|
if len(manifest.Version) == 0 {
|
2014-09-03 21:16:00 +00:00
|
|
|
allErrs = append(allErrs, errs.NewFieldRequired("version", manifest.Version))
|
2014-07-08 06:20:30 +00:00
|
|
|
} else if !supportedManifestVersions.Has(strings.ToLower(manifest.Version)) {
|
2014-09-03 21:16:00 +00:00
|
|
|
allErrs = append(allErrs, errs.NewFieldNotSupported("version", manifest.Version))
|
2014-07-01 20:01:39 +00:00
|
|
|
}
|
2014-09-09 23:08:21 +00:00
|
|
|
allVolumes, vErrs := validateVolumes(manifest.Volumes)
|
|
|
|
allErrs = append(allErrs, vErrs.Prefix("volumes")...)
|
2014-08-20 03:54:20 +00:00
|
|
|
allErrs = append(allErrs, validateContainers(manifest.Containers, allVolumes).Prefix("containers")...)
|
2014-08-26 18:25:17 +00:00
|
|
|
allErrs = append(allErrs, validateRestartPolicy(&manifest.RestartPolicy).Prefix("restartPolicy")...)
|
2014-08-16 20:48:48 +00:00
|
|
|
return allErrs
|
2014-07-01 20:01:39 +00:00
|
|
|
}
|
2014-07-10 19:45:01 +00:00
|
|
|
|
2014-08-26 18:25:17 +00:00
|
|
|
func validateRestartPolicy(restartPolicy *api.RestartPolicy) errs.ErrorList {
|
|
|
|
numPolicies := 0
|
|
|
|
allErrors := errs.ErrorList{}
|
|
|
|
if restartPolicy.Always != nil {
|
|
|
|
numPolicies++
|
|
|
|
}
|
|
|
|
if restartPolicy.OnFailure != nil {
|
|
|
|
numPolicies++
|
|
|
|
}
|
|
|
|
if restartPolicy.Never != nil {
|
|
|
|
numPolicies++
|
|
|
|
}
|
|
|
|
if numPolicies == 0 {
|
|
|
|
restartPolicy.Always = &api.RestartPolicyAlways{}
|
2014-07-22 18:45:12 +00:00
|
|
|
}
|
2014-08-26 18:25:17 +00:00
|
|
|
if numPolicies > 1 {
|
|
|
|
allErrors = append(allErrors, errs.NewFieldInvalid("", restartPolicy))
|
|
|
|
}
|
|
|
|
return allErrors
|
|
|
|
}
|
2014-07-22 18:45:12 +00:00
|
|
|
|
2014-08-26 18:25:17 +00:00
|
|
|
func ValidatePodState(podState *api.PodState) errs.ErrorList {
|
|
|
|
allErrs := errs.ErrorList(ValidateManifest(&podState.Manifest)).Prefix("manifest")
|
2014-08-16 20:48:48 +00:00
|
|
|
return allErrs
|
2014-07-22 18:45:12 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// ValidatePod tests if required fields in the pod are set.
|
2014-08-30 01:20:27 +00:00
|
|
|
func ValidatePod(pod *api.Pod) errs.ErrorList {
|
2014-08-14 20:46:22 +00:00
|
|
|
allErrs := errs.ErrorList{}
|
2014-08-20 02:57:48 +00:00
|
|
|
if len(pod.ID) == 0 {
|
2014-09-03 21:16:00 +00:00
|
|
|
allErrs = append(allErrs, errs.NewFieldRequired("id", pod.ID))
|
2014-07-29 01:02:30 +00:00
|
|
|
}
|
2014-08-20 03:54:20 +00:00
|
|
|
allErrs = append(allErrs, ValidatePodState(&pod.DesiredState).Prefix("desiredState")...)
|
2014-08-16 20:48:48 +00:00
|
|
|
return allErrs
|
2014-07-29 01:02:30 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 13:53:39 +00:00
|
|
|
// ValidateService tests if required fields in the service are set.
|
2014-08-30 01:20:27 +00:00
|
|
|
func ValidateService(service *api.Service) errs.ErrorList {
|
2014-08-14 20:46:22 +00:00
|
|
|
allErrs := errs.ErrorList{}
|
2014-08-20 02:57:48 +00:00
|
|
|
if len(service.ID) == 0 {
|
2014-09-03 21:16:00 +00:00
|
|
|
allErrs = append(allErrs, errs.NewFieldRequired("id", service.ID))
|
2014-08-04 04:02:10 +00:00
|
|
|
} else if !util.IsDNS952Label(service.ID) {
|
2014-09-03 21:16:00 +00:00
|
|
|
allErrs = append(allErrs, errs.NewFieldInvalid("id", service.ID))
|
2014-07-10 19:45:01 +00:00
|
|
|
}
|
2014-08-22 21:44:21 +00:00
|
|
|
if !util.IsValidPortNum(service.Port) {
|
2014-09-03 21:16:00 +00:00
|
|
|
allErrs = append(allErrs, errs.NewFieldInvalid("Service.Port", service.Port))
|
2014-08-22 21:44:21 +00:00
|
|
|
}
|
2014-09-10 16:53:40 +00:00
|
|
|
if len(service.Protocol) == 0 {
|
|
|
|
service.Protocol = "TCP"
|
|
|
|
} else if !supportedPortProtocols.Has(strings.ToUpper(service.Protocol)) {
|
|
|
|
allErrs = append(allErrs, errs.NewFieldNotSupported("protocol", service.Protocol))
|
|
|
|
}
|
2014-07-25 16:15:17 +00:00
|
|
|
if labels.Set(service.Selector).AsSelector().Empty() {
|
2014-09-03 21:16:00 +00:00
|
|
|
allErrs = append(allErrs, errs.NewFieldRequired("selector", service.Selector))
|
2014-07-10 19:45:01 +00:00
|
|
|
}
|
2014-08-16 20:48:48 +00:00
|
|
|
return allErrs
|
2014-07-10 19:45:01 +00:00
|
|
|
}
|
2014-07-25 16:15:17 +00:00
|
|
|
|
|
|
|
// ValidateReplicationController tests if required fields in the replication controller are set.
|
2014-08-30 01:20:27 +00:00
|
|
|
func ValidateReplicationController(controller *api.ReplicationController) errs.ErrorList {
|
2014-08-16 20:48:48 +00:00
|
|
|
allErrs := errs.ErrorList{}
|
2014-08-20 02:57:48 +00:00
|
|
|
if len(controller.ID) == 0 {
|
2014-09-03 21:16:00 +00:00
|
|
|
allErrs = append(allErrs, errs.NewFieldRequired("id", controller.ID))
|
2014-07-25 16:15:17 +00:00
|
|
|
}
|
2014-09-16 16:54:38 +00:00
|
|
|
allErrs = append(allErrs, ValidateReplicationControllerState(&controller.DesiredState).Prefix("desiredState")...)
|
|
|
|
return allErrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateReplicationControllerState tests if required fields in the replication controller state are set.
|
|
|
|
func ValidateReplicationControllerState(state *api.ReplicationControllerState) errs.ErrorList {
|
|
|
|
allErrs := errs.ErrorList{}
|
|
|
|
if labels.Set(state.ReplicaSelector).AsSelector().Empty() {
|
|
|
|
allErrs = append(allErrs, errs.NewFieldRequired("replicaSelector", state.ReplicaSelector))
|
2014-07-25 16:15:17 +00:00
|
|
|
}
|
2014-09-16 16:54:38 +00:00
|
|
|
selector := labels.Set(state.ReplicaSelector).AsSelector()
|
|
|
|
labels := labels.Set(state.PodTemplate.Labels)
|
2014-08-22 00:02:39 +00:00
|
|
|
if !selector.Matches(labels) {
|
2014-09-16 16:54:38 +00:00
|
|
|
allErrs = append(allErrs, errs.NewFieldInvalid("podTemplate.labels", state.PodTemplate))
|
2014-08-22 00:02:39 +00:00
|
|
|
}
|
2014-09-16 16:54:38 +00:00
|
|
|
if state.Replicas < 0 {
|
|
|
|
allErrs = append(allErrs, errs.NewFieldInvalid("replicas", state.Replicas))
|
2014-08-04 19:02:51 +00:00
|
|
|
}
|
2014-09-16 16:54:38 +00:00
|
|
|
allErrs = append(allErrs, ValidateManifest(&state.PodTemplate.DesiredState.Manifest).Prefix("podTemplate.desiredState.manifest")...)
|
2014-08-16 20:48:48 +00:00
|
|
|
return allErrs
|
2014-07-25 16:15:17 +00:00
|
|
|
}
|