Class: Clusters::KnativeServicesFinder

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize, ReactiveCaching
Defined in:
app/finders/clusters/knative_services_finder.rb

Constant Summary collapse

KNATIVE_STATES =
{
  'checking' => 'checking',
  'installed' => 'installed',
  'not_found' => 'not_found'
}.freeze

Constants included from ReactiveCaching

ReactiveCaching::ExceededReactiveCacheLimit, ReactiveCaching::InvalidateReactiveCache, ReactiveCaching::WORK_TYPE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cluster, environment) ⇒ KnativeServicesFinder

Returns a new instance of KnativeServicesFinder.



19
20
21
22
# File 'app/finders/clusters/knative_services_finder.rb', line 19

def initialize(cluster, environment)
  @cluster = cluster
  @environment = environment
end

Instance Attribute Details

#clusterObject (readonly)

Returns the value of attribute cluster.



17
18
19
# File 'app/finders/clusters/knative_services_finder.rb', line 17

def cluster
  @cluster
end

#environmentObject (readonly)

Returns the value of attribute environment.



17
18
19
# File 'app/finders/clusters/knative_services_finder.rb', line 17

def environment
  @environment
end

Class Method Details

.from_cache(cluster_id, environment_id) ⇒ Object



34
35
36
37
38
39
# File 'app/finders/clusters/knative_services_finder.rb', line 34

def self.from_cache(cluster_id, environment_id)
  cluster = Clusters::Cluster.find(cluster_id)
  environment = Environment.find(environment_id)

  new(cluster, environment)
end

Instance Method Details

#cache_argsObject



59
60
61
# File 'app/finders/clusters/knative_services_finder.rb', line 59

def cache_args
  [cluster.id, environment.id]
end

#calculate_reactive_cacheObject



41
42
43
44
45
46
47
48
49
50
# File 'app/finders/clusters/knative_services_finder.rb', line 41

def calculate_reactive_cache(*)
  # read_services calls knative_client.discover implicitily. If we stop
  # detecting services but still want to detect knative, we'll need to
  # explicitily call: knative_client.discover
  #
  # We didn't create it separately to avoid 2 cluster requests.
  ksvc = read_services
  pods = knative_client.discovered ? read_pods : []
  { services: ksvc, pods: pods, knative_detected: knative_client.discovered }
end

#clear_cache!Object



30
31
32
# File 'app/finders/clusters/knative_services_finder.rb', line 30

def clear_cache!
  clear_reactive_cache!(*cache_args)
end

#knative_detectedObject



70
71
72
73
74
75
76
77
78
79
# File 'app/finders/clusters/knative_services_finder.rb', line 70

def knative_detected
  cached_data = with_reactive_cache_memoized(*cache_args) { |data| data }

  knative_state = cached_data.to_h[:knative_detected]

  return KNATIVE_STATES['checking'] if knative_state.nil?
  return KNATIVE_STATES['installed'] if knative_state

  KNATIVE_STATES['uninstalled']
end

#model_nameObject



81
82
83
# File 'app/finders/clusters/knative_services_finder.rb', line 81

def model_name
  self.class.name.underscore.tr('/', '_')
end

#service_pod_details(service) ⇒ Object



63
64
65
66
67
68
# File 'app/finders/clusters/knative_services_finder.rb', line 63

def service_pod_details(service)
  cached_data = with_reactive_cache_memoized(*cache_args) { |data| data }
  cached_data.to_h.fetch(:pods, []).select do |pod|
    filter_pods(pod, service)
  end
end

#servicesObject



52
53
54
55
56
57
# File 'app/finders/clusters/knative_services_finder.rb', line 52

def services
  return [] unless search_namespace

  cached_data = with_reactive_cache_memoized(*cache_args) { |data| data }
  cached_data.to_h.fetch(:services, [])
end

#with_reactive_cache_memoized(*cache_args, &block) ⇒ Object



24
25
26
27
28
# File 'app/finders/clusters/knative_services_finder.rb', line 24

def with_reactive_cache_memoized(*cache_args, &block)
  strong_memoize(:reactive_cache) do
    with_reactive_cache(*cache_args, &block)
  end
end