1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00

added validation - resource not found in cluster

This commit is contained in:
NoSkillGirl 2020-11-03 02:01:20 +05:30
parent 2b979fec4d
commit cd5609fd58
3 changed files with 9 additions and 20 deletions

View file

@ -6,16 +6,4 @@ Apply Command - Valid Parameter Combinations and their interpretation!
| 2. | policy.yaml | -r resourceName | true | | apply policy from file to the resource in cluster | | 2. | policy.yaml | -r resourceName | true | | apply policy from file to the resource in cluster |
| 3. | policy.yaml | | true | | apply policy from file to all the resources in cluster | | 3. | policy.yaml | | true | | apply policy from file to all the resources in cluster |
| 4. | policy.yaml | -r resourceName | true | -n=namespace | apply policy from file to the resource in cluster in mentioned namespace | | 4. | policy.yaml | -r resourceName | true | -n=namespace | apply policy from file to the resource in cluster in mentioned namespace |
| 5. | policy.yaml | | true | -n=namespace | apply policy from file to all the resources in cluster in mentioned namespace | | 5. | policy.yaml | | true | -n=namespace | apply policy from file to all the resources in cluster in mentioned namespace |
| 6. | policyName | -r resourceName | true | | apply policy from cluster to the resource in cluster |
| 7. | policyName | | true | | apply polify from cluster to all the resouces in cluster |
| 8. | policyName | -r resourceName | true | -n=namespace | apply policy from cluster to the resource in cluster in mentioned namespace |
| 9. | policyName | | true | -n=namespace | apply polify from cluster to all the resouces in cluster in mentioned namespace |
| 10. | | -r resourceName | true | | applying all policies from the cluster to resouce in cluster |
| 11. | | -r resourceName | true | -n=namespace | applying all policies from the cluster to resouce in cluster in mentioned namespace |
| 12. | | | true | | applying all policies from the cluster to all resouces in cluster |
| 13. | | | true | | applying all policies from the cluster to all resouces in cluster in mentioned namespace |

View file

@ -301,7 +301,7 @@ func printReportOrViolation(policyReport bool, engineResponses []response.Engine
if policyReport { if policyReport {
resps := buildPolicyReports(engineResponses) resps := buildPolicyReports(engineResponses)
if len(resps) > 0 { if len(resps) > 0 {
fmt.Println("----------------------------------------------------------------------\nPOLICY REPORT:") fmt.Println("----------------------------------------------------------------------\nPOLICY REPORT:\n----------------------------------------------------------------------")
report, _ := generateCLIraw(resps) report, _ := generateCLIraw(resps)
yamlReport, _ := yaml1.Marshal(report) yamlReport, _ := yaml1.Marshal(report)
fmt.Println(string(yamlReport)) fmt.Println(string(yamlReport))

View file

@ -3,8 +3,11 @@ package common
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"github.com/kyverno/kyverno/pkg/kyverno/sanitizedError"
v1 "github.com/kyverno/kyverno/pkg/api/kyverno/v1" v1 "github.com/kyverno/kyverno/pkg/api/kyverno/v1"
"github.com/kyverno/kyverno/pkg/client/clientset/versioned/scheme" "github.com/kyverno/kyverno/pkg/client/clientset/versioned/scheme"
client "github.com/kyverno/kyverno/pkg/dclient" client "github.com/kyverno/kyverno/pkg/dclient"
@ -53,14 +56,9 @@ func GetResources(policies []*v1.ClusterPolicy, resourcePaths []string, dClient
} }
for _, resourcePath := range resourcePaths { for _, resourcePath := range resourcePaths {
lenOfResource := len(resources)
resourceBytes, err := getFileBytes(resourcePath) resourceBytes, err := getFileBytes(resourcePath)
if err != nil { if err != nil {
// check in the cluster for the given resource name
// what if two resources have same name ?
//r, err := getResourceFromCluster(resourceTypes, resourcePath, dClient)
//if err != nil {
//}
if cluster { if cluster {
for _, rm := range resourceMap { for _, rm := range resourceMap {
for rn, rr := range rm { for rn, rr := range rm {
@ -73,6 +71,9 @@ func GetResources(policies []*v1.ClusterPolicy, resourcePaths []string, dClient
} else { } else {
return nil, err 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) getResources, err := GetResource(resourceBytes)