Ejemplo n.º 1
0
func (s *FusisStore) AddDestination(svc *types.Service, dst *types.Destination) error {
	dstKey := s.key("destinations", svc.GetId(), dst.GetId())
	ipvsKey := s.key("ipvs-ids", "destinations", dst.IpvsId())

	// Validating destination
	if err := s.validateDestination(dst); err != nil {
		return err
	}
	if err := s.validateDestinationNameUniqueness(dstKey); err != nil {
		return err
	}
	if err := s.validateDestinationIpvsUniqueness(ipvsKey); err != nil {
		return err
	}

	// Persisting destination
	value, err := json.Marshal(dst)
	if err != nil {
		return errors.Wrapf(err, "error marshaling destination: %v", dst)
	}
	err = s.kv.Put(dstKey, value, nil)
	if err != nil {
		return errors.Wrapf(err, "error sending destination to store: %v", dst)
	}
	log.Debugf("[store] Added destination: %s with key: %s", value, dstKey)

	// Persisting IPVS key. So it can be validated.
	err = s.kv.Put(ipvsKey, []byte("true"), nil)
	if err != nil {
		return errors.Wrapf(err, "error sending destination ipvs id to store: %v", dst.IpvsId())
	}

	return nil
}
Ejemplo n.º 2
0
func (s *FusisState) GetDestinations(svc *types.Service) []types.Destination {
	s.RLock()
	defer s.RUnlock()

	dsts := []types.Destination{}
	for _, d := range s.destinations {
		if d.ServiceId == svc.GetId() {
			dsts = append(dsts, d)
		}
	}

	return dsts
}
Ejemplo n.º 3
0
func (s *FusisStore) DeleteDestination(svc *types.Service, dst *types.Destination) error {
	key := s.key("destinations", svc.GetId(), dst.GetId())

	err := s.kv.DeleteTree(key)
	if err != nil {
		return errors.Wrapf(err, "error trying to delete destination: %v", dst)
	}
	log.Debugf("[store] Deleted destination: %s", key)

	ipvsKey := s.key("ipvs-ids", "destinations", dst.IpvsId())
	err = s.kv.Delete(ipvsKey)
	if err != nil {
		return errors.Wrapf(err, "error trying to delete destination ipvs id: %s", ipvsKey)
	}

	return nil
}
Ejemplo n.º 4
0
// AddService adds a new services to store. It validates the name
// uniqueness and the IPVS uniqueness by saving the IPVS key
// in the store, which consists in a combination of address, port
// and protocol.
func (s *FusisStore) AddService(svc *types.Service) error {
	svcKey := s.key("services", svc.GetId(), "config")
	ipvsKey := s.key("ipvs-ids", "services", svc.IpvsId())
	// Validating service
	if err := s.validateService(svc); err != nil {
		return err
	}
	if err := s.validateServiceNameUniqueness(svcKey); err != nil {
		return err
	}
	if err := s.validateServiceIpvsUniqueness(ipvsKey); err != nil {
		return err
	}

	// TODO: Make the persistence of service and ipvs id atomic.
	// Pesisting service
	value, err := json.Marshal(svc)
	if err != nil {
		return errors.Wrapf(err, "error marshaling service: %v", svc)
	}
	err = s.kv.Put(svcKey, value, nil)
	if err != nil {
		return errors.Wrapf(err, "error sending service to store: %v", svc)
	}

	// Persisting IPVS key. So it can be validated.
	err = s.kv.Put(ipvsKey, []byte("true"), nil)
	if err != nil {
		return errors.Wrapf(err, "error sending service ipvs id to store: %v", svc.IpvsId())
	}

	return nil
}
Ejemplo n.º 5
0
//Allocate allocates a new avaliable ip
func (i *Ipam) AllocateVIP(s *types.Service) error {
	if i.rangeCursor == nil {
		return ErrNoVipAvailable
	}

	for pos := i.rangeCursor.Next(); pos != nil; pos = i.rangeCursor.Next() {
		//TODO: make it compatible if IPv6
		// Verifies if it is a base IP address. example: 10.0.100.0; 192.168.1.0
		if pos.IP.To4()[3] == 0 {
			pos = i.rangeCursor.Next()
		}

		assigned := i.ipIsAssigned(pos.IP.String(), i.state)

		if !assigned {
			i.rangeCursor.Set(i.rangeCursor.First())
			s.Address = pos.IP.String()
			return nil
		}
	}

	return ErrNoVipAvailable
}
Ejemplo n.º 6
0
func (s *FusisState) DeleteService(svc *types.Service) {
	s.Lock()
	defer s.Unlock()

	delete(s.services, svc.GetId())
}
Ejemplo n.º 7
0
func (s *FusisState) AddService(svc types.Service) {
	s.Lock()
	defer s.Unlock()

	s.services[svc.GetId()] = svc
}
Ejemplo n.º 8
0
func (s *FusisStore) DeleteService(svc *types.Service) error {
	// TODO: Make all these actions atomic

	key := s.key("services", svc.GetId())
	err := s.kv.DeleteTree(key)
	if err != nil {
		return errors.Wrapf(err, "error trying to delete service: %v", svc)
	}

	ipvsKey := s.key("ipvs-ids", "services", svc.IpvsId())
	err = s.kv.Delete(ipvsKey)
	if err != nil {
		return errors.Wrapf(err, "error trying to delete service ipvs id: %s", ipvsKey)
	}

	// Deleting dependent destionations
	dsts, err := s.GetDestinations()
	if err != nil {
		return errors.Wrapf(err, "get destinations from deleted service failed: %#v", svc)
	}

	for _, dst := range dsts {
		if dst.ServiceId == svc.GetId() {
			if err := s.DeleteDestination(svc, &dst); err != nil {
				return errors.Wrap(err, "deleting dependent destination failed")
			}
		}
	}

	// Deleting checks
	exists, err := s.kv.Exists(s.key("checks", svc.GetId()))
	if err != nil {
		return errors.Wrapf(err, "verifying health check to service: %s failed", svc.GetId())
	}

	if exists {
		if err := s.DeleteCheck(types.CheckSpec{ServiceID: svc.GetId()}); err != nil {
			return errors.Wrap(err, "deleting dependent check failed")
		}
	}

	return nil
}