Skip to content

Commit

Permalink
Merge pull request #444 from UffizziCloud/feature/443_set_resource_ca…
Browse files Browse the repository at this point in the history
…p_of_8_gb_per_deployment

[443] added total limit of 8 gb for a deployment
  • Loading branch information
moklidia authored Jul 24, 2023
2 parents 3e5f7f7 + 86ddfd1 commit 9744930
Show file tree
Hide file tree
Showing 18 changed files with 113 additions and 96 deletions.
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ compose:
continuous_preview:
default_delete_preview_after: 72
deployment:
max_memory_limit: 8000
subdomain:
length_limit: 63
default_job_retry_count: 5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ def domain_module
module_class(:domain_module)
end

def deployment_memory_module
return unless module_exists?(:deployment_memory_module)

module_class(:deployment_memory_module)
end

def template_memory_module
return unless module_exists?(:template_memory_module)

module_class(:template_memory_module)
end

private

def module_exists?(module_name)
Expand Down
14 changes: 0 additions & 14 deletions core/app/forms/uffizzi_core/api/cli/v1/deployment/create_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ class UffizziCore::Api::Cli::V1::Deployment::CreateForm < UffizziCore::Deploymen

validate :check_all_containers_have_unique_ports
validate :check_exists_ingress_container
validate :check_max_memory_limit
validate :check_max_memory_request
validate :check_secrets_exist_in_database

def assign_dependences!(project, user)
Expand Down Expand Up @@ -87,18 +85,6 @@ def check_exists_ingress_container
errors.add(:containers, :incorrect_ingress_container) unless UffizziCore::DeploymentService.ingress_container?(active_containers)
end

def check_max_memory_limit
return if UffizziCore::DeploymentService.valid_containers_memory_limit?(self)

errors.add(:containers, :max_memory_limit_error, max: project.account.container_memory_limit)
end

def check_max_memory_request
return if UffizziCore::DeploymentService.valid_containers_memory_request?(self)

errors.add(:containers, :max_memory_request_error, max: project.account.container_memory_limit)
end

def check_secrets_exist_in_database
return if compose_file.nil?

Expand Down
14 changes: 0 additions & 14 deletions core/app/forms/uffizzi_core/api/cli/v1/deployment/update_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class UffizziCore::Api::Cli::V1::Deployment::UpdateForm < UffizziCore::Deploymen

validate :check_all_containers_have_unique_ports
validate :check_exists_ingress_container
validate :check_max_memory_limit
validate :check_max_memory_request

def assign_dependences!(project, user)
self.project = project
Expand Down Expand Up @@ -84,16 +82,4 @@ def check_exists_ingress_container

errors.add(:containers, :incorrect_ingress_container) unless UffizziCore::DeploymentService.ingress_container?(active_containers)
end

def check_max_memory_limit
return if UffizziCore::DeploymentService.valid_containers_memory_limit?(self)

errors.add(:containers, :max_memory_limit_error, max: project.account.container_memory_limit)
end

def check_max_memory_request
return if UffizziCore::DeploymentService.valid_containers_memory_request?(self)

errors.add(:containers, :max_memory_request_error, max: project.account.container_memory_limit)
end
end
16 changes: 0 additions & 16 deletions core/app/forms/uffizzi_core/api/cli/v1/template/create_form.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
# frozen_string_literal: true

class UffizziCore::Api::Cli::V1::Template::CreateForm < UffizziCore::Template
validate :check_max_memory_limit
validate :check_max_memory_request

private

def check_max_memory_limit
return if valid_containers_memory_limit?

errors.add(:payload, :max_memory_limit_error, max: project.account.container_memory_limit)
end

def check_max_memory_request
return if valid_containers_memory_request?

errors.add(:payload, :max_memory_request_error, max: project.account.container_memory_limit)
end
end
16 changes: 0 additions & 16 deletions core/app/lib/uffizzi_core/concerns/models/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,5 @@ module UffizziCore::Concerns::Models::Template
enumerize :creation_source, in: [:manual, :compose_file, :system], predicates: true, scope: true

validates :name, presence: true

def valid_containers_memory_limit?
containers_attributes = payload['containers_attributes']
container_memory_limit = project.account.container_memory_limit
return true if container_memory_limit.nil?

containers_attributes.all? { |container| container['memory_limit'].to_i <= container_memory_limit }
end

def valid_containers_memory_request?
containers_attributes = payload['containers_attributes']
container_memory_limit = project.account.container_memory_limit
return true if container_memory_limit.nil?

containers_attributes.all? { |container| container['memory_request'].to_i <= container_memory_limit }
end
end
end
10 changes: 10 additions & 0 deletions core/app/models/uffizzi_core/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@

class UffizziCore::Deployment < UffizziCore::ApplicationRecord
include UffizziCore::Concerns::Models::Deployment
include UffizziCore::DependencyInjectionConcern

validate :check_max_memory_limit

def check_max_memory_limit
return if deployment_memory_module.valid_memory_limit?(self)

deployment_memory_module.memory_limit_error_message(self)
errors.add(:containers, message)
end
end
10 changes: 10 additions & 0 deletions core/app/models/uffizzi_core/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@

class UffizziCore::Template < UffizziCore::ApplicationRecord
include UffizziCore::Concerns::Models::Template
include UffizziCore::DependencyInjectionConcern

validate :check_max_memory_limit

def check_max_memory_limit
return if template_memory_module.valid_memory_limit?(self)

message = template_memory_module.memory_limit_error_message(self)
errors.add(:payload, message)
end
end
16 changes: 0 additions & 16 deletions core/app/services/uffizzi_core/container_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,6 @@ def continuously_deploy_enabled?(container)
container.aasm(:continuously_deploy).current_state == UffizziCore::Container::STATE_CD_ENABLED
end

def valid_memory_limit?(container)
max_memory_limit = container.deployment.project.account.container_memory_limit
container_memory_limit = container.memory_limit
return true if max_memory_limit.nil? || container_memory_limit.nil?

container_memory_limit <= max_memory_limit
end

def valid_memory_request?(container)
max_memory_limit = container.deployment.project.account.container_memory_limit
container_memory_request = container.memory_request
return true if max_memory_limit.nil? || container_memory_request.nil?

container_memory_request <= max_memory_limit
end

def last_state(container)
pods = pods_by_container(container)
container_status = container_status(container, pods)
Expand Down
11 changes: 11 additions & 0 deletions core/app/services/uffizzi_core/deployment/memory_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class UffizziCore::Deployment::MemoryService
class << self
def valid_memory_limit?(_deployment)
true
end

def memory_limit_error_message(_deployment); end
end
end
16 changes: 0 additions & 16 deletions core/app/services/uffizzi_core/deployment_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,6 @@ def all_containers_have_unique_ports?(containers)
containers.empty? || ports.size == ports.uniq.size
end

def valid_containers_memory_limit?(deployment)
containers = deployment.containers
container_memory_limit = deployment.project.account.container_memory_limit
return true if container_memory_limit.nil?

containers.all? { |container| container.memory_limit <= container_memory_limit }
end

def valid_containers_memory_request?(deployment)
containers = deployment.containers
container_memory_limit = deployment.project.account.container_memory_limit
return true if container_memory_limit.nil?

containers.all? { |container| container.memory_request <= container_memory_limit }
end

def ingress_container?(containers)
containers.empty? || containers.map(&:receive_incoming_requests).count(true) == 1
end
Expand Down
11 changes: 11 additions & 0 deletions core/app/services/uffizzi_core/template/memory_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class UffizziCore::Template::MemoryService
class << self
def valid_memory_limit?(_template)
true
end

def memory_limit_error_message(_template); end
end
end
3 changes: 1 addition & 2 deletions core/config/locales/en.activerecord.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ en:
uffizzi_core/deployment:
attributes:
containers:
max_memory_limit_error: "Memory limit of containers must be less than %{max}"
max_memory_request_error: "Memory request of containers must be less than %{max}"
max_memory_limit_error: "Total memory limit of containers must be less than %{max}"
uffizzi_core/user:
attributes:
email:
Expand Down
2 changes: 2 additions & 0 deletions core/lib/uffizzi_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
module UffizziCore
mattr_accessor :dependencies, default: {
rbac: 'UffizziCore::Rbac::UserAccessService',
deployment_memory_module: 'UffizziCore::Deployment::MemoryService',
template_memory_module: 'UffizziCore::Template::MemoryService',
}
mattr_accessor :table_names, default: {
accounts: :uffizzi_core_accounts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,8 @@ class UffizziCore::Api::Cli::V1::Projects::DeploymentsControllerTest < ActionCon

expected_default_container_params = {
secret_variables: [],
memory_limit: nil,
memory_request: nil,
memory_limit: Settings.compose.default_memory,
memory_request: Settings.compose.default_memory,
entrypoint: nil,
command: nil,
port: nil,
Expand Down
1 change: 1 addition & 0 deletions core/test/dummy/config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ compose:
continuous_preview:
default_delete_preview_after: 72
deployment:
max_memory_limit: 8000
subdomain:
length_limit: 63
vcluster:
Expand Down
2 changes: 2 additions & 0 deletions core/test/factories/containers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
continuously_deploy { UffizziCore::Container::STATE_CD_DISABLED }
healthcheck { nil }
volumes { nil }
memory_limit { Settings.compose.default_memory }
memory_request { Settings.compose.default_memory }

trait :continuously_deploy_enabled do
continuously_deploy { UffizziCore::Container::STATE_CD_ENABLED }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
services:
redis:
image: redis:latest

postgres:
image: postgres:9.6
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres

nginx:
image: nginx:latest
deploy:
resources:
limits:
memory: 4000M
configs:
- source: vote_conf
target: /etc/nginx/conf.d
entrypoint: /usr/sbin/nginx-debug
command:
- "-g"
- "daemon off;"

worker:
image: uffizzicloud/example-worker:latest
deploy:
resources:
limits:
memory: 4000M

vote:
image: uffizzicloud/example-vote:latest
deploy:
resources:
limits:
memory: 4000M

result:
image: uffizzicloud/example-result:latest

x-uffizzi-ingress:
service: nginx
port: 8080

configs:
vote_conf:
file: configs/vote.conf
defaulf_conf:
file: config_files/config_file.conf

0 comments on commit 9744930

Please sign in to comment.