Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using custom kubectl binary #46

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions skylib/k8s.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ load(
"@com_adobe_rules_gitops//skylib/kustomize:kustomize.bzl",
"KustomizeInfo",
"imagePushStatements",
"kubectl",
kustomize_kubectl = "kubectl",
"kustomize",
kustomize_gitops = "gitops",
)
Expand Down Expand Up @@ -128,6 +128,7 @@ def k8s_deploy(
flatten_manifest_directories = False,
start_tag = "{{",
end_tag = "}}",
kubectl = None,
visibility = None):
""" k8s_deploy
"""
Expand Down Expand Up @@ -181,22 +182,24 @@ def k8s_deploy(
objects = objects,
visibility = visibility,
)
kubectl(
kustomize_kubectl(
name = name + ".apply",
srcs = [name],
cluster = cluster,
user = user,
namespace = namespace,
kubectl = kubectl,
visibility = visibility,
)
kubectl(
kustomize_kubectl(
name = name + ".delete",
srcs = [name],
command = "delete",
cluster = cluster,
push = False,
user = user,
namespace = namespace,
kubectl = kubectl,
visibility = visibility,
)
show(
Expand Down Expand Up @@ -239,12 +242,13 @@ def k8s_deploy(
common_annotations = common_annotations,
patches = patches,
)
kubectl(
kustomize_kubectl(
name = name + ".apply",
srcs = [name],
cluster = cluster,
user = user,
namespace = namespace,
kubectl = kubectl,
visibility = visibility,
)
kustomize_gitops(
Expand Down
56 changes: 37 additions & 19 deletions skylib/kustomize/kustomize.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ gitops = rule(

def _kubectl_impl(ctx):
files = [] + ctx.files.srcs
transitive = depset([])

cluster_arg = ctx.attr.cluster
cluster_arg = ctx.expand_make_variables("cluster", cluster_arg, {})
Expand All @@ -479,49 +480,61 @@ def _kubectl_impl(ctx):
if "{" in ctx.attr.user:
user_arg = stamp(ctx, user_arg, files, ctx.label.name + ".user-name", True)

kubectl = "kubectl"
if ctx.executable.kubectl:
kubectl = "./" + ctx.executable.kubectl.short_path
files.append(ctx.executable.kubectl)
transitive = depset(transitive = [transitive, ctx.attr.kubectl.default_runfiles.files])

kubectl_command_arg = ctx.attr.command
kubectl_command_arg = ctx.expand_make_variables("kubectl_command", kubectl_command_arg, {})

statements = ""
transitive = None
statements = []

if ctx.attr.push:
trans_img_pushes = depset(transitive = [obj[KustomizeInfo].image_pushes for obj in ctx.attr.srcs]).to_list()
statements += "\n".join([
"echo pushing {}/{}:{}".format(exe[PushInfo].registry, exe[PushInfo].repository, exe[PushInfo].tag)
statements += [
"""echo pushing {}/{}:{}""".format(exe[PushInfo].registry, exe[PushInfo].repository, exe[PushInfo].tag)
for exe in trans_img_pushes
]) + "\n"
statements += "\n".join([
"async \"${RUNFILES}/%s\"" % _get_runfile_path(ctx, exe.files_to_run.executable)
]
statements += [
"""async \"${{RUNFILES}}/{}\"""".format(_get_runfile_path(ctx, exe.files_to_run.executable))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any particular reason why you ha switched to a multiline string here (and couple other places)? Most of rules code originated from Google uses "double quoted strings" in situations like those.

for exe in trans_img_pushes
]) + "\nwaitpids\n"
]
statements.append("waitpids")
files += [obj.files_to_run.executable for obj in trans_img_pushes]
transitive = depset(transitive = [obj.default_runfiles.files for obj in trans_img_pushes])
transitive = depset(transitive = [transitive] + [obj.default_runfiles.files for obj in trans_img_pushes])

namespace = ctx.attr.namespace
for inattr in ctx.attr.srcs:
for infile in inattr.files.to_list():
statements += "{template_engine} --template={infile} --variable=NAMESPACE={namespace} --stamp_info_file={info_file} | kubectl --cluster=\"{cluster}\" --user=\"{user}\" {kubectl_command} -f -\n".format(
infile = infile.short_path,
cluster = cluster_arg,
user = user_arg,
kubectl_command = kubectl_command_arg,
template_engine = "${RUNFILES}/%s" % _get_runfile_path(ctx, ctx.executable._template_engine),
namespace = namespace,
info_file = ctx.file._info_file.short_path,
statements.append(
"""{template_engine} --template={infile} --variable=NAMESPACE={namespace} --stamp_info_file={info_file} | {kubectl} --cluster=\"{cluster}\" --user=\"{user}\" {kubectl_command} -f -""".format(
infile = infile.short_path,
cluster = cluster_arg,
user = user_arg,
kubectl_command = kubectl_command_arg,
template_engine = "${{RUNFILES}}/{}".format(_get_runfile_path(ctx, ctx.executable._template_engine)),
namespace = namespace,
info_file = ctx.file._info_file.short_path,
kubectl = kubectl,
)
)

files += [ctx.executable._template_engine, ctx.file._info_file]

ctx.actions.expand_template(
template = ctx.file._template,
substitutions = {
"%{statements}": statements,
"%{statements}": "\n".join(statements),
},
output = ctx.outputs.executable,
)
return [
DefaultInfo(runfiles = ctx.runfiles(files = files, transitive_files = transitive)),
DefaultInfo(
executable = ctx.outputs.executable,
runfiles = ctx.runfiles(files = files, transitive_files = transitive),
),
]

kubectl = rule(
Expand All @@ -532,6 +545,11 @@ kubectl = rule(
"command": attr.string(default = "apply"),
"user": attr.string(default = "{BUILD_USER}"),
"push": attr.bool(default = True),
"kubectl" : attr.label(
default = None,
cfg = "host",
executable = True,
),
"_build_user_value": attr.label(
default = Label("//skylib:build_user_value.txt"),
allow_single_file = True,
Expand Down