Class: Kdep::TemplateContext
- Inherits:
-
Object
- Object
- Kdep::TemplateContext
- Defined in:
- lib/kdep/template_context.rb
Instance Method Summary collapse
-
#configmap_value(value, indent = 4) ⇒ Object
Helper: render one ConfigMap
data:value. - #get_binding ⇒ Object
-
#image_ref ⇒ Object
Helper: fully-qualified container image reference, "[registry/]image:tag".
-
#initialize(config, secrets = {}) ⇒ TemplateContext
constructor
A new instance of TemplateContext.
-
#yaml_value(val) ⇒ Object
Helper: render a value as YAML-safe string.
Constructor Details
#initialize(config, secrets = {}) ⇒ TemplateContext
Returns a new instance of TemplateContext.
5 6 7 8 9 10 11 12 13 |
# File 'lib/kdep/template_context.rb', line 5 def initialize(config, secrets = {}) @config = config @secrets = secrets # Expose top-level config keys as methods for cleaner templates config.each do |key, value| define_singleton_method(key.to_sym) { value } if key.is_a?(String) end end |
Instance Method Details
#configmap_value(value, indent = 4) ⇒ Object
Helper: render one ConfigMap data: value.
Multiline values become YAML block scalars, so config files mounted into
containers (prometheus.yml, fluent.conf, curator action files...) survive
intact instead of collapsing into one quoted line (repleadfy/kdep#1).
Single-line values stay quoted strings: ConfigMap data values must be
strings, so an unquoted 8080 or true would be rejected by the API.
indent is the column the block body sits at -- two levels under data:.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/kdep/template_context.rb', line 50 def configmap_value(value, indent = 4) str = value.to_s return str.to_json unless str.include?("\n") pad = " " * indent # `|` keeps a single trailing newline, `|-` strips it -- preserve whichever # the value actually had, since some consumers care. chomp = str.end_with?("\n") body = chomp ? str.sub(/\n\z/, "") : str lines = body.split("\n", -1).map { |line| line.empty? ? "" : "#{pad}#{line}" } "#{chomp ? "|" : "|-"}\n#{lines.join("\n")}" end |
#get_binding ⇒ Object
15 16 17 |
# File 'lib/kdep/template_context.rb', line 15 def get_binding binding end |
#image_ref ⇒ Object
Helper: fully-qualified container image reference, "[registry/]image:tag".
An empty or whitespace-only registry: counts as absent. Ruby treats ""
as truthy, so the old inline @config["registry"] ? ... : ... ternary in
every template emitted a leading slash ("/image:tag") for registry: "" --
a real gotcha for upstream-image deploys (repleadfy/kdep#1). Same for a
blank tag:, which would otherwise render an invalid trailing colon.
31 32 33 34 35 36 37 38 39 |
# File 'lib/kdep/template_context.rb', line 31 def image_ref registry = @config["registry"].to_s.strip image = @config["image"].to_s tag = @config["tag"].to_s.strip tag = "latest" if tag.empty? repository = registry.empty? ? image : "#{registry}/#{image}" "#{repository}:#{tag}" end |
#yaml_value(val) ⇒ Object
Helper: render a value as YAML-safe string
20 21 22 |
# File 'lib/kdep/template_context.rb', line 20 def yaml_value(val) val.is_a?(String) ? val : val.to_s end |