Class: Projects::Serverless::FunctionsFinder

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize, ReactiveCaching
Defined in:
app/finders/projects/serverless/functions_finder.rb

Constant Summary collapse

MAX_CLUSTERS =
10

Constants included from ReactiveCaching

ReactiveCaching::ExceededReactiveCacheLimit, ReactiveCaching::InvalidateReactiveCache, ReactiveCaching::WORK_TYPE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(project) ⇒ FunctionsFinder

Returns a new instance of FunctionsFinder.


17
18
19
# File 'app/finders/projects/serverless/functions_finder.rb', line 17

def initialize(project)
  @project = project
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project


9
10
11
# File 'app/finders/projects/serverless/functions_finder.rb', line 9

def project
  @project
end

Class Method Details

.from_cache(project_id) ⇒ Object


56
57
58
59
60
# File 'app/finders/projects/serverless/functions_finder.rb', line 56

def self.from_cache(project_id)
  project = Project.find(project_id)

  new(project)
end

Instance Method Details

#cache_key(*args) ⇒ Object


62
63
64
# File 'app/finders/projects/serverless/functions_finder.rb', line 62

def cache_key(*args)
  [project.id]
end

#calculate_reactive_cacheObject


66
67
68
69
70
71
72
73
# File 'app/finders/projects/serverless/functions_finder.rb', line 66

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

#executeObject


21
22
23
# File 'app/finders/projects/serverless/functions_finder.rb', line 21

def execute
  knative_services.flatten.compact
end

#has_prometheus?(environment_scope) ⇒ Boolean

Returns:

  • (Boolean)

50
51
52
53
54
# File 'app/finders/projects/serverless/functions_finder.rb', line 50

def has_prometheus?(environment_scope)
  finders_for_scope(environment_scope).any? do |finder|
    finder.cluster.application_prometheus_available?
  end
end

#invocation_metrics(environment_scope, name) ⇒ Object


41
42
43
44
45
46
47
48
# File 'app/finders/projects/serverless/functions_finder.rb', line 41

def invocation_metrics(environment_scope, name)
  environment = finders_for_scope(environment_scope).first&.environment

  if environment.present? && environment.prometheus_adapter&.can_query?
    func = ::Serverless::Function.new(project, name, environment.deployment_namespace)
    environment.prometheus_adapter.query(:knative_invocation, func)
  end
end

#knative_installedObject


25
26
27
28
29
30
31
32
33
34
35
# File 'app/finders/projects/serverless/functions_finder.rb', line 25

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
    end
  end

  states.any? { |state| state == ::Clusters::KnativeServicesFinder::KNATIVE_STATES['installed'] }
end

#service(environment_scope, name) ⇒ Object


37
38
39
# File 'app/finders/projects/serverless/functions_finder.rb', line 37

def service(environment_scope, name)
  knative_service(environment_scope, name)&.first
end