From 9ed18728645807dc94c2885f8c23077831446efd Mon Sep 17 00:00:00 2001 From: Vyankatesh Kudtarkar Date: Mon, 21 Mar 2022 19:31:23 +0530 Subject: [PATCH] fix cli panic for --cluster flag (#3436) --- pkg/kyverno/common/common.go | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/pkg/kyverno/common/common.go b/pkg/kyverno/common/common.go index 8c49016e63..1ec51bc0b2 100644 --- a/pkg/kyverno/common/common.go +++ b/pkg/kyverno/common/common.go @@ -735,33 +735,34 @@ func GetResourceAccordingToResourcePath(fs billy.Filesystem, resourcePaths []str return nil, sanitizederror.NewWithError("failed to extract the resources", err) } } - } else if (len(resourcePaths) > 0 && resourcePaths[0] != "-") || len(resourcePaths) < 0 || cluster { - - fileDesc, err := os.Stat(resourcePaths[0]) - if err != nil { - return nil, err - } - if fileDesc.IsDir() { - - files, err := ioutil.ReadDir(resourcePaths[0]) + } else { + if len(resourcePaths) > 0 { + fileDesc, err := os.Stat(resourcePaths[0]) if err != nil { - return nil, sanitizederror.NewWithError(fmt.Sprintf("failed to parse %v", resourcePaths[0]), err) + return nil, err } - listOfFiles := make([]string, 0) - for _, file := range files { - ext := filepath.Ext(file.Name()) - if ext == ".yaml" || ext == ".yml" { - listOfFiles = append(listOfFiles, filepath.Join(resourcePaths[0], file.Name())) + if fileDesc.IsDir() { + + files, err := ioutil.ReadDir(resourcePaths[0]) + if err != nil { + return nil, sanitizederror.NewWithError(fmt.Sprintf("failed to parse %v", resourcePaths[0]), err) } + listOfFiles := make([]string, 0) + for _, file := range files { + ext := filepath.Ext(file.Name()) + if ext == ".yaml" || ext == ".yml" { + listOfFiles = append(listOfFiles, filepath.Join(resourcePaths[0], file.Name())) + } + } + resourcePaths = listOfFiles } - resourcePaths = listOfFiles } + resources, err = GetResources(policies, resourcePaths, dClient, cluster, namespace, policyReport) if err != nil { return resources, err } } - } return resources, err }