1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-07 00:17:13 +00:00

seperated logic for resources from cluster

This commit is contained in:
NoSkillGirl 2020-11-11 15:27:55 +05:30
parent ca31568e2d
commit 489f070a41
2 changed files with 24 additions and 20 deletions

View file

@ -133,6 +133,9 @@ func Command() *cobra.Command {
return sanitizedError.NewWithError("failed to load resources", err) 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) mutatedPolicies, err := mutatePolices(policies)

View file

@ -9,7 +9,6 @@ import (
v1 "github.com/kyverno/kyverno/pkg/api/kyverno/v1" v1 "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
client "github.com/kyverno/kyverno/pkg/dclient" client "github.com/kyverno/kyverno/pkg/dclient"
engineutils "github.com/kyverno/kyverno/pkg/engine/utils" engineutils "github.com/kyverno/kyverno/pkg/engine/utils"
"github.com/kyverno/kyverno/pkg/kyverno/sanitizedError"
"github.com/kyverno/kyverno/pkg/utils" "github.com/kyverno/kyverno/pkg/utils"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
@ -51,14 +50,9 @@ func GetResources(policies []*v1.ClusterPolicy, resourcePaths []string, dClient
resources = append(resources, rr) resources = append(resources, rr)
} }
} }
} } else {
}
for _, resourcePath := range resourcePaths { for _, resourcePath := range resourcePaths {
lenOfResource := len(resources) lenOfResource := len(resources)
resourceBytes, err := getFileBytes(resourcePath)
if err != nil {
if cluster {
for _, rm := range resourceMap { for _, rm := range resourceMap {
for rn, rr := range rm { for rn, rr := range rm {
if rn == resourcePath { if rn == resourcePath {
@ -67,22 +61,29 @@ func GetResources(policies []*v1.ClusterPolicy, resourcePaths []string, dClient
} }
} }
} }
} else { if lenOfResource <= len(resources){
return nil, err fmt.Printf("\n----------------------------------------------------------------------\n%s not found in cluster\n----------------------------------------------------------------------\n", resourcePath)
} }
if len(resources) <= lenOfResource {
return nil, sanitizedError.NewWithError(fmt.Sprintf("resource %s not found in cluster", resourcePath), errors.New("resource not found"))
} }
} }
} 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) getResources, err := GetResource(resourceBytes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, resource := range getResources { for _, resource := range getResources {
resources = append(resources, resource) resources = append(resources, resource)
} }
} }
}
return resources, nil return resources, nil
} }