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

Attributes inherited from BaseBackend

#is_working, #release_name

Instance Method Summary collapse

Methods included from Mixins::CmBackendTesting

#data_readable?, #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, #delete_entry, #entries, #entry_set, #prime, #save, #working?

Constructor Details

#initialize(auth_bundle, release_name, namespace) ⇒ ConfigMapBackend

Returns a new instance of ConfigMapBackend.

Parameters:

  • auth_bundle (Hash)

    generated by Kerbi::Utils::K8sAuth

  • release_name (String)

    Kubernetes namespace where configmap lives



23
24
25
26
27
# File 'lib/state/config_map_backend.rb', line 23

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

Instance Attribute Details

#auth_bundleHash (readonly)

Credentials generated by Utils::K8sAuth to use for auth

Returns:

  • (Hash)

    list of hashes



14
15
16
# File 'lib/state/config_map_backend.rb', line 14

def auth_bundle
  @auth_bundle
end

#namespaceString (readonly)

Kubernetes namespace where configmap lives

Returns:

  • (String)

    namespace



19
20
21
# File 'lib/state/config_map_backend.rb', line 19

def namespace
  @namespace
end

Instance Method Details

#apply_resource(resource_desc, mode: 'create') ⇒ 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)


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/state/config_map_backend.rb', line 52

def apply_resource(resource_desc, mode: 'create')
  if mode == 'create'
    #noinspection RubyResolve
    client("v1").create_config_map(resource_desc)
  elsif mode == 'update'
    #noinspection RubyResolve
    client("v1").update_config_map(resource_desc)
  else
    raise "What kind of sick mode is #{mode}?"
  end
end

#create_namespaceObject

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



81
82
83
84
85
86
# File 'lib/state/config_map_backend.rb', line 81

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

#create_resourceObject

Creates the configmap with 0 entries.



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

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



33
34
35
36
37
38
39
# File 'lib/state/config_map_backend.rb', line 33

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("configmaps/#{namespace}/#{cm_name}", cm_existed, opts)
end

#resource_nameObject



88
89
90
# File 'lib/state/config_map_backend.rb', line 88

def resource_name
  cm_name
end

#resource_signatureObject



92
93
94
# File 'lib/state/config_map_backend.rb', line 92

def resource_signature
  "configmaps/#{namespace}/#{resource_name}"
end

#template_resource(entries) ⇒ Hash

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

Parameters:

Returns:

  • (Hash)


69
70
71
72
73
74
75
76
# File 'lib/state/config_map_backend.rb', line 69

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