forked from adobe/rules_gitops
-
Notifications
You must be signed in to change notification settings - Fork 1
/
k8s.bzl
592 lines (548 loc) · 20.8 KB
/
k8s.bzl
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# Copyright 2020 Adobe. All rights reserved.
# This file is licensed to you under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. You may obtain a copy
# of the License at http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.
load(
"@io_bazel_rules_docker//skylib:path.bzl",
_get_runfile_path = "runfile",
)
load(
"//skylib/kustomize:kustomize.bzl",
"KustomizeInfo",
"imagePushStatements",
"kubectl",
"kustomize",
kustomize_gitops = "gitops",
)
load("//skylib:push.bzl", "k8s_container_push")
def _runfiles(ctx, f):
return "${RUNFILES}/%s" % _get_runfile_path(ctx, f)
def _python_runfiles(ctx, f):
return "PYTHON_RUNFILES=${RUNFILES} %s" % _runfiles(ctx, f)
def _show_impl(ctx):
script_content = "#!/bin/bash\nset -e\n"
kustomize_outputs = []
script_template = "{template_engine} --template={infile} --variable=NAMESPACE={namespace} --stamp_info_file={info_file}\n"
for dep in ctx.attr.src.files.to_list():
kustomize_outputs.append(script_template.format(
infile = dep.short_path,
template_engine = ctx.executable._template_engine.short_path,
namespace = ctx.attr.namespace,
info_file = ctx.file._info_file.short_path,
))
# ensure kustomize outputs are separated by '---' delimiters
script_content += "echo '---'\n".join(kustomize_outputs)
ctx.actions.write(ctx.outputs.executable, script_content, is_executable = True)
return [
DefaultInfo(runfiles = ctx.runfiles(files = [ctx.executable._template_engine, ctx.file._info_file] + ctx.files.src)),
]
show = rule(
implementation = _show_impl,
attrs = {
"src": attr.label(
doc = "Input file.",
mandatory = True,
),
"namespace": attr.string(
doc = "kubernetes namespace.",
mandatory = True,
),
"_info_file": attr.label(
default = Label("//skylib:more_stable_status.txt"),
allow_single_file = True,
),
"_template_engine": attr.label(
default = Label("//templating:fast_template_engine"),
executable = True,
cfg = "host",
),
},
executable = True,
)
def k8s_deploy(
name, # name of the rule is important for gitops, since it will become a part of the target manifest file name in /cloud
cluster = "dev",
user = "{BUILD_USER}",
namespace = None,
configmaps_srcs = None,
secrets_srcs = None,
configmaps_renaming = None, # configmaps renaming policy. Could be None or 'hash'.
manifests = [],
name_prefix = None,
name_suffix = None,
patches = None,
substitutions = {}, # dict of template parameter substitutions. CLUSTER and NAMESPACE parameters are added automatically.
deps = [],
deps_aliases = {},
images = {},
image_chroot = None, # DEPRECATED. ignored now. If default repo path is not working for you, use image_repository to change it
image_registry = "docker.io", # registry to push container to. jenkins will need an access configured for gitops to work. Ignored for mynamespace.
image_repository = None, # repository (registry path) to push container to. Generated from the image bazel path if empty.
image_repository_prefix = None, # Mutually exclusive with 'image_repository'. Add a prefix to the repository name generated from the image bazel path
objects = [],
gitops = True, # make sure to use gitops = False to work with individual namespace. This option will be turned False if namespace is '{BUILD_USER}'
deployment_branch = None,
release_branch_prefix = "master",
flatten_manifest_directories = False,
start_tag = "{{",
end_tag = "}}",
visibility = None):
""" k8s_deploy
"""
if not manifests:
manifests = native.glob(["*.yaml", "*.yaml.tpl"])
for reservedname in ["CLUSTER", "NAMESPACE"]:
if substitutions.get(reservedname):
fail("do not put %s in substitutions parameter of k8s_deploy. It will be added autimatically" % reservedname)
substitutions = dict(substitutions)
substitutions["CLUSTER"] = cluster
# NAMESPACE substitution is deferred until test_setup/kubectl/gitops
if namespace == "{BUILD_USER}":
gitops = False
if image_chroot:
print("image_chroot parameter of k8s_deploy rule in %s is ignored now. If default repo path is not working for you, use image_repository to change it." % native.package_name())
if not gitops:
# Mynamespace option
if not namespace:
namespace = "{BUILD_USER}"
images_v = []
for imgname in images:
img = images[imgname]
images_v.append(imgname + "_mynamespace_push")
if not native.existing_rule(imgname + "_mynamespace_push"):
k8s_container_push(
name = imgname + "_mynamespace_push",
image = img,
legacy_image_name = imgname,
registry = image_registry,
repository = image_repository,
repository_prefix = "{BUILD_USER}",
)
kustomize(
name = name,
namespace = namespace,
configmaps_srcs = configmaps_srcs,
secrets_srcs = secrets_srcs,
# disable_name_suffix_hash is renamed to configmaps_renaming in recent Kustomize
disable_name_suffix_hash = (configmaps_renaming != "hash"),
images = images_v,
manifests = manifests,
substitutions = substitutions,
deps = deps,
deps_aliases = deps_aliases,
start_tag = start_tag,
end_tag = end_tag,
name_prefix = name_prefix,
name_suffix = name_suffix,
patches = patches,
objects = objects,
visibility = visibility,
)
kubectl(
name = name + ".apply",
srcs = [name],
cluster = cluster,
user = user,
namespace = namespace,
visibility = visibility,
)
kubectl(
name = name + ".delete",
srcs = [name],
command = "delete",
cluster = cluster,
user = user,
namespace = namespace,
visibility = visibility,
)
show(
name = name + ".show",
namespace = namespace,
src = name,
visibility = visibility,
)
else:
# gitops
if objects:
print("Warning: objects parameter of k8s_deploy should not be used for gitops in %s. make sure dependencies are processed independently." % native.package_name())
if not namespace:
fail("namespace must be defined for gitops k8s_deploy")
images_v = []
for imgname in images:
img = images[imgname]
images_v.append(imgname + "_push")
if not native.existing_rule(imgname + "_push"):
k8s_container_push(
name = imgname + "_push",
image = img,
legacy_image_name = imgname,
registry = image_registry,
repository = image_repository,
repository_prefix = image_repository_prefix,
)
kustomize(
name = name,
namespace = namespace,
configmaps_srcs = configmaps_srcs,
secrets_srcs = secrets_srcs,
# disable_name_suffix_hash is renamed to configmaps_renaming in recent Kustomize
disable_name_suffix_hash = (configmaps_renaming != "hash"),
images = images_v,
manifests = manifests,
visibility = visibility,
substitutions = substitutions,
deps = deps,
deps_aliases = deps_aliases,
start_tag = start_tag,
end_tag = end_tag,
name_prefix = name_prefix,
name_suffix = name_suffix,
patches = patches,
)
kubectl(
name = name + ".apply",
srcs = [name],
cluster = cluster,
user = user,
namespace = namespace,
visibility = visibility,
)
kustomize_gitops(
name = name + ".gitops",
srcs = [name],
cluster = cluster,
namespace = namespace,
strip_prefixes = [
namespace + "-",
cluster + "-",
],
deployment_branch = deployment_branch,
release_branch_prefix = release_branch_prefix,
visibility = ["//visibility:public"],
)
show(
name = name + ".show",
src = name,
namespace = namespace,
visibility = visibility,
)
# kubectl template
def _kubectl_config(repository_ctx, args):
kubectl = repository_ctx.path("kubectl")
kubeconfig_yaml = repository_ctx.path("kubeconfig")
exec_result = repository_ctx.execute(
[kubectl, "--kubeconfig", kubeconfig_yaml, "config"] + args,
environment = {
# prevent kubectl config to stumble on shared .kube/config.lock file
"HOME": str(repository_ctx.path(".")),
},
quiet = True,
)
if exec_result.return_code != 0:
fail("Error executing kubectl config %s" % " ".join(args))
def _kubeconfig_impl(repository_ctx):
"""Find local kubernetes certificates"""
# find and symlink kubectl
kubectl = repository_ctx.which("kubectl")
if not kubectl:
fail("Unable to find kubectl executable. PATH=%s" % repository_ctx.path)
repository_ctx.symlink(kubectl, "kubectl")
# TODO: figure out how to use BUILD_USER
if "USER" in repository_ctx.os.environ:
user = repository_ctx.os.environ["USER"]
else:
exec_result = repository_ctx.execute(["whoami"])
if exec_result.return_code != 0:
fail("Error detecting current user")
user = exec_result.stdout.rstrip()
token = None
ca_crt = None
kubecert_cert = None
kubecert_key = None
server = repository_ctx.attr.server
# check service account first
serviceaccount = repository_ctx.path("/var/run/secrets/kubernetes.io/serviceaccount")
if serviceaccount.exists:
ca_crt = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
token_file = serviceaccount.get_child("token")
if token_file.exists:
exec_result = repository_ctx.execute(["cat", token_file.realpath])
if exec_result.return_code != 0:
fail("Error reading user token")
token = exec_result.stdout.rstrip()
# use master url from the environemnt
if "KUBERNETES_SERVICE_HOST" in repository_ctx.os.environ:
server = "https://%s:%s" % (
repository_ctx.os.environ["KUBERNETES_SERVICE_HOST"],
repository_ctx.os.environ["KUBERNETES_SERVICE_PORT"],
)
else:
# fall back to the default
server = "https://kubernetes.default"
print("Using in cluster configuration. Kubernetes master is running at %s" % server)
else:
home = repository_ctx.path(repository_ctx.os.environ["HOME"])
certs = home.get_child(".kube").get_child("certs")
ca_crt = certs.get_child("ca.crt").realpath
kubecert_cert = certs.get_child("kubecert.cert")
kubecert_key = certs.get_child("kubecert.key")
# config set-cluster {cluster} \
# --certificate-authority=... \
# --server=https://dev3.k8s.tubemogul.info:443 \
# --embed-certs",
_kubectl_config(repository_ctx, [
"set-cluster",
repository_ctx.attr.cluster,
"--server",
server,
"--certificate-authority",
ca_crt,
])
# config set-credentials {user} --token=...",
if token:
_kubectl_config(repository_ctx, [
"set-credentials",
user,
"--token",
token,
])
# config set-credentials {user} --client-certificate=... --embed-certs",
if kubecert_cert and kubecert_cert.exists:
_kubectl_config(repository_ctx, [
"set-credentials",
user,
"--client-certificate",
kubecert_cert.realpath,
])
# config set-credentials {user} --client-key=... --embed-certs",
if kubecert_key and kubecert_key.exists:
_kubectl_config(repository_ctx, [
"set-credentials",
user,
"--client-key",
kubecert_key.realpath,
])
# export repostory contents
repository_ctx.file("BUILD", """exports_files(["kubeconfig", "kubectl"])""", False)
return {
"cluster": repository_ctx.attr.cluster,
"server": repository_ctx.attr.server,
}
kubeconfig = repository_rule(
attrs = {
"cluster": attr.string(),
"server": attr.string(),
},
environ = [
"HOME",
"USER",
"KUBERNETES_SERVICE_HOST",
"KUBERNETES_SERVICE_PORT",
],
local = True,
implementation = _kubeconfig_impl,
)
def _stamp(ctx, string, output):
stamps = [ctx.file._info_file]
stamp_args = [
"--stamp-info-file=%s" % sf.path
for sf in stamps
]
ctx.actions.run(
executable = ctx.executable._stamper,
arguments = [
"--format=%s" % string,
"--output=%s" % output.path,
] + stamp_args,
inputs = [ctx.executable._stamper] + stamps,
outputs = [output],
mnemonic = "Stamp",
)
def _k8s_cmd_impl(ctx):
files = [ctx.executable._stamper, ctx.file.kubectl, ctx.file.kubeconfig]
# replace placeholders in the parameter value
# see: https://github.com/bazelbuild/rules_docker#stamping
command_arg = ctx.expand_make_variables("command", ctx.attr.command, {})
if "{" in ctx.attr.command:
command_file = ctx.actions.declare_file(ctx.label.name + ".command")
_stamp(ctx, ctx.attr.command, command_file)
command_arg = "source %s" % _runfiles(ctx, command_file)
files += [command_file]
ctx.actions.expand_template(
template = ctx.file._template,
substitutions = {
"%{kubeconfig}": ctx.file.kubeconfig.path,
"%{kubectl}": ctx.file.kubectl.path,
"%{statements}": command_arg,
},
output = ctx.outputs.executable,
)
return [
DefaultInfo(runfiles = ctx.runfiles(files = files)),
]
_k8s_cmd = rule(
attrs = {
"command": attr.string(),
"kubeconfig": attr.label(
allow_single_file = True,
),
"kubectl": attr.label(
cfg = "host",
executable = True,
allow_single_file = True,
),
"_info_file": attr.label(
default = Label("//skylib:more_stable_status.txt"),
allow_single_file = True,
),
"_stamper": attr.label(
default = Label("@io_bazel_rules_k8s//k8s:stamper"),
cfg = "host",
executable = True,
allow_files = True,
),
"_template": attr.label(
default = Label("//skylib:k8s_cmd.sh.tpl"),
allow_single_file = True,
),
},
executable = True,
implementation = _k8s_cmd_impl,
)
def _k8s_test_namespace_impl(ctx):
files = [] # runfiles list
# add files referenced by rule attributes to runfiles
files = [ctx.file.kubectl, ctx.file.kubeconfig]
# create namespace reservation script
namespace_create = ctx.actions.declare_file(ctx.label.name + ".create")
ctx.actions.expand_template(
template = ctx.file._namespace_template,
substitutions = {
"%{kubeconfig}": ctx.file.kubeconfig.path,
"%{kubectl}": ctx.file.kubectl.path,
},
output = namespace_create,
is_executable = True,
)
files += [namespace_create]
return [DefaultInfo(
executable = namespace_create,
runfiles = ctx.runfiles(files = files),
)]
k8s_test_namespace = rule(
attrs = {
"kubeconfig": attr.label(
allow_single_file = True,
),
"kubectl": attr.label(
cfg = "host",
executable = True,
allow_single_file = True,
),
"_namespace_template": attr.label(
default = Label("//skylib:k8s_test_namespace.sh.tpl"),
allow_single_file = True,
),
},
executable = True,
implementation = _k8s_test_namespace_impl,
)
def _k8s_test_setup_impl(ctx):
files = [] # runfiles list
transitive = []
commands = [] # the list of commands to execute
# add files referenced by rule attributes to runfiles
files = [ctx.executable._stamper, ctx.file.kubectl, ctx.file.kubeconfig, ctx.executable._kustomize, ctx.executable._it_sidecar]
files += ctx.files._set_namespace
push_statements, files, pushes_runfiles = imagePushStatements(ctx, [o for o in ctx.attr.objects if KustomizeInfo in o], files)
# execute all objects targets
for obj in ctx.attr.objects:
if obj.files_to_run.executable:
# add object' targets and excutables to runfiles
files += [obj.files_to_run.executable]
transitive.append(obj.default_runfiles.files)
# add object' execution command
commands += [_runfiles(ctx, obj.files_to_run.executable) + " | ${SET_NAMESPACE} $NAMESPACE | ${KUBECTL} apply -f -"]
else:
files += obj.files.to_list()
commands += [ctx.executable._template_engine.short_path + " --template=" + filename.short_path + " --variable=NAMESPACE=${NAMESPACE} | ${SET_NAMESPACE} $NAMESPACE | ${KUBECTL} apply -f -" for filename in obj.files.to_list()]
files += [ctx.executable._template_engine]
# create namespace script
ctx.actions.expand_template(
template = ctx.file._namespace_template,
substitutions = {
"%{it_sidecar}": ctx.executable._it_sidecar.short_path,
"%{kubeconfig}": ctx.file.kubeconfig.path,
"%{kubectl}": ctx.file.kubectl.path,
"%{portforwards}": " ".join(["-portforward=" + p for p in ctx.attr.portforward_services]),
"%{push_statements}": push_statements,
"%{set_namespace}": ctx.executable._set_namespace.path,
"%{statements}": "\n".join(commands),
"%{test_timeout}": ctx.attr.setup_timeout,
"%{waitforapps}": " ".join(["-waitforapp=" + p for p in ctx.attr.wait_for_apps]),
},
output = ctx.outputs.executable,
)
rf = ctx.runfiles(files = files, transitive_files = depset(transitive = transitive))
for dep_rf in pushes_runfiles:
rf = rf.merge(dep_rf)
return [DefaultInfo(
executable = ctx.outputs.executable,
runfiles = rf,
)]
k8s_test_setup = rule(
attrs = {
"kubeconfig": attr.label(
default = Label("@k8s_test//:kubeconfig"),
allow_single_file = True,
),
"kubectl": attr.label(
default = Label("@k8s_test//:kubectl"),
cfg = "host",
executable = True,
allow_single_file = True,
),
"objects": attr.label_list(
cfg = "target",
),
"portforward_services": attr.string_list(),
"setup_timeout": attr.string(default = "10m"),
"wait_for_apps": attr.string_list(),
"_it_sidecar": attr.label(
default = Label("//testing/it_sidecar:it_sidecar"),
cfg = "host",
executable = True,
),
"_kustomize": attr.label(
default = Label("@kustomize_bin//:kustomize"),
cfg = "host",
executable = True,
),
"_namespace_template": attr.label(
default = Label("//skylib:k8s_test_namespace.sh.tpl"),
allow_single_file = True,
),
"_set_namespace": attr.label(
default = Label("//skylib/kustomize:set_namespace"),
cfg = "host",
executable = True,
),
"_stamper": attr.label(
default = Label("@io_bazel_rules_k8s//k8s:stamper"),
cfg = "host",
executable = True,
allow_files = True,
),
"_template_engine": attr.label(
default = Label("//templating:fast_template_engine"),
executable = True,
cfg = "host",
),
},
executable = True,
implementation = _k8s_test_setup_impl,
)