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 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
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
10 changes: 8 additions & 2 deletions skylib/k8s_test_namespace.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@ 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`
NAMESPACE=$(whoami)
# do not delete namespace after the test is complete
DELETE_NAMESPACE_FLAG=""
else
Comment on lines 48 to 61

Choose a reason for hiding this comment

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

The script's logic for determining the namespace and whether to delete it after tests could lead to unintended consequences due to its reliance on environment variables without clear validation or fallback. Specifically, the script assumes that if K8S_TEST_NAMESPACE or K8S_MYNAMESPACE is set, these should be used without further checks. This could potentially lead to using incorrect namespaces or not deleting namespaces when expected, especially in complex CI environments where environment variables might be set unintentionally.

To improve this, introduce explicit validation for the namespace values being used, ensuring they conform to expected patterns or naming conventions. Additionally, consider adding a clear, documented default behavior for namespace creation and deletion when neither environment variable is set, to avoid ambiguity and ensure clean-up is handled correctly in all scenarios. This approach will enhance the script's robustness and predictability, making it safer to use in diverse environments.

Expand Down