Add validation when processing pod manifests from a URL.

This commit is contained in:
Robert Bailey 2014-08-07 17:00:55 -07:00
parent 053e75d8e9
commit 9ce364d498

View File

@ -66,23 +66,18 @@ func (s *SourceURL) extractFromURL() error {
} }
// First try as if it's a single manifest // First try as if it's a single manifest
var pod kubelet.Pod var manifest api.ContainerManifest
singleErr := yaml.Unmarshal(data, &pod.Manifest) singleErr := yaml.Unmarshal(data, &manifest)
// TODO: replace with validation
if singleErr == nil && pod.Manifest.Version == "" {
// If data is a []ContainerManifest, trying to put it into a ContainerManifest
// will not give an error but also won't set any of the fields.
// Our docs say that the version field is mandatory, so using that to judge wether
// this was actually successful.
singleErr = fmt.Errorf("got blank version field")
}
if singleErr == nil { if singleErr == nil {
name := pod.Manifest.ID if errs := api.ValidateManifest(&manifest); len(errs) > 0 {
if name == "" { singleErr = fmt.Errorf("invalid manifest: %v", errs)
name = "1" }
}
if singleErr == nil {
pod := kubelet.Pod{Name: manifest.ID, Manifest: manifest}
if pod.Name == "" {
pod.Name = "1"
} }
pod.Name = name
s.updates <- kubelet.PodUpdate{[]kubelet.Pod{pod}, kubelet.SET} s.updates <- kubelet.PodUpdate{[]kubelet.Pod{pod}, kubelet.SET}
return nil return nil
} }
@ -93,18 +88,21 @@ func (s *SourceURL) extractFromURL() error {
// We're not sure if the person reading the logs is going to care about the single or // We're not sure if the person reading the logs is going to care about the single or
// multiple manifest unmarshalling attempt, so we need to put both in the logs, as is // multiple manifest unmarshalling attempt, so we need to put both in the logs, as is
// done at the end. Hence not returning early here. // done at the end. Hence not returning early here.
if multiErr == nil && len(manifests) > 0 && manifests[0].Version == "" { if multiErr == nil {
multiErr = fmt.Errorf("got blank version field") for _, manifest := range manifests {
if errs := api.ValidateManifest(&manifest); len(errs) > 0 {
multiErr = fmt.Errorf("invalid manifest: %v", errs)
break
}
}
} }
if multiErr == nil { if multiErr == nil {
pods := []kubelet.Pod{} pods := []kubelet.Pod{}
for i := range manifests { for i, manifest := range manifests {
pod := kubelet.Pod{Manifest: manifests[i]} pod := kubelet.Pod{Name: manifest.ID, Manifest: manifest}
name := pod.Manifest.ID if pod.Name == "" {
if name == "" { pod.Name = fmt.Sprintf("%d", i+1)
name = fmt.Sprintf("%d", i+1)
} }
pod.Name = name
pods = append(pods, pod) pods = append(pods, pod)
} }
s.updates <- kubelet.PodUpdate{pods, kubelet.SET} s.updates <- kubelet.PodUpdate{pods, kubelet.SET}
@ -113,5 +111,5 @@ func (s *SourceURL) extractFromURL() error {
return fmt.Errorf("%v: received '%v', but couldn't parse as a "+ return fmt.Errorf("%v: received '%v', but couldn't parse as a "+
"single manifest (%v: %+v) or as multiple manifests (%v: %+v).\n", "single manifest (%v: %+v) or as multiple manifests (%v: %+v).\n",
s.url, string(data), singleErr, pod.Manifest, multiErr, manifests) s.url, string(data), singleErr, manifest, multiErr, manifests)
} }