Skip to content

Commit 39f94ee

Browse files
authored
cleanup test namespace (#42)
* move test namespace cleanup to the setup script
1 parent d49f3cb commit 39f94ee

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
common --enable_bzlmod
22
build --nolegacy_external_runfiles
3+
build --remote_download_all
34
build --verbose_failures
45
build --@io_bazel_rules_go//go/config:pure
56
test --test_output=errors

e2e/.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
build --action_env HOME --test_env HOME
22
common:bzlmod --enable_bzlmod
3+
build --remote_download_all
34
build --nolegacy_external_runfiles
45
build --@io_bazel_rules_go//go/config:pure

skylib/k8s_test_namespace.sh.tpl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,22 @@ echo "Cluster: ${CLUSTER}" >&2
4646
echo "User: ${USER}" >&2
4747

4848
set +e
49+
50+
ns_cleanup() {
51+
echo "Performing namespace ${NAMESPACE} cleanup..."
52+
${KUBECTL} --kubeconfig=${KUBECONFIG} --cluster=${CLUSTER} --user=${USER} delete namespace --wait=false ${NAMESPACE}
53+
}
54+
4955
if [ -n "${K8S_TEST_NAMESPACE:-}" ]
5056
then
5157
# use provided namespace
5258
NAMESPACE=${K8S_TEST_NAMESPACE}
53-
# do not delete namespace after the test is complete
54-
DELETE_NAMESPACE_FLAG=""
5559
elif [ -n "${K8S_MYNAMESPACE:-}" ]
5660
then
5761
# do not create random namesspace
5862
NAMESPACE=$(whoami)
59-
# do not delete namespace after the test is complete
60-
DELETE_NAMESPACE_FLAG=""
6163
else
6264
# create random namespace
63-
DELETE_NAMESPACE_FLAG="-delete_namespace"
6465
COUNT="0"
6566
while true; do
6667
NAMESPACE=`whoami`-$(( (RANDOM) + 32767 ))
@@ -71,6 +72,8 @@ else
7172
exit 1
7273
fi
7374
done
75+
# delete namespace after the test is complete or failed
76+
trap ns_cleanup EXIT
7477
fi
7578
echo "Namespace: ${NAMESPACE}" >&2
7679
set -e
@@ -110,4 +113,4 @@ function waitpids() {
110113
# create k8s objects
111114
%{statements}
112115

113-
%{it_sidecar} -namespace=${NAMESPACE} %{sidecar_args} ${DELETE_NAMESPACE_FLAG} "$@"
116+
%{it_sidecar} -namespace=${NAMESPACE} %{sidecar_args} "$@"

testing/it_sidecar/it_sidecar.go

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@ func (i *arrayFlags) Set(value string) error {
6666
}
6767

6868
var (
69-
namespace = flag.String("namespace", os.Getenv("NAMESPACE"), "kubernetes namespace")
70-
timeout = flag.Duration("timeout", time.Second*30, "execution timeout")
71-
deleteNamespace = flag.Bool("delete_namespace", false, "delete namespace as part of the cleanup")
72-
pfconfig = portForwardConf{services: make(map[string][]uint16)}
73-
kubeconfig string
74-
waitForApps arrayFlags
75-
allowErrors bool
76-
disablePodLogs bool
69+
namespace = flag.String("namespace", os.Getenv("NAMESPACE"), "kubernetes namespace")
70+
timeout = flag.Duration("timeout", time.Second*30, "execution timeout")
71+
pfconfig = portForwardConf{services: make(map[string][]uint16)}
72+
kubeconfig string
73+
waitForApps arrayFlags
74+
allowErrors bool
75+
disablePodLogs bool
7776
)
7877

7978
func init() {
@@ -329,17 +328,6 @@ func portForward(ctx context.Context, clientset *kubernetes.Clientset, config *r
329328
return nil
330329
}
331330

332-
func cleanup(clientset *kubernetes.Clientset) {
333-
log.Print("Cleanup")
334-
if *deleteNamespace && *namespace != "" {
335-
log.Printf("deleting namespace %s", *namespace)
336-
s := meta_v1.DeletePropagationBackground
337-
if err := clientset.CoreV1().Namespaces().Delete(context.Background(), *namespace, meta_v1.DeleteOptions{PropagationPolicy: &s}); err != nil {
338-
log.Printf("Unable to delete namespace %s: %v", *namespace, err)
339-
}
340-
}
341-
}
342-
343331
var ErrTimedOut = errors.New("timed out")
344332
var ErrStdinClosed = errors.New("stdin closed")
345333
var ErrTermSignalReceived = errors.New("TERM signal received")
@@ -382,7 +370,6 @@ func main() {
382370
log.Fatal(err)
383371
}
384372
clientset = kubernetes.NewForConfigOrDie(config)
385-
defer cleanup(clientset)
386373

387374
go func() {
388375
err := stern.Run(ctx, *namespace, clientset, allowErrors, disablePodLogs)

0 commit comments

Comments
 (0)