Class: Kdep::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/kdep/renderer.rb

Constant Summary collapse

NS_ENV_SUFFIXES =

Convention: Infisical project slug = K8s namespace stripped of -stage/-dev suffix; envSlug = stage/dev/prod derived from the same suffix.

{ "-stage" => "stage", "-staging" => "stage", "-dev" => "dev" }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config, deploy_dir, env: nil) ⇒ Renderer

Returns a new instance of Renderer.



10
11
12
13
14
# File 'lib/kdep/renderer.rb', line 10

def initialize(config, deploy_dir, env: nil)
  @config = config
  @deploy_dir = deploy_dir
  @env = env
end

Instance Method Details

#env_specObject

Public so commands (e.g. ‘kdep env check`) can introspect the contract.



67
68
69
70
71
# File 'lib/kdep/renderer.rb', line 67

def env_spec
  return @env_spec if defined?(@env_spec)
  path = env_spec_path
  @env_spec = path && File.exist?(path) ? EnvSpec.parse_file(path) : nil
end

#render_helm_ingressesObject

Feature B: render N Ingress resources from config. Returns [[suffix, yaml], …] so the caller can emit multiple files.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kdep/renderer.rb', line 51

def render_helm_ingresses
  entries = @config["ingresses"] || []
  return [] if entries.empty?

  template_path = File.join(Kdep.templates_dir, "resources", "helm_ingress.yml.erb")
  raise "helm_ingress template not found at #{template_path}" unless File.exist?(template_path)
  erb = build_erb(File.read(template_path))

  entries.map do |ingress|
    context = TemplateContext.new(@config)
    context.instance_variable_set(:@ingress, ingress)
    [ingress["name"], erb.result(context.get_binding)]
  end
end

#render_resource(resource_name) ⇒ Object



16
17
18
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
45
46
47
# File 'lib/kdep/renderer.rb', line 16

def render_resource(resource_name)
  # When env.spec is present, the `secret` slot becomes an InfisicalSecret CR
  # instead of a native K8s Secret -- the Infisical operator materializes
  # the K8s Secret in-cluster. Same env_from: secretRef wiring, no template
  # changes in user-overridden resources.
  effective_resource = resource_name
  if resource_name == "secret" && env_spec
    effective_resource = "infisical_secret"
  end

  template_path = resolve_template(effective_resource)
  raise "Template not found: #{effective_resource}.yml.erb" unless template_path

  template_content = File.read(template_path)
  erb = build_erb(template_content)

  effective_config = @config
  secrets = {}

  case effective_resource
  when "configmap"
    effective_config = @config.merge("configmap" => resolved_configmap)
  when "infisical_secret"
    return nil if secret_keys_for_env.empty?
    effective_config = @config.merge("infisical" => infisical_render_context)
  when "secret"
    secrets = legacy_load_secrets
  end

  context = TemplateContext.new(effective_config, secrets)
  erb.result(context.get_binding)
end