Пример #1
0
func (sr *ServiceRegistryStorage) Create(obj interface{}) (<-chan interface{}, error) {
	srv := obj.(api.Service)
	if srv.ID == "" {
		return nil, fmt.Errorf("ID should not be empty: %#v", srv)
	}
	return apiserver.MakeAsync(func() (interface{}, error) {
		// TODO: Consider moving this to a rectification loop, so that we make/remove external load balancers
		// correctly no matter what http operations happen.
		if srv.CreateExternalLoadBalancer {
			var balancer cloudprovider.TCPLoadBalancer
			var ok bool
			if sr.cloud != nil {
				balancer, ok = sr.cloud.TCPLoadBalancer()
			}
			if ok && balancer != nil {
				hosts, err := sr.machines.List()
				if err != nil {
					return nil, err
				}
				err = balancer.CreateTCPLoadBalancer(srv.ID, "us-central1", srv.Port, hosts)
				if err != nil {
					return nil, err
				}
			} else {
				return nil, fmt.Errorf("requested an external service, but no cloud provider supplied.")
			}
		}
		// TODO actually wait for the object to be fully created here.
		err := sr.registry.CreateService(srv)
		if err != nil {
			return nil, err
		}
		return sr.registry.GetService(srv.ID)
	}), nil
}
Пример #2
0
func (sr *ServiceRegistryStorage) Create(obj interface{}) (<-chan interface{}, error) {
	srv := obj.(api.Service)
	if errs := api.ValidateService(&srv); len(errs) > 0 {
		return nil, fmt.Errorf("Validation errors: %v", errs)
	}
	return apiserver.MakeAsync(func() (interface{}, error) {
		// TODO: Consider moving this to a rectification loop, so that we make/remove external load balancers
		// correctly no matter what http operations happen.
		if srv.CreateExternalLoadBalancer {
			var balancer cloudprovider.TCPLoadBalancer
			var zones cloudprovider.Zones
			var ok bool
			if sr.cloud != nil {
				balancer, ok = sr.cloud.TCPLoadBalancer()
				if ok {
					zones, ok = sr.cloud.Zones()
				}
			}
			if ok && balancer != nil && zones != nil {
				hosts, err := sr.machines.List()
				if err != nil {
					return nil, err
				}
				zone, err := zones.GetZone()
				if err != nil {
					return nil, err
				}
				err = balancer.CreateTCPLoadBalancer(srv.ID, zone, srv.Port, hosts)
				if err != nil {
					return nil, err
				}
			} else {
				return nil, fmt.Errorf("requested an external service, but no cloud provider supplied.")
			}
		}
		// TODO actually wait for the object to be fully created here.
		err := sr.registry.CreateService(srv)
		if err != nil {
			return nil, err
		}
		return sr.registry.GetService(srv.ID)
	}), nil
}
Пример #3
0
func (sr *ServiceRegistryStorage) Delete(id string) (<-chan interface{}, error) {
	svc, err := sr.Get(id)
	if err != nil {
		return nil, err
	}
	if svc.(*api.Service).CreateExternalLoadBalancer {
		var balancer cloudprovider.TCPLoadBalancer
		var ok bool
		if sr.cloud != nil {
			balancer, ok = sr.cloud.TCPLoadBalancer()
		}
		if ok && balancer != nil {
			err = balancer.DeleteTCPLoadBalancer(id, "us-central1")
			if err != nil {
				return nil, err
			}
		}
	}
	return apiserver.MakeAsync(func() interface{} { return apiserver.Status{Success: true} }), sr.registry.DeleteService(id)
}
Пример #4
0
func (sr *ServiceRegistryStorage) Create(obj interface{}) (<-chan interface{}, error) {
	srv := obj.(api.Service)
	if srv.CreateExternalLoadBalancer {
		var balancer cloudprovider.TCPLoadBalancer
		var ok bool
		if sr.cloud != nil {
			balancer, ok = sr.cloud.TCPLoadBalancer()
		}
		if ok && balancer != nil {
			err := balancer.CreateTCPLoadBalancer(srv.ID, "us-central1", srv.Port, sr.hosts)
			if err != nil {
				return nil, err
			}
		} else {
			return nil, fmt.Errorf("requested an external service, but no cloud provider supplied.")
		}
	}
	// TODO actually wait for the object to be fully created here.
	return apiserver.MakeAsync(func() interface{} { return obj }), sr.registry.CreateService(srv)
}
Пример #5
0
func (sr *ServiceRegistryStorage) Delete(id string) error {
	svc, err := sr.Get(id)
	if err != nil {
		return err
	}
	if svc.(*api.Service).CreateExternalLoadBalancer {
		var balancer cloudprovider.TCPLoadBalancer
		if sr.cloud != nil {
			balancer, err = sr.cloud.TCPLoadBalancer()
			if err != nil {
				return err
			}
		}
		if balancer != nil {
			err = balancer.DeleteTCPLoadBalancer(id, "us-central1")
			if err != nil {
				return err
			}
		}
	}
	return sr.registry.DeleteService(id)
}
Пример #6
0
func (sr *ServiceRegistryStorage) Create(obj interface{}) error {
	srv := obj.(api.Service)
	if srv.CreateExternalLoadBalancer {
		var balancer cloudprovider.TCPLoadBalancer
		if sr.cloud != nil {
			var err error
			balancer, err = sr.cloud.TCPLoadBalancer()
			if err != nil {
				return err
			}
		}
		if balancer != nil {
			err := balancer.CreateTCPLoadBalancer(srv.ID, "us-central1", srv.Port, sr.hosts)
			if err != nil {
				return err
			}
		} else {
			return fmt.Errorf("requested an external service, but no cloud provider supplied.")
		}
	}
	return sr.registry.CreateService(srv)
}