debian-mirror-gitlab/app/finders/projects/serverless/functions_finder.rb

153 lines
4.2 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
module Projects
module Serverless
class FunctionsFinder
2019-10-12 21:52:04 +05:30
include Gitlab::Utils::StrongMemoize
2020-03-09 13:42:32 +05:30
include ReactiveCaching
2019-10-12 21:52:04 +05:30
2019-09-04 21:01:54 +05:30
attr_reader :project
2020-03-09 13:42:32 +05:30
self.reactive_cache_key = ->(finder) { finder.cache_key }
self.reactive_cache_worker_finder = ->(_id, *args) { from_cache(*args) }
MAX_CLUSTERS = 10
2019-07-07 11:18:12 +05:30
def initialize(project)
@project = project
2019-02-15 15:39:39 +05:30
end
def execute
knative_services.flatten.compact
end
2019-09-04 21:01:54 +05:30
def knative_installed
2020-03-09 13:42:32 +05:30
return knative_installed_from_cluster?(*cache_key) if available_environments.empty?
2019-10-12 21:52:04 +05:30
states = services_finders.map do |finder|
finder.knative_detected.tap do |state|
2019-09-04 21:01:54 +05:30
return state if state == ::Clusters::KnativeServicesFinder::KNATIVE_STATES['checking'] # rubocop:disable Cop/AvoidReturnFromBlocks
end
end
states.any? { |state| state == ::Clusters::KnativeServicesFinder::KNATIVE_STATES['installed'] }
2019-02-15 15:39:39 +05:30
end
2019-03-02 22:35:43 +05:30
def service(environment_scope, name)
knative_service(environment_scope, name)&.first
end
2019-07-07 11:18:12 +05:30
def invocation_metrics(environment_scope, name)
2019-10-12 21:52:04 +05:30
environment = finders_for_scope(environment_scope).first&.environment
2019-07-07 11:18:12 +05:30
2019-10-12 21:52:04 +05:30
if environment.present? && environment.prometheus_adapter&.can_query?
func = ::Serverless::Function.new(project, name, environment.deployment_namespace)
environment.prometheus_adapter.query(:knative_invocation, func)
2019-07-07 11:18:12 +05:30
end
end
def has_prometheus?(environment_scope)
2019-10-12 21:52:04 +05:30
finders_for_scope(environment_scope).any? do |finder|
finder.cluster.application_prometheus_available?
2019-07-07 11:18:12 +05:30
end
end
2020-03-09 13:42:32 +05:30
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
2019-02-15 15:39:39 +05:30
private
2020-03-09 13:42:32 +05:30
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
2019-03-02 22:35:43 +05:30
def knative_service(environment_scope, name)
2019-10-12 21:52:04 +05:30
finders_for_scope(environment_scope).map do |finder|
services = finder
2019-09-04 21:01:54 +05:30
.services
2019-03-02 22:35:43 +05:30
.select { |svc| svc["metadata"]["name"] == name }
2020-03-09 13:42:32 +05:30
attributes = add_metadata(finder, services).first
next unless attributes
Gitlab::Serverless::Service.new(attributes)
2019-03-02 22:35:43 +05:30
end
end
2019-02-15 15:39:39 +05:30
def knative_services
2019-10-12 21:52:04 +05:30
services_finders.map do |finder|
2020-03-09 13:42:32 +05:30
attributes = add_metadata(finder, finder.services)
2019-09-04 21:01:54 +05:30
2020-03-09 13:42:32 +05:30
attributes&.map do |attributes|
Gitlab::Serverless::Service.new(attributes)
end
2019-03-02 22:35:43 +05:30
end
end
2019-10-12 21:52:04 +05:30
def add_metadata(finder, services)
2020-03-09 13:42:32 +05:30
return if services.nil?
2019-10-12 21:52:04 +05:30
add_pod_count = services.one?
2019-03-02 22:35:43 +05:30
services.each do |s|
2019-10-12 21:52:04 +05:30
s["environment_scope"] = finder.cluster.environment_scope
2020-03-09 13:42:32 +05:30
s["environment"] = finder.environment
s["cluster"] = finder.cluster
2019-03-02 22:35:43 +05:30
2019-10-12 21:52:04 +05:30
if add_pod_count
s["podcount"] = finder
2019-09-04 21:01:54 +05:30
.service_pod_details(s["metadata"]["name"])
.length
2019-03-02 22:35:43 +05:30
end
2019-02-15 15:39:39 +05:30
end
end
2019-10-12 21:52:04 +05:30
def services_finders
strong_memoize(:services_finders) do
available_environments.map(&:knative_services_finder).compact
end
end
def available_environments
@project.environments.available.preload_cluster
end
def finders_for_scope(environment_scope)
services_finders.select do |finder|
environment_scope == finder.cluster.environment_scope
end
2019-07-07 11:18:12 +05:30
end
2020-03-09 13:42:32 +05:30
def id
nil
end
2019-02-15 15:39:39 +05:30
end
end
end