diff --git a/.github/workflows/nightly-postgresql-upgrade-test.yaml b/.github/workflows/nightly-postgresql-upgrade-test.yaml index 39092d9c1..44bce5a9a 100644 --- a/.github/workflows/nightly-postgresql-upgrade-test.yaml +++ b/.github/workflows/nightly-postgresql-upgrade-test.yaml @@ -68,6 +68,9 @@ jobs: cat < "$RUNNER_TEMP/kind-config.yaml" apiVersion: kind.x-k8s.io/v1alpha4 kind: Cluster + networking: + disableDefaultCNI: true + podSubnet: "192.168.0.0/16" nodes: - role: control-plane extraPortMappings: @@ -91,8 +94,13 @@ jobs: with: cluster_name: test-cluster config: ${{ runner.temp }}/kind-config.yaml + # Nodes stay NotReady until a CNI is installed, so don't wait here. + wait: "0" ignore_failed_clean: true + - name: Install Calico CNI (NetworkPolicy enforcement) + run: ./hack/install-calico.sh + - name: Load target operator image run: kind load docker-image --name test-cluster "$TO_OPERATOR_IMAGE" diff --git a/.github/workflows/nightly-upgrade-test.yaml b/.github/workflows/nightly-upgrade-test.yaml index b5a4c8565..1aca3c3e4 100644 --- a/.github/workflows/nightly-upgrade-test.yaml +++ b/.github/workflows/nightly-upgrade-test.yaml @@ -83,6 +83,9 @@ jobs: cat < /tmp/kind-config.yaml apiVersion: kind.x-k8s.io/v1alpha4 kind: Cluster + networking: + disableDefaultCNI: true + podSubnet: "192.168.0.0/16" nodes: - role: control-plane # extraMounts: @@ -112,8 +115,14 @@ jobs: with: cluster_name: test-cluster config: /tmp/kind-config.yaml + # Nodes stay NotReady until a CNI is installed, so don't wait here. + wait: "0" ignore_failed_clean: true + - name: Install Calico CNI (NetworkPolicy enforcement) + if: ${{ steps.operator-image-existence-checker.outputs.FROM_OPERATOR_IMAGE_EXISTS == 'true' && steps.operator-image-existence-checker.outputs.TO_OPERATOR_IMAGE_EXISTS == 'true' }} + run: ./hack/install-calico.sh + - name: Install Ingress Controller if: ${{ steps.operator-image-existence-checker.outputs.FROM_OPERATOR_IMAGE_EXISTS == 'true' && steps.operator-image-existence-checker.outputs.TO_OPERATOR_IMAGE_EXISTS == 'true' }} run: | diff --git a/.github/workflows/nightly.yaml b/.github/workflows/nightly.yaml index 5790d17df..1d981902e 100644 --- a/.github/workflows/nightly.yaml +++ b/.github/workflows/nightly.yaml @@ -85,8 +85,15 @@ jobs: uses: helm/kind-action@a1b0e391336a6ee6713a0583f8c6240d70863de3 # v1.12.0 with: cluster_name: integration-test-cluster + config: hack/kind-config.yaml + # Nodes stay NotReady until a CNI is installed, so don't wait here. + wait: "0" ignore_failed_clean: true + - name: Install Calico CNI (integration tests) + if: ${{ steps.operator-image-existence-checker.outputs.OPERATOR_IMAGE_EXISTS == 'true' }} + run: ./hack/install-calico.sh + - name: Run Controller if: ${{ steps.operator-image-existence-checker.outputs.OPERATOR_IMAGE_EXISTS == 'true' }} # run this stage only if there are changes that match the includes and not the excludes @@ -120,6 +127,9 @@ jobs: cat < /tmp/kind-config.yaml apiVersion: kind.x-k8s.io/v1alpha4 kind: Cluster + networking: + disableDefaultCNI: true + podSubnet: "192.168.0.0/16" nodes: - role: control-plane extraPortMappings: @@ -137,8 +147,14 @@ jobs: with: cluster_name: e2e-test-cluster config: /tmp/kind-config.yaml + # Nodes stay NotReady until a CNI is installed, so don't wait here. + wait: "0" ignore_failed_clean: true + - name: Install Calico CNI (E2E NetworkPolicy enforcement) + if: ${{ steps.operator-image-existence-checker.outputs.OPERATOR_IMAGE_EXISTS == 'true' }} + run: ./hack/install-calico.sh + - name: Install Ingress Controller if: ${{ steps.operator-image-existence-checker.outputs.OPERATOR_IMAGE_EXISTS == 'true' }} run: | diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index 5b8a5efbc..ef0a4e5e8 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -76,8 +76,15 @@ jobs: uses: helm/kind-action@a1b0e391336a6ee6713a0583f8c6240d70863de3 # v1.12.0 with: cluster_name: test-cluster + config: hack/kind-config.yaml + # Nodes stay NotReady until a CNI is installed, so don't wait here. + wait: "0" ignore_failed_clean: true + - name: Install Calico CNI (NetworkPolicy enforcement) + if: steps.changed-files.outputs.any_changed == 'true' + run: ./hack/install-calico.sh + - name: Run Controller if: steps.changed-files.outputs.any_changed == 'true' run: | diff --git a/hack/install-calico.sh b/hack/install-calico.sh new file mode 100755 index 000000000..226bb2f39 --- /dev/null +++ b/hack/install-calico.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +# +# Install Calico on a KinD (or any Kubernetes) cluster so that NetworkPolicies are +# actually enforced. +# +# KinD's default CNI (kindnet) does not implement NetworkPolicy enforcement, which +# means the NetworkPolicies shipped by the RHDH Operator are silently ignored and +# tests can pass even when the policies are wrong or incomplete. +# +# The KinD cluster must be created with: +# +# networking: +# disableDefaultCNI: true +# podSubnet: "192.168.0.0/16" # matches Calico's default IPv4 pool +# +# See hack/kind-config.yaml for a ready-to-use configuration. +# +# Usage: +# ./hack/install-calico.sh +# CALICO_VERSION=v3.31.7 ./hack/install-calico.sh +# +set -euo pipefail + +# The workflows in this repo create KinD clusters with kind v0.26 (Kubernetes 1.32), +# so pin Calico to a release tested against that Kubernetes version. +CALICO_VERSION="${CALICO_VERSION:-v3.31.7}" +WAIT_TIMEOUT="${WAIT_TIMEOUT:-300s}" +MANIFEST_URL="https://raw.githubusercontent.com/projectcalico/calico/${CALICO_VERSION}/manifests/calico.yaml" + +log() { + echo "[install-calico] $*" +} + +if kubectl get daemonset -n kube-system kindnet >/dev/null 2>&1; then + echo "[install-calico] ERROR: kindnet is still installed in this cluster." >&2 + echo "[install-calico] Recreate the cluster with networking.disableDefaultCNI=true (see hack/kind-config.yaml)." >&2 + exit 1 +fi + +log "Installing Calico ${CALICO_VERSION} (NetworkPolicy enforcement)..." + +manifest="$(mktemp)" +trap 'rm -f "${manifest}"' EXIT + +curl --proto '=https' --proto-redir '=https' \ + -fsSL --retry 5 --retry-all-errors --retry-delay 2 "${MANIFEST_URL}" -o "${manifest}" + +# Calico's manifest is large; server-side apply avoids the client-side +# "metadata.annotations: Too long" limitation. +kubectl apply --server-side --force-conflicts -f "${manifest}" + +log "Waiting for calico-node DaemonSet to roll out..." +kubectl -n kube-system rollout status daemonset/calico-node --timeout="${WAIT_TIMEOUT}" + +log "Waiting for calico-kube-controllers Deployment to roll out..." +kubectl -n kube-system rollout status deployment/calico-kube-controllers --timeout="${WAIT_TIMEOUT}" + +log "Waiting for all nodes to become Ready..." +kubectl wait --for=condition=Ready nodes --all --timeout="${WAIT_TIMEOUT}" + +log "Calico is ready; NetworkPolicies are now enforced." diff --git a/hack/kind-config.yaml b/hack/kind-config.yaml new file mode 100644 index 000000000..40c61ff37 --- /dev/null +++ b/hack/kind-config.yaml @@ -0,0 +1,13 @@ +# KinD cluster configuration with NetworkPolicy enforcement. +# +# kindnet (KinD's default CNI) does not enforce NetworkPolicies, so we disable it +# and install Calico after the cluster is created (see hack/install-calico.sh). +# +# The podSubnet must match Calico's default IPv4 pool (192.168.0.0/16). +apiVersion: kind.x-k8s.io/v1alpha4 +kind: Cluster +networking: + disableDefaultCNI: true + podSubnet: "192.168.0.0/16" +nodes: + - role: control-plane diff --git a/tests/e2e/README.md b/tests/e2e/README.md index b9afc182c..6c3257a33 100644 --- a/tests/e2e/README.md +++ b/tests/e2e/README.md @@ -75,8 +75,14 @@ To do so, you can: ##### `kind` +The CI Kind clusters are created with NetworkPolicy enforcement enabled (KinD's +default CNI, kindnet, ignores NetworkPolicies). To reproduce this locally, create +the cluster with [`hack/kind-config.yaml`](../../hack/kind-config.yaml) and install +Calico before running the tests: + ```shell -$ kind create cluster +$ kind create cluster --config hack/kind-config.yaml --wait 0 +$ ./hack/install-calico.sh $ make test-e2e \ BACKSTAGE_OPERATOR_TESTS_BUILD_IMAGES=true \ BACKSTAGE_OPERATOR_TESTS_PLATFORM=kind