Class: Kdep::Commands::Bump

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Bump.



21
22
23
24
25
26
# File 'lib/kdep/commands/bump.rb', line 21

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
13
14
15
16
17
18
19
# File 'lib/kdep/commands/bump.rb', line 6

def self.option_parser
  OptionParser.new do |opts|
    opts.banner = "Usage: kdep bump [deploy] [env]"
    opts.separator ""
    opts.separator "Full pipeline: registry query -> version increment -> build -> push -> render -> apply."
    opts.separator ""
    opts.on("--skip-apply", "Build and push only, skip render+apply")
    opts.on("--dry-run", "Run full pipeline but skip kubectl apply")
    opts.on("--no-dashboard", "Skip TUI dashboard after deploy")
    opts.on("--platform=PLATFORM", "Target platform (e.g., linux/amd64)")
    opts.on("--init-state", "Seed state.yml at 0.0 when missing (non-interactive)")
    opts.on("--no-check", "Skip preflight check: validators")
  end
end

Instance Method Details

#executeObject



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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/kdep/commands/bump.rb', line 28

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

  # Step 1: Discovery + config
  discovery = Kdep::Discovery.new
  kdep_dir = discovery.find_kdep_dir

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

  deploy_dir = resolve_deploy_dir(kdep_dir, deploy_name, discovery)
  unless deploy_dir
    exit 1
  end

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

  # Feature G: preflight validators. Runs for every preset, halts on
  # first failure. --no-check skips.
  unless @command_options[:"no-check"]
    run_preflight_checks(config, deploy_dir, kdep_dir)
  end

  # Feature E: dispatch on preset before the docker-heavy app pipeline.
  # Helm and custom presets don't build/push images — they route to
  # preset-specific pipelines that keep the rest of bump's guarantees
  # (context guard, rollout tracking, etc.).
  preset = config["preset"]
  if preset == "helm"
    run_helm_pipeline(deploy_dir, env)
    return
  elsif preset == "custom"
    run_custom_pipeline(deploy_dir, config, kdep_dir)
    return
  end

  # Step 2: Local config sanity — read state.yml BEFORE touching the
  # cluster. A missing/corrupt state.yml is a local config error and
  # should fail fast without waking kubectl.
  begin
    current_tag = Kdep::State.tag(deploy_dir) || handle_missing_state(deploy_dir)
  rescue Kdep::State::Error => e
    @ui.error(e.message)
    exit 1
  end
  # to_fix.md #3 — warn if someone has gitignored state.yml
  require "kdep/doctor/check_state_gitignored"
  gitignore_result = Kdep::Doctor::CheckStateGitignored.new(deploy_dir).run
  if gitignore_result.severity == :error
    @ui.warn(gitignore_result.message)
    @ui.warn(gitignore_result.hint) if gitignore_result.hint
  end

  # Step 3: Context guard (bump touches the cluster)
  begin
    Kdep::ContextGuard.new(config["context"]).validate!
  rescue Kdep::Kubectl::Error => e
    @ui.error(e.message)
    exit 1
  end

  # Pre-apply cluster health check
  dry_run = @command_options[:"dry-run"]
  unless @command_options[:"skip-apply"] || dry_run
    health = Kdep::ClusterHealth.new
    health.report(@ui)
  end

  image = config["image"] || config["name"]
  registry_url = config["registry"]
  repo_root = File.dirname(kdep_dir)

  begin
    # Step 4: Increment minor version.
    parsed = Kdep::VersionTagger.parse(current_tag)
    next_version = parsed ? "#{parsed[0]}.#{parsed[1] + 1}" : "0.1"
    @ui.info("#{current_tag} -> #{next_version}")

    # Step 5: Build
    if registry_url && !registry_url.to_s.empty?
      full_tag = "#{registry_url}/#{image}:#{next_version}"
    else
      full_tag = "#{image}:#{next_version}"
    end

    Kdep::Docker.build(
      tag: full_tag,
      context_dir: repo_root,
      dockerfile: config["dockerfile"],
      target: config["target"],
      platform: @command_options[:platform] || config["platform"],
      build_args: config["build_args"]
    )
    @ui.info("Built: #{full_tag}")

    # Step 6: Push
    Kdep::Docker.push(full_tag)
    @ui.info("Pushed: #{full_tag}")

    # Step 7: Render + Apply (unless --skip-apply)
    unless @command_options[:"skip-apply"]
      config["tag"] = next_version

      # Render manifests
      preset = Kdep::Preset.new(config["preset"], deploy_dir)
      resources = preset.resources
      output_dir = File.join(deploy_dir, ".rendered")

      writer = Kdep::Writer.new(output_dir)
      writer.clean
      renderer = Kdep::Renderer.new(config, deploy_dir)
      validator = Kdep::Validator.new
      validation_errors = []

      resources.each_with_index do |resource_name, idx|
        index = idx + 1
        content = renderer.render_resource(resource_name)

        unless content.nil? || content.strip.empty?
          result = validator.validate(content, resource_name)
          unless result["valid"]
            result["errors"].each do |err|
              validation_errors << "#{resource_name}: #{err}"
            end
          end
        end

        writer.write(resource_name, content, index)
      end

      unless validation_errors.empty?
        validation_errors.each { |err| @ui.error(err) }
        @ui.error("Manifest validation failed -- fix errors before applying")
        exit 1
      end

      rendered_files = Dir.glob(File.join(output_dir, "*.yml")).sort

      if dry_run
        rendered_files.each do |f|
          @ui.info("Would apply: #{File.basename(f)}")
        end
        @ui.info("#{rendered_files.length} files validated (dry-run, no cluster changes)")
      else
        # Apply
        namespace = config["namespace"]
        applied = 0

        rendered_files.each do |file_path|
          begin
            Kdep::Kubectl.apply(file_path, namespace: namespace)
            @ui.info("applied: #{File.basename(file_path)}")
            applied += 1
          rescue Kdep::Kubectl::Error => e
            @ui.error("#{File.basename(file_path)}: #{e.message}")
            exit 1
          end
        end

        @ui.info("#{applied} files applied, 0 errors")

        # Save new tag to state.yml
        Kdep::State.write(deploy_dir, tag: next_version)

        # Launch TUI dashboard for live monitoring
        unless @command_options[:"no-dashboard"]
          dashboard = Kdep::Dashboard.new(
            "name" => config["name"],
            "namespace" => namespace,
            "registry" => config["registry"],
            "image" => image
          )
          dashboard.run
        end
      end
    end

  rescue Kdep::Docker::Error => e
    @ui.error(e.message)
    exit 1
  rescue Kdep::Registry::Error => e
    @ui.error(e.message)
    exit 1
  end
end