Commit b129f067 authored by Shinya Maeda's avatar Shinya Maeda

Fix out of sync with KubernetesService. Remove namespace pramas from...

Fix out of sync with KubernetesService. Remove namespace pramas from controller. Use time_with_zone in schema. Remove Gcp::Clusters from safe_model_attributes.ym
parent 43859143
...@@ -94,9 +94,6 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -94,9 +94,6 @@ class Projects::ClustersController < Projects::ApplicationController
:name, :name,
:platform_type, :platform_type,
:provider_type, :provider_type,
platform_kubernetes_attributes: [
:namespace
],
provider_gcp_attributes: [ provider_gcp_attributes: [
:gcp_project_id, :gcp_project_id,
:zone, :zone,
...@@ -106,11 +103,7 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -106,11 +103,7 @@ class Projects::ClustersController < Projects::ApplicationController
end end
def update_params def update_params
params.require(:cluster).permit( params.require(:cluster).permit(:enabled)
:enabled,
platform_kubernetes_attributes: [
:namespace
])
end end
def authorize_google_api def authorize_google_api
......
...@@ -21,10 +21,16 @@ module Clusters ...@@ -21,10 +21,16 @@ module Clusters
validates :name, cluster_name: true validates :name, cluster_name: true
validate :restrict_modification, on: :update validate :restrict_modification, on: :update
# TODO: Move back this into Clusters::Platforms::Kubernetes in 10.3
# We need callback here because `enabled` belongs to Clusters::Cluster
# Callbacks in Clusters::Platforms::Kubernetes will not be called after update
after_save :update_kubernetes_integration!
delegate :status, to: :provider, allow_nil: true delegate :status, to: :provider, allow_nil: true
delegate :status_reason, to: :provider, allow_nil: true delegate :status_reason, to: :provider, allow_nil: true
delegate :status_name, to: :provider, allow_nil: true delegate :status_name, to: :provider, allow_nil: true
delegate :on_creation?, to: :provider, allow_nil: true delegate :on_creation?, to: :provider, allow_nil: true
delegate :update_kubernetes_integration!, to: :platform, allow_nil: true
enum platform_type: { enum platform_type: {
kubernetes: 1 kubernetes: 1
......
module Clusters module Clusters
module Platforms module Platforms
class Kubernetes < ActiveRecord::Base class Kubernetes < ActiveRecord::Base
include Gitlab::CurrentSettings
self.table_name = 'cluster_platforms_kubernetes' self.table_name = 'cluster_platforms_kubernetes'
belongs_to :cluster, inverse_of: :platform_kubernetes, class_name: 'Clusters::Cluster' belongs_to :cluster, inverse_of: :platform_kubernetes, class_name: 'Clusters::Cluster'
...@@ -28,13 +26,10 @@ module Clusters ...@@ -28,13 +26,10 @@ module Clusters
} }
# We expect to be `active?` only when enabled and cluster is created (the api_url is assigned) # We expect to be `active?` only when enabled and cluster is created (the api_url is assigned)
with_options presence: true, if: :enabled? do
validates :api_url, url: true, presence: true validates :api_url, url: true, presence: true
validates :token, presence: true validates :token, presence: true
end
# TODO: Glue code till we migrate Kubernetes Integration into Platforms::Kubernetes # TODO: Glue code till we migrate Kubernetes Integration into Platforms::Kubernetes
after_save :update_kubernetes_integration!
after_destroy :destroy_kubernetes_integration! after_destroy :destroy_kubernetes_integration!
alias_attribute :ca_pem, :ca_cert alias_attribute :ca_pem, :ca_cert
...@@ -60,6 +55,18 @@ module Clusters ...@@ -60,6 +55,18 @@ module Clusters
self.class.namespace_for_project(project) if project self.class.namespace_for_project(project) if project
end end
def update_kubernetes_integration!
raise 'Kubernetes service already configured' unless manages_kubernetes_service?
ensure_kubernetes_service.update!(
active: enabled?,
api_url: api_url,
namespace: namespace,
token: token,
ca_pem: ca_cert
)
end
private private
def enforce_namespace_to_lower_case def enforce_namespace_to_lower_case
...@@ -79,24 +86,12 @@ module Clusters ...@@ -79,24 +86,12 @@ module Clusters
kubernetes_service.destroy! kubernetes_service.destroy!
end end
def update_kubernetes_integration!
return raise 'Kubernetes service already configured' unless manages_kubernetes_service?
ensure_kubernetes_service.update!(
active: enabled?,
api_url: api_url,
namespace: namespace,
token: token,
ca_pem: ca_cert,
)
end
def kubernetes_service def kubernetes_service
@kubernetes_service ||= project.kubernetes_service || project.build_kubernetes_service @kubernetes_service ||= project&.kubernetes_service
end end
def ensure_kubernetes_service def ensure_kubernetes_service
@kubernetes_service ||= kubernetes_service || project.build_kubernetes_service @kubernetes_service ||= kubernetes_service || project&.build_kubernetes_service
end end
end end
end end
......
...@@ -13,11 +13,7 @@ module Clusters ...@@ -13,11 +13,7 @@ module Clusters
private private
def create_cluster def create_cluster
Clusters::Cluster.create!( Clusters::Cluster.create(cluster_params)
cluster_params.merge(
projects: [project]))
rescue ActiveRecord::RecordInvalid => e
e.record
end end
def cluster_params def cluster_params
...@@ -27,7 +23,7 @@ module Clusters ...@@ -27,7 +23,7 @@ module Clusters
provider[:access_token] = access_token provider[:access_token] = access_token
end end
@cluster_params = params.merge(user: current_user) @cluster_params = params.merge(user: current_user, projects: [project])
end end
end end
end end
...@@ -9,11 +9,9 @@ module Clusters ...@@ -9,11 +9,9 @@ module Clusters
configure_provider configure_provider
configure_kubernetes configure_kubernetes
provider.make_created! cluster.save!
rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e
provider.make_errored!("Failed to request to CloudPlatform; #{e.message}") provider.make_errored!("Failed to request to CloudPlatform; #{e.message}")
rescue KubeException => e
provider.make_errored!("Failed to request to Kubernetes; #{e.message}")
rescue ActiveRecord::RecordInvalid => e rescue ActiveRecord::RecordInvalid => e
provider.make_errored!("Failed to configure GKE Cluster: #{e.message}") provider.make_errored!("Failed to configure GKE Cluster: #{e.message}")
end end
...@@ -22,6 +20,7 @@ module Clusters ...@@ -22,6 +20,7 @@ module Clusters
def configure_provider def configure_provider
provider.endpoint = gke_cluster.endpoint provider.endpoint = gke_cluster.endpoint
provider.status_event = :make_created
end end
def configure_kubernetes def configure_kubernetes
...@@ -39,7 +38,7 @@ module Clusters ...@@ -39,7 +38,7 @@ module Clusters
'https://' + gke_cluster.endpoint, 'https://' + gke_cluster.endpoint,
Base64.decode64(gke_cluster.master_auth.cluster_ca_certificate), Base64.decode64(gke_cluster.master_auth.cluster_ca_certificate),
gke_cluster.master_auth.username, gke_cluster.master_auth.username,
gke_cluster.master_auth.password) gke_cluster.master_auth.password).execute
end end
def gke_cluster def gke_cluster
......
...@@ -464,8 +464,8 @@ ActiveRecord::Schema.define(version: 20171017145932) do ...@@ -464,8 +464,8 @@ ActiveRecord::Schema.define(version: 20171017145932) do
create_table "cluster_platforms_kubernetes", force: :cascade do |t| create_table "cluster_platforms_kubernetes", force: :cascade do |t|
t.integer "cluster_id", null: false t.integer "cluster_id", null: false
t.datetime "created_at", null: false t.datetime_with_timezone "created_at", null: false
t.datetime "updated_at", null: false t.datetime_with_timezone "updated_at", null: false
t.text "api_url" t.text "api_url"
t.text "ca_cert" t.text "ca_cert"
t.string "namespace" t.string "namespace"
...@@ -492,8 +492,8 @@ ActiveRecord::Schema.define(version: 20171017145932) do ...@@ -492,8 +492,8 @@ ActiveRecord::Schema.define(version: 20171017145932) do
t.integer "cluster_id", null: false t.integer "cluster_id", null: false
t.integer "status" t.integer "status"
t.integer "num_nodes", null: false t.integer "num_nodes", null: false
t.datetime "created_at", null: false t.datetime_with_timezone "created_at", null: false
t.datetime "updated_at", null: false t.datetime_with_timezone "updated_at", null: false
t.text "status_reason" t.text "status_reason"
t.string "gcp_project_id", null: false t.string "gcp_project_id", null: false
t.string "zone", null: false t.string "zone", null: false
...@@ -510,8 +510,8 @@ ActiveRecord::Schema.define(version: 20171017145932) do ...@@ -510,8 +510,8 @@ ActiveRecord::Schema.define(version: 20171017145932) do
t.integer "user_id", null: false t.integer "user_id", null: false
t.integer "provider_type" t.integer "provider_type"
t.integer "platform_type" t.integer "platform_type"
t.datetime "created_at", null: false t.datetime_with_timezone "created_at", null: false
t.datetime "updated_at", null: false t.datetime_with_timezone "updated_at", null: false
t.boolean "enabled", default: true t.boolean "enabled", default: true
t.string "name", null: false t.string "name", null: false
end end
......
...@@ -356,32 +356,6 @@ Clusters::Platforms::Kubernetes: ...@@ -356,32 +356,6 @@ Clusters::Platforms::Kubernetes:
- encrypted_token_iv - encrypted_token_iv
- created_at - created_at
- updated_at - updated_at
Gcp::Cluster:
- id
- project_id
- user_id
- service_id
- enabled
- status
- status_reason
- project_namespace
- endpoint
- ca_cert
- encrypted_kubernetes_token
- encrypted_kubernetes_token_iv
- username
- encrypted_password
- encrypted_password_iv
- gcp_project_id
- gcp_cluster_zone
- gcp_cluster_name
- gcp_cluster_size
- gcp_machine_type
- gcp_operation_id
- encrypted_gcp_token
- encrypted_gcp_token_iv
- created_at
- updated_at
DeployKey: DeployKey:
- id - id
- user_id - user_id
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment