Class: Kerbi::State::ConfigMapBackend

Inherits:
BaseBackend show all
Includes:
Mixins::CmBackendTesting
Defined in:
lib/state/config_map_backend.rb

Overview

Treats a Kubernetes configmap in a namespace as a persistent store for state entries. Reads and writes to the configmap.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::CmBackendTesting

#echo_init, #namespace_exists?, #puts_outcome, #read_write_ready?, #resource_exists?, #test_connection, #test_list_namespaces, #test_target_ns_exists

Methods inherited from BaseBackend

#delete_entry, #entries, #entry_set, #save

Constructor Details

#initialize(auth_bundle, namespace) ⇒ ConfigMapBackend

Returns a new instance of ConfigMapBackend.

Parameters:

  • auth_bundle (Hash)

    generated by Kerbi::Utils::K8sAuth

  • namespace (String)

    Kubernetes namespace where configmap lives



16
17
18
19
# File 'lib/state/config_map_backend.rb', line 16

def initialize(auth_bundle, namespace)
  @auth_bundle = auth_bundle.freeze
  @namespace = namespace.freeze
end

Instance Attribute Details

#auth_bundleObject (readonly)

Returns the value of attribute auth_bundle.



11
12
13
# File 'lib/state/config_map_backend.rb', line 11

def auth_bundle
  @auth_bundle
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



12
13
14
# File 'lib/state/config_map_backend.rb', line 12

def namespace
  @namespace
end

Instance Method Details

#apply_resource(resource_desc) ⇒ Object

Creates the configmap given an exact dict representation of its contents. This method doesn’t actually get used outside of rspec, but it’s super useful there so keeping for time being.

Parameters:

  • resource_desc (Hash)


44
45
46
47
# File 'lib/state/config_map_backend.rb', line 44

def apply_resource(resource_desc)
  #noinspection RubyResolve
  client("v1").create_config_map(resource_desc)
end

#create_namespaceObject

Creates the required namespace resource for this configmap in the cluster.



63
64
65
66
67
68
# File 'lib/state/config_map_backend.rb', line 63

def create_namespace
  opts = { release_name: namespace }
  dict = Kerbi::State::NamespaceMixer.new({}, **opts).run.first
  #noinspection RubyResolve
  client("v1").create_namespace(dict)
end

#create_resourceObject

Creates the configmap with 0 entries.



35
36
37
# File 'lib/state/config_map_backend.rb', line 35

def create_resource
  apply_resource(template_resource([]))
end

#provision_missing_resources(**opts) ⇒ Object

Checks for the namespace and configmap, creating along the way if missing. Does not raise if already exists.

Parameters:

  • opts (Hash)

    for things like verbose



25
26
27
28
29
30
31
# File 'lib/state/config_map_backend.rb', line 25

def provision_missing_resources(**opts)
  create_namespace unless (ns_existed = namespace_exists?)
  echo_init("namespaces/#{namespace}", ns_existed, opts)

  create_resource unless (cm_existed = resource_exists?)
  echo_init("#{namespace}/configmaps/#{cm_name}", cm_existed, opts)
end

#template_resource(entries) ⇒ Hash

Outputs the dict representation of the configmap, templated with the given entries.

Parameters:

Returns:

  • (Hash)


54
55
56
57
58
# File 'lib/state/config_map_backend.rb', line 54

def template_resource(entries)
  values = { consts::ENTRIES_ATTR => entries.map(&:to_h) }
  opts = { release_name: namespace }
  Kerbi::State::ConfigMapMixer.new(values, **opts).run.first
end