2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Projects
|
|
|
|
module Serverless
|
|
|
|
class FunctionsFinder
|
2019-09-04 21:01:54 +05:30
|
|
|
attr_reader :project
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def initialize(project)
|
|
|
|
@clusters = project.clusters
|
|
|
|
@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
|
|
|
|
states = @clusters.map do |cluster|
|
|
|
|
cluster.application_knative
|
|
|
|
cluster.knative_services_finder(project).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'] }
|
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)
|
|
|
|
return unless prometheus_adapter&.can_query?
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
cluster = @clusters.find do |c|
|
2019-07-07 11:18:12 +05:30
|
|
|
environment_scope == c.environment_scope
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
func = ::Serverless::Function.new(project, name, cluster.kubernetes_namespace_for(project))
|
2019-07-07 11:18:12 +05:30
|
|
|
prometheus_adapter.query(:knative_invocation, func)
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_prometheus?(environment_scope)
|
2019-09-04 21:01:54 +05:30
|
|
|
@clusters.any? do |cluster|
|
2019-07-07 11:18:12 +05:30
|
|
|
environment_scope == cluster.environment_scope && cluster.application_prometheus_available?
|
|
|
|
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-09-04 21:01:54 +05:30
|
|
|
@clusters.map do |cluster|
|
2019-03-02 22:35:43 +05:30
|
|
|
next if environment_scope != cluster.environment_scope
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
services = cluster
|
|
|
|
.knative_services_finder(project)
|
|
|
|
.services
|
2019-03-02 22:35:43 +05:30
|
|
|
.select { |svc| svc["metadata"]["name"] == name }
|
|
|
|
|
|
|
|
add_metadata(cluster, services).first unless services.nil?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def knative_services
|
2019-09-04 21:01:54 +05:30
|
|
|
@clusters.map do |cluster|
|
|
|
|
services = cluster
|
|
|
|
.knative_services_finder(project)
|
|
|
|
.services
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
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
|
2019-09-04 21:01:54 +05:30
|
|
|
s["podcount"] = cluster
|
|
|
|
.knative_services_finder(project)
|
|
|
|
.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-07-07 11:18:12 +05:30
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
|
|
|
def prometheus_adapter
|
2019-09-04 21:01:54 +05:30
|
|
|
@prometheus_adapter ||= ::Prometheus::AdapterService.new(project).prometheus_adapter
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|