Skip to content

Commit

Permalink
Use namespace from kube context when namespace not supplied (#69)
Browse files Browse the repository at this point in the history
* use namespace from kube context when namespace not supplied

* fix script

* fix replace variable

* fix replacements and skip ns creations when not set

* namespace attribute default

* language

* formatting

---------

Co-authored-by: Mark Smithson <msmithson@roku.com>
  • Loading branch information
marksmithson and msmithson-roku authored Dec 22, 2023
1 parent 7f162ca commit d9660c9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Example of use:
helm_release(
name = "chart_install",
chart = ":chart",
namespace_name = "myapp",
namespace = "myapp",
tiller_namespace = "tiller-system",
release_name = "release-name",
values_yaml = glob(["charts/myapp/values.yaml"]),
Expand Down Expand Up @@ -224,14 +224,20 @@ The following attributes are accepted by the rule (some of them are mandatory).
| Attribute | Mandatory| Default | Notes |
| ---------- | --- | ------ | -------------- |
| chart | yes | - | Chart package (targz). Must be a label that specifies where the helm package file (Chart.yaml) is. It accepts the path of the targz file (that bazel will resolve to the file) or the label to a target rule that generates a helm package as output (`helm_chart` rule). |
| namespace | false | default | Namespace name literal where this release is installed to. It supports the use of `stamp_variables`. |
| namespace | false | default | Namespace name literal where this release is installed to. It supports the use of `stamp_variables`. Set to `""` to use namespace from current kube context.<br/>⚠️ Please note deprecations below |
| namespace_dep | false | - | Namespace where this release is installed to. Must be a label to a k8s_namespace rule. It takes precedence over namespace |
| tiller_namespace | false | kube-system | Namespace where Tiller lives in the Kubernetes Cluste. It supports the use of `stamp_variables`. Unnecessary using helm v3 |
| tiller_namespace | false | kube-system | Namespace where Tiller lives in the Kubernetes Cluster. It supports the use of `stamp_variables`. Unnecessary using helm v3 |
| release_name | yes | - | Name of the Helm release. It supports the use of `stamp_variables`|
| values_yaml | no | - | Several values files can be passed when installing release |
| helm_version | no | "" | Force the use of helm v2 or v3 to deploy the release. The attribute can be set to **v2** or **v3** |
| kubernetes_context | no | "" | Context of kubernetes cluster |

#### ⚠️ Deprecations

The default value of the `namespace` attribute will be changing from `"default"` to `""`.

`""` uses the namespace of the current kubernetes context and most users will see no change in behavour. If you are relying on charts being explicitly installed into the `default` namespace, please update your `BUILD` files to include `namespace = "default"`.

## Sops rules
Decrypting secrets using [sops](https://github.com/mozilla/sops) is now supported.

Expand Down
10 changes: 4 additions & 6 deletions helm/helm-release.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def _helm_release_impl(ctx):
Args:
name: A unique name for this rule.
chart: Chart to install
namespace: Namespace where release is installed to
namespace_dep: k8s_namespace label for the namesapce where release is installed
namespace: Namespace where release is installed to, if namespace_dep not specified. Defaults to "default" - will change to "" in a future version
release_name: Name of the helm release
values_yaml: Specify values yaml to override default
secrets_yaml: Specify sops encrypted values to override defaulrt values (need to define sops_value as well)
Expand Down Expand Up @@ -53,10 +54,7 @@ def _helm_release_impl(ctx):
if ctx.attr.namespace_dep:
namespace = ctx.attr.namespace_dep[NamespaceDataInfo].namespace
else:
if ctx.attr.namespace:
namespace = ctx.attr.namespace
else:
namespace = "default"
namespace = ctx.attr.namespace

# Generates the exec bash file with the provided substitutions
ctx.actions.expand_template(
Expand Down Expand Up @@ -103,7 +101,7 @@ helm_release = rule(
"chart": attr.label(allow_single_file = True, mandatory = True),
"force": attr.string(mandatory = False, default = ""), # could actually be a boolean
"namespace_dep": attr.label(mandatory = False),
"namespace": attr.string(mandatory = False),
"namespace": attr.string(mandatory = False, default = "default"),
"tiller_namespace": attr.string(mandatory = False, default = "tiller-system"),
"release_name": attr.string(mandatory = True),
"values_yaml": attr.label_list(allow_files = True, mandatory = False),
Expand Down
19 changes: 13 additions & 6 deletions helm/helm-release.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ FORCE=""
HELM_OPTIONS=""
TIMEOUT=""
WAIT=""
NAMESPACE=""

if [ "{FORCE}" != "" ]; then
FORCE="--force"
Expand All @@ -59,21 +60,27 @@ if [ "{WAIT}" != "" ]; then
WAIT="--wait"
fi

if [ "{NAMESPACE}" != "" ]; then
NAMESPACE="--namespace {NAMESPACE}"
fi

# Check if tiller is running inside the cluster to guess which version of helm have to run
if [ "$FORCE_HELM_VERSION" == "v2" ] || ( [ "$FORCE_HELM_VERSION" != "v3" ] && [ $({KUBECTL_PATH} get pods -n {TILLER_NAMESPACE} | grep tiller | wc -l) -ge 1 ] ); then
if [ "$FORCE_HELM_VERSION" == "v2" ] || ( [ "$FORCE_HELM_VERSION" != "v3" ] && [ $({KUBECTL_PATH} get pods -n {TILLER_NAMESPACE} | grep tiller | wc -l) -ge 1 ] ); then
# tiller pods were found, we will use helm 2 to make the release
echo "Using helm v2 to deploy the {RELEASE_NAME} release"

{HELM_PATH} init -c

echo "{HELM_PATH} upgrade --install --tiller-namespace {TILLER_NAMESPACE} $HELM_OPTIONS --namespace {NAMESPACE} ${TIMEOUT} ${FORCE} ${WAIT} {VALUES_YAML} {RELEASE_NAME} {CHART_PATH}"
{HELM_PATH} upgrade --install --tiller-namespace {TILLER_NAMESPACE} $HELM_OPTIONS --namespace {NAMESPACE} ${TIMEOUT} ${FORCE} ${WAIT} {VALUES_YAML} {RELEASE_NAME} {CHART_PATH}
echo "{HELM_PATH} upgrade --install --tiller-namespace {TILLER_NAMESPACE} $HELM_OPTIONS $NAMESPACE ${TIMEOUT} ${FORCE} ${WAIT} {VALUES_YAML} {RELEASE_NAME} {CHART_PATH}"
{HELM_PATH} upgrade --install --tiller-namespace {TILLER_NAMESPACE} $HELM_OPTIONS $NAMESPACE ${TIMEOUT} ${FORCE} ${WAIT} {VALUES_YAML} {RELEASE_NAME} {CHART_PATH}
else
# tiller pods were not found, we will use helm 3 to make the release
echo "Using helm v3 to deploy the {RELEASE_NAME} release"

{KUBECTL_PATH} create namespace {NAMESPACE} 2> /dev/null || true
if [ "$NAMESPACE" != "" ]; then
{KUBECTL_PATH} create namespace {NAMESPACE} 2> /dev/null || true
fi

echo "{HELM3_PATH} upgrade {RELEASE_NAME} {CHART_PATH} --install $HELM_OPTIONS --namespace {NAMESPACE} $CREATE_NAMESPACE $TIMEOUT $FORCE $WAIT {VALUES_YAML}"
{HELM3_PATH} upgrade {RELEASE_NAME} {CHART_PATH} --install $HELM_OPTIONS --namespace {NAMESPACE} $CREATE_NAMESPACE $TIMEOUT $FORCE $WAIT {VALUES_YAML}
echo "{HELM3_PATH} upgrade {RELEASE_NAME} {CHART_PATH} --install $HELM_OPTIONS $NAMESPACE $CREATE_NAMESPACE $TIMEOUT $FORCE $WAIT {VALUES_YAML}"
{HELM3_PATH} upgrade {RELEASE_NAME} {CHART_PATH} --install $HELM_OPTIONS $NAMESPACE $CREATE_NAMESPACE $TIMEOUT $FORCE $WAIT {VALUES_YAML}
fi

0 comments on commit d9660c9

Please sign in to comment.