2018-12-06 13:08:49 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Projects
|
|
|
|
module Serverless
|
|
|
|
class FunctionsFinder
|
2019-08-07 00:40:29 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2019-05-16 18:49:12 -04:00
|
|
|
attr_reader :project
|
|
|
|
|
2019-04-05 22:02:39 -04:00
|
|
|
def initialize(project)
|
|
|
|
@project = project
|
2018-12-06 13:08:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
knative_services.flatten.compact
|
|
|
|
end
|
|
|
|
|
2019-06-03 10:42:06 -04:00
|
|
|
# Possible return values: Clusters::KnativeServicesFinder::KNATIVE_STATE
|
2019-04-09 15:45:58 -04:00
|
|
|
def knative_installed
|
2019-08-07 00:40:29 -04:00
|
|
|
states = services_finders.map do |finder|
|
|
|
|
finder.knative_detected.tap do |state|
|
2019-06-03 10:42:06 -04:00
|
|
|
return state if state == ::Clusters::KnativeServicesFinder::KNATIVE_STATES['checking'] # rubocop:disable Cop/AvoidReturnFromBlocks
|
2019-04-09 15:45:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-03 10:42:06 -04:00
|
|
|
states.any? { |state| state == ::Clusters::KnativeServicesFinder::KNATIVE_STATES['installed'] }
|
2018-12-06 13:08:49 -05:00
|
|
|
end
|
|
|
|
|
2018-12-17 02:31:38 -05:00
|
|
|
def service(environment_scope, name)
|
|
|
|
knative_service(environment_scope, name)&.first
|
|
|
|
end
|
|
|
|
|
2019-04-05 22:02:39 -04:00
|
|
|
def invocation_metrics(environment_scope, name)
|
2019-08-07 00:40:29 -04:00
|
|
|
environment = finders_for_scope(environment_scope).first&.environment
|
2019-04-05 22:02:39 -04:00
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
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-04-05 22:02:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_prometheus?(environment_scope)
|
2019-08-07 00:40:29 -04:00
|
|
|
finders_for_scope(environment_scope).any? do |finder|
|
|
|
|
finder.cluster.application_prometheus_available?
|
2019-04-05 22:02:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-06 13:08:49 -05:00
|
|
|
private
|
|
|
|
|
2018-12-17 02:31:38 -05:00
|
|
|
def knative_service(environment_scope, name)
|
2019-08-07 00:40:29 -04:00
|
|
|
finders_for_scope(environment_scope).map do |finder|
|
|
|
|
services = finder
|
2019-04-09 15:45:58 -04:00
|
|
|
.services
|
2018-12-17 02:31:38 -05:00
|
|
|
.select { |svc| svc["metadata"]["name"] == name }
|
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
add_metadata(finder, services).first unless services.nil?
|
2018-12-17 02:31:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-06 13:08:49 -05:00
|
|
|
def knative_services
|
2019-08-07 00:40:29 -04:00
|
|
|
services_finders.map do |finder|
|
|
|
|
services = finder.services
|
2019-04-09 15:45:58 -04:00
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
add_metadata(finder, services) unless services.nil?
|
2018-12-17 02:31:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
def add_metadata(finder, services)
|
|
|
|
add_pod_count = services.one?
|
|
|
|
|
2018-12-17 02:31:38 -05:00
|
|
|
services.each do |s|
|
2019-08-07 00:40:29 -04:00
|
|
|
s["environment_scope"] = finder.cluster.environment_scope
|
|
|
|
s["cluster_id"] = finder.cluster.id
|
2018-12-17 02:31:38 -05:00
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
if add_pod_count
|
|
|
|
s["podcount"] = finder
|
2019-04-09 15:45:58 -04:00
|
|
|
.service_pod_details(s["metadata"]["name"])
|
|
|
|
.length
|
2018-12-17 02:31:38 -05:00
|
|
|
end
|
2018-12-06 13:08:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-07 00:40:29 -04:00
|
|
|
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-04-05 22:02:39 -04:00
|
|
|
end
|
2018-12-06 13:08:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|