Skip to content

Commit 623b9c0

Browse files
authored
Upload multi-cluster e2e coverage to codecov (#3984)
1. Add a new target on Makefile to build MC controller image with instrumented binary 2. Add steps on test-mc.sh to collect coverage files from leader and member cluster controllers and upload it to codecov Signed-off-by: Lan Luo <luola@vmware.com>
1 parent 4acd0ea commit 623b9c0

9 files changed

Lines changed: 197 additions & 17 deletions

File tree

Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,17 @@ endif
398398
docker tag antrea/antrea-mc-controller:$(DOCKER_IMG_VERSION) projects.registry.vmware.com/antrea/antrea-mc-controller
399399
docker tag antrea/antrea-mc-controller:$(DOCKER_IMG_VERSION) projects.registry.vmware.com/antrea/antrea-mc-controller:$(DOCKER_IMG_VERSION)
400400

401+
.PHONY: antrea-mc-controller-coverage
402+
antrea-mc-controller-coverage:
403+
@echo "===> Building antrea/antrea-mc-controller-coverage Docker image <==="
404+
ifneq ($(NO_PULL),)
405+
docker build -t antrea/antrea-mc-controller-coverage:$(DOCKER_IMG_VERSION) -f multicluster/build/images/Dockerfile.build.coverage $(DOCKER_BUILD_ARGS) .
406+
else
407+
docker build --pull -t antrea/antrea-mc-controller-coverage:$(DOCKER_IMG_VERSION) -f multicluster/build/images/Dockerfile.build.coverage $(DOCKER_BUILD_ARGS) .
408+
endif
409+
docker tag antrea/antrea-mc-controller-coverage:$(DOCKER_IMG_VERSION) antrea/antrea-mc-controller-coverage
410+
docker tag antrea/antrea-mc-controller-coverage:$(DOCKER_IMG_VERSION) projects.registry.vmware.com/antrea/antrea-mc-controller-coverage
411+
401412
.PHONY: flow-visibility-clickhouse-monitor
402413
flow-visibility-clickhouse-monitor:
403414
@echo "===> Building antrea/flow-visibility-clickhouse-monitor Docker image <==="

ci/jenkins/test-mc.sh

Lines changed: 86 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ EAST_CLUSTER_CONFIG="--kubeconfig=$MULTICLUSTER_KUBECONFIG_PATH/east"
3434
WEST_CLUSTER_CONFIG="--kubeconfig=$MULTICLUSTER_KUBECONFIG_PATH/west"
3535
ENABLE_MC_GATEWAY=false
3636
IS_CONTAINERD=false
37+
CODECOV_TOKEN=""
38+
COVERAGE=false
3739

3840
multicluster_kubeconfigs=($EAST_CLUSTER_CONFIG $LEADER_CLUSTER_CONFIG $WEST_CLUSTER_CONFIG)
3941
membercluster_kubeconfigs=($EAST_CLUSTER_CONFIG $WEST_CLUSTER_CONFIG)
@@ -42,15 +44,17 @@ membercluster_kubeconfigs=($EAST_CLUSTER_CONFIG $WEST_CLUSTER_CONFIG)
4244
CLEAN_STALE_IMAGES="docker system prune --force --all --filter until=48h"
4345

4446
_usage="Usage: $0 [--kubeconfigs-path <KubeconfigSavePath>] [--workdir <HomePath>]
45-
[--testcase <e2e>] [--mc-gateway]
47+
[--testcase <e2e>] [--mc-gateway] [--codecov-token] [--coverage]
4648
4749
Run Antrea multi-cluster e2e tests on a remote (Jenkins) Linux Cluster Set.
4850
4951
--kubeconfigs-path Path of cluster set kubeconfigs.
5052
--workdir Home path for Go, vSphere information and antrea_logs during cluster setup. Default is $WORKDIR.
5153
--testcase Antrea multi-cluster e2e test cases on a Linux cluster set.
5254
--registry The docker registry to use instead of dockerhub.
53-
--mc-gateway Enable Multicluster Gateway."
55+
--mc-gateway Enable Multicluster Gateway.
56+
--codecov-token Token used to upload coverage report(s) to Codecov.
57+
--coverage Run e2e with coverage."
5458

5559
function print_usage {
5660
echoerr "$_usage"
@@ -82,6 +86,14 @@ case $key in
8286
ENABLE_MC_GATEWAY=true
8387
shift
8488
;;
89+
--codecov-token)
90+
CODECOV_TOKEN="$2"
91+
shift 2
92+
;;
93+
--coverage)
94+
COVERAGE=true
95+
shift
96+
;;
8597
-h|--help)
8698
print_usage
8799
exit 0
@@ -161,10 +173,9 @@ function wait_for_antrea_multicluster_pods_ready {
161173
function wait_for_multicluster_controller_ready {
162174
echo "====== Deploying Antrea Multicluster Leader Cluster with ${LEADER_CLUSTER_CONFIG} ======"
163175
kubectl create ns antrea-multicluster "${LEADER_CLUSTER_CONFIG}" || true
164-
kubectl apply -f ./multicluster/test/yamls/manifest.yml "${LEADER_CLUSTER_CONFIG}"
165176
kubectl apply -f ./multicluster/build/yamls/antrea-multicluster-leader-global.yml "${LEADER_CLUSTER_CONFIG}"
177+
kubectl apply -f ./multicluster/test/yamls/leader-manifest.yml "${LEADER_CLUSTER_CONFIG}"
166178
kubectl rollout status deployment/antrea-mc-controller -n antrea-multicluster "${LEADER_CLUSTER_CONFIG}" || true
167-
kubectl apply -f ./multicluster/test/yamls/manifest.yml "${LEADER_CLUSTER_CONFIG}"
168179
kubectl create -f ./multicluster/test/yamls/leader-access-token-secret.yml "${LEADER_CLUSTER_CONFIG}" || true
169180
kubectl get secret -n antrea-multicluster leader-access-token "${LEADER_CLUSTER_CONFIG}" -o yaml > ./multicluster/test/yamls/leader-access-token.yml
170181

@@ -180,7 +191,7 @@ function wait_for_multicluster_controller_ready {
180191
for config in "${membercluster_kubeconfigs[@]}";
181192
do
182193
echo "====== Deploying Antrea Multicluster Member Cluster with ${config} ======"
183-
kubectl apply -f ./multicluster/build/yamls/antrea-multicluster-member.yml ${config}
194+
kubectl apply -f ./multicluster/test/yamls/member-manifest.yml ${config}
184195
kubectl rollout status deployment/antrea-mc-controller -n kube-system ${config}
185196
kubectl apply -f ./multicluster/test/yamls/leader-access-token.yml ${config}
186197
done
@@ -191,6 +202,34 @@ function wait_for_multicluster_controller_ready {
191202
kubectl apply -f ./multicluster/test/yamls/clusterset.yml "${LEADER_CLUSTER_CONFIG}"
192203
}
193204

205+
# We run the function in a subshell with "set -e" to ensure that it exits in
206+
# case of error (e.g. integrity check), no matter the context in which the
207+
# function is called.
208+
function run_codecov { (set -e
209+
flag=$1
210+
file=$2
211+
dir=$3
212+
213+
rm -f trustedkeys.gpg codecov
214+
# This is supposed to be a one-time step, but there should be no harm in
215+
# getting the key every time. It does not come from the codecov.io
216+
# website. Anyway, this is needed when the VM is re-created for every test.
217+
curl https://keybase.io/codecovsecurity/pgp_keys.asc | gpg --no-default-keyring --keyring trustedkeys.gpg --import
218+
curl -Os https://uploader.codecov.io/latest/linux/codecov
219+
curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM
220+
curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM.sig
221+
222+
# Check that the sha256 matches the signature
223+
gpgv codecov.SHA256SUM.sig codecov.SHA256SUM
224+
# Then check the integrity of the codecov binary
225+
shasum -a 256 -c codecov.SHA256SUM
226+
227+
chmod +x codecov
228+
./codecov -c -t ${CODECOV_TOKEN} -F ${flag} -f ${file} -s ${dir} -C ${GIT_COMMIT} -r antrea-io/antrea
229+
230+
rm -f trustedkeys.gpg codecov
231+
)}
232+
194233
function deliver_antrea_multicluster {
195234
echo "====== Building Antrea for the Following Commit ======"
196235
export GO111MODULE=on
@@ -231,10 +270,17 @@ function deliver_multicluster_controller {
231270
export GOROOT=/usr/local/go
232271
export PATH=${GOROOT}/bin:$PATH
233272

234-
export NO_PULL=1;make antrea-mc-controller
235-
236-
docker save "${DOCKER_REGISTRY}"/antrea/antrea-mc-controller:latest -o "${WORKDIR}"/antrea-mcs.tar
237-
./multicluster/hack/generate-manifest.sh -l antrea-multicluster > ./multicluster/test/yamls/manifest.yml
273+
if $COVERAGE;then
274+
export NO_PULL=1;make antrea-mc-controller-coverage
275+
docker save "${DOCKER_REGISTRY}"/antrea/antrea-mc-controller-coverage:latest -o "${WORKDIR}"/antrea-mcs.tar
276+
./multicluster/hack/generate-manifest.sh -l antrea-multicluster -c > ./multicluster/test/yamls/leader-manifest.yml
277+
./multicluster/hack/generate-manifest.sh -m -c > ./multicluster/test/yamls/member-manifest.yml
278+
else
279+
export NO_PULL=1;make antrea-mc-controller
280+
docker save "${DOCKER_REGISTRY}"/antrea/antrea-mc-controller:latest -o "${WORKDIR}"/antrea-mcs.tar
281+
./multicluster/hack/generate-manifest.sh -l antrea-multicluster > ./multicluster/test/yamls/leader-manifest.yml
282+
./multicluster/hack/generate-manifest.sh -m > ./multicluster/test/yamls/member-manifest.yml
283+
fi
238284

239285
for kubeconfig in "${multicluster_kubeconfigs[@]}"
240286
do
@@ -264,7 +310,7 @@ function deliver_multicluster_controller {
264310
}
265311

266312
function run_multicluster_e2e {
267-
echo "====== Running Multicluster e2e Tests ======"
313+
echo "====== Running Multicluster e2e Tests ======"
268314
export GO111MODULE=on
269315
export GOPATH=${WORKDIR}/go
270316
export GOROOT=/usr/local/go
@@ -311,11 +357,12 @@ EOF
311357
done
312358

313359
set +e
314-
mkdir -p `pwd`/antrea-multicluster-test-logs
360+
CURRENT_DIR=`pwd`
361+
mkdir -p ${CURRENT_DIR}/antrea-multicluster-test-logs
315362
if ${ENABLE_MC_GATEWAY};then
316-
go test -v antrea.io/antrea/multicluster/test/e2e --logs-export-dir `pwd`/antrea-multicluster-test-logs --mc-gateway
363+
go test -v antrea.io/antrea/multicluster/test/e2e --logs-export-dir `pwd`/antrea-multicluster-test-logs --mc-gateway
317364
else
318-
go test -v antrea.io/antrea/multicluster/test/e2e --logs-export-dir `pwd`/antrea-multicluster-test-logs
365+
go test -v antrea.io/antrea/multicluster/test/e2e --logs-export-dir `pwd`/antrea-multicluster-test-logs
319366
fi
320367

321368
if [[ "$?" != "0" ]]; then
@@ -324,6 +371,22 @@ EOF
324371
set -e
325372
}
326373

374+
function collect_coverage {
375+
COVERAGE_DIR=$1
376+
timestamp=$(date +%Y%m%d%H%M%S)
377+
echo "====== Collect Multicluster e2e Tests Coverage Files ======"
378+
for kubeconfig in "${multicluster_kubeconfigs[@]}"; do
379+
namespace="kube-system"
380+
if [[ ${kubeconfig} =~ "leader" ]];then
381+
namespace="antrea-multicluster"
382+
fi
383+
mc_controller_pod_name="$(kubectl get pods --selector=app=antrea,component=antrea-mc-controller -n ${namespace} --no-headers=true ${kubeconfig} | awk '{ print $1 }')"
384+
controller_pid="$(kubectl exec -i $mc_controller_pod_name -n ${namespace} ${kubeconfig} -- pgrep antrea)"
385+
kubectl exec -i $mc_controller_pod_name -n ${namespace} ${kubeconfig} -- kill -SIGINT $controller_pid
386+
kubectl cp ${namespace}/$mc_controller_pod_name:antrea-mc-controller.cov.out ${COVERAGE_DIR}/$mc_controller_pod_name-$timestamp ${kubeconfig}
387+
done
388+
}
389+
327390
trap clean_multicluster EXIT
328391
clean_tmp
329392
clean_images
@@ -341,6 +404,16 @@ if [[ ${TESTCASE} =~ "e2e" ]]; then
341404
deliver_antrea_multicluster
342405
deliver_multicluster_controller
343406
run_multicluster_e2e
407+
if $COVERAGE;then
408+
CURRENT_DIR=`pwd`
409+
rm -rf mc-e2e-coverage
410+
mkdir -p mc-e2e-coverage
411+
collect_coverage ${CURRENT_DIR}/mc-e2e-coverage
412+
# Backup coverage files for later analysis
413+
set +e;find ${DEFAULT_WORKDIR}/mc-e2e-coverage -maxdepth 1 -mtime +1 -type f | xargs -n 1 rm;set -e; # Clean up backup files older than one day.
414+
cp -r mc-e2e-coverage ${DEFAULT_WORKDIR}
415+
run_codecov "e2e-tests" "*antrea-mc*" "${CURRENT_DIR}/mc-e2e-coverage"
416+
fi
344417
fi
345418

346419
if [[ ${TEST_FAILURE} == true ]]; then

ci/jenkins/test-vmc.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ CONTROL_PLANE_NODE_ROLE="master"
4242

4343
_usage="Usage: $0 [--cluster-name <VMCClusterNameToUse>] [--kubeconfig <KubeconfigSavePath>] [--workdir <HomePath>]
4444
[--log-mode <SonobuoyResultLogLevel>] [--testcase <e2e|conformance|all-features-conformance|whole-conformance|networkpolicy>]
45-
[--garbage-collection] [--setup-only] [--cleanup-only] [--coverage] [--test-only] [--registry]
45+
[--garbage-collection] [--setup-only] [--cleanup-only] [--coverage] [--test-only] [--codecov-token] [--registry]
4646
4747
Setup a VMC cluster to run K8s e2e community tests (E2e, Conformance, all features Conformance, whole Conformance & Network Policy).
4848
@@ -301,7 +301,7 @@ function copy_image {
301301
${SSH_WITH_ANTREA_CI_KEY} -n capv@${IP} "sudo crictl images | grep '<none>' | awk '{print \$3}' | xargs -r crictl rmi"
302302
}
303303

304-
# We run the function in a subshell with "set -e" to ensure that it exists in
304+
# We run the function in a subshell with "set -e" to ensure that it exits in
305305
# case of error (e.g. integrity check), no matter the context in which the
306306
# function is called.
307307
function run_codecov { (set -e

multicluster/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ endif
8585
build: generate fmt vet ## Build manager binary.
8686
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/antrea-mc-controller antrea.io/antrea/multicluster/cmd/...
8787

88+
.PHONY: antrea-mc-instr-binary
89+
antrea-mc-instr-binary:
90+
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go test -tags testbincover -covermode count -coverpkg=antrea.io/antrea/multicluster/... -c -o bin/antrea-mc-controller-coverage antrea.io/antrea/multicluster/cmd/...
91+
8892
run: manifests generate fmt vet ## Run a controller from your host.
8993
go run ./main.go
9094

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
ARG GO_VERSION
2+
FROM golang:${GO_VERSION} as antrea-build
3+
4+
WORKDIR /antrea
5+
6+
COPY go.mod /antrea/go.mod
7+
8+
RUN go mod download
9+
10+
COPY . /antrea
11+
12+
RUN cd multicluster && make antrea-mc-instr-binary
13+
14+
FROM ubuntu:20.04
15+
16+
LABEL maintainer="Antrea <projectantrea-dev@googlegroups.com>"
17+
LABEL description="The Docker image to deploy the Antrea Multicluster controller with code coverage measurement enabled (used for testing)."
18+
19+
USER root
20+
21+
COPY --from=antrea-build /antrea/multicluster/bin/antrea-mc-controller-coverage /
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2022 Antrea Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build testbincover
16+
// +build testbincover
17+
18+
package main
19+
20+
import (
21+
"testing"
22+
23+
"github.com/confluentinc/bincover"
24+
)
25+
26+
func TestBincoverRunMain(t *testing.T) {
27+
bincover.RunTest(main)
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: controller
5+
namespace: system
6+
spec:
7+
template:
8+
spec:
9+
containers:
10+
- command: ["/bin/sh"]
11+
args: ["-c", "/antrea-mc-controller-coverage -test.run=TestBincoverRunMain -test.coverprofile=antrea-mc-controller.cov.out leader; while true; do sleep 5 & wait $!; done"]
12+
name: antrea-mc-controller
13+
image: projects.registry.vmware.com/antrea/antrea-mc-controller-coverage:latest
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: controller
5+
namespace: system
6+
spec:
7+
template:
8+
spec:
9+
containers:
10+
- command: ["/bin/sh"]
11+
args: ["-c", "/antrea-mc-controller-coverage -test.run=TestBincoverRunMain -test.coverprofile=antrea-mc-controller.cov.out member; while true; do sleep 5 & wait $!; done"]
12+
name: antrea-mc-controller
13+
image: projects.registry.vmware.com/antrea/antrea-mc-controller-coverage:latest

multicluster/hack/generate-manifest.sh

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Generate a YAML manifest for Antrea MultiCluster using Kustomize and print it to
2727
--leader | -l Generate a per-namespace manifest for a Cluster as leader in a ClusterSet.
2828
All resources will be in the given namespace
2929
--member | -m Generate a manifest for a Cluster as member in a ClusterSet
30+
--coverage| -c Generate a manifest which supports measuring code coverage
3031
--help | -h Print this message and exit
3132
3233
Environment variables IMG_NAME and IMG_TAG must be set when release mode is enabled.
@@ -43,6 +44,7 @@ function print_help {
4344
OVERLAY=member
4445
NAMESPACE=antrea-multicluster
4546
MODE=""
47+
COVERAGE=false
4648

4749
while [[ $# -gt 0 ]]
4850
do
@@ -52,6 +54,10 @@ case $key in
5254
MODE="release"
5355
shift
5456
;;
57+
--coverage|-c)
58+
COVERAGE=true
59+
shift
60+
;;
5561
--leader|-l)
5662
OVERLAY=leader-ns
5763
NAMESPACE="$2"
@@ -113,7 +119,7 @@ then
113119
cp $KUSTOMIZATION_DIR/overlays/leader-ns/prefix_transformer.yaml .
114120
sed -ie "s/antrea-multicluster/$NAMESPACE/g" prefix_transformer.yaml
115121

116-
cat << EOF > kustomization.yaml
122+
cat << EOF > kustomization.yaml
117123
namespace: $NAMESPACE
118124
119125
bases:
@@ -122,8 +128,13 @@ bases:
122128
transformers:
123129
- prefix_transformer.yaml
124130
EOF
131+
132+
if $COVERAGE; then
133+
cp ../../overlays/$OVERLAY/coverage/manager_command_patch_coverage.yaml .
134+
$KUSTOMIZE edit add patch --path ./manager_command_patch_coverage.yaml
135+
fi
125136
else
126-
cat << EOF > kustomization.yaml
137+
cat << EOF > kustomization.yaml
127138
bases:
128139
- ../overlays/$OVERLAY
129140
EOF
@@ -134,5 +145,11 @@ if [ "$MODE" == "release" ]; then
134145
else
135146
$KUSTOMIZE edit set image antrea/antrea-mc-controller=projects.registry.vmware.com/antrea/antrea-mc-controller:latest
136147
fi
148+
149+
if [ "$OVERLAY" == "member" ] && $COVERAGE; then
150+
cp ../overlays/$OVERLAY/coverage/manager_command_patch_coverage.yaml .
151+
$KUSTOMIZE edit add patch --path ./manager_command_patch_coverage.yaml
152+
fi
153+
137154
$KUSTOMIZE build
138155
rm -rf $TMP_DIR

0 commit comments

Comments
 (0)