gha: k8s: Ensure tests are running on a specific namespace

Let's make sure we run our tests in a specific namespace, as in case of
any kind of issue, we will just get rid of the namespace itself, which
will take care of cleaning up any leftover from failing tests.

One important thing to mention is why we can get rid of the `namespace:
${namespace}` on the tests that are already using it, and let's do it in
parts:
* namespace: default
  We can easily get rid of this as that's the default namespace where
  pods are created, so it was a no-op so far.
* namespace: test-quota-ns
  My understanding is that we'd need this in order to get a clean
  namespace where we'd be setting a quota for.  Doing this in the
  namespace that's only used for tests should **not** cause any
  side-effect on the tests, as we're running those in serial and there's
  no other pods running on the `kata-containers-k8s-tests` namespace

Last but not least, we're not dynamically creating namespaces as the
tests are not running in parallel, **never**, not in the case of having
2 tests being ran at same time, neither in the case of having 2 jobs
being scheduled to the same machine.

Fixes: #6864

Signed-off-by: Fabiano Fidêncio <fabiano.fidencio@intel.com>
This commit is contained in:
Fabiano Fidêncio 2023-07-05 19:50:32 +02:00
parent a3fc673121
commit 7c0de8703c
7 changed files with 20 additions and 8 deletions

View File

@ -58,6 +58,12 @@ function get_cluster_credentials() {
function run_tests() { function run_tests() {
platform="${1}" platform="${1}"
# Emsure we're in the default namespace
kubectl config set-context --current --namespace=default
# Delete any spurious tests namespace that was left behind
kubectl delete namespace kata-containers-k8s-tests &> /dev/null || true
sed -i -e "s|quay.io/kata-containers/kata-deploy:latest|${DOCKER_REGISTRY}/${DOCKER_REPO}:${DOCKER_TAG}|g" "${tools_dir}/packaging/kata-deploy/kata-deploy/base/kata-deploy.yaml" sed -i -e "s|quay.io/kata-containers/kata-deploy:latest|${DOCKER_REGISTRY}/${DOCKER_REPO}:${DOCKER_TAG}|g" "${tools_dir}/packaging/kata-deploy/kata-deploy/base/kata-deploy.yaml"
cat "${tools_dir}/packaging/kata-deploy/kata-deploy/base/kata-deploy.yaml" cat "${tools_dir}/packaging/kata-deploy/kata-deploy/base/kata-deploy.yaml"
cat "${tools_dir}/packaging/kata-deploy/kata-deploy/base/kata-deploy.yaml" | grep "${DOCKER_REGISTRY}/${DOCKER_REPO}:${DOCKER_TAG}" || die "Failed to setup the tests image" cat "${tools_dir}/packaging/kata-deploy/kata-deploy/base/kata-deploy.yaml" | grep "${DOCKER_REGISTRY}/${DOCKER_REPO}:${DOCKER_TAG}" || die "Failed to setup the tests image"
@ -80,6 +86,10 @@ function run_tests() {
sleep 60s sleep 60s
fi fi
# Create a new namespace for the tests and switch to it
kubectl apply -f ${integration_dir}/kubernetes/runtimeclass_workloads/tests-namespace.yaml
kubectl config set-context --current --namespace=kata-containers-k8s-tests
pushd "${integration_dir}/kubernetes" pushd "${integration_dir}/kubernetes"
bash setup.sh bash setup.sh
bash run_kubernetes_tests.sh bash run_kubernetes_tests.sh
@ -89,6 +99,10 @@ function run_tests() {
function cleanup() { function cleanup() {
platform="${1}" platform="${1}"
# Switch back to the default namespace and delete the tests one
kubectl config set-context --current --namespace=default
kubectl delete namespace kata-containers-k8s-tests
if [ "${platform}" = "tdx" ]; then if [ "${platform}" = "tdx" ]; then
deploy_spec="-k "${tools_dir}/packaging/kata-deploy/kata-deploy/overlays/k3s"" deploy_spec="-k "${tools_dir}/packaging/kata-deploy/kata-deploy/overlays/k3s""
cleanup_spec="-k "${tools_dir}/packaging/kata-deploy/kata-cleanup/overlays/k3s"" cleanup_spec="-k "${tools_dir}/packaging/kata-deploy/kata-cleanup/overlays/k3s""

View File

@ -14,13 +14,12 @@ setup() {
@test "Pod quota" { @test "Pod quota" {
resource_name="pod-quota" resource_name="pod-quota"
deployment_name="deploymenttest" deployment_name="deploymenttest"
namespace="test-quota-ns"
# Create the resourcequota # Create the resourcequota
kubectl create -f "${pod_config_dir}/resource-quota.yaml" kubectl create -f "${pod_config_dir}/resource-quota.yaml"
# View information about resourcequota # View information about resourcequota
kubectl get -n "$namespace" resourcequota "$resource_name" \ kubectl get resourcequota "$resource_name" \
--output=yaml | grep 'pods: "2"' --output=yaml | grep 'pods: "2"'
# Create deployment # Create deployment
@ -28,10 +27,9 @@ setup() {
# View deployment # View deployment
kubectl wait --for=condition=Available --timeout=$timeout \ kubectl wait --for=condition=Available --timeout=$timeout \
-n "$namespace" deployment/${deployment_name} deployment/${deployment_name}
} }
teardown() { teardown() {
kubectl delete -n "$namespace" deployment "$deployment_name"
kubectl delete -f "${pod_config_dir}/resource-quota.yaml" kubectl delete -f "${pod_config_dir}/resource-quota.yaml"
} }

View File

@ -6,7 +6,6 @@
apiVersion: v1 apiVersion: v1
kind: Pod kind: Pod
metadata: metadata:
namespace: default
name: custom-dns-test name: custom-dns-test
spec: spec:
terminationGracePeriodSeconds: 0 terminationGracePeriodSeconds: 0

View File

@ -8,7 +8,6 @@ apiVersion: v1
kind: Pod kind: Pod
metadata: metadata:
name: pod-oom name: pod-oom
namespace: default
spec: spec:
runtimeClassName: kata runtimeClassName: kata
restartPolicy: Never restartPolicy: Never

View File

@ -7,7 +7,6 @@ apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
name: deploymenttest name: deploymenttest
namespace: test-quota-ns
spec: spec:
selector: selector:
matchLabels: matchLabels:

View File

@ -14,7 +14,6 @@ items:
kind: ResourceQuota kind: ResourceQuota
metadata: metadata:
name: pod-quota name: pod-quota
namespace: test-quota-ns
spec: spec:
hard: hard:
pods: "2" pods: "2"

View File

@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: kata-containers-k8s-tests