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

101 lines
2.8 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
2019-09-04 21:01:54 +05:30
attr_reader :project
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
# Possible return values: Clusters::KnativeServicesFinder::KNATIVE_STATE
def knative_installed
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
2019-02-15 15:39:39 +05:30
private
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 }
2019-10-12 21:52:04 +05:30
add_metadata(finder, services).first unless services.nil?
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|
services = finder.services
2019-09-04 21:01:54 +05:30
2019-10-12 21:52:04 +05:30
add_metadata(finder, services) unless services.nil?
2019-03-02 22:35:43 +05:30
end
end
2019-10-12 21:52:04 +05:30
def add_metadata(finder, services)
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
s["cluster_id"] = finder.cluster.id
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
2019-02-15 15:39:39 +05:30
end
end
end