-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #431 from UffizziCloud/feature/430_uffizzi_cluster…
…_api [430] added clusters api
- Loading branch information
Showing
46 changed files
with
661 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,3 +70,5 @@ deployment: | |
subdomain: | ||
length_limit: 63 | ||
default_job_retry_count: 5 | ||
vcluster: | ||
max_creation_retry_count: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
core/app/controller_modules/uffizzi_core/api/cli/v1/projects/clusters_controller_module.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module UffizziCore::Api::Cli::V1::Projects::ClustersControllerModule | ||
private | ||
|
||
def update_show_trial_quota_exceeded_warning; end | ||
|
||
def stop_if_deployment_forbidden; end | ||
end |
46 changes: 46 additions & 0 deletions
46
core/app/controllers/uffizzi_core/api/cli/v1/projects/clusters_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
class UffizziCore::Api::Cli::V1::Projects::ClustersController < UffizziCore::Api::Cli::V1::Projects::ApplicationController | ||
include UffizziCore::Api::Cli::V1::Projects::ClustersControllerModule | ||
|
||
before_action :authorize_uffizzi_core_api_cli_v1_projects_clusters | ||
before_action :stop_if_deployment_forbidden, only: [:create] | ||
after_action :update_show_trial_quota_exceeded_warning, only: [:create, :destroy] | ||
|
||
def index | ||
clusters = resource_project.clusters.enabled | ||
|
||
respond_with clusters | ||
end | ||
|
||
def create | ||
cluster_form = UffizziCore::Api::Cli::V1::Cluster::CreateForm.new(cluster_params) | ||
cluster_form.project = resource_project | ||
cluster_form.deployed_by = current_user | ||
return respond_with cluster_form unless cluster_form.save | ||
|
||
UffizziCore::ClusterService.start_deploy(cluster_form) | ||
|
||
respond_with cluster_form | ||
end | ||
|
||
def show | ||
respond_with resource_cluster | ||
end | ||
|
||
def destroy | ||
resource_cluster.disable! | ||
|
||
head(:no_content) | ||
end | ||
|
||
private | ||
|
||
def resource_cluster | ||
@resource_cluster ||= resource_project.clusters.enabled.find_by!(name: params[:name]) | ||
end | ||
|
||
def cluster_params | ||
params.require(:cluster) | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
core/app/forms/uffizzi_core/api/cli/v1/cluster/create_form.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class UffizziCore::Api::Cli::V1::Cluster::CreateForm < UffizziCore::Cluster | ||
include UffizziCore::ApplicationForm | ||
|
||
permit :name, :manifest | ||
|
||
validate :check_manifest, if: -> { manifest.present? } | ||
|
||
private | ||
|
||
def check_manifest | ||
YAML.load_stream(manifest) | ||
rescue Psych::SyntaxError => e | ||
err = [e.problem, e.context].compact.join(' ') | ||
|
||
errors.add(:manifest, err) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class UffizziCore::Cluster::DeleteJob < UffizziCore::ApplicationJob | ||
sidekiq_options queue: :deployments, retry: 5 | ||
|
||
def perform(id) | ||
Rails.logger.info("DEPLOYMENT_PROCESS cluster_id=#{id} DeleteJob") | ||
|
||
cluster = UffizziCore::Cluster.find(id) | ||
UffizziCore::ControllerService.delete_namespace(cluster) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class UffizziCore::Cluster::DeployJob < UffizziCore::ApplicationJob | ||
sidekiq_options queue: :deployments, retry: 5 | ||
|
||
def perform(id) | ||
cluster = UffizziCore::Cluster.find(id) | ||
|
||
UffizziCore::ClusterService.deploy_cluster(cluster) | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
core/app/jobs/uffizzi_core/cluster/manage_deploying_job.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
class UffizziCore::Cluster::ManageDeployingJob < UffizziCore::ApplicationJob | ||
sidekiq_options queue: :deployments, retry: 5 | ||
|
||
def perform(id, try = 1) | ||
cluster = UffizziCore::Cluster.find(id) | ||
|
||
UffizziCore::ClusterService.manage_deploying(cluster, try) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
module UffizziCore::Concerns::Models::Cluster | ||
extend ActiveSupport::Concern | ||
include UffizziCore::ClusterRepo | ||
|
||
included do | ||
include AASM | ||
|
||
self.table_name = UffizziCore.table_names[:clusters] | ||
|
||
belongs_to :project, class_name: UffizziCore::Project.name | ||
belongs_to :deployed_by, class_name: UffizziCore::User.name, foreign_key: :deployed_by_id, optional: true | ||
validates_uniqueness_of :name, conditions: -> { enabled } | ||
validates :name, presence: true, format: { with: /([A-Za-z0-9\-_]+)/ } | ||
|
||
aasm(:state) do | ||
state :deploying_namespace, initial: true | ||
state :failed_deploy_namespace | ||
state :deploying | ||
state :deployed | ||
state :failed | ||
state :disabled | ||
|
||
event :start_deploying do | ||
transitions from: [:deploying_namespace], to: :deploying | ||
end | ||
|
||
event :fail_deploy_namespace do | ||
transitions from: [:deploying_namespace], to: :failed_deploy_namespace | ||
end | ||
|
||
event :finish_deploy do | ||
transitions from: [:deploying], to: :deployed | ||
end | ||
|
||
event :fail do | ||
transitions from: [:deploying], to: :failed | ||
end | ||
|
||
event :disable, after: :after_disable do | ||
transitions from: [:failed_deploy_namespace, :deploying, :deployed, :failed], to: :disabled | ||
end | ||
end | ||
|
||
def after_disable | ||
UffizziCore::Cluster::DeleteJob.perform_async(id) | ||
end | ||
|
||
def namespace | ||
return name if name.present? | ||
|
||
"cluster-#{id}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class UffizziCore::Cluster < ApplicationRecord | ||
include UffizziCore::Concerns::Models::Cluster | ||
end |
19 changes: 19 additions & 0 deletions
19
core/app/policies/uffizzi_core/api/cli/v1/projects/clusters_policy.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class UffizziCore::Api::Cli::V1::Projects::ClustersPolicy < UffizziCore::ApplicationPolicy | ||
def index? | ||
context.user_access_module.any_access_to_project?(context.user, context.project) | ||
end | ||
|
||
def show? | ||
context.user_access_module.any_access_to_project?(context.user, context.project) | ||
end | ||
|
||
def create? | ||
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 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
module UffizziCore::ClusterRepo | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
scope :deployed, -> { where(state: UffizziCore::Cluster::STATE_DEPLOYED) } | ||
scope :enabled, -> { where.not(state: UffizziCore::Cluster::STATE_DISABLED) } | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
core/app/serializers/uffizzi_core/api/cli/v1/projects/cluster_serializer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class UffizziCore::Api::Cli::V1::Projects::ClusterSerializer < UffizziCore::BaseSerializer | ||
type :cluster | ||
|
||
attributes :name, :state, :kubeconfig | ||
end |
Oops, something went wrong.