Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ health_interval = 300
# Hard Limits the total number of app instances that can be deployed by an user
# Set app_limit = -1 if no hard limit is to be imposed
app_limit = 10
# Specifies the maximum CPU allocation for a container created by a non admin user
max_container_cpu = 0.25
# Specifies the maximum memory allocation for a container created by a non admin user
max_container_memory = 0.5

#############################
# DbMaker Configuration #
Expand Down
8 changes: 5 additions & 3 deletions configs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ type GenericService struct {
// AppMakerService is the default configuration for appmaker microservice
type AppMakerService struct {
GenericService
MetricsInterval time.Duration `toml:"metrics_interval"`
HealthInterval time.Duration `toml:"health_interval"`
AppLimit int `toml:"app_limit"`
MetricsInterval time.Duration `toml:"metrics_interval"`
HealthInterval time.Duration `toml:"health_interval"`
AppLimit int `toml:"app_limit"`
MaxContainerMemory float64 `toml:"max_container_memory"`
MaxContainerCPU float64 `toml:"max_container_cpu"`
}

// MasterService is the default configuration for Master microservice
Expand Down
24 changes: 16 additions & 8 deletions services/appmaker/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,23 @@ func (s *server) Create(ctx context.Context, body *pb.RequestBody) (*pb.Response
maxCount := configs.ServiceConfig.AppMaker.AppLimit
rateCount := configs.ServiceConfig.RateLimit
timeInterval := configs.ServiceConfig.RateInterval
if !user.IsAdmin() && maxCount >= 0 {
rateLimitCount := mongo.CountInstanceInTimeFrame(body.GetOwner(), mongo.AppInstance, timeInterval)
totalCount := mongo.CountInstancesByUser(body.GetOwner(), mongo.AppInstance)
if totalCount < maxCount {
if rateLimitCount >= rateCount && rateCount >= 0 {
return nil, fmt.Errorf("cannot deploy more than %d app instances in %d hours", rateCount, timeInterval)
if !user.IsAdmin() {
if maxCount >= 0 {
rateLimitCount := mongo.CountInstanceInTimeFrame(body.GetOwner(), mongo.AppInstance, timeInterval)
totalCount := mongo.CountInstancesByUser(body.GetOwner(), mongo.AppInstance)
if totalCount < maxCount {
if rateLimitCount >= rateCount && rateCount >= 0 {
return nil, fmt.Errorf("cannot deploy more than %d app instances in %d hours", rateCount, timeInterval)
}
} else {
return nil, fmt.Errorf("cannot deploy more than %d app instances", maxCount)
}
} else {
return nil, fmt.Errorf("cannot deploy more than %d app instances", maxCount)
}
if app.Resources.Memory > configs.ServiceConfig.AppMaker.MaxContainerMemory {
return nil, fmt.Errorf("memory limit cannot exceed %f", configs.ServiceConfig.AppMaker.MaxContainerMemory)
}
if app.Resources.CPU > configs.ServiceConfig.AppMaker.MaxContainerCPU {
return nil, fmt.Errorf("cpu limit cannot exceed %f", configs.ServiceConfig.AppMaker.MaxContainerCPU)
}
}

Expand Down
19 changes: 17 additions & 2 deletions services/master/controllers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"

"github.com/gin-gonic/gin"
"github.com/sdslabs/gasper/configs"
"github.com/sdslabs/gasper/lib/mongo"
"github.com/sdslabs/gasper/lib/utils"
"github.com/sdslabs/gasper/services/master/middlewares"
Expand Down Expand Up @@ -51,6 +52,12 @@ func validateUpdatePayload(data types.M) error {
func UpdateData(app *types.ApplicationConfig, data *types.UpdatePayload) error {
totalCPU := runtime.NumCPU()
vMemory, err := mem.VirtualMemory()

user, err := mongo.FetchSingleUserWithoutPassword(app.Owner)
if err != nil {
return errors.New("Error Fetching Owner of App")
}

if err != nil {
utils.LogError("Error fetching memory", err)
}
Expand Down Expand Up @@ -82,14 +89,22 @@ func UpdateData(app *types.ApplicationConfig, data *types.UpdatePayload) error {
}
if data.Resources != nil && err == nil {
if data.Resources.CPU > 0 && data.Resources.CPU <= float64(totalCPU-1) { // 1 CPU reserved for system
app.Resources.CPU = data.Resources.CPU
if user.IsAdmin() || data.Resources.CPU <= configs.ServiceConfig.AppMaker.MaxContainerCPU {
app.Resources.CPU = data.Resources.CPU
} else {
return fmt.Errorf("cpu limit cannot exceed %f", configs.ServiceConfig.AppMaker.MaxContainerCPU)
}
} else if data.Resources.CPU > float64(totalCPU-1) && data.Resources.CPU <= float64(totalCPU) {
return errors.New("cpu value too high, risks system failure")
} else {
return errors.New("invalid cpu value")
}
if data.Resources.Memory > 0 && data.Resources.Memory <= totalMemory-1 { // 1 GB reserved for system
app.Resources.Memory = data.Resources.Memory
if user.IsAdmin() || data.Resources.Memory <= configs.ServiceConfig.AppMaker.MaxContainerMemory {
app.Resources.Memory = data.Resources.Memory
} else {
return fmt.Errorf("memory limit cannot exceed %f", configs.ServiceConfig.AppMaker.MaxContainerMemory)
}
} else if data.Resources.Memory > totalMemory-1 && data.Resources.Memory <= totalMemory {
return errors.New("memory value too high, risks system failure")
} else {
Expand Down