mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-05 07:26:55 +00:00
Improve CLI test times by instantiating openapi controller once (#3297)
Signed-off-by: Sambhav Kothari <sambhavs.email@gmail.com>
This commit is contained in:
parent
388b160840
commit
c4075af3d1
3 changed files with 30 additions and 17 deletions
11
.github/workflows/cli.yaml
vendored
11
.github/workflows/cli.yaml
vendored
|
@ -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
|
||||
|
|
16
Makefile
16
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
|
||||
|
|
|
@ -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{}
|
||||
|
|
Loading…
Add table
Reference in a new issue