diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-30 18:09:15 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-30 18:09:15 +0300 |
commit | 536aa3a1f4b96abc4ca34489bf2cbe503afcded7 (patch) | |
tree | 88d08f7dfa29a32d6526773c4fe0fefd9f2bc7d1 /app/finders | |
parent | 50ae4065530c4eafbeb7c5ff2c462c48c02947ca (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/finders')
-rw-r--r-- | app/finders/projects/serverless/functions_finder.rb | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/app/finders/projects/serverless/functions_finder.rb b/app/finders/projects/serverless/functions_finder.rb index e8c50ef1a88..4e0b69f47e5 100644 --- a/app/finders/projects/serverless/functions_finder.rb +++ b/app/finders/projects/serverless/functions_finder.rb @@ -4,9 +4,15 @@ module Projects module Serverless class FunctionsFinder include Gitlab::Utils::StrongMemoize + include ReactiveCaching attr_reader :project + self.reactive_cache_key = ->(finder) { finder.cache_key } + self.reactive_cache_worker_finder = ->(_id, *args) { from_cache(*args) } + + MAX_CLUSTERS = 10 + def initialize(project) @project = project end @@ -15,8 +21,9 @@ module Projects knative_services.flatten.compact end - # Possible return values: Clusters::KnativeServicesFinder::KNATIVE_STATE def knative_installed + return knative_installed_from_cluster?(*cache_key) if available_environments.empty? + states = services_finders.map do |finder| finder.knative_detected.tap do |state| return state if state == ::Clusters::KnativeServicesFinder::KNATIVE_STATES['checking'] # rubocop:disable Cop/AvoidReturnFromBlocks @@ -45,8 +52,41 @@ module Projects end end + def self.from_cache(project_id) + project = Project.find(project_id) + + new(project) + end + + def cache_key(*args) + [project.id] + end + + def calculate_reactive_cache(*) + # rubocop: disable CodeReuse/ActiveRecord + project.all_clusters.enabled.take(MAX_CLUSTERS).any? do |cluster| + cluster.kubeclient.knative_client.discover + rescue Kubeclient::ResourceNotFoundError + next + end + end + private + def knative_installed_from_cluster?(*cache_key) + cached_data = with_reactive_cache_memoized(*cache_key) { |data| data } + + return ::Clusters::KnativeServicesFinder::KNATIVE_STATES['checking'] if cached_data.nil? + + cached_data ? true : false + end + + def with_reactive_cache_memoized(*cache_key) + strong_memoize(:reactive_cache) do + with_reactive_cache(*cache_key) { |data| data } + end + end + def knative_service(environment_scope, name) finders_for_scope(environment_scope).map do |finder| services = finder @@ -95,6 +135,10 @@ module Projects environment_scope == finder.cluster.environment_scope end end + + def id + nil + end end end end |