2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-07 07:50:39 -05:00
|
|
|
module Gitlab
|
|
|
|
module Kubernetes
|
|
|
|
class Namespace
|
|
|
|
attr_accessor :name
|
|
|
|
|
|
|
|
def initialize(name, client)
|
2017-11-07 11:51:30 -05:00
|
|
|
@name = name
|
2017-11-07 07:50:39 -05:00
|
|
|
@client = client
|
|
|
|
end
|
|
|
|
|
|
|
|
def exists?
|
|
|
|
@client.get_namespace(name)
|
2018-11-13 07:46:01 -05:00
|
|
|
rescue ::Kubeclient::ResourceNotFoundError
|
2017-11-07 07:50:39 -05:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def create!
|
|
|
|
resource = ::Kubeclient::Resource.new(metadata: { name: name })
|
|
|
|
|
2019-04-10 02:38:27 -04:00
|
|
|
log_event(:begin_create)
|
2017-11-07 07:50:39 -05:00
|
|
|
@client.create_namespace(resource)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_exists!
|
|
|
|
exists? || create!
|
2019-04-10 02:38:27 -04:00
|
|
|
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
|
2017-11-07 07:50:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|