debian-mirror-gitlab/app/finders/clusters/knative_services_finder.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 lines
3 KiB
Ruby
Raw Normal View History

2019-09-04 21:01:54 +05:30
# 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 }
2020-05-24 23:13:21 +05:30
self.reactive_cache_work_type = :external_dependency
2019-09-04 21:01:54 +05:30
self.reactive_cache_worker_finder = ->(_id, *cache_args) { from_cache(*cache_args) }
2019-10-12 21:52:04 +05:30
attr_reader :cluster, :environment
2019-09-04 21:01:54 +05:30
2019-10-12 21:52:04 +05:30
def initialize(cluster, environment)
2019-09-04 21:01:54 +05:30
@cluster = cluster
2019-10-12 21:52:04 +05:30
@environment = environment
2019-09-04 21:01:54 +05:30
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-10-12 21:52:04 +05:30
def self.from_cache(cluster_id, environment_id)
2019-09-04 21:01:54 +05:30
cluster = Clusters::Cluster.find(cluster_id)
2019-10-12 21:52:04 +05:30
environment = Environment.find(environment_id)
2019-09-04 21:01:54 +05:30
2019-10-12 21:52:04 +05:30
new(cluster, environment)
2019-09-04 21:01:54 +05:30
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-10-12 21:52:04 +05:30
[cluster.id, environment.id]
2019-09-04 21:01:54 +05:30
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-10-12 21:52:04 +05:30
@search_namespace ||= cluster.kubernetes_namespace_for(environment)
2019-09-04 21:01:54 +05:30
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