From c4075af3d17c59fe73b50083bb206d85a1cb38ba Mon Sep 17 00:00:00 2001 From: Sambhav Kothari Date: Thu, 24 Feb 2022 15:34:12 +0000 Subject: [PATCH] Improve CLI test times by instantiating openapi controller once (#3297) Signed-off-by: Sambhav Kothari --- .github/workflows/cli.yaml | 11 +++-------- Makefile | 16 ++++++++++++++++ pkg/kyverno/test/test_command.go | 20 +++++++++++--------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/.github/workflows/cli.yaml b/.github/workflows/cli.yaml index 7ddbf5edf1..5ddc44c7ce 100644 --- a/.github/workflows/cli.yaml +++ b/.github/workflows/cli.yaml @@ -46,18 +46,13 @@ jobs: run: | if [[ ${{ github.event_name }} == "push" ]] then - GIT_BRANCH=${GITHUB_REF##*/} + export TEST_GIT_BRANCH=${GITHUB_REF##*/} elif [[ ${{ github.event_name }} == "pull_request" ]] then - GIT_BRANCH=${{ github.event.pull_request.base.ref }} + export TEST_GIT_BRANCH=${{ github.event.pull_request.base.ref }} fi - make cli + make -j4 test-cli CLI_PATH=$PWD/cmd/cli/kubectl-kyverno/kyverno - $CLI_PATH test https://github.com/kyverno/policies/$GIT_BRANCH - $CLI_PATH test https://github.com/kyverno/policies --git-branch $GIT_BRANCH - $CLI_PATH test https://github.com/kyverno/policies/pod-security/restricted -b $GIT_BRANCH - $CLI_PATH test ./test/cli/test-mutate - $CLI_PATH test ./test/cli/test $CLI_PATH test ./test/cli/test-fail/missing-policy && exit 1 || exit 0 $CLI_PATH test ./test/cli/test-fail/missing-rule && exit 1 || exit 0 $CLI_PATH test ./test/cli/test-fail/missing-resource && exit 1 || exit 0 diff --git a/Makefile b/Makefile index 08251ba374..bb17de97c8 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ LD_FLAGS="-s -w -X $(PACKAGE)/pkg/version.BuildVersion=$(GIT_VERSION) -X $(PACKA LD_FLAGS_DEV="-s -w -X $(PACKAGE)/pkg/version.BuildVersion=$(GIT_VERSION_DEV) -X $(PACKAGE)/pkg/version.BuildHash=$(GIT_HASH) -X $(PACKAGE)/pkg/version.BuildTime=$(TIMESTAMP)" K8S_VERSION ?= $(shell kubectl version --short | grep -i server | cut -d" " -f3 | cut -c2-) export K8S_VERSION +TEST_GIT_BRANCH ?= main ################################## # KYVERNO ################################## @@ -221,6 +222,21 @@ test-clean: @echo " cleaning test cache" go clean -testcache ./... +.PHONY: test-cli +test-cli: test-cli-policies test-cli-local test-cli-local-mutate + +.PHONY: test-cli-policies +test-cli-policies: cli + cmd/cli/kubectl-kyverno/kyverno test https://github.com/kyverno/policies/$(TEST_GIT_BRANCH) + +.PHONY: test-cli-local +test-cli-local: cli + cmd/cli/kubectl-kyverno/kyverno test ./test/cli/test + +.PHONY: test-cli-local-mutate +test-cli-local-mutate: cli + cmd/cli/kubectl-kyverno/kyverno test ./test/cli/test + # go get downloads and installs the binary # we temporarily add the GO_ACC to the path diff --git a/pkg/kyverno/test/test_command.go b/pkg/kyverno/test/test_command.go index 0a39fc1cac..d6492efd36 100644 --- a/pkg/kyverno/test/test_command.go +++ b/pkg/kyverno/test/test_command.go @@ -194,7 +194,6 @@ results: fmt.Println(string(testFile)) return nil } - _, err = testCommandExecute(dirPath, valuesFile, fileName, gitBranch) if err != nil { log.Log.V(3).Info("a directory is required") @@ -274,7 +273,10 @@ func testCommandExecute(dirPath []string, valuesFile string, fileName string, gi if len(dirPath) == 0 { return rc, sanitizederror.NewWithError(fmt.Sprintf("a directory is required"), err) } - + openAPIController, err := openapi.NewOpenAPIController() + if err != nil { + return rc, fmt.Errorf("unable to create open api controller, %w", err) + } if strings.Contains(string(dirPath[0]), "https://") { gitURL, err := url.Parse(dirPath[0]) if err != nil { @@ -352,7 +354,7 @@ func testCommandExecute(dirPath []string, valuesFile string, fileName string, gi continue } - if err := applyPoliciesFromPath(fs, policyBytes, valuesFile, true, policyresoucePath, rc); err != nil { + if err := applyPoliciesFromPath(fs, policyBytes, valuesFile, true, policyresoucePath, rc, openAPIController); err != nil { return rc, sanitizederror.NewWithError("failed to apply test command", err) } } @@ -369,7 +371,7 @@ func testCommandExecute(dirPath []string, valuesFile string, fileName string, gi var testFiles int var deprecatedFiles int path := filepath.Clean(dirPath[0]) - errors = getLocalDirTestFiles(fs, path, fileName, valuesFile, rc, &testFiles, &deprecatedFiles) + errors = getLocalDirTestFiles(fs, path, fileName, valuesFile, rc, &testFiles, &deprecatedFiles, openAPIController) if testFiles == 0 { fmt.Printf("\n No test files found. Please provide test YAML files named kyverno-test.yaml \n") @@ -396,7 +398,7 @@ func testCommandExecute(dirPath []string, valuesFile string, fileName string, gi return rc, nil } -func getLocalDirTestFiles(fs billy.Filesystem, path, fileName, valuesFile string, rc *resultCounts, testFiles *int, deprecatedFiles *int) []error { +func getLocalDirTestFiles(fs billy.Filesystem, path, fileName, valuesFile string, rc *resultCounts, testFiles *int, deprecatedFiles *int, openAPIController *openapi.Controller) []error { var errors []error files, err := ioutil.ReadDir(path) @@ -405,7 +407,7 @@ func getLocalDirTestFiles(fs billy.Filesystem, path, fileName, valuesFile string } for _, file := range files { if file.IsDir() { - getLocalDirTestFiles(fs, filepath.Join(path, file.Name()), fileName, valuesFile, rc, testFiles, deprecatedFiles) + getLocalDirTestFiles(fs, filepath.Join(path, file.Name()), fileName, valuesFile, rc, testFiles, deprecatedFiles, openAPIController) continue } if strings.Contains(file.Name(), fileName) || strings.Contains(file.Name(), "test.yaml") { @@ -424,7 +426,7 @@ func getLocalDirTestFiles(fs billy.Filesystem, path, fileName, valuesFile string errors = append(errors, sanitizederror.NewWithError("failed to convert json", err)) continue } - if err := applyPoliciesFromPath(fs, valuesBytes, valuesFile, false, path, rc); err != nil { + if err := applyPoliciesFromPath(fs, valuesBytes, valuesFile, false, path, rc, openAPIController); err != nil { errors = append(errors, sanitizederror.NewWithError(fmt.Sprintf("failed to apply test command from file %s", file.Name()), err)) continue } @@ -660,8 +662,8 @@ func getFullPath(paths []string, policyResourcePath string, isGit bool) []string return paths } -func applyPoliciesFromPath(fs billy.Filesystem, policyBytes []byte, valuesFile string, isGit bool, policyResourcePath string, rc *resultCounts) (err error) { - openAPIController, err := openapi.NewOpenAPIController() +func applyPoliciesFromPath(fs billy.Filesystem, policyBytes []byte, valuesFile string, isGit bool, policyResourcePath string, rc *resultCounts, openAPIController *openapi.Controller) (err error) { + engineResponses := make([]*response.EngineResponse, 0) var dClient *client.Client values := &Test{}