Commit c471430a authored by Kamil Trzcinski's avatar Kamil Trzcinski

Gcp::Cluster improvements

- introduce state machine
- use before transitions to clear data
- introduce namespace validation
parent 7e3e8ae8
...@@ -22,50 +22,53 @@ module Gcp ...@@ -22,50 +22,53 @@ module Gcp
algorithm: 'aes-256-cbc' algorithm: 'aes-256-cbc'
enum status: { enum status: {
unknown: nil,
scheduled: 1, scheduled: 1,
creating: 2, creating: 2,
created: 3, created: 3,
errored: 4 errored: 4
} }
validates :gcp_project_id, presence: true state_machine :status, initial: :scheduled do
validates :gcp_cluster_zone, presence: true event :creating do
validates :gcp_cluster_name, presence: true transition any - [:creating] => :creating
validates :gcp_cluster_size, presence: true, end
numericality: { only_integer: true, greater_than: 0 }
validate :restrict_modification, on: :update
def errored!(reason)
self.status = :errored
self.status_reason = reason
self.gcp_token = nil
save!(validate: false) event :created do
transition any - [:created] => :created
end end
def creating!(gcp_operation_id) event :errored do
self.status = :creating transition any - [:errored] => :errored
self.gcp_operation_id = gcp_operation_id end
save!(validate: false) before_transition any => [:errored, :created] do |cluster|
cluster.gcp_token = nil
cluster.gcp_operation_id = nil
end end
def created!(endpoint, ca_cert, kubernetes_token, username, password) before_transition any => [:errored] do |cluster|
self.status = :created status_reason = transition.args.first
self.enabled = true cluster.status_reason = status_reason
self.endpoint = endpoint end
self.ca_cert = ca_cert
self.kubernetes_token = kubernetes_token
self.username = username
self.password = password
self.service = project.find_or_initialize_service('kubernetes')
self.gcp_token = nil
self.gcp_operation_id = nil
save!
end end
validates :gcp_project_id, presence: true
validates :gcp_cluster_zone, presence: true
validates :gcp_cluster_name, presence: true
validates :gcp_cluster_size, presence: true,
numericality: { only_integer: true, greater_than: 0 }
validates :project_namespace,
allow_blank: true,
length: 1..63,
format: {
with: Gitlab::Regex.kubernetes_namespace_regex,
message: Gitlab::Regex.kubernetes_namespace_regex_message
}
# if we do not do status transition we prevent change
validate :restrict_modification, on: :update, unless: :status_changed?
def on_creation? def on_creation?
scheduled? || creating? scheduled? || creating?
end end
......
...@@ -2,7 +2,15 @@ module Ci ...@@ -2,7 +2,15 @@ module Ci
class IntegrateClusterService class IntegrateClusterService
def execute(cluster, endpoint, ca_cert, token, username, password) def execute(cluster, endpoint, ca_cert, token, username, password)
Gcp::Cluster.transaction do Gcp::Cluster.transaction do
cluster.created!(endpoint, ca_cert, token, username, password) cluster.update!(
enabled: true,
endpoint: endpoint,
ca_cert: ca_cert,
kubernetes_token: token,
username: username,
password: password,
service: project.find_or_initialize_service('kubernetes'),
status_event: :created)
cluster.service.update!( cluster.service.update!(
active: true, active: true,
......
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