From da61a87ce03875e12387e9bb6177f778a3fc6946 Mon Sep 17 00:00:00 2001 From: eryalito Date: Mon, 25 Sep 2023 17:05:35 +0200 Subject: [PATCH] Allow extra manually added pull secrets to managed SA (#418) * feat: add annotation on SA to keep track of pull secrets Add an annotation to keep track of managed pullsecrets on the created service account * feat: use actual pull secret comparision instead of annotations * feat: tidy current e2e tests and add cleanups * fix: add banner * feat: add e2e service account tests * fix: yq syntax fix * fix: yq eval environment variable * fix: e2e scripts base_dir reference * Revert "fix: e2e scripts base_dir reference" This reverts commit e0704033694851d12af22b8d5ec9e5b59083c44d. * fix: copy missing tests folder into e2e container * feat: bump rok8s to @13 and ci-images to v13 --- .circleci/config.yml | 4 +- e2e/pre.sh | 8 +- .../cluterrolebindings/cleanup.sh | 2 + e2e/rbacdefinition/cluterrolebindings/main.sh | 21 +++++ .../cluterrolebindings/setup.sh | 16 ++++ .../cluterrolebindings/tests.sh | 10 +++ e2e/rbacdefinition/run.sh | 18 +++++ e2e/rbacdefinition/serviceaccounts/cleanup.sh | 2 + e2e/rbacdefinition/serviceaccounts/main.sh | 21 +++++ e2e/rbacdefinition/serviceaccounts/setup.sh | 18 +++++ e2e/rbacdefinition/serviceaccounts/tests.sh | 68 ++++++++++++++++ e2e/test.sh | 39 ++-------- pkg/reconciler/matcher.go | 35 +++++++-- pkg/reconciler/matcher_test.go | 77 +++++++++++++++++++ pkg/reconciler/parser.go | 7 ++ 15 files changed, 300 insertions(+), 46 deletions(-) create mode 100644 e2e/rbacdefinition/cluterrolebindings/cleanup.sh create mode 100644 e2e/rbacdefinition/cluterrolebindings/main.sh create mode 100644 e2e/rbacdefinition/cluterrolebindings/setup.sh create mode 100644 e2e/rbacdefinition/cluterrolebindings/tests.sh create mode 100644 e2e/rbacdefinition/run.sh create mode 100644 e2e/rbacdefinition/serviceaccounts/cleanup.sh create mode 100644 e2e/rbacdefinition/serviceaccounts/main.sh create mode 100644 e2e/rbacdefinition/serviceaccounts/setup.sh create mode 100644 e2e/rbacdefinition/serviceaccounts/tests.sh diff --git a/.circleci/config.yml b/.circleci/config.yml index 57e302fb..7102f0ff 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,7 +1,7 @@ version: 2.1 orbs: - rok8s: fairwinds/rok8s-scripts@11 + rok8s: fairwinds/rok8s-scripts@13 oss-docs: fairwinds/oss-docs@0 references: @@ -18,7 +18,7 @@ references: e2e_configuration: &e2e_configuration pre_script: e2e/pre.sh script: e2e/test.sh - command_runner_image: quay.io/reactiveops/ci-images:v12-buster + command_runner_image: quay.io/reactiveops/ci-images:v13-buster enable_docker_layer_caching: true attach-workspace: true requires: diff --git a/e2e/pre.sh b/e2e/pre.sh index e15831c8..d53a5d1f 100644 --- a/e2e/pre.sh +++ b/e2e/pre.sh @@ -2,7 +2,7 @@ set -e -wget -O /usr/local/bin/yq "https://github.com/mikefarah/yq/releases/download/2.4.0/yq_linux_amd64" +wget -O /usr/local/bin/yq "https://github.com/mikefarah/yq/releases/download/v4.35.1/yq_linux_amd64" chmod +x /usr/local/bin/yq if [ -z "$CI_SHA1" ]; then @@ -26,8 +26,10 @@ echo "** END LOADING IMAGE **" echo "********************************************************************" printf "\n\n" -yq w -i deploy/3_deployment.yaml 'spec.template.spec.containers[0].image' "quay.io/reactiveops/rbac-manager:${CI_SHA1}-amd64" -yq w -i deploy/3_deployment.yaml 'spec.template.spec.containers[0].imagePullPolicy' "IfNotPresent" +export newImage=quay.io/reactiveops/rbac-manager:${CI_SHA1}-amd64 +yq -i '.spec.template.spec.containers[0].image = env(newImage)' deploy/3_deployment.yaml +yq -i '.spec.template.spec.containers[0].imagePullPolicy = "IfNotPresent"' deploy/3_deployment.yaml cat deploy/3_deployment.yaml docker cp deploy e2e-command-runner:/ +docker cp e2e/rbacdefinition e2e-command-runner:/ diff --git a/e2e/rbacdefinition/cluterrolebindings/cleanup.sh b/e2e/rbacdefinition/cluterrolebindings/cleanup.sh new file mode 100644 index 00000000..dc175b27 --- /dev/null +++ b/e2e/rbacdefinition/cluterrolebindings/cleanup.sh @@ -0,0 +1,2 @@ +kubectl delete clusterrole test-rbac-manager --ignore-not-found +kubectl delete RBACDefinition rbac-manager-definition --ignore-not-found \ No newline at end of file diff --git a/e2e/rbacdefinition/cluterrolebindings/main.sh b/e2e/rbacdefinition/cluterrolebindings/main.sh new file mode 100644 index 00000000..18726ef7 --- /dev/null +++ b/e2e/rbacdefinition/cluterrolebindings/main.sh @@ -0,0 +1,21 @@ +BASE_DIR=$(dirname $BASH_SOURCE) + +printf "\n\n" +echo "********************************************************************" +echo "** Test clusterrolebindings **" +echo "********************************************************************" +printf "\n\n" + +# Execute the setup, then execute the tests just if the setup contains no errors. +# Finally always execute the cleanup and return the whole error of the steps +error=$((0)) +bash "$BASE_DIR/setup.sh" +error=$(( error | $? )) + +if [ $error -eq 0 ]; then +bash "$BASE_DIR/tests.sh" +error=$(( error | $? )) +fi + +bash "$BASE_DIR/cleanup.sh" +exit $(( error | $? )) diff --git a/e2e/rbacdefinition/cluterrolebindings/setup.sh b/e2e/rbacdefinition/cluterrolebindings/setup.sh new file mode 100644 index 00000000..170df18a --- /dev/null +++ b/e2e/rbacdefinition/cluterrolebindings/setup.sh @@ -0,0 +1,16 @@ +kubectl create clusterrole test-rbac-manager --verb="create" --resource=deployment + +cat <&2 echo "error: The Service account must exists" +fi +kubectl delete -n rbac-manager serviceaccount/test-rbac-manager +kubectl get -n rbac-manager serviceaccount/test-rbac-manager +error=$(( error | $? )) +if [ "$error" -eq 1 ]; then + >&2 echo "error: The Service account must be recreated" +fi + +# ImagePullSecret is created +contents=$(kubectl get -n rbac-manager serviceaccount/test-rbac-manager -oyaml | yq 'select(.imagePullSecrets[] | .name == "robot-secret")') +if [ -z "$contents" ]; then + error=$(( error | 1 )) +fi +if [ "$error" -eq 1 ]; then + >&2 echo "error: ImagePullSecret \"robot-secret\" must exists" +fi + +# ImagePullSecret is re-created if deleted +cat <&2 echo "error: ImagePullSecret \"robot-secret\" must be re-created" +fi + +# If ImagePullSecret is added it should not be removed + +cat <&2 echo "error: ImagePullSecret \"new-secret-name\" must be kept" +fi + +exit $error \ No newline at end of file diff --git a/e2e/test.sh b/e2e/test.sh index 52a637a2..dfdabfe7 100755 --- a/e2e/test.sh +++ b/e2e/test.sh @@ -1,6 +1,6 @@ #!/bin/bash - +BASE_DIR=$(dirname $BASH_SOURCE) printf "\n\n" echo "**************************" @@ -20,36 +20,7 @@ printf "\n\n" kubectl apply -f deploy/ kubectl -n rbac-manager wait deployment/rbac-manager --timeout=120s --for condition=available - -printf "\n\n" -echo "********************************************************************" -echo "** Test rbacDefinition **" -echo "********************************************************************" -printf "\n\n" -kubectl create clusterrole test-rbac-manager --verb="create" --resource=deployment - -cat <