Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
moklidia committed Oct 19, 2023
2 parents 4fc7e73 + aa32c68 commit a57b521
Show file tree
Hide file tree
Showing 21 changed files with 262 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: core
specs:
uffizzi_core (2.2.24)
uffizzi_core (2.2.25)
aasm
actionpack (~> 6.1.0)
active_model_serializers
Expand Down Expand Up @@ -111,7 +111,7 @@ GEM
ast (2.4.2)
awesome_print (1.9.2)
aws-eventstream (1.2.0)
aws-partitions (1.835.0)
aws-partitions (1.838.0)
aws-sdk-core (3.185.1)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.651.0)
Expand Down
4 changes: 2 additions & 2 deletions core/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM ruby:3.0.2-alpine3.14

RUN apk update && apk upgrade && apk add bash curl-dev ruby-dev build-base git \
RUN apk update && apk upgrade && apk add bash curl-dev ruby-dev build-base git gcompat \
curl ruby-json openssl postgresql-dev postgresql-client tzdata

RUN mkdir -p /gem
Expand All @@ -15,4 +15,4 @@ RUN bundle install --jobs 4

COPY . /gem

ENV PATH /gem/bin:$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH
ENV PATH /gem/bin:$GEM_HOME/bin:$GEM_HOME/gems/bin:$PATH
6 changes: 3 additions & 3 deletions core/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
uffizzi_core (2.2.24)
uffizzi_core (2.2.25)
aasm
actionpack (~> 6.1.0)
active_model_serializers
Expand Down Expand Up @@ -110,7 +110,7 @@ GEM
activerecord (>= 5.2.6)
awesome_print (1.9.2)
aws-eventstream (1.2.0)
aws-partitions (1.835.0)
aws-partitions (1.838.0)
aws-sdk-core (3.185.1)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.651.0)
Expand Down Expand Up @@ -379,7 +379,7 @@ GEM
swagger_yard (1.1.1)
parslet
yard
thor (1.2.2)
thor (1.3.0)
thread_safe (0.3.6)
timeout (0.4.0)
tzinfo (2.0.4)
Expand Down
8 changes: 8 additions & 0 deletions core/app/clients/uffizzi_core/controller_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ def show_cluster(namespace:, name:)
get("/namespaces/#{namespace}/cluster/#{name}")
end

def patch_cluster(name:, namespace:, body:)
patch("/namespaces/#{namespace}/cluster/#{name}", body)
end

private

def get(url, params = {})
Expand All @@ -88,6 +92,10 @@ def post(url, params = {})
make_request(:post, url, params)
end

def patch(url, params = {})
make_request(:patch, url, params)
end

def make_request(method, url, params)
response = connection.send(method, url, params)
body = response.body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ def create
respond_with cluster_form
end

def scale_down
if resource_cluster.deployed?
UffizziCore::ClusterService.scale_down!(resource_cluster)
return respond_with resource_cluster
end

return render_scale_error(I18n.t('cluster.already_asleep', name: resource_cluster.name)) if resource_cluster.scaled_down?

if resource_cluster.deploying_namespace? || resource_cluster.deploying?
render_scale_error(I18n.t('cluster.deploy_in_process', name: resource_cluster.name))
end
rescue AASM::InvalidTransition, UffizziCore::ClusterScaleError => e
render_scale_error(e.message)
end

def scale_up
if resource_cluster.scaled_down?
UffizziCore::ClusterService.scale_up!(resource_cluster)
return respond_with resource_cluster
end

return render_scale_error(I18n.t('cluster.already_awake', name: resource_cluster.name)) if resource_cluster.deployed?
rescue AASM::InvalidTransition, UffizziCore::ClusterScaleError => e
render_scale_error(e.message)
end

def show
respond_with resource_cluster
end
Expand Down Expand Up @@ -63,6 +89,10 @@ def cluster_params
params.require(:cluster)
end

def render_scale_error(message)
render json: { errors: { state: [message] } }, status: :unprocessable_entity
end

def find_kubernetes_distribution(version)
return UffizziCore::KubernetesDistribution.default if version.blank?

Expand Down
9 changes: 9 additions & 0 deletions core/app/errors/uffizzi_core/cluster_scale_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class UffizziCore::ClusterScaleError < StandardError
def initialize(action)
message = I18n.t('cluster.scaling_failed', action: action)

super(message)
end
end
11 changes: 11 additions & 0 deletions core/app/jobs/uffizzi_core/cluster/manage_scaling_down_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class UffizziCore::Cluster::ManageScalingDownJob < UffizziCore::ApplicationJob
sidekiq_options queue: :clusters, retry: Settings.default_job_retry_count

def perform(id)
cluster = UffizziCore::Cluster.find(id)

UffizziCore::ClusterService.manage_scale_down(cluster)
end
end
11 changes: 11 additions & 0 deletions core/app/jobs/uffizzi_core/cluster/manage_scaling_up_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class UffizziCore::Cluster::ManageScalingUpJob < UffizziCore::ApplicationJob
sidekiq_options queue: :clusters, retry: Settings.default_job_retry_count

def perform(id, try = 1)
cluster = UffizziCore::Cluster.find(id)

UffizziCore::ClusterService.manage_scale_up(cluster, try)
end
end
36 changes: 35 additions & 1 deletion core/app/lib/uffizzi_core/concerns/models/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module UffizziCore::Concerns::Models::Cluster

NAMESPACE_PREFIX = 'c'

# rubocop:disable Metrics/BlockLength
included do
include AASM
extend Enumerize
Expand All @@ -27,6 +28,10 @@ module UffizziCore::Concerns::Models::Cluster
state :failed_deploy_namespace
state :deploying
state :deployed
state :scaling_down
state :scaled_down
state :scaling_up
state :failed_scale_up
state :failed
state :disabled

Expand All @@ -42,12 +47,40 @@ module UffizziCore::Concerns::Models::Cluster
transitions from: [:deploying], to: :deployed
end

event :start_scaling_down do
transitions from: [:deployed], to: :scaling_down
end

event :scale_down do
transitions from: [:scaling_down], to: :scaled_down
end

event :start_scaling_up do
transitions from: [:scaled_down, :failed_scale_up], to: :scaling_up
end

event :scale_up do
transitions from: [:scaling_up], to: :deployed
end

event :fail_scale_up do
transitions from: [:scaling_up], to: :failed_scale_up
end

event :fail do
transitions from: [:deploying], to: :failed
end

event :disable, after: :after_disable do
transitions from: [:deploying_namespace, :failed_deploy_namespace, :deploying, :deployed, :failed], to: :disabled
transitions from: [
:deploying_namespace,
:failed_deploy_namespace,
:deploying,
:deployed,
:scaling_down,
:scaled_down,
:failed,
], to: :disabled
end
end

Expand All @@ -59,4 +92,5 @@ def namespace
[NAMESPACE_PREFIX, id].join
end
end
# rubocop:enable Metrics/BlockLength
end
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def create?
context.user_access_module.admin_or_developer_access_to_project?(context.user, context.project)
end

def scale_down?
context.user_access_module.admin_or_developer_access_to_project?(context.user, context.project)
end

def scale_up?
context.user_access_module.admin_or_developer_access_to_project?(context.user, context.project)
end

def destroy?
context.user_access_module.admin_or_developer_access_to_project?(context.user, context.project)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class UffizziCore::Controller::UpdateCluster::ClusterSerializer < UffizziCore::BaseSerializer
include UffizziCore::DependencyInjectionConcern
include_module_if_exists('UffizziCore::Controller::UpdateCluster::ClusterSerializerModule')

attributes :name, :manifest, :base_ingress_host

def base_ingress_host
managed_dns_zone = controller_settings_service.vcluster(object).managed_dns_zone

[object.namespace, managed_dns_zone].join('.')
end
end
40 changes: 40 additions & 0 deletions core/app/services/uffizzi_core/cluster_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ def deploy_cluster(cluster)
UffizziCore::Cluster::ManageDeployingJob.perform_in(5.seconds, cluster.id)
end

def scale_up!(cluster)
cluster.start_scaling_up!
UffizziCore::ControllerService.patch_cluster(cluster, sleep: false)
UffizziCore::Cluster::ManageScalingUpJob.perform_in(5.seconds, cluster.id)
end

def manage_scale_up(cluster, try)
return cluster.fail_scale_up! if try > Settings.vcluster.max_scale_up_retry_count
return cluster.scale_up! if ready?(cluster)

UffizziCore::Cluster::ManageScalingUpJob.perform_in(5.seconds, cluster.id, ++try)
end

def scale_down!(cluster)
cluster.start_scaling_down!
UffizziCore::ControllerService.patch_cluster(cluster, sleep: true)

UffizziCore::Cluster::ManageScalingDownJob.perform_in(5.seconds, cluster.id)
end

def manage_scale_down(cluster)
return cluster.scale_down! unless awake?(cluster)

UffizziCore::Cluster::ManageScalingDownJob.perform_in(5.seconds, cluster.id)
end

def manage_deploying(cluster, try)
return if cluster.disabled?
return cluster.fail! if try > Settings.vcluster.max_creation_retry_count
Expand All @@ -41,5 +67,19 @@ def manage_deploying(cluster, try)

UffizziCore::Cluster::ManageDeployingJob.perform_in(5.seconds, cluster.id, ++try)
end

private

def awake?(cluster)
data = UffizziCore::ControllerService.show_cluster(cluster)

!data.status.sleep
end

def ready?(cluster)
data = UffizziCore::ControllerService.show_cluster(cluster)

data.status.ready
end
end
end
7 changes: 7 additions & 0 deletions core/app/services/uffizzi_core/controller_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ def delete_cluster(cluster)
controller_client(cluster).delete_cluster(namespace: cluster.namespace)
end

def patch_cluster(cluster, sleep:)
body = UffizziCore::Controller::UpdateCluster::ClusterSerializer.new(cluster).as_json
body[:sleep] = sleep

controller_client(cluster).patch_cluster(name: cluster.name, namespace: cluster.namespace, body: body)
end

private

def check_any_container_has_public_port(containers)
Expand Down
6 changes: 6 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ en:
deployment:
invalid_state: Preview with ID deployment-%{id} %{state}
already_exists: An active deployment already exists

cluster:
already_asleep: The cluster %{name} is already asleep.
already_awake: The cluster %{name} is already awake.
scaling_failed: Failed to %{action} cluster.
deploy_in_process: Please wait until the cluster %{name} is deployed.

session:
unsupported_login_type: This type of login is not supported
Expand Down
7 changes: 6 additions & 1 deletion core/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
resources :projects, only: ['index', 'show', 'destroy'], param: :slug do
scope module: :projects do
resource :compose_file, only: ['show', 'create', 'destroy']
resources :clusters, only: [:index, :create, :show, :destroy], param: :name
resources :clusters, only: [:index, :create, :show, :destroy], param: :name do
member do
put :scale_down
put :scale_up
end
end
resources :deployments, only: ['index', 'show', 'create', 'destroy', 'update'] do
post :deploy_containers, on: :member
scope module: :deployments do
Expand Down
2 changes: 1 addition & 1 deletion core/lib/uffizzi_core/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module UffizziCore
VERSION = '2.2.24'
VERSION = '2.2.25'
end
Loading

0 comments on commit a57b521

Please sign in to comment.