diff --git a/.bazelrc b/.bazelrc index e70267f0..67987760 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,5 +1,6 @@ common --enable_bzlmod build --nolegacy_external_runfiles +build --remote_download_all build --verbose_failures build --@io_bazel_rules_go//go/config:pure test --test_output=errors diff --git a/e2e/.bazelrc b/e2e/.bazelrc index b3c80a5a..b580950f 100644 --- a/e2e/.bazelrc +++ b/e2e/.bazelrc @@ -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 diff --git a/skylib/k8s_test_namespace.sh.tpl b/skylib/k8s_test_namespace.sh.tpl index 378d1ab7..9c704446 100644 --- a/skylib/k8s_test_namespace.sh.tpl +++ b/skylib/k8s_test_namespace.sh.tpl @@ -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 + # 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} "$@" diff --git a/testing/it_sidecar/it_sidecar.go b/testing/it_sidecar/it_sidecar.go index 3ffb7e07..43904496 100644 --- a/testing/it_sidecar/it_sidecar.go +++ b/testing/it_sidecar/it_sidecar.go @@ -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)