2020-03-11 11:09:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module PodLogs
|
|
|
|
class BaseService < ::BaseService
|
|
|
|
include ReactiveCaching
|
|
|
|
include Stepable
|
|
|
|
|
|
|
|
attr_reader :cluster, :namespace, :params
|
|
|
|
|
|
|
|
CACHE_KEY_GET_POD_LOG = 'get_pod_log'
|
|
|
|
K8S_NAME_MAX_LENGTH = 253
|
|
|
|
|
2020-09-22 08:09:39 -04:00
|
|
|
self.reactive_cache_work_type = :external_dependency
|
|
|
|
|
2020-03-11 11:09:37 -04:00
|
|
|
def id
|
|
|
|
cluster.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(cluster, namespace, params: {})
|
|
|
|
@cluster = cluster
|
|
|
|
@namespace = namespace
|
|
|
|
@params = filter_params(params.dup.stringify_keys).to_hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
with_reactive_cache(
|
|
|
|
CACHE_KEY_GET_POD_LOG,
|
|
|
|
namespace,
|
|
|
|
params
|
|
|
|
) do |result|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def calculate_reactive_cache(request, _namespace, _params)
|
|
|
|
case request
|
|
|
|
when CACHE_KEY_GET_POD_LOG
|
|
|
|
execute_steps
|
|
|
|
else
|
|
|
|
exception = StandardError.new('Unknown reactive cache request')
|
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(exception, request: request)
|
|
|
|
error(_('Unknown cache key'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def valid_params
|
|
|
|
%w(pod_name container_name)
|
|
|
|
end
|
|
|
|
|
2020-03-12 20:09:34 -04:00
|
|
|
def success_return_keys
|
|
|
|
%i(status logs pod_name container_name pods)
|
|
|
|
end
|
|
|
|
|
2020-03-11 11:09:37 -04:00
|
|
|
def check_arguments(result)
|
|
|
|
return error(_('Cluster does not exist')) if cluster.nil?
|
|
|
|
return error(_('Namespace is empty')) if namespace.blank?
|
|
|
|
|
2020-03-17 23:09:43 -04:00
|
|
|
result[:pod_name] = params['pod_name'].presence
|
|
|
|
result[:container_name] = params['container_name'].presence
|
2020-03-11 11:09:37 -04:00
|
|
|
|
2020-05-07 05:09:51 -04:00
|
|
|
return error(_('Invalid pod_name')) if result[:pod_name] && !result[:pod_name].is_a?(String)
|
|
|
|
return error(_('Invalid container_name')) if result[:container_name] && !result[:container_name].is_a?(String)
|
|
|
|
|
2020-03-17 23:09:43 -04:00
|
|
|
success(result)
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_raw_pods(result)
|
2020-04-15 14:09:36 -04:00
|
|
|
raise NotImplementedError
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_pod_names(result)
|
2020-04-15 14:09:36 -04:00
|
|
|
result[:pods] = result[:raw_pods].map { |p| p[:name] }
|
2020-03-11 11:09:37 -04:00
|
|
|
|
|
|
|
success(result)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pod_logs(result)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_return_keys(result)
|
2020-03-12 20:09:34 -04:00
|
|
|
result.slice(*success_return_keys)
|
2020-03-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def filter_params(params)
|
|
|
|
params.slice(*valid_params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|