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

implement K8S_TEST_NAMESPACE #20

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,18 @@ kubeconfig(

An executable that performs Kubernetes test setup:

- creates temporary namespace
- creates temporary namespace.
- creates kubectl configuration with the default context set to the created namespace
- deploys all dependent ***objects***
- forwards service ports

There are two ways to override the name of the namespace created by the `k8s_test_setup` rule:

- specify a `K8S_TEST_NAMESPACE` environment variable. The value of the environment variable will be used as the namespace name.
- specify a `K8S_MYNAMESPACE` environment variable. The presence of the variable would trigger the `k8s_test_setup` rule to use a namespace with the `whoami` name, typically reusing the `mynamespace` environment.

In both cases, the namespace will not be deleted after the test is finished. This is useful for debugging purposes.

| Parameter | Default | Description
| -------------------------- | -------------- | -----------
| ***kubeconfig*** | `@k8s_test//:kubeconfig` | The Kubernetes configuration file target.
Expand Down
8 changes: 7 additions & 1 deletion skylib/k8s_test_namespace.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ echo "Cluster: ${CLUSTER}" >&2
echo "User: ${USER}" >&2

set +e
if [ -n "${K8S_MYNAMESPACE:-}" ]
if [ -n "${K8S_TEST_NAMESPACE:-}" ]
then
# use provided namespace
NAMESPACE=${K8S_TEST_NAMESPACE}
# do not delete namespace after the test is complete
DELETE_NAMESPACE_FLAG=""
elif [ -n "${K8S_MYNAMESPACE:-}" ]
then
# do not create random namesspace
NAMESPACE=`whoami`

Choose a reason for hiding this comment

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

The script uses set +e which disables the automatic exit when a command fails. This can lead to the script continuing execution even after critical commands fail, potentially leading to unexpected behavior or security issues. It's recommended to handle errors explicitly and use set -e to ensure the script exits on failure.

The use of backticks for command substitution in line 58 is deprecated in favor of $(...) syntax. The latter is more readable, especially in complex commands or when nested. It's recommended to replace backticks with $(whoami) for better readability and maintainability.

Expand Down