forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_definition.rb
294 lines (268 loc) · 9.76 KB
/
object_definition.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
class ContainerOrchestrator
module ObjectDefinition
private
def deployment_definition(name)
deployment = {
:metadata => {
:name => name,
:labels => common_labels,
:namespace => my_namespace,
:ownerReferences => owner_references
},
:spec => {
:selector => {:matchLabels => {:name => name}},
:template => {
:metadata => {:name => name, :labels => common_labels.merge(:name => name)},
:spec => {
:affinity => {
:nodeAffinity => {
:requiredDuringSchedulingIgnoredDuringExecution => {
:nodeSelectorTerms => [{
:matchExpressions => [
{:key => "kubernetes.io/arch", :operator => "In", :values => ContainerOrchestrator.new.my_node_affinity_arch_values}
]}
]
}
}
},
:serviceAccountName => ENV["WORKER_SERVICE_ACCOUNT"],
:containers => [{
:name => name,
:env => default_environment,
:livenessProbe => liveness_probe,
:securityContext => {
:allowPrivilegeEscalation => false,
:privileged => false,
:runAsNonRoot => true,
:capabilities => {
:drop => ["ALL"]
}
},
:volumeMounts => [
{:name => "database-secret", :readOnly => true, :mountPath => "/run/secrets/postgresql"},
{:name => "messaging-env-secret", :readOnly => true, :mountPath => "/run/secrets/messaging"},
{:name => "encryption-key", :readOnly => true, :mountPath => "/run/secrets/manageiq/application"},
]
}],
:volumes => [
{
:name => "database-secret",
:secret => {
:secretName => "postgresql-secrets",
:items => [
{:key => "dbname", :path => "POSTGRESQL_DATABASE"},
{:key => "hostname", :path => "POSTGRESQL_HOSTNAME"},
{:key => "password", :path => "POSTGRESQL_PASSWORD"},
{:key => "port", :path => "POSTGRESQL_PORT"},
{:key => "username", :path => "POSTGRESQL_USER"},
],
}
},
{
:name => "messaging-env-secret",
:projected => {
:sources => [
{
:secret => {
:name => "messaging-env-secret",
:items => [
{:key => "hostname", :path => "MESSAGING_HOSTNAME"},
{:key => "username", :path => "MESSAGING_USERNAME"},
{:key => "port", :path => "MESSAGING_PORT"},
{:key => "sasl_mechanism", :path => "MESSAGING_SASL_MECHANISM"},
],
}
},
{
:secret => {
:name => "#{ENV["APP_NAME"]}-kafka-admin",
:items => [
{:key => "password", :path => "MESSAGING_PASSWORD"},
],
}
},
]
}
},
{
:name => "encryption-key",
:secret => {
:secretName => "app-secrets",
:items => [
{:key => "encryption-key", :path => "encryption_key"},
],
}
}
]
},
}
}
}
if File.file?("/.postgresql/root.crt")
deployment[:spec][:template][:spec][:containers][0][:volumeMounts] << {
:mountPath => "/.postgresql",
:name => "pg-root-certificate",
:readOnly => true,
}
deployment[:spec][:template][:spec][:volumes] << {
:name => "pg-root-certificate",
:secret => {
:secretName => "postgresql-secrets",
:items => [
:key => "rootcertificate",
:path => "root.crt",
],
}
}
end
if ENV["SSL_SECRET_NAME"].present?
deployment[:spec][:template][:spec][:containers][0][:volumeMounts] ||= []
deployment[:spec][:template][:spec][:containers][0][:volumeMounts] << {
:mountPath => "/etc/pki/ca-trust/source/anchors",
:name => "internal-root-certificate",
:readOnly => true,
}
deployment[:spec][:template][:spec][:volumes] ||= []
deployment[:spec][:template][:spec][:volumes] << {
:name => "internal-root-certificate",
:secret => {
:secretName => ENV["SSL_SECRET_NAME"],
:items => [
:key => "root_crt",
:path => "root.crt",
],
}
}
else
deployment[:spec][:template][:spec][:containers][0][:volumeMounts] ||= []
deployment[:spec][:template][:spec][:containers][0][:volumeMounts] << {
:mountPath => "/etc/pki/ca-trust/source/anchors",
:name => "messaging-certificate",
:readOnly => true,
}
deployment[:spec][:template][:spec][:volumes] ||= []
deployment[:spec][:template][:spec][:volumes] << {
:name => "messaging-certificate",
:secret => {
:secretName => "manageiq-cluster-ca-cert",
:items => [
:key => "ca.crt",
:path => "ca.crt",
],
}
}
end
deployment
end
def service_definition(name, selector, port)
{
:metadata => {
:name => name,
:labels => common_labels,
:namespace => my_namespace,
:ownerReferences => owner_references
},
:spec => {
:selector => selector,
:ports => [{
:name => "#{name}-#{port}",
:port => port,
:targetPort => port
}]
}
}
end
def secret_definition(name, string_data)
{
:metadata => {
:name => name,
:labels => common_labels,
:namespace => my_namespace,
:ownerReferences => owner_references
},
:stringData => string_data
}
end
def default_environment
[
{:name => "GUID", :value => MiqServer.my_guid},
{:name => "HOME", :value => Rails.root.join("tmp").to_s},
{:name => "APPLICATION_DOMAIN", :value => ENV["APPLICATION_DOMAIN"]},
{:name => "MEMCACHED_SERVER", :value => ENV["MEMCACHED_SERVER"]},
{:name => "MEMCACHED_SERVICE_NAME", :value => ENV["MEMCACHED_SERVICE_NAME"]},
{:name => "WORKER_HEARTBEAT_FILE", :value => Rails.root.join("tmp", "worker.hb").to_s},
{:name => "WORKER_HEARTBEAT_METHOD", :value => "file"},
] + database_environment + memcached_environment + messaging_environment
end
def database_environment
[
{:name => "DATABASE_SSL_MODE", :value => ENV["DATABASE_SSL_MODE"]},
]
end
def memcached_environment
return [] unless ENV["MEMCACHED_ENABLE_SSL"].present?
[
{:name => "MEMCACHED_ENABLE_SSL", :value => ENV["MEMCACHED_ENABLE_SSL"]},
{:name => "MEMCACHED_SSL_CA", :value => ENV["MEMCACHED_SSL_CA"]},
]
end
def messaging_environment
return [] unless ENV["MESSAGING_TYPE"].present?
[
{:name => "MESSAGING_PORT", :value => ENV["MESSAGING_PORT"]},
{:name => "MESSAGING_TYPE", :value => ENV["MESSAGING_TYPE"]},
{:name => "MESSAGING_SSL_CA", :value => ENV["MESSAGING_SSL_CA"]},
{:name => "MESSAGING_SASL_MECHANISM", :value => ENV["MESSAGING_SASL_MECHANISM"]},
{:name => "MESSAGING_HOSTNAME", :value => ENV["MESSAGING_HOSTNAME"]},
{:name => "MESSAGING_PASSWORD", :value => ENV["MESSAGING_PASSWORD"]},
{:name => "MESSAGING_USERNAME", :value => ENV["MESSAGING_USERNAME"]}
]
end
def liveness_probe
{
:exec => {:command => ["/usr/local/bin/manageiq_liveness_check"]},
:initialDelaySeconds => 240,
:timeoutSeconds => 10,
:periodSeconds => 15
}
end
NAMESPACE_FILE = "/run/secrets/kubernetes.io/serviceaccount/namespace".freeze
def my_namespace
@my_namespace ||= File.read(NAMESPACE_FILE)
end
def app_name
ENV["APP_NAME"]
end
def app_name_label
{:app => app_name}
end
def app_name_selector
"app=#{app_name}"
end
def common_labels
app_name_label.merge(orchestrated_by_label)
end
def orchestrated_by_label
{:"#{app_name}-orchestrated-by" => pod_name}
end
def orchestrated_by_selector
"#{app_name}-orchestrated-by=#{pod_name}"
end
def owner_references
[{
:apiVersion => "v1",
:blockOwnerDeletion => true,
:controller => true,
:kind => "Pod",
:name => pod_name,
:uid => pod_uid
}]
end
def pod_name
ENV['POD_NAME']
end
def pod_uid
ENV["POD_UID"]
end
end
end