Module: Util::StateManager

Extended by:
Logging
Defined in:
lib/util/state_manager.rb

Constant Summary collapse

EVENTS_TRACKER_CM =
'discovery-state-tracker'.freeze

Constants included from Logging

Logging::SEV_LABEL, Logging::TRACE

Class Method Summary collapse

Methods included from Logging

logger, logger=

Class Method Details

.init(prefix, namespace) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/util/state_manager.rb', line 19

def init(prefix, namespace)
  return if @is_intialized

  @events_tracker_configmap = prefix + '-' + EVENTS_TRACKER_CM
  @namespace = namespace
  @state = Dto::State.new
  state_config_map = Util::KubectlOps.get_configmap(@events_tracker_configmap, @namespace)
  if state_config_map.nil?
    # This is first instance of cronjob execution
    # therefore, events tracker configmap does not exist in cluster
    logger.info("configmap/#{@events_tracker_configmap} not found.")
    @state.last_timestamp = 0 # 1 January 1970 00:00
    begin
      Util::KubectlOps.create_configmap(@events_tracker_configmap, @namespace, @state.to_hash)
    rescue KubeException => e
      logger.error("Missing permission to create config map in namespace - #{@namespace}")
      raise e
    end
    logger.info("Created new Configmap - #{@events_tracker_configmap}.")
  else
    last_timestamp = state_config_map[:data][:last_timestamp]
    logger.info("timestamp fetched from configmap - #{last_timestamp} - #{Time.at(last_timestamp.to_i).getutc}")
    @state.last_timestamp = last_timestamp.to_i
  end
  @is_intialized = true
end

.init_checkObject

Raises:

  • (StandardError)


46
47
48
# File 'lib/util/state_manager.rb', line 46

def init_check
  raise StandardError, 'Method call before initializig Module - StateManager' unless @is_intialized
end

.stateObject



50
51
52
53
# File 'lib/util/state_manager.rb', line 50

def state
  init_check
  @state
end

.update_state_configmapObject



55
56
57
58
59
# File 'lib/util/state_manager.rb', line 55

def update_state_configmap
  init_check
  Util::KubectlOps.patch_configmap(@events_tracker_configmap, @namespace, @state.to_hash)
  logger.info("Updated configmap - #{@events_tracker_configmap} with new state.")
end