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
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
|
# File 'lib/kube_deploy_tools/render_deploys_hook.rb', line 16
def self.render_deploys(config_file, input_dir, output_root, file_filters)
config = YAML.load_file(config_file)
t = KubeDeployTools::Templater.new
get_valid_files(file_filters, input_dir).each do |yml|
output_base = File.basename(yml, TEMPLATING_SUFFIX)
subdir = File.dirname(yml[input_dir.size..-1])
if subdir == '.'
output_dir = output_root
else
output_dir = File.join(output_root, subdir)
end
output_file = File.join(output_dir, output_base)
if yml.end_with? TEMPLATING_SUFFIX
Logger.debug("Generating #{output_file} from #{yml}")
t.template_to_file(yml, config, output_file)
else
Logger.debug("Copying #{output_file} from #{yml}")
FileUtils.mkdir_p output_dir
FileUtils.copy(yml, output_file)
end
begin
if File.file?(output_file)
yaml = []
YAML.load_stream(File.read(output_file)) { |doc| yaml << doc }
yaml = yaml.
compact.
map do |data|
must_have_ns = [
'ConfigMap', 'CronJob', 'DaemonSet', 'Deployment', 'Endpoints', 'HorizontalPodAutoscaler',
'Ingress', 'PersistentVolumeClaim', 'PodDisruptionBudget', 'ServiceAccount', 'Secret', 'Service'
]
if must_have_ns.member?(data.fetch('kind'))
raise "Rendered Kubernetes template missing a .metadata.namespace: #{yml}" if data.fetch('metadata', {}).fetch('namespace', '').empty?
end
annotations = data['metadata']['annotations'] || {}
if config['git_commit']
annotations['git_commit'] = config['git_commit']
end
if config['git_project']
annotations['git_project'] = config['git_project']
end
data['metadata']['annotations'] = annotations
data
end
File.open(output_file, 'w') { |f| f << YAML.dump_stream(*yaml) }
end
rescue => e
raise "Failed to YAML validate #{output_file} (generated from #{yml}): #{e}"
end
end
end
|