Esempio n. 1
0
func (c *Resources) checkResources(cluster core.ClusterInterface, service framework.ServiceConfig) error {
	requiredInstances, err := c.requiredInstances(cluster, service)
	if err != nil {
		return err
	}

	if requiredInstances == 0 {
		return nil
	}

	slaves, err := cluster.Slaves()
	if err != nil {
		return err
	}

	allowedInstances := 0
	for _, v := range slaves {
		allowed := v.AllowedInstances(service.CPUShares, float64(service.Memory))
		logger.Instance().Infof("Checking slave %s - Available CPU %f Mem %f - Allowed instances %d", v.Hostname, v.AvailableCpu(), v.AvailableMem(), allowed)
		if allowed > 0 {
			allowedInstances += allowed
		}
	}

	if allowedInstances < requiredInstances {
		return fmt.Errorf("Cluster %s doesn't have enough resources. It only accept %d instances.", cluster.Id(), allowedInstances)
	}
	logger.Instance().Infof("Checking Resources in cluster %s done", cluster.Id())

	return nil
}
Esempio n. 2
0
func (c *Resources) requiredInstances(cluster core.ClusterInterface, service framework.ServiceConfig) (int, error) {
	requiredInstances := 0
	deployedService, err := cluster.FindService(service.Namespace, service.ServiceName, service.CompatibilityVersion)
	if err != nil {
		return 0, err
	}

	if deployedService == nil {
		requiredInstances = service.Instances
		logger.Instance().Infof("The service is new in cluster %s, deploy needs resources for %d instances", cluster.Id(), requiredInstances)
	} else if len(deployedService.Instances) == service.Instances {
		requiredInstances = int(math.Max(1, math.Floor(float64(service.Instances)*0.2)))
		logger.Instance().Infof("Service replacement in cluster %s, deploy needs resources for %d instances", cluster.Id(), requiredInstances)
	} else if len(deployedService.Instances) < service.Instances {
		requiredInstances = service.Instances - len(deployedService.Instances)
		logger.Instance().Infof("Service up scale in cluster %s, deploy needs resources for %d instances", cluster.Id(), requiredInstances)
	} else {
		logger.Instance().Infof("Service down scale in cluster %s, the cluster should have enough resources", cluster.Id())
	}

	return requiredInstances, nil
}