diff options
-rw-r--r-- | app/finders/clusters/cluster/knative_services_finder.rb | 114 | ||||
-rw-r--r-- | app/finders/clusters/knative_services_finder.rb | 112 | ||||
-rw-r--r-- | app/finders/projects/serverless/functions_finder.rb | 7 | ||||
-rw-r--r-- | spec/finders/clusters/knative_services_finder_spec.rb (renamed from spec/finders/clusters/cluster/knative_services_finder_spec.rb) | 2 | ||||
-rw-r--r-- | spec/models/clusters/cluster_spec.rb | 2 |
5 files changed, 118 insertions, 119 deletions
diff --git a/app/finders/clusters/cluster/knative_services_finder.rb b/app/finders/clusters/cluster/knative_services_finder.rb deleted file mode 100644 index 85b0967e935..00000000000 --- a/app/finders/clusters/cluster/knative_services_finder.rb +++ /dev/null @@ -1,114 +0,0 @@ -# frozen_string_literal: true -module Clusters - class Cluster - 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) } - - attr_reader :cluster, :project - - def initialize(cluster, project) - @cluster = cluster - @project = project - 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 - - def self.from_cache(cluster_id, project_id) - cluster = Clusters::Cluster.find(cluster_id) - project = ::Project.find(project_id) - - new(cluster, project) - 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 - [cluster.id, project.id] - 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 - @search_namespace ||= cluster.kubernetes_namespace_for(project) - 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 -end diff --git a/app/finders/clusters/knative_services_finder.rb b/app/finders/clusters/knative_services_finder.rb new file mode 100644 index 00000000000..7d3b53ef663 --- /dev/null +++ b/app/finders/clusters/knative_services_finder.rb @@ -0,0 +1,112 @@ +# 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) } + + attr_reader :cluster, :project + + def initialize(cluster, project) + @cluster = cluster + @project = project + 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 + + def self.from_cache(cluster_id, project_id) + cluster = Clusters::Cluster.find(cluster_id) + project = ::Project.find(project_id) + + new(cluster, project) + 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 + [cluster.id, project.id] + 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 + @search_namespace ||= cluster.kubernetes_namespace_for(project) + 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 diff --git a/app/finders/projects/serverless/functions_finder.rb b/app/finders/projects/serverless/functions_finder.rb index 9b8d7ed5a58..ebe50806ca1 100644 --- a/app/finders/projects/serverless/functions_finder.rb +++ b/app/finders/projects/serverless/functions_finder.rb @@ -14,15 +14,16 @@ module Projects knative_services.flatten.compact end - # Possible return values: Clusters::Cluster::KnativeServicesFinder::KNATIVE_STATE + # Possible return values: Clusters::KnativeServicesFinder::KNATIVE_STATE def knative_installed states = @clusters.map do |cluster| + cluster.application_knative cluster.knative_services_finder(project).knative_detected.tap do |state| - return state if state == ::Clusters::Cluster::KnativeServicesFinder::KNATIVE_STATES['checking'] # rubocop:disable Cop/AvoidReturnFromBlocks + return state if state == ::Clusters::KnativeServicesFinder::KNATIVE_STATES['checking'] # rubocop:disable Cop/AvoidReturnFromBlocks end end - states.any? { |state| state == ::Clusters::Cluster::KnativeServicesFinder::KNATIVE_STATES['installed'] } + states.any? { |state| state == ::Clusters::KnativeServicesFinder::KNATIVE_STATES['installed'] } end def service(environment_scope, name) diff --git a/spec/finders/clusters/cluster/knative_services_finder_spec.rb b/spec/finders/clusters/knative_services_finder_spec.rb index 277200d06f4..b731c2bd6bf 100644 --- a/spec/finders/clusters/cluster/knative_services_finder_spec.rb +++ b/spec/finders/clusters/knative_services_finder_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -describe Clusters::Cluster::KnativeServicesFinder do +describe Clusters::KnativeServicesFinder do include KubernetesHelpers include ReactiveCachingHelpers diff --git a/spec/models/clusters/cluster_spec.rb b/spec/models/clusters/cluster_spec.rb index 60a19ccd48a..f206bb41f45 100644 --- a/spec/models/clusters/cluster_spec.rb +++ b/spec/models/clusters/cluster_spec.rb @@ -40,7 +40,7 @@ describe Clusters::Cluster, :use_clean_rails_memory_store_caching do it do expect(subject.knative_services_finder(subject.project)) - .to be_instance_of(Clusters::Cluster::KnativeServicesFinder) + .to be_instance_of(Clusters::KnativeServicesFinder) end describe '.enabled' do |