From 489f070a4104463fb200c45ec43da455270597f1 Mon Sep 17 00:00:00 2001 From: NoSkillGirl Date: Wed, 11 Nov 2020 15:27:55 +0530 Subject: [PATCH] seperated logic for resources from cluster --- pkg/kyverno/apply/command.go | 3 +++ pkg/kyverno/common/fetch.go | 41 ++++++++++++++++++------------------ 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/pkg/kyverno/apply/command.go b/pkg/kyverno/apply/command.go index 3e6e8da909..2cbc3f087b 100644 --- a/pkg/kyverno/apply/command.go +++ b/pkg/kyverno/apply/command.go @@ -133,6 +133,9 @@ func Command() *cobra.Command { return sanitizedError.NewWithError("failed to load resources", err) } } + if len(resources) == 0 { + return sanitizedError.NewWithError("valid resource(s) not provided", err) + } mutatedPolicies, err := mutatePolices(policies) diff --git a/pkg/kyverno/common/fetch.go b/pkg/kyverno/common/fetch.go index 4cd290c4a2..2e51957da0 100644 --- a/pkg/kyverno/common/fetch.go +++ b/pkg/kyverno/common/fetch.go @@ -9,7 +9,6 @@ import ( v1 "github.com/kyverno/kyverno/pkg/api/kyverno/v1" client "github.com/kyverno/kyverno/pkg/dclient" engineutils "github.com/kyverno/kyverno/pkg/engine/utils" - "github.com/kyverno/kyverno/pkg/kyverno/sanitizedError" "github.com/kyverno/kyverno/pkg/utils" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" @@ -51,14 +50,9 @@ func GetResources(policies []*v1.ClusterPolicy, resourcePaths []string, dClient resources = append(resources, rr) } } - } - } - - for _, resourcePath := range resourcePaths { - lenOfResource := len(resources) - resourceBytes, err := getFileBytes(resourcePath) - if err != nil { - if cluster { + } else { + for _, resourcePath := range resourcePaths { + lenOfResource := len(resources) for _, rm := range resourceMap { for rn, rr := range rm { if rn == resourcePath { @@ -67,20 +61,27 @@ func GetResources(policies []*v1.ClusterPolicy, resourcePaths []string, dClient } } } - } else { + if lenOfResource <= len(resources){ + fmt.Printf("\n----------------------------------------------------------------------\n%s not found in cluster\n----------------------------------------------------------------------\n", resourcePath) + } + } + } + } else if len(resourcePaths) > 0 { + for _, resourcePath := range resourcePaths { + resourceBytes, err := getFileBytes(resourcePath) + if err != nil { + fmt.Printf("\n----------------------------------------------------------------------\nfailed to load resources: %s. \nerror: %s\n----------------------------------------------------------------------\n", resourcePath, err) + continue + } + + getResources, err := GetResource(resourceBytes) + if err != nil { return nil, err } - if len(resources) <= lenOfResource { - return nil, sanitizedError.NewWithError(fmt.Sprintf("resource %s not found in cluster", resourcePath), errors.New("resource not found")) - } - } - getResources, err := GetResource(resourceBytes) - if err != nil { - return nil, err - } - for _, resource := range getResources { - resources = append(resources, resource) + for _, resource := range getResources { + resources = append(resources, resource) + } } } return resources, nil