2018-12-06 13:08:49 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Projects
|
|
|
|
module Serverless
|
|
|
|
class FunctionsFinder
|
2019-05-16 18:49:12 -04:00
|
|
|
attr_reader :project
|
|
|
|
|
2019-04-05 22:02:39 -04:00
|
|
|
def initialize(project)
|
|
|
|
@clusters = project.clusters
|
|
|
|
@project = project
|
2018-12-06 13:08:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
knative_services.flatten.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def installed?
|
|
|
|
clusters_with_knative_installed.exists?
|
|
|
|
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)
|
|
|
|
return unless prometheus_adapter&.can_query?
|
|
|
|
|
|
|
|
cluster = clusters_with_knative_installed.preload_knative.find do |c|
|
|
|
|
environment_scope == c.environment_scope
|
|
|
|
end
|
|
|
|
|
2019-05-16 18:49:12 -04:00
|
|
|
func = ::Serverless::Function.new(project, name, cluster.kubernetes_namespace_for(project))
|
2019-04-05 22:02:39 -04:00
|
|
|
prometheus_adapter.query(:knative_invocation, func)
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_prometheus?(environment_scope)
|
|
|
|
clusters_with_knative_installed.preload_knative.to_a.any? do |cluster|
|
|
|
|
environment_scope == cluster.environment_scope && cluster.application_prometheus_available?
|
|
|
|
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)
|
|
|
|
clusters_with_knative_installed.preload_knative.map do |cluster|
|
|
|
|
next if environment_scope != cluster.environment_scope
|
|
|
|
|
2019-05-16 18:49:12 -04:00
|
|
|
services = cluster.application_knative.services_for(ns: cluster.kubernetes_namespace_for(project))
|
2018-12-17 02:31:38 -05:00
|
|
|
.select { |svc| svc["metadata"]["name"] == name }
|
|
|
|
|
|
|
|
add_metadata(cluster, services).first unless services.nil?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-06 13:08:49 -05:00
|
|
|
def knative_services
|
|
|
|
clusters_with_knative_installed.preload_knative.map do |cluster|
|
2019-05-16 18:49:12 -04:00
|
|
|
services = cluster.application_knative.services_for(ns: cluster.kubernetes_namespace_for(project))
|
2018-12-17 02:31:38 -05:00
|
|
|
add_metadata(cluster, services) unless services.nil?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_metadata(cluster, services)
|
|
|
|
services.each do |s|
|
|
|
|
s["environment_scope"] = cluster.environment_scope
|
|
|
|
s["cluster_id"] = cluster.id
|
|
|
|
|
|
|
|
if services.length == 1
|
|
|
|
s["podcount"] = cluster.application_knative.service_pod_details(
|
2019-05-16 18:49:12 -04:00
|
|
|
cluster.kubernetes_namespace_for(project),
|
2018-12-17 02:31:38 -05:00
|
|
|
s["metadata"]["name"]).length
|
|
|
|
end
|
2018-12-06 13:08:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clusters_with_knative_installed
|
|
|
|
@clusters.with_knative_installed
|
|
|
|
end
|
2019-04-05 22:02:39 -04:00
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
|
|
|
def prometheus_adapter
|
2019-05-16 18:49:12 -04:00
|
|
|
@prometheus_adapter ||= ::Prometheus::AdapterService.new(project).prometheus_adapter
|
2019-04-05 22:02:39 -04:00
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2018-12-06 13:08:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|