2019-06-03 10:42:06 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
module Clusters
|
|
|
|
class KnativeServicesFinder
|
|
|
|
include ReactiveCaching
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
KNATIVE_STATES = {
|
|
|
|
'checking' => 'checking',
|
|
|
|
'installed' => 'installed',
|
|
|
|
'not_found' => 'not_found'
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
self.reactive_cache_key = ->(finder) { finder.model_name }
|
|
|
|
self.reactive_cache_worker_finder = ->(_id, *cache_args) { from_cache(*cache_args) }
|
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
attr_reader :cluster, :environment
|
2019-06-03 10:42:06 -04:00
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
def initialize(cluster, environment)
|
2019-06-03 10:42:06 -04:00
|
|
|
@cluster = cluster
|
2019-08-07 00:40:29 -04:00
|
|
|
@environment = environment
|
2019-06-03 10:42:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def with_reactive_cache_memoized(*cache_args, &block)
|
|
|
|
strong_memoize(:reactive_cache) do
|
|
|
|
with_reactive_cache(*cache_args, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_cache!
|
|
|
|
clear_reactive_cache!(*cache_args)
|
|
|
|
end
|
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
def self.from_cache(cluster_id, environment_id)
|
2019-06-03 10:42:06 -04:00
|
|
|
cluster = Clusters::Cluster.find(cluster_id)
|
2019-08-07 00:40:29 -04:00
|
|
|
environment = Environment.find(environment_id)
|
2019-06-03 10:42:06 -04:00
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
new(cluster, environment)
|
2019-06-03 10:42:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def calculate_reactive_cache(*)
|
|
|
|
# read_services calls knative_client.discover implicitily. If we stop
|
|
|
|
# detecting services but still want to detect knative, we'll need to
|
|
|
|
# explicitily call: knative_client.discover
|
|
|
|
#
|
|
|
|
# We didn't create it separately to avoid 2 cluster requests.
|
|
|
|
ksvc = read_services
|
|
|
|
pods = knative_client.discovered ? read_pods : []
|
|
|
|
{ services: ksvc, pods: pods, knative_detected: knative_client.discovered }
|
|
|
|
end
|
|
|
|
|
|
|
|
def services
|
|
|
|
return [] unless search_namespace
|
|
|
|
|
|
|
|
cached_data = with_reactive_cache_memoized(*cache_args) { |data| data }
|
|
|
|
cached_data.to_h.fetch(:services, [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_args
|
2019-08-07 00:40:29 -04:00
|
|
|
[cluster.id, environment.id]
|
2019-06-03 10:42:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def service_pod_details(service)
|
|
|
|
cached_data = with_reactive_cache_memoized(*cache_args) { |data| data }
|
|
|
|
cached_data.to_h.fetch(:pods, []).select do |pod|
|
|
|
|
filter_pods(pod, service)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def knative_detected
|
|
|
|
cached_data = with_reactive_cache_memoized(*cache_args) { |data| data }
|
|
|
|
|
|
|
|
knative_state = cached_data.to_h[:knative_detected]
|
|
|
|
|
|
|
|
return KNATIVE_STATES['checking'] if knative_state.nil?
|
|
|
|
return KNATIVE_STATES['installed'] if knative_state
|
|
|
|
|
|
|
|
KNATIVE_STATES['uninstalled']
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_name
|
|
|
|
self.class.name.underscore.tr('/', '_')
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def search_namespace
|
2019-08-07 00:40:29 -04:00
|
|
|
@search_namespace ||= cluster.kubernetes_namespace_for(environment)
|
2019-06-03 10:42:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def knative_client
|
|
|
|
cluster.kubeclient.knative_client
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_pods(pod, service)
|
|
|
|
pod["metadata"]["labels"]["serving.knative.dev/service"] == service
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_services
|
|
|
|
knative_client.get_services(namespace: search_namespace).as_json
|
|
|
|
rescue Kubeclient::ResourceNotFoundError
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_pods
|
|
|
|
cluster.kubeclient.core_client.get_pods(namespace: search_namespace).as_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def id
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|