From d1737c1aa2477f136498ced1f62fa03bbb7d94ba Mon Sep 17 00:00:00 2001 From: Stefan Sauer Date: Fri, 1 Dec 2017 14:15:46 +0100 Subject: [PATCH] Make the namespace attribute on k8s_object optional. Only pass the --namespace parameter to kubectl if the namespace attribute is specified. This will let people specify this in the deployment template instead. See #81 --- README.md | 3 ++- k8s/apply.sh.tpl | 2 +- k8s/create.sh.tpl | 2 +- k8s/delete.sh.tpl | 3 +-- k8s/describe.sh.tpl | 4 ++-- k8s/object.bzl | 5 ++++- k8s/replace.sh.tpl | 2 +- 7 files changed, 12 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c7b89a66..da44dd33 100644 --- a/README.md +++ b/README.md @@ -410,7 +410,8 @@ A rule for interacting with Kubernetes objects.

string, optional

The namespace on the cluster within which the actions are performed.

-

If this is omitted, it will default to +

If this is omitted, it will default to the value specified + in the template or if also unspecified there, to the value "default".

diff --git a/k8s/apply.sh.tpl b/k8s/apply.sh.tpl index f76628c7..272f0761 100644 --- a/k8s/apply.sh.tpl +++ b/k8s/apply.sh.tpl @@ -24,4 +24,4 @@ function guess_runfiles() { RUNFILES="${PYTHON_RUNFILES:-$(guess_runfiles)}" PYTHON_RUNFILES=${RUNFILES} %{resolve_script} | \ - kubectl --cluster="%{cluster}" --namespace="%{namespace}" apply -f - + kubectl --cluster="%{cluster}" %{namespace_arg} apply -f - diff --git a/k8s/create.sh.tpl b/k8s/create.sh.tpl index ca7dd3ee..27ea8b7d 100644 --- a/k8s/create.sh.tpl +++ b/k8s/create.sh.tpl @@ -26,4 +26,4 @@ RUNFILES="${PYTHON_RUNFILES:-$(guess_runfiles)}" # TODO(mattmoor): Should we create namespaces that do not exist? PYTHON_RUNFILES=${RUNFILES} %{resolve_script} | \ - kubectl --cluster="%{cluster}" --namespace="%{namespace}" create -f - + kubectl --cluster="%{cluster}" %{namespace_arg} create -f - diff --git a/k8s/delete.sh.tpl b/k8s/delete.sh.tpl index cbb29bcb..3d212e42 100644 --- a/k8s/delete.sh.tpl +++ b/k8s/delete.sh.tpl @@ -23,5 +23,4 @@ function guess_runfiles() { RUNFILES="${PYTHON_RUNFILES:-$(guess_runfiles)}" -kubectl --cluster="%{cluster}" --namespace="%{namespace}" delete \ - -f %{unresolved} +kubectl --cluster="%{cluster}" %{namespace_arg} delete k8s/...-f %{unresolved} diff --git a/k8s/describe.sh.tpl b/k8s/describe.sh.tpl index c3cba11b..c56ea6b4 100644 --- a/k8s/describe.sh.tpl +++ b/k8s/describe.sh.tpl @@ -25,5 +25,5 @@ RUNFILES="${PYTHON_RUNFILES:-$(guess_runfiles)}" RESOURCE_NAME=$(kubectl create --dry-run -f "%{unresolved}" | cut -d'"' -f 2) -kubectl --cluster="%{cluster}" --namespace="%{namespace}" \ - describe %{kind} "${RESOURCE_NAME}" +kubectl --cluster="%{cluster}" %{namespace_arg} describe %{kind} \ + "${RESOURCE_NAME}" diff --git a/k8s/object.bzl b/k8s/object.bzl index 3342c57d..023c9814 100644 --- a/k8s/object.bzl +++ b/k8s/object.bzl @@ -140,9 +140,12 @@ def _common_impl(ctx): namespace_arg = "$(cat %s)" % _runfiles(ctx, namespace_file) files += [namespace_file] + if namespace_arg: + namespace_arg = "--namespace=\"" + namespace_arg + "\"" + substitutions = { "%{cluster}": cluster_arg, - "%{namespace}": namespace_arg, + "%{namespace_arg}": namespace_arg, "%{kind}": ctx.attr.kind, } diff --git a/k8s/replace.sh.tpl b/k8s/replace.sh.tpl index 2dc39732..48c44c9d 100644 --- a/k8s/replace.sh.tpl +++ b/k8s/replace.sh.tpl @@ -24,4 +24,4 @@ function guess_runfiles() { RUNFILES="${PYTHON_RUNFILES:-$(guess_runfiles)}" PYTHON_RUNFILES=${RUNFILES} %{resolve_script} | \ - kubectl --cluster="%{cluster}" --namespace="%{namespace}" replace -f - + kubectl --cluster="%{cluster}" %{namespace_arg} replace -f -