-
Notifications
You must be signed in to change notification settings - Fork 11
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
cleanup test namespace #42
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
build --action_env HOME --test_env HOME | ||
common:bzlmod --enable_bzlmod | ||
build --remote_download_all | ||
build --nolegacy_external_runfiles | ||
build --@io_bazel_rules_go//go/config:pure |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,21 +46,22 @@ echo "Cluster: ${CLUSTER}" >&2 | |
echo "User: ${USER}" >&2 | ||
|
||
set +e | ||
|
||
ns_cleanup() { | ||
echo "Performing namespace ${NAMESPACE} cleanup..." | ||
${KUBECTL} --kubeconfig=${KUBECONFIG} --cluster=${CLUSTER} --user=${USER} delete namespace --wait=false ${NAMESPACE} | ||
} | ||
|
||
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) | ||
# do not delete namespace after the test is complete | ||
DELETE_NAMESPACE_FLAG="" | ||
else | ||
# create random namespace | ||
DELETE_NAMESPACE_FLAG="-delete_namespace" | ||
COUNT="0" | ||
while true; do | ||
NAMESPACE=`whoami`-$(( (RANDOM) + 32767 )) | ||
|
@@ -71,6 +72,8 @@ else | |
exit 1 | ||
fi | ||
done | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The while loop that generates a random namespace does not have a termination condition if the namespace creation keeps failing. This can lead to an infinite loop. Consider adding a maximum retry limit to avoid potential infinite loops. |
||
# delete namespace after the test is complete or failed | ||
trap ns_cleanup EXIT | ||
fi | ||
echo "Namespace: ${NAMESPACE}" >&2 | ||
set -e | ||
|
@@ -110,4 +113,4 @@ function waitpids() { | |
# create k8s objects | ||
%{statements} | ||
|
||
%{it_sidecar} -namespace=${NAMESPACE} %{sidecar_args} ${DELETE_NAMESPACE_FLAG} "$@" | ||
%{it_sidecar} -namespace=${NAMESPACE} %{sidecar_args} "$@" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,14 +66,13 @@ func (i *arrayFlags) Set(value string) error { | |
} | ||
|
||
var ( | ||
namespace = flag.String("namespace", os.Getenv("NAMESPACE"), "kubernetes namespace") | ||
timeout = flag.Duration("timeout", time.Second*30, "execution timeout") | ||
deleteNamespace = flag.Bool("delete_namespace", false, "delete namespace as part of the cleanup") | ||
pfconfig = portForwardConf{services: make(map[string][]uint16)} | ||
kubeconfig string | ||
waitForApps arrayFlags | ||
allowErrors bool | ||
disablePodLogs bool | ||
namespace = flag.String("namespace", os.Getenv("NAMESPACE"), "kubernetes namespace") | ||
timeout = flag.Duration("timeout", time.Second*30, "execution timeout") | ||
pfconfig = portForwardConf{services: make(map[string][]uint16)} | ||
kubeconfig string | ||
waitForApps arrayFlags | ||
allowErrors bool | ||
disablePodLogs bool | ||
) | ||
|
||
func init() { | ||
|
@@ -329,17 +328,6 @@ func portForward(ctx context.Context, clientset *kubernetes.Clientset, config *r | |
return nil | ||
} | ||
|
||
func cleanup(clientset *kubernetes.Clientset) { | ||
log.Print("Cleanup") | ||
if *deleteNamespace && *namespace != "" { | ||
log.Printf("deleting namespace %s", *namespace) | ||
s := meta_v1.DeletePropagationBackground | ||
if err := clientset.CoreV1().Namespaces().Delete(context.Background(), *namespace, meta_v1.DeleteOptions{PropagationPolicy: &s}); err != nil { | ||
log.Printf("Unable to delete namespace %s: %v", *namespace, err) | ||
} | ||
} | ||
} | ||
|
||
var ErrTimedOut = errors.New("timed out") | ||
var ErrStdinClosed = errors.New("stdin closed") | ||
var ErrTermSignalReceived = errors.New("TERM signal received") | ||
|
@@ -382,7 +370,6 @@ func main() { | |
log.Fatal(err) | ||
} | ||
clientset = kubernetes.NewForConfigOrDie(config) | ||
defer cleanup(clientset) | ||
|
||
go func() { | ||
err := stern.Run(ctx, *namespace, clientset, allowErrors, disablePodLogs) | ||
Comment on lines
374
to
375
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The goroutine starting at line 374 calls Recommendation: Handle the error returned by |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
set +e
command disables the immediate exit on error, which can lead to the script continuing execution even if critical commands fail. This can cause unexpected behavior and make debugging difficult. Consider handling errors explicitly where needed and re-enablingset -e
after the critical section.