diff --git a/pkg/registry/etcd_registry.go b/pkg/registry/etcd_registry.go index d3b2c4e6149..0b439069a92 100644 --- a/pkg/registry/etcd_registry.go +++ b/pkg/registry/etcd_registry.go @@ -71,7 +71,8 @@ func makePodKey(machine, podID string) string { func (registry *EtcdRegistry) ListPods(query labels.Query) ([]api.Pod, error) { pods := []api.Pod{} for _, machine := range registry.machines { - machinePods, err := registry.listPodsForMachine(machine) + var machinePods []api.Pod + err := registry.extractList("/registry/hosts/"+machine+"/pods", &machinePods) if err != nil { return pods, err } @@ -120,9 +121,30 @@ func (r *EtcdRegistry) extractList(key string, slicePtr interface{}) error { return nil } -func (registry *EtcdRegistry) listPodsForMachine(machine string) (pods []api.Pod, err error) { - err = registry.extractList("/registry/hosts/"+machine+"/pods", &pods) - return +// Unmarshals json found at key into objPtr. +func (r *EtcdRegistry) extractObj(key string, objPtr interface{}, ignoreNotFound bool) error { + response, err := r.etcdClient.Get(key, false, false) + returnZero := false + if err != nil { + if ignoreNotFound && isEtcdNotFound(err) { + returnZero = true + } else { + return err + } + } + if !returnZero && (response.Node == nil || len(response.Node.Value) == 0) { + if ignoreNotFound { + returnZero = true + } else { + return fmt.Errorf("key '%v' found no nodes field: %#v", key, response) + } + } + if returnZero { + pv := reflect.ValueOf(objPtr) + pv.Elem().Set(reflect.Zero(pv.Type().Elem())) + return nil + } + return json.Unmarshal([]byte(response.Node.Value), objPtr) } func (registry *EtcdRegistry) GetPod(podID string) (*api.Pod, error) { @@ -134,19 +156,9 @@ func makeContainerKey(machine string) string { return "/registry/hosts/" + machine + "/kubelet" } -func (registry *EtcdRegistry) loadManifests(machine string) ([]api.ContainerManifest, error) { - var manifests []api.ContainerManifest - response, err := registry.etcdClient.Get(makeContainerKey(machine), false, false) - - if err != nil { - if isEtcdNotFound(err) { - err = nil - manifests = []api.ContainerManifest{} - } - } else { - err = json.Unmarshal([]byte(response.Node.Value), &manifests) - } - return manifests, err +func (registry *EtcdRegistry) loadManifests(machine string) (manifests []api.ContainerManifest, err error) { + err = registry.extractObj(makeContainerKey(machine), &manifests, true) + return } func (registry *EtcdRegistry) updateManifests(machine string, manifests []api.ContainerManifest) error { @@ -227,23 +239,14 @@ func (registry *EtcdRegistry) deletePodFromMachine(machine, podID string) error return err } -func (registry *EtcdRegistry) getPodForMachine(machine, podID string) (api.Pod, error) { +func (registry *EtcdRegistry) getPodForMachine(machine, podID string) (pod api.Pod, err error) { key := makePodKey(machine, podID) - result, err := registry.etcdClient.Get(key, false, false) + err = registry.extractObj(key, &pod, false) if err != nil { - if isEtcdNotFound(err) { - return api.Pod{}, fmt.Errorf("not found (%#v).", err) - } else { - return api.Pod{}, err - } + return } - if result.Node == nil || len(result.Node.Value) == 0 { - return api.Pod{}, fmt.Errorf("no nodes field: %#v", result) - } - pod := api.Pod{} - err = json.Unmarshal([]byte(result.Node.Value), &pod) pod.CurrentState.Host = machine - return pod, err + return } func (registry *EtcdRegistry) findPod(podID string) (api.Pod, string, error) { @@ -286,19 +289,11 @@ func makeControllerKey(id string) string { func (registry *EtcdRegistry) GetController(controllerID string) (*api.ReplicationController, error) { var controller api.ReplicationController key := makeControllerKey(controllerID) - result, err := registry.etcdClient.Get(key, false, false) + err := registry.extractObj(key, &controller, false) if err != nil { - if isEtcdNotFound(err) { - return nil, fmt.Errorf("controller %s not found", controllerID) - } else { - return nil, err - } + return nil, err } - if result.Node == nil || len(result.Node.Value) == 0 { - return nil, fmt.Errorf("no nodes field: %#v", result) - } - err = json.Unmarshal([]byte(result.Node.Value), &controller) - return &controller, err + return &controller, nil } func (registry *EtcdRegistry) CreateController(controller api.ReplicationController) error { @@ -344,20 +339,12 @@ func (registry *EtcdRegistry) CreateService(svc api.Service) error { func (registry *EtcdRegistry) GetService(name string) (*api.Service, error) { key := makeServiceKey(name) - response, err := registry.etcdClient.Get(key, false, false) - if err != nil { - if isEtcdNotFound(err) { - return nil, fmt.Errorf("service %s was not found.", name) - } else { - return nil, err - } - } var svc api.Service - err = json.Unmarshal([]byte(response.Node.Value), &svc) + err := registry.extractObj(key, &svc, false) if err != nil { return nil, err } - return &svc, err + return &svc, nil } func (registry *EtcdRegistry) DeleteService(name string) error {