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
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
|
# File 'lib/kdep/commands/bump.rb', line 26
def execute
deploy_name = @args[0]
env = @args[1]
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
begin
Kdep::ContextGuard.new(config["context"]).validate!
rescue Kdep::Kubectl::Error => e
@ui.error(e.message)
exit 1
end
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
state_path = File.join(deploy_dir, "state.yml")
if File.exist?(state_path)
state = YAML.safe_load(File.read(state_path)) || {}
current_tag = state["tag"] || "0.0"
else
current_tag = "0.0"
end
parsed = Kdep::VersionTagger.parse(current_tag)
next_version = parsed ? "#{parsed[0]}.#{parsed[1] + 1}" : "0.1"
@ui.info("#{current_tag} -> #{next_version}")
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}")
Kdep::Docker.push(full_tag)
@ui.info("Pushed: #{full_tag}")
unless @command_options[:"skip-apply"]
config["tag"] = next_version
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
begin
content = renderer.render_resource(resource_name)
rescue => e
@ui.error("#{resource_name}: #{e.message}")
next
end
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
namespace = config["namespace"]
errors = []
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
errors << { "file" => File.basename(file_path), "error" => e.message }
@ui.error("#{File.basename(file_path)}: #{e.message}")
end
end
if errors.empty?
@ui.info("#{applied} files applied, 0 errors")
File.write(File.join(deploy_dir, "state.yml"), YAML.dump({"tag" => next_version}))
unless @command_options[:"no-dashboard"]
dashboard = Kdep::Dashboard.new(
"name" => config["name"],
"namespace" => namespace,
"registry" => config["registry"],
"image" => image
)
dashboard.run
end
else
@ui.info("#{applied} files applied, #{errors.length} errors")
exit 1
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
|