Esempio n. 1
0
func TestGetEndpointsMissingService(t *testing.T) {
	registry := &registrytest.ServiceRegistry{
		Err: apiserver.NewNotFoundErr("service", "foo"),
	}
	storage := NewStorage(registry)

	// returns service not found
	_, err := storage.Get("foo")
	if !apiserver.IsNotFound(err) || !reflect.DeepEqual(err, apiserver.NewNotFoundErr("service", "foo")) {
		t.Errorf("expected NotFound error, got %#v", err)
	}

	// returns empty endpoints
	registry.Err = nil
	registry.Service = &api.Service{
		JSONBase: api.JSONBase{ID: "foo"},
	}
	obj, err := storage.Get("foo")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if obj.(*api.Endpoints).Endpoints != nil {
		t.Errorf("unexpected endpoints: %#v", obj)
	}
}
Esempio n. 2
0
// DeletePod deletes an existing pod specified by its ID.
func (registry *EtcdRegistry) DeletePod(podID string) error {
	var pod api.Pod
	podKey := makePodKey(podID)
	err := registry.helper.ExtractObj(podKey, &pod, false)
	if tools.IsEtcdNotFound(err) {
		return apiserver.NewNotFoundErr("pod", podID)
	}
	if err != nil {
		return err
	}

	// First delete the pod, so a scheduler doesn't notice it getting removed from the
	// machine and attempt to put it somewhere.
	err = registry.helper.Delete(podKey, true)
	if tools.IsEtcdNotFound(err) {
		return apiserver.NewNotFoundErr("pod", podID)
	}
	if err != nil {
		return err
	}

	machine := pod.DesiredState.Host
	if machine == "" {
		// Pod was never scheduled anywhere, just return.
		return nil
	}

	// Next, remove the pod from the machine atomically.
	contKey := makeContainerKey(machine)
	return registry.helper.AtomicUpdate(contKey, &api.ContainerManifestList{}, func(in interface{}) (interface{}, error) {
		manifests := in.(*api.ContainerManifestList)
		newManifests := make([]api.ContainerManifest, 0, len(manifests.Items))
		found := false
		for _, manifest := range manifests.Items {
			if manifest.ID != podID {
				newManifests = append(newManifests, manifest)
			} else {
				found = true
			}
		}
		if !found {
			// This really shouldn't happen, it indicates something is broken, and likely
			// there is a lost pod somewhere.
			// However it is "deleted" so log it and move on
			glog.Infof("Couldn't find: %s in %#v", podID, manifests)
		}
		manifests.Items = newManifests
		return manifests, nil
	})
}
Esempio n. 3
0
func (registry *EtcdRegistry) deletePodFromMachine(machine, podID string) error {
	// First delete the pod, so a scheduler doesn't notice it getting removed from the
	// machine and attempt to put it somewhere.
	podKey := makePodKey(machine, podID)
	_, err := registry.etcdClient.Delete(podKey, true)
	if tools.IsEtcdNotFound(err) {
		return apiserver.NewNotFoundErr("pod", podID)
	}
	if err != nil {
		return err
	}

	// Next, remove the pod from the machine atomically.
	contKey := makeContainerKey(machine)
	return registry.helper().AtomicUpdate(contKey, &[]api.ContainerManifest{}, func(in interface{}) (interface{}, error) {
		manifests := *in.(*[]api.ContainerManifest)
		newManifests := make([]api.ContainerManifest, 0, len(manifests))
		found := false
		for _, manifest := range manifests {
			if manifest.ID != podID {
				newManifests = append(newManifests, manifest)
			} else {
				found = true
			}
		}
		if !found {
			// This really shouldn't happen, it indicates something is broken, and likely
			// there is a lost pod somewhere.
			// However it is "deleted" so log it and move on
			glog.Infof("Couldn't find: %s in %#v", podID, manifests)
		}
		return newManifests, nil
	})
}
Esempio n. 4
0
func (registry *MemoryRegistry) DeleteController(controllerID string) error {
	if _, ok := registry.controllerData[controllerID]; !ok {
		return apiserver.NewNotFoundErr("replicationController", controllerID)
	}
	delete(registry.controllerData, controllerID)
	return nil
}
Esempio n. 5
0
func (registry *MemoryRegistry) DeletePod(podID string) error {
	if _, ok := registry.podData[podID]; !ok {
		return apiserver.NewNotFoundErr("pod", podID)
	}
	delete(registry.podData, podID)
	return nil
}
Esempio n. 6
0
func (registry *MemoryRegistry) UpdatePod(pod api.Pod) error {
	if _, ok := registry.podData[pod.ID]; !ok {
		return apiserver.NewNotFoundErr("pod", pod.ID)
	}
	registry.podData[pod.ID] = pod
	return nil
}
Esempio n. 7
0
func (registry *MemoryRegistry) DeleteService(name string) error {
	if _, ok := registry.serviceData[name]; !ok {
		return apiserver.NewNotFoundErr("service", name)
	}
	delete(registry.serviceData, name)
	return nil
}
Esempio n. 8
0
func (registry *MemoryRegistry) UpdateController(controller api.ReplicationController) error {
	if _, ok := registry.controllerData[controller.ID]; !ok {
		return apiserver.NewNotFoundErr("replicationController", controller.ID)
	}
	registry.controllerData[controller.ID] = controller
	return nil
}
Esempio n. 9
0
// GetService returns an *api.Service for the named service.
// It returns an error if the service is not found in the registry.
func (r *Registry) GetService(name string) (*api.Service, error) {
	svc, found := r.serviceData[name]
	if !found {
		return nil, apiserver.NewNotFoundErr("service", name)
	}
	return &svc, nil
}
Esempio n. 10
0
func (registry *MemoryRegistry) GetController(controllerID string) (*api.ReplicationController, error) {
	controller, found := registry.controllerData[controllerID]
	if found {
		return &controller, nil
	} else {
		return nil, apiserver.NewNotFoundErr("replicationController", controllerID)
	}
}
Esempio n. 11
0
func (registry *MemoryRegistry) GetPod(podID string) (*api.Pod, error) {
	pod, found := registry.podData[podID]
	if found {
		return &pod, nil
	} else {
		return nil, apiserver.NewNotFoundErr("pod", podID)
	}
}
Esempio n. 12
0
// DeleteController deletes a ReplicationController specified by its ID.
func (registry *EtcdRegistry) DeleteController(controllerID string) error {
	key := makeControllerKey(controllerID)
	_, err := registry.etcdClient.Delete(key, false)
	if tools.IsEtcdNotFound(err) {
		return apiserver.NewNotFoundErr("replicationController", controllerID)
	}
	return err
}
Esempio n. 13
0
func (registry *MemoryRegistry) GetService(name string) (*api.Service, error) {
	svc, found := registry.serviceData[name]
	if found {
		return &svc, nil
	} else {
		return nil, apiserver.NewNotFoundErr("service", name)
	}
}
Esempio n. 14
0
// GetEndpoints obtains the endpoints for the service identified by 'name'.
func (r *Registry) GetEndpoints(name string) (*api.Endpoints, error) {
	key := makeServiceEndpointsKey(name)
	var endpoints api.Endpoints
	err := r.ExtractObj(key, &endpoints, false)
	if tools.IsEtcdNotFound(err) {
		return nil, apiserver.NewNotFoundErr("endpoints", name)
	}
	if err != nil {
		return nil, err
	}
	return &endpoints, nil
}
Esempio n. 15
0
// GetService obtains a Service specified by its name.
func (registry *EtcdRegistry) GetService(name string) (*api.Service, error) {
	key := makeServiceKey(name)
	var svc api.Service
	err := registry.helper().ExtractObj(key, &svc, false)
	if tools.IsEtcdNotFound(err) {
		return nil, apiserver.NewNotFoundErr("service", name)
	}
	if err != nil {
		return nil, err
	}
	return &svc, nil
}
Esempio n. 16
0
// GetController gets a specific ReplicationController specified by its ID.
func (registry *EtcdRegistry) GetController(controllerID string) (*api.ReplicationController, error) {
	var controller api.ReplicationController
	key := makeControllerKey(controllerID)
	err := registry.helper().ExtractObj(key, &controller, false)
	if tools.IsEtcdNotFound(err) {
		return nil, apiserver.NewNotFoundErr("replicationController", controllerID)
	}
	if err != nil {
		return nil, err
	}
	return &controller, nil
}
Esempio n. 17
0
// DeleteService deletes a Service specified by its name.
func (registry *EtcdRegistry) DeleteService(name string) error {
	key := makeServiceKey(name)
	_, err := registry.etcdClient.Delete(key, true)
	if tools.IsEtcdNotFound(err) {
		return apiserver.NewNotFoundErr("service", name)
	}
	if err != nil {
		return err
	}
	key = "/registry/services/endpoints/" + name
	_, err = registry.etcdClient.Delete(key, true)
	return err
}
Esempio n. 18
0
func (registry *EtcdRegistry) findPod(podID string) (api.Pod, string, error) {
	machines, err := registry.machines.List()
	if err != nil {
		return api.Pod{}, "", err
	}
	for _, machine := range machines {
		pod, err := registry.getPodForMachine(machine, podID)
		if err == nil {
			return pod, machine, nil
		}
	}
	return api.Pod{}, "", apiserver.NewNotFoundErr("pod", podID)
}
Esempio n. 19
0
// DeleteService deletes a Service specified by its name.
func (r *Registry) DeleteService(name string) error {
	key := makeServiceKey(name)
	err := r.Delete(key, true)
	if tools.IsEtcdNotFound(err) {
		return apiserver.NewNotFoundErr("service", name)
	}
	if err != nil {
		return err
	}
	key = makeServiceEndpointsKey(name)
	err = r.Delete(key, true)
	if !tools.IsEtcdNotFound(err) {
		return err
	}
	return nil
}
Esempio n. 20
0
func (registry *MemoryRegistry) UpdateService(svc api.Service) error {
	if _, ok := registry.serviceData[svc.ID]; !ok {
		return apiserver.NewNotFoundErr("service", svc.ID)
	}
	return registry.CreateService(svc)
}
Esempio n. 21
0
// Delete returns an error because bindings are write-only objects.
func (*BindingStorage) Delete(id string) (<-chan interface{}, error) {
	return nil, apiserver.NewNotFoundErr("binding", id)
}
Esempio n. 22
0
// Get returns an error because bindings are write-only objects.
func (*BindingStorage) Get(id string) (interface{}, error) {
	return nil, apiserver.NewNotFoundErr("binding", id)
}
Esempio n. 23
0
// List returns an error because bindings are write-only objects.
func (*BindingStorage) List(selector labels.Selector) (interface{}, error) {
	return nil, apiserver.NewNotFoundErr("binding", "list")
}