Class: Kdep::Commands::Render

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_options:, command_options:, args:) ⇒ Render

Returns a new instance of Render.



14
15
16
17
18
19
# File 'lib/kdep/commands/render.rb', line 14

def initialize(global_options:, command_options:, args:)
  @global_options = global_options
  @command_options = command_options
  @args = args
  @ui = Kdep::UI.new(color: false)
end

Class Method Details

.option_parserObject



6
7
8
9
10
11
12
# File 'lib/kdep/commands/render.rb', line 6

def self.option_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: kdep render [deploy] [env]"
    opts.separator ""
    opts.separator "Renders Kubernetes manifests for the specified deploy target and environment."
  end
end

Instance Method Details

#executeObject



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/kdep/commands/render.rb', line 21

def execute
  deploy_name = @args[0]
  env = @args[1]

  # Discover kdep/ directory
  discovery = Kdep::Discovery.new
  kdep_dir = discovery.find_kdep_dir

  unless kdep_dir
    @ui.error("No kdep/ directory found")
    exit 1
  end

  # Resolve deploy directory
  deploy_dir = resolve_deploy_dir(kdep_dir, deploy_name, discovery)
  unless deploy_dir
    exit 1
  end

  # Load config
  config = Kdep::Config.new(deploy_dir, env).load

  # Load preset resources
  preset = Kdep::Preset.new(config["preset"], deploy_dir)
  resources = preset.resources

  # Determine output directory (.rendered/ in deploy target dir)
  repo_root = File.dirname(kdep_dir)
  output_dir = File.join(deploy_dir, ".rendered")

  # Set up writer and renderer
  writer = Kdep::Writer.new(output_dir)
  writer.clean
  renderer = Kdep::Renderer.new(config, deploy_dir)
  validator = Kdep::Validator.new

  files_written = 0
  errors = []

  resources.each_with_index do |resource_name, idx|
    index = idx + 1

    # Render
    begin
      content = renderer.render_resource(resource_name)
    rescue => e
      errors << {"resource" => resource_name, "error" => e.message}
      next
    end

    # Validate (skip empty content like ingress with no domains)
    unless content.nil? || content.strip.empty?
      result = validator.validate(content, resource_name)
      unless result["valid"]
        result["errors"].each do |err|
          errors << {"resource" => resource_name, "error" => err}
        end
      end
    end

    # Write
    path = writer.write(resource_name, content, index)
    if path
      relative_path = path.sub(repo_root + "/", "")
      @ui.file_written(relative_path)
      files_written += 1
    end
  end

  # Print errors inline
  errors.each do |err|
    @ui.error("#{err["resource"]}: #{err["error"]}")
  end

  # Print summary
  @ui.summary(files_written, errors.length)

  exit 1 if errors.length > 0
end