This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.rb
222 lines (179 loc) · 6.66 KB
/
manifest.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
216
217
218
219
220
221
222
# frozen_string_literal: true
require 'octokit'
require 'fileutils'
require_relative 'templates/kustomization'
require_relative 'templates/flux'
require_relative 'templates/ingress'
require_relative 'templates/namespace'
require_relative 'templates/service'
Octokit.configure do |c|
c.connection_options = {
request: {
open_timeout: 5,
timeout: 5
}
}
end
class Manifest
attr_reader :client, :repo, :service, :namespace, :image, :services
BASE_DIR = 'base'
def initialize(service:, cluster_repo:, namespace:, target_image:, tag:, token:)
puts 'Connecting to GitHub...'
@client = Octokit::Client.new(access_token: token)
@repo = cluster_repo
@service = service
@namespace = namespace
@image = "#{target_image}:#{tag}"
@services = fetch_services
@templates = []
end
def self.create(options = {})
instance = new(**options.slice(:service, :cluster_repo, :namespace, :target_image, :tag, :token))
instance.supporting_services.each do |svc|
instance.create_supporting_manifests(svc)
end
instance.create_namespace_manifest
instance.create_primary_manifests
instance.create_flux_manifest if options[:flux]
if options[:dry_run]
instance.dry_run(options[:built])
else
instance.commit_overlay_to_github
puts 'Done!'
end
end
def supporting_services
services - [service]
end
def create_namespace_manifest
return puts "Using existing namespace '#{namespace}'" unless include_namespace?
puts "Creating namespace '#{namespace}'..."
@templates << Templates::Namespace.new(service: service, namespace: namespace)
end
def create_primary_manifests # rubocop:disable Metrics/AbcSize
puts "Creating #{service} manifests with #{image}..."
# check each type of file for the service we're updating, and create an overlay
@templates << Templates::Ingress.new(service: service, namespace: namespace, hosts: base_ingress_hosts(service)) if include_ingress?(service)
@templates << Templates::Kustomization.new(service: service, namespace: namespace, image: image, templates: @templates, primary: true)
end
def create_flux_manifest
generators = flux_generators
services.each do |svc|
generators << { 'command' => "kustomize build ./#{svc}/overlays/#{namespace}" }
end
@templates << Templates::Flux.new(service: service, namespace: namespace, generators: generators.uniq)
end
def create_supporting_manifests(svc) # rubocop:disable Metrics/AbcSize
return puts "Using existing manifests for #{svc}" unless create_overlay?(svc)
puts "Creating #{svc} manifests pointing to #{svc}.default.svc.cluster.local..."
@templates << Templates::Ingress.new(service: svc, namespace: namespace, hosts: base_ingress_hosts(svc)) if include_ingress?(svc)
@templates << Templates::Service.new(service: svc, namespace: namespace) if include_service?(svc)
@templates << Templates::Kustomization.new(service: svc, namespace: namespace, templates: @templates, primary: false)
end
def commit_overlay_to_github # rubocop:disable Metrics/AbcSize
puts "Creating overlays for '#{namespace}' in GitHub repository #{repo}..."
ref = 'heads/master'
sha_latest_commit = client.ref(repo, ref).object.sha
sha_base_tree = client.commit(repo, sha_latest_commit).commit.tree.sha
sha_new_tree = client.create_tree(repo, new_blobs, base_tree: sha_base_tree).sha
commit_message = "Create #{service} in namespace '#{namespace}' with image #{image}"
sha_new_commit = client.create_commit(repo, commit_message, sha_new_tree, sha_latest_commit).sha
client.update_ref(repo, ref, sha_new_commit)
end
def dry_run(built = false)
puts @templates.any? ? 'Printing yaml files...' : 'No yaml files to print!'
puts
built ? print_manifests : print_templates
end
private
def fetch_services
puts 'Collecting known services...'
services = client.contents(repo).select { |c| c[:type] == 'dir' }.map(&:name)
if services.include? service
puts "Found services #{services}"
services
else
exit_code("Unknown service. Please choose one of #{services}", 2)
end
end
def flux_generators
flux = client.contents(repo, path: "#{Templates::Flux::NAME}.yaml")
hash = YAML.safe_load Base64.decode64(flux.content)
hash['commandUpdated']['generators']
rescue StandardError
[]
end
def include_ingress?(svc)
base_manifest_names(svc).include?(Templates::Service::NAME)
end
def include_service?(svc)
base_manifest_names(svc).include?(Templates::Service::NAME)
end
def create_overlay?(svc)
overlay_manifest_names(svc).empty?
end
def include_namespace?
manifests = supporting_services.map { |svc| overlay_manifest_names(svc) }.flatten
!manifests.include?(Templates::Namespace::NAME)
end
def base_manifests(svc)
@base_manifests ||= {}
@base_manifests[svc] ||= client.contents(repo, path: [svc, BASE_DIR].join('/')).select { |c| c[:type] == 'file' }
end
def base_manifest_names(svc)
base_manifests(svc).map { |m| m.name.gsub(/.ya*ml/, '') }
rescue StandardError
[]
end
def overlay_manifests(svc)
@overlay_manifests ||= {}
@overlay_manifests[svc] ||= client.contents(repo, path: [svc, 'overlays', namespace].join('/')).select { |c| c[:type] == 'file' }
end
def overlay_manifest_names(svc)
overlay_manifests(svc).map { |m| m.name.gsub(/.ya*ml/, '') }
rescue StandardError
[]
end
def base_ingress_hosts(svc)
ingress = client.contents(repo, path: [svc, BASE_DIR, "#{Templates::Ingress::NAME}.yaml"].join('/'))
hash = YAML.safe_load Base64.decode64(ingress.content)
hash['spec']['rules'].map { |r| r['host'] }
rescue StandardError
[]
end
def new_blobs
@templates.map do |t|
{
path: t.path,
mode: '100644',
type: 'blob',
sha: client.create_blob(repo, Base64.encode64(t.manifest.to_yaml), 'base64')
}
end
end
def print_templates
@templates.each do |t|
puts t.manifest.to_yaml
end
puts "\n"
end
def print_manifests # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
services.each do |svc|
base_manifests(svc).each do |m|
FileUtils.mkdir_p "/tmp/#{svc}/base"
File.write "/tmp/#{m.path}", Base64.decode64(client.contents(repo, path: m.path).content)
end
end
@templates.each do |t|
FileUtils.mkdir_p "/tmp/#{t.directory.join('/')}"
File.write "/tmp/#{t.path}", t.manifest.to_yaml
end
puts @templates.find(&:flux?)&.manifest&.to_yaml
@templates.map(&:directory).uniq.each do |dir|
next if dir.empty?
puts '---'
puts `kustomize build /tmp/#{dir.join('/')}`
end
puts "\n"
end
end