Skip to content

Commit

Permalink
Merge pull request #486 from UffizziCloud/feature/485_add_k8s_version…
Browse files Browse the repository at this point in the history
…_param

Feature/485 add k8s version param
  • Loading branch information
moklidia authored Oct 16, 2023
2 parents d5b2ab4 + 51d6039 commit 8513063
Show file tree
Hide file tree
Showing 20 changed files with 194 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ def index
clusters = resource_project.clusters.enabled
return respond_with clusters if request_by_admin? || valid_request_from_ci_workflow?

respond_with clusters.deployed_by_user(current_user)
respond_with clusters.deployed_by_user(current_user), each_serializer: UffizziCore::Api::Cli::V1::Projects::ShortClusterSerializer
end

def create
version = cluster_params[:k8s_version]
kubernetes_distribution = find_kubernetes_distribution(version)
return render_distribution_version_error(version) if kubernetes_distribution.blank?

cluster_form = UffizziCore::Api::Cli::V1::Cluster::CreateForm.new(cluster_params)
cluster_form.project = resource_project
cluster_form.deployed_by = current_user
cluster_form.kubernetes_distribution = kubernetes_distribution
return respond_with cluster_form unless cluster_form.save

UffizziCore::ClusterService.start_deploy(cluster_form)
Expand Down Expand Up @@ -57,4 +62,16 @@ def valid_request_from_ci_workflow?
def cluster_params
params.require(:cluster)
end

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

UffizziCore::KubernetesDistribution.find_by(version: version)
end

def render_distribution_version_error(version)
available_versions = UffizziCore::KubernetesDistribution.pluck(:version).join(', ')
message = I18n.t('kubernetes_distribution.not_available', version: version, available_versions: available_versions)
render json: { errors: { kubernetes_distribution: [message] } }, status: :unprocessable_entity
end
end
1 change: 1 addition & 0 deletions core/app/lib/uffizzi_core/concerns/models/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module UffizziCore::Concerns::Models::Cluster
enumerize :creation_source, in: UffizziCore.cluster_creation_sources, scope: true, predicates: true
attribute :creation_source, :string, default: :manual
validates :creation_source, presence: true
belongs_to :kubernetes_distribution, optional: true

aasm(:state) do
state :deploying_namespace, initial: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module UffizziCore::Concerns::Models::KubernetesDistribution
extend ActiveSupport::Concern

included do
self.table_name = UffizziCore.table_names[:kubernetes_distributions]

has_many :clusters

def self.default
find_by(default: true)
end
end
end
5 changes: 5 additions & 0 deletions core/app/models/uffizzi_core/kubernetes_distribution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class UffizziCore::KubernetesDistribution < UffizziCore::ApplicationRecord
include UffizziCore::Concerns::Models::KubernetesDistribution
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
class UffizziCore::Api::Cli::V1::Projects::ClusterSerializer < UffizziCore::BaseSerializer
type :cluster

attributes :id, :name, :state, :kubeconfig, :created_at, :host
attributes :id, :name, :state, :kubeconfig, :created_at, :host, :k8s_version

def k8s_version
object.kubernetes_distribution&.version || UffizziCore::KubernetesDistribution.default.version
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class UffizziCore::Api::Cli::V1::Projects::ShortClusterSerializer < UffizziCore::BaseSerializer
type :cluster

attributes :id, :name
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,25 @@ class UffizziCore::Controller::CreateCluster::ClusterSerializer < UffizziCore::B
include UffizziCore::DependencyInjectionConcern
include_module_if_exists('UffizziCore::Controller::CreateCluster::ClusterSerializerModule')

attributes :name, :manifest, :base_ingress_host
attributes :name, :manifest, :base_ingress_host, :distro, :image

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

[object.namespace, managed_dns_zone].join('.')
end

def image
kubernetes_distribution.image
end

def distro
kubernetes_distribution.distro
end

private

def kubernetes_distribution
@kubernetes_distribution ||= object.kubernetes_distribution
end
end
3 changes: 3 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ en:
unsupported_login_type: This type of login is not supported
unauthorized: Unauthorized

kubernetes_distribution:
not_available: "The k8s version %{version} is not supported. Available version are: %{available_versions}."

enumerize:
credential:
type:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class CreateUffizziCoreKubernetesDistributions < ActiveRecord::Migration[6.1]
def change
create_table :uffizzi_core_kubernetes_distributions do |t|
t.string :version
t.string :distro
t.string :image
t.boolean :default, default: false
t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddKubernetesDistributionIdToUffizziCoreClusters < ActiveRecord::Migration[6.1]
def change
add_column(:uffizzi_core_clusters, :kubernetes_distribution_id, :integer, foreign_key: true)
end
end
1 change: 1 addition & 0 deletions core/lib/uffizzi_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module UffizziCore
host_volume_files: :uffizzi_core_host_volume_files,
container_host_volume_files: :uffizzi_core_container_host_volume_files,
deployment_events: :uffizzi_core_deployment_events,
kubernetes_distributions: :uffizzi_core_kubernetes_distributions,
}
mattr_accessor :user_creation_sources, default: [:system, :online_registration, :google, :sso]
mattr_accessor :user_project_roles, default: [:admin, :developer, :viewer]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class UffizziCore::Api::Cli::V1::Projects::ClustersControllerTest < ActionContro
@developer = create(:user)
create(:membership, :developer, account: @account, user: @developer)
create(:user_project, :developer, project: @project, user: @developer)
create(:kubernetes_distribution, :default)
end

teardown do
Expand Down Expand Up @@ -83,6 +84,28 @@ class UffizziCore::Api::Cli::V1::Projects::ClustersControllerTest < ActionContro
assert_requested(stubbed_cluster_request)
end

test '#create with wrong k8s version' do
sign_in(@admin)

params = {
project_slug: @project.slug,
cluster: {
name: 'my cluster',
k8s_version: 'wrong.version',
},
}

differences = {
-> { UffizziCore::Cluster.count } => 0,
}

assert_difference differences do
post :create, params: params, format: :json
end

assert_response(:unprocessable_entity)
end

test '#create when enabled cluster with the same name exists' do
sign_in(@admin)
name = 'test'
Expand Down
12 changes: 11 additions & 1 deletion core/test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_08_24_150022) do
ActiveRecord::Schema.define(version: 2023_10_09_182412) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -84,6 +84,7 @@
t.datetime "updated_at", precision: 6, null: false
t.string "host"
t.string "creation_source"
t.integer "kubernetes_distribution_id"
t.index ["project_id"], name: "index_cluster_on_project_id"
end

Expand Down Expand Up @@ -277,6 +278,15 @@
t.index ["token"], name: "index_invitations_on_token", unique: true
end

create_table "uffizzi_core_kubernetes_distributions", force: :cascade do |t|
t.string "version"
t.string "distro"
t.string "image"
t.boolean "default", default: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "uffizzi_core_memberships", force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "account_id", null: false
Expand Down
13 changes: 13 additions & 0 deletions core/test/factories/kubernetes_distribution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

FactoryBot.define do
factory :kubernetes_distribution, class: UffizziCore::KubernetesDistribution do
version { generate(:version) }
image { generate(:string) }
distro { generate(:string) }

trait :default do
default { true }
end
end
end
4 changes: 4 additions & 0 deletions core/test/factories/sequences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,8 @@
sequence :cluster_name do |n|
"cluster-name-#{n}"
end

sequence :version do |n|
"#{n}.#{n}#{n}"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

# This migration comes from uffizzi_core (originally 20230810140316)

class AddSourceToUffizziCoreClusters < ActiveRecord::Migration[6.1]
def change
add_column(:uffizzi_core_clusters, :creation_source, :string)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

# This migration comes from uffizzi_core (originally 20231009162719)
class CreateUffizziCoreKubernetesDistributions < ActiveRecord::Migration[6.1]
def change
create_table :uffizzi_core_kubernetes_distributions do |t|
t.string :version
t.string :distro
t.string :image
t.boolean :default
t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

# This migration comes from uffizzi_core (originally 20231009182412)
class AddKubernetesDistributionIdToUffizziCoreClusters < ActiveRecord::Migration[6.1]
def change
add_column(:uffizzi_core_clusters, :kubernetes_distribution_id, :integer, foreign_key: true)
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_08_24_150022) do
ActiveRecord::Schema.define(version: 2023_10_09_163516) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -83,6 +83,7 @@
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "host"
t.string "creation_source"
t.index ["project_id"], name: "index_cluster_on_project_id"
end

Expand Down Expand Up @@ -276,6 +277,15 @@
t.index ["token"], name: "index_invitations_on_token", unique: true
end

create_table "uffizzi_core_kubernetes_distributions", force: :cascade do |t|
t.string "version"
t.string "distro"
t.string "image"
t.boolean "default"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "uffizzi_core_memberships", force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "account_id", null: false
Expand Down
11 changes: 11 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# frozen_string_literal: true

puts 'Creating User'

user = UffizziCore::User.create!(
email: '[email protected]',
password: 'password',
state: UffizziCore::User::STATE_ACTIVE,
creation_source: UffizziCore::User.creation_source.system,
)

puts 'Creating Accounts'

personal_account = UffizziCore::Account.create!(
owner: user,
name: 'personal',
Expand All @@ -30,3 +34,10 @@
organizational_project = organizational_account.projects.create!(name: 'uffizzi', slug: 'uffizzi',
state: UffizziCore::Project::STATE_ACTIVE)
organizational_project.user_projects.create!(user: user, role: UffizziCore::UserProject.role.admin)

puts 'Creating Kubernetes Distributions'

UffizziCore::KubernetesDistribution.create(distro: 'k3s', version: '1.25', image: 'rancher/k3s:v1.25.14-k3s1')
UffizziCore::KubernetesDistribution.create(distro: 'k3s', version: '1.26', image: 'rancher/k3s:v1.26.9-k3s1')
UffizziCore::KubernetesDistribution.create(distro: 'k3s', version: '1.27', default: true, image: 'rancher/k3s:v1.27.6-k3s1')
UffizziCore::KubernetesDistribution.create(distro: 'k3s', version: '1.28', image: 'rancher/k3s:v1.28.2-k3s1')

0 comments on commit 8513063

Please sign in to comment.