2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module Gitlab
|
|
|
|
module Kubernetes
|
|
|
|
class Namespace
|
2020-03-09 13:42:32 +05:30
|
|
|
attr_accessor :name, :labels
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
def initialize(name, client, labels: nil)
|
2018-03-17 18:26:18 +05:30
|
|
|
@name = name
|
|
|
|
@client = client
|
2020-03-09 13:42:32 +05:30
|
|
|
@labels = labels
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def exists?
|
|
|
|
@client.get_namespace(name)
|
2019-02-15 15:39:39 +05:30
|
|
|
rescue ::Kubeclient::ResourceNotFoundError
|
2018-03-17 18:26:18 +05:30
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def create!
|
2020-03-09 13:42:32 +05:30
|
|
|
resource = ::Kubeclient::Resource.new(metadata: { name: name, labels: labels })
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
log_event(:begin_create)
|
2018-03-17 18:26:18 +05:30
|
|
|
@client.create_namespace(resource)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_exists!
|
|
|
|
exists? || create!
|
2019-07-31 22:56:46 +05:30
|
|
|
rescue ::Kubeclient::HttpError => error
|
|
|
|
log_create_failed(error)
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def log_create_failed(error)
|
|
|
|
logger.error({
|
|
|
|
exception: error.class.name,
|
|
|
|
status_code: error.error_code,
|
|
|
|
namespace: name,
|
|
|
|
class_name: self.class.name,
|
|
|
|
event: :failed_to_create_namespace,
|
|
|
|
message: error.message
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_event(event)
|
|
|
|
logger.info(
|
|
|
|
namespace: name,
|
|
|
|
class_name: self.class.name,
|
|
|
|
event: event
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def logger
|
|
|
|
@logger ||= Gitlab::Kubernetes::Logger.build
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|