uffizzi_core 2.2.24 → 2.2.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2d8112c651358e76a331ea2e85d1562ecd15724adaf89eeebc10146958ef9550
4
- data.tar.gz: fbc115075ccb252fbc6446b51fccd93c8571d3b106a83376b173b63c7de25fd7
3
+ metadata.gz: 70847fae8bc28249be5879f8496e7436e05079077dc23ad53394d08676158be6
4
+ data.tar.gz: 2ac3e45d651e069f5e11b69b022efa285c5f0c0f176fb5a17c0bc58c3d4dad88
5
5
  SHA512:
6
- metadata.gz: 55f7fb96c94e8dfca56310456bc3202764d1e16173fe57e96c027609748ae10bd758af06c355682c2c8ef2634b10402ce899778efb7467a99f16bd5aa4745a7f
7
- data.tar.gz: 8d323b2dff07e8d9d03918e5642cd10093a9e2decc0f2cbf3a9e017b2ad99e8165bd447ea210a28a0497153b955244ebd68ce0d9d8db20424ed9cb05cfbf3273
6
+ metadata.gz: c128f0afe2384d0763597a9bbc3977748282a454a7d6396680b6f6642f00aa3b6ea32ee0d6c1125846338fec29e8ec02a0bf135d16943a3a2f3ff1f7caf5e797
7
+ data.tar.gz: 8a0877404587daa608bb636f6500c03578d711ab7cb8e58db97ed9cb663114374cef6f8a619f6dbc9a5c0c0ac3fe4478bf559f53df1c3b6f266d3820f7661f3c
@@ -78,6 +78,10 @@ class UffizziCore::ControllerClient
78
78
  get("/namespaces/#{namespace}/cluster/#{name}")
79
79
  end
80
80
 
81
+ def patch_cluster(name:, namespace:, body:)
82
+ patch("/namespaces/#{namespace}/cluster/#{name}", body)
83
+ end
84
+
81
85
  private
82
86
 
83
87
  def get(url, params = {})
@@ -88,6 +92,10 @@ class UffizziCore::ControllerClient
88
92
  make_request(:post, url, params)
89
93
  end
90
94
 
95
+ def patch(url, params = {})
96
+ make_request(:patch, url, params)
97
+ end
98
+
91
99
  def make_request(method, url, params)
92
100
  response = connection.send(method, url, params)
93
101
  body = response.body
@@ -30,6 +30,32 @@ class UffizziCore::Api::Cli::V1::Projects::ClustersController < UffizziCore::Api
30
30
  respond_with cluster_form
31
31
  end
32
32
 
33
+ def scale_down
34
+ if resource_cluster.deployed?
35
+ UffizziCore::ClusterService.scale_down!(resource_cluster)
36
+ return respond_with resource_cluster
37
+ end
38
+
39
+ return render_scale_error(I18n.t('cluster.already_asleep', name: resource_cluster.name)) if resource_cluster.scaled_down?
40
+
41
+ if resource_cluster.deploying_namespace? || resource_cluster.deploying?
42
+ render_scale_error(I18n.t('cluster.deploy_in_process', name: resource_cluster.name))
43
+ end
44
+ rescue AASM::InvalidTransition, UffizziCore::ClusterScaleError => e
45
+ render_scale_error(e.message)
46
+ end
47
+
48
+ def scale_up
49
+ if resource_cluster.scaled_down?
50
+ UffizziCore::ClusterService.scale_up!(resource_cluster)
51
+ return respond_with resource_cluster
52
+ end
53
+
54
+ return render_scale_error(I18n.t('cluster.already_awake', name: resource_cluster.name)) if resource_cluster.deployed?
55
+ rescue AASM::InvalidTransition, UffizziCore::ClusterScaleError => e
56
+ render_scale_error(e.message)
57
+ end
58
+
33
59
  def show
34
60
  respond_with resource_cluster
35
61
  end
@@ -63,6 +89,10 @@ class UffizziCore::Api::Cli::V1::Projects::ClustersController < UffizziCore::Api
63
89
  params.require(:cluster)
64
90
  end
65
91
 
92
+ def render_scale_error(message)
93
+ render json: { errors: { state: [message] } }, status: :unprocessable_entity
94
+ end
95
+
66
96
  def find_kubernetes_distribution(version)
67
97
  return UffizziCore::KubernetesDistribution.default if version.blank?
68
98
 
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UffizziCore::ClusterScaleError < StandardError
4
+ def initialize(action)
5
+ message = I18n.t('cluster.scaling_failed', action: action)
6
+
7
+ super(message)
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UffizziCore::Cluster::ManageScalingDownJob < UffizziCore::ApplicationJob
4
+ sidekiq_options queue: :clusters, retry: Settings.default_job_retry_count
5
+
6
+ def perform(id)
7
+ cluster = UffizziCore::Cluster.find(id)
8
+
9
+ UffizziCore::ClusterService.manage_scale_down(cluster)
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UffizziCore::Cluster::ManageScalingUpJob < UffizziCore::ApplicationJob
4
+ sidekiq_options queue: :clusters, retry: Settings.default_job_retry_count
5
+
6
+ def perform(id, try = 1)
7
+ cluster = UffizziCore::Cluster.find(id)
8
+
9
+ UffizziCore::ClusterService.manage_scale_up(cluster, try)
10
+ end
11
+ end
@@ -6,6 +6,7 @@ module UffizziCore::Concerns::Models::Cluster
6
6
 
7
7
  NAMESPACE_PREFIX = 'c'
8
8
 
9
+ # rubocop:disable Metrics/BlockLength
9
10
  included do
10
11
  include AASM
11
12
  extend Enumerize
@@ -27,6 +28,10 @@ module UffizziCore::Concerns::Models::Cluster
27
28
  state :failed_deploy_namespace
28
29
  state :deploying
29
30
  state :deployed
31
+ state :scaling_down
32
+ state :scaled_down
33
+ state :scaling_up
34
+ state :failed_scale_up
30
35
  state :failed
31
36
  state :disabled
32
37
 
@@ -42,12 +47,40 @@ module UffizziCore::Concerns::Models::Cluster
42
47
  transitions from: [:deploying], to: :deployed
43
48
  end
44
49
 
50
+ event :start_scaling_down do
51
+ transitions from: [:deployed], to: :scaling_down
52
+ end
53
+
54
+ event :scale_down do
55
+ transitions from: [:scaling_down], to: :scaled_down
56
+ end
57
+
58
+ event :start_scaling_up do
59
+ transitions from: [:scaled_down, :failed_scale_up], to: :scaling_up
60
+ end
61
+
62
+ event :scale_up do
63
+ transitions from: [:scaling_up], to: :deployed
64
+ end
65
+
66
+ event :fail_scale_up do
67
+ transitions from: [:scaling_up], to: :failed_scale_up
68
+ end
69
+
45
70
  event :fail do
46
71
  transitions from: [:deploying], to: :failed
47
72
  end
48
73
 
49
74
  event :disable, after: :after_disable do
50
- transitions from: [:deploying_namespace, :failed_deploy_namespace, :deploying, :deployed, :failed], to: :disabled
75
+ transitions from: [
76
+ :deploying_namespace,
77
+ :failed_deploy_namespace,
78
+ :deploying,
79
+ :deployed,
80
+ :scaling_down,
81
+ :scaled_down,
82
+ :failed,
83
+ ], to: :disabled
51
84
  end
52
85
  end
53
86
 
@@ -59,4 +92,5 @@ module UffizziCore::Concerns::Models::Cluster
59
92
  [NAMESPACE_PREFIX, id].join
60
93
  end
61
94
  end
95
+ # rubocop:enable Metrics/BlockLength
62
96
  end
@@ -13,6 +13,14 @@ class UffizziCore::Api::Cli::V1::Projects::ClustersPolicy < UffizziCore::Applica
13
13
  context.user_access_module.admin_or_developer_access_to_project?(context.user, context.project)
14
14
  end
15
15
 
16
+ def scale_down?
17
+ context.user_access_module.admin_or_developer_access_to_project?(context.user, context.project)
18
+ end
19
+
20
+ def scale_up?
21
+ context.user_access_module.admin_or_developer_access_to_project?(context.user, context.project)
22
+ end
23
+
16
24
  def destroy?
17
25
  context.user_access_module.admin_or_developer_access_to_project?(context.user, context.project)
18
26
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UffizziCore::Controller::UpdateCluster::ClusterSerializer < UffizziCore::BaseSerializer
4
+ include UffizziCore::DependencyInjectionConcern
5
+ include_module_if_exists('UffizziCore::Controller::UpdateCluster::ClusterSerializerModule')
6
+
7
+ attributes :name, :manifest, :base_ingress_host
8
+
9
+ def base_ingress_host
10
+ managed_dns_zone = controller_settings_service.vcluster(object).managed_dns_zone
11
+
12
+ [object.namespace, managed_dns_zone].join('.')
13
+ end
14
+ end
@@ -24,6 +24,32 @@ class UffizziCore::ClusterService
24
24
  UffizziCore::Cluster::ManageDeployingJob.perform_in(5.seconds, cluster.id)
25
25
  end
26
26
 
27
+ def scale_up!(cluster)
28
+ cluster.start_scaling_up!
29
+ UffizziCore::ControllerService.patch_cluster(cluster, sleep: false)
30
+ UffizziCore::Cluster::ManageScalingUpJob.perform_in(5.seconds, cluster.id)
31
+ end
32
+
33
+ def manage_scale_up(cluster, try)
34
+ return cluster.fail_scale_up! if try > Settings.vcluster.max_scale_up_retry_count
35
+ return cluster.scale_up! if ready?(cluster)
36
+
37
+ UffizziCore::Cluster::ManageScalingUpJob.perform_in(5.seconds, cluster.id, ++try)
38
+ end
39
+
40
+ def scale_down!(cluster)
41
+ cluster.start_scaling_down!
42
+ UffizziCore::ControllerService.patch_cluster(cluster, sleep: true)
43
+
44
+ UffizziCore::Cluster::ManageScalingDownJob.perform_in(5.seconds, cluster.id)
45
+ end
46
+
47
+ def manage_scale_down(cluster)
48
+ return cluster.scale_down! unless awake?(cluster)
49
+
50
+ UffizziCore::Cluster::ManageScalingDownJob.perform_in(5.seconds, cluster.id)
51
+ end
52
+
27
53
  def manage_deploying(cluster, try)
28
54
  return if cluster.disabled?
29
55
  return cluster.fail! if try > Settings.vcluster.max_creation_retry_count
@@ -41,5 +67,19 @@ class UffizziCore::ClusterService
41
67
 
42
68
  UffizziCore::Cluster::ManageDeployingJob.perform_in(5.seconds, cluster.id, ++try)
43
69
  end
70
+
71
+ private
72
+
73
+ def awake?(cluster)
74
+ data = UffizziCore::ControllerService.show_cluster(cluster)
75
+
76
+ !data.status.sleep
77
+ end
78
+
79
+ def ready?(cluster)
80
+ data = UffizziCore::ControllerService.show_cluster(cluster)
81
+
82
+ data.status.ready
83
+ end
44
84
  end
45
85
  end
@@ -110,6 +110,13 @@ class UffizziCore::ControllerService
110
110
  controller_client(cluster).delete_cluster(namespace: cluster.namespace)
111
111
  end
112
112
 
113
+ def patch_cluster(cluster, sleep:)
114
+ body = UffizziCore::Controller::UpdateCluster::ClusterSerializer.new(cluster).as_json
115
+ body[:sleep] = sleep
116
+
117
+ controller_client(cluster).patch_cluster(name: cluster.name, namespace: cluster.namespace, body: body)
118
+ end
119
+
113
120
  private
114
121
 
115
122
  def check_any_container_has_public_port(containers)
@@ -83,6 +83,12 @@ en:
83
83
  deployment:
84
84
  invalid_state: Preview with ID deployment-%{id} %{state}
85
85
  already_exists: An active deployment already exists
86
+
87
+ cluster:
88
+ already_asleep: The cluster %{name} is already asleep.
89
+ already_awake: The cluster %{name} is already awake.
90
+ scaling_failed: Failed to %{action} cluster.
91
+ deploy_in_process: Please wait until the cluster %{name} is deployed.
86
92
 
87
93
  session:
88
94
  unsupported_login_type: This type of login is not supported
data/config/routes.rb CHANGED
@@ -10,7 +10,12 @@ UffizziCore::Engine.routes.draw do
10
10
  resources :projects, only: ['index', 'show', 'destroy'], param: :slug do
11
11
  scope module: :projects do
12
12
  resource :compose_file, only: ['show', 'create', 'destroy']
13
- resources :clusters, only: [:index, :create, :show, :destroy], param: :name
13
+ resources :clusters, only: [:index, :create, :show, :destroy], param: :name do
14
+ member do
15
+ put :scale_down
16
+ put :scale_up
17
+ end
18
+ end
14
19
  resources :deployments, only: ['index', 'show', 'create', 'destroy', 'update'] do
15
20
  post :deploy_containers, on: :member
16
21
  scope module: :deployments do
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UffizziCore
4
- VERSION = '2.2.24'
4
+ VERSION = '2.2.25'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uffizzi_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.24
4
+ version: 2.2.25
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Thurman
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-10-16 00:00:00.000000000 Z
12
+ date: 2023-10-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aasm
@@ -804,6 +804,7 @@ files:
804
804
  - app/controllers/uffizzi_core/api/cli/v1/projects_controller.rb
805
805
  - app/controllers/uffizzi_core/api/cli/v1/sessions_controller.rb
806
806
  - app/controllers/uffizzi_core/application_controller.rb
807
+ - app/errors/uffizzi_core/cluster_scale_error.rb
807
808
  - app/errors/uffizzi_core/compose_file/build_error.rb
808
809
  - app/errors/uffizzi_core/compose_file/credential_error.rb
809
810
  - app/errors/uffizzi_core/compose_file/parse_error.rb
@@ -841,6 +842,8 @@ files:
841
842
  - app/jobs/uffizzi_core/cluster/delete_job.rb
842
843
  - app/jobs/uffizzi_core/cluster/deploy_job.rb
843
844
  - app/jobs/uffizzi_core/cluster/manage_deploying_job.rb
845
+ - app/jobs/uffizzi_core/cluster/manage_scaling_down_job.rb
846
+ - app/jobs/uffizzi_core/cluster/manage_scaling_up_job.rb
844
847
  - app/jobs/uffizzi_core/config_file/apply_job.rb
845
848
  - app/jobs/uffizzi_core/deployment/create_credential_job.rb
846
849
  - app/jobs/uffizzi_core/deployment/create_credentials_job.rb
@@ -1000,6 +1003,7 @@ files:
1000
1003
  - app/serializers/uffizzi_core/controller/deploy_containers/container_serializer/container_host_volume_file_serializer.rb
1001
1004
  - app/serializers/uffizzi_core/controller/deploy_containers/credential_serializer.rb
1002
1005
  - app/serializers/uffizzi_core/controller/deploy_containers/host_volume_file_serializer.rb
1006
+ - app/serializers/uffizzi_core/controller/update_cluster/cluster_serializer.rb
1003
1007
  - app/services/uffizzi_core/account_service.rb
1004
1008
  - app/services/uffizzi_core/activity_item_service.rb
1005
1009
  - app/services/uffizzi_core/ci_service.rb